2 * Copyright (C) 2011, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "modules/webaudio/OfflineAudioDestinationNode.h"
32 #include "platform/audio/AudioBus.h"
33 #include "platform/audio/HRTFDatabaseLoader.h"
34 #include "modules/webaudio/AudioContext.h"
35 #include "platform/Task.h"
36 #include "public/platform/Platform.h"
37 #include "wtf/MainThread.h"
43 const size_t renderQuantumSize = 128;
45 OfflineAudioDestinationNode::OfflineAudioDestinationNode(AudioContext* context, AudioBuffer* renderTarget)
46 : AudioDestinationNode(context, renderTarget->sampleRate())
47 , m_renderTarget(renderTarget)
48 , m_startedRendering(false)
50 m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuantumSize);
53 OfflineAudioDestinationNode::~OfflineAudioDestinationNode()
58 void OfflineAudioDestinationNode::initialize()
63 AudioNode::initialize();
66 void OfflineAudioDestinationNode::uninitialize()
72 m_renderThread.clear();
74 AudioNode::uninitialize();
77 void OfflineAudioDestinationNode::startRendering()
79 ASSERT(isMainThread());
80 ASSERT(m_renderTarget.get());
81 if (!m_renderTarget.get())
84 if (!m_startedRendering) {
85 m_startedRendering = true;
86 ref(); // See corresponding deref() call in notifyCompleteDispatch().
87 m_renderThread = adoptPtr(blink::Platform::current()->createThread("Offline Audio Renderer"));
88 m_renderThread->postTask(new Task(WTF::bind(&OfflineAudioDestinationNode::offlineRender, this)));
92 void OfflineAudioDestinationNode::offlineRender()
94 ASSERT(!isMainThread());
95 ASSERT(m_renderBus.get());
96 if (!m_renderBus.get())
99 bool isAudioContextInitialized = context()->isInitialized();
100 ASSERT(isAudioContextInitialized);
101 if (!isAudioContextInitialized)
104 bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numberOfChannels();
105 ASSERT(channelsMatch);
109 bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize;
110 ASSERT(isRenderBusAllocated);
111 if (!isRenderBusAllocated)
114 // Break up the render target into smaller "render quantize" sized pieces.
115 // Render until we're finished.
116 size_t framesToProcess = m_renderTarget->length();
117 unsigned numberOfChannels = m_renderTarget->numberOfChannels();
120 while (framesToProcess > 0) {
121 // Render one render quantum.
122 render(0, m_renderBus.get(), renderQuantumSize);
124 size_t framesAvailableToCopy = min(framesToProcess, renderQuantumSize);
126 for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++channelIndex) {
127 const float* source = m_renderBus->channel(channelIndex)->data();
128 float* destination = m_renderTarget->getChannelData(channelIndex)->data();
129 memcpy(destination + n, source, sizeof(float) * framesAvailableToCopy);
132 n += framesAvailableToCopy;
133 framesToProcess -= framesAvailableToCopy;
136 // Our work is done. Let the AudioContext know.
137 callOnMainThread(notifyCompleteDispatch, this);
140 void OfflineAudioDestinationNode::notifyCompleteDispatch(void* userData)
142 OfflineAudioDestinationNode* destinationNode = static_cast<OfflineAudioDestinationNode*>(userData);
143 ASSERT(destinationNode);
144 if (!destinationNode)
147 destinationNode->notifyComplete();
148 destinationNode->deref();
151 void OfflineAudioDestinationNode::notifyComplete()
153 context()->fireCompletionEvent();
156 void OfflineAudioDestinationNode::trace(Visitor* visitor)
158 visitor->trace(m_renderTarget);
159 AudioDestinationNode::trace(visitor);
162 } // namespace WebCore
164 #endif // ENABLE(WEB_AUDIO)