| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011, Google Inc. All rights reserved. | 2 * Copyright (C) 2011, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 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. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 */ | 23 */ |
| 24 | 24 |
| 25 #include "config.h" | 25 #include "config.h" |
| 26 #if ENABLE(WEB_AUDIO) | 26 #if ENABLE(WEB_AUDIO) |
| 27 #include "modules/webaudio/OfflineAudioDestinationNode.h" | 27 #include "modules/webaudio/OfflineAudioDestinationNode.h" |
| 28 | 28 |
| 29 #include "core/dom/CrossThreadTask.h" | 29 #include "core/dom/CrossThreadTask.h" |
| 30 #include "modules/webaudio/AudioContext.h" | 30 #include "modules/webaudio/AudioContext.h" |
| 31 #include "modules/webaudio/OfflineAudioContext.h" |
| 31 #include "platform/Task.h" | 32 #include "platform/Task.h" |
| 32 #include "platform/audio/AudioBus.h" | 33 #include "platform/audio/AudioBus.h" |
| 33 #include "platform/audio/HRTFDatabaseLoader.h" | 34 #include "platform/audio/HRTFDatabaseLoader.h" |
| 34 #include "public/platform/Platform.h" | 35 #include "public/platform/Platform.h" |
| 35 #include <algorithm> | 36 #include <algorithm> |
| 36 | 37 |
| 37 namespace blink { | 38 namespace blink { |
| 38 | 39 |
| 39 const size_t renderQuantumSize = 128; | 40 const size_t renderQuantumSize = 128; |
| 40 | 41 |
| 41 OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node,
AudioBuffer* renderTarget) | 42 OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node,
AudioBuffer* renderTarget) |
| 42 : AudioDestinationHandler(node, renderTarget->sampleRate()) | 43 : AudioDestinationHandler(node, renderTarget->sampleRate()) |
| 43 , m_renderTarget(renderTarget) | 44 , m_renderTarget(renderTarget) |
| 44 , m_startedRendering(false) | 45 , m_framesProcessed(0) |
| 46 , m_framesToProcess(0) |
| 45 { | 47 { |
| 46 m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuant
umSize); | 48 m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuant
umSize); |
| 47 } | 49 } |
| 48 | 50 |
| 49 PassRefPtr<OfflineAudioDestinationHandler> OfflineAudioDestinationHandler::creat
e(AudioNode& node, AudioBuffer* renderTarget) | 51 PassRefPtr<OfflineAudioDestinationHandler> OfflineAudioDestinationHandler::creat
e(AudioNode& node, AudioBuffer* renderTarget) |
| 50 { | 52 { |
| 51 return adoptRef(new OfflineAudioDestinationHandler(node, renderTarget)); | 53 return adoptRef(new OfflineAudioDestinationHandler(node, renderTarget)); |
| 52 } | 54 } |
| 53 | 55 |
| 54 OfflineAudioDestinationHandler::~OfflineAudioDestinationHandler() | 56 OfflineAudioDestinationHandler::~OfflineAudioDestinationHandler() |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 AudioHandler::uninitialize(); | 83 AudioHandler::uninitialize(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void OfflineAudioDestinationHandler::startRendering() | 86 void OfflineAudioDestinationHandler::startRendering() |
| 85 { | 87 { |
| 86 ASSERT(isMainThread()); | 88 ASSERT(isMainThread()); |
| 87 ASSERT(m_renderTarget); | 89 ASSERT(m_renderTarget); |
| 88 if (!m_renderTarget) | 90 if (!m_renderTarget) |
| 89 return; | 91 return; |
| 90 | 92 |
| 91 if (!m_startedRendering) { | 93 // Rendering was not started. Starting now. |
| 92 m_startedRendering = true; | 94 if (!m_renderThread) { |
| 93 m_renderThread = adoptPtr(Platform::current()->createThread("Offline Aud
io Renderer")); | 95 m_renderThread = adoptPtr(Platform::current()->createThread("Offline Aud
io Renderer")); |
| 94 m_renderThread->postTask(FROM_HERE, new Task(threadSafeBind(&OfflineAudi
oDestinationHandler::offlineRender, PassRefPtr<OfflineAudioDestinationHandler>(t
his)))); | 96 m_renderThread->postTask(FROM_HERE, new Task(threadSafeBind(&OfflineAudi
oDestinationHandler::startOfflineRendering, this))); |
| 97 return; |
| 95 } | 98 } |
| 99 |
| 100 // Rendering is already started, which implicitly means we resume the |
| 101 // rendering by calling |runOfflineRendering| on m_renderThread. |
| 102 m_renderThread->postTask(FROM_HERE, threadSafeBind(&OfflineAudioDestinationH
andler::runOfflineRendering, this)); |
| 96 } | 103 } |
| 97 | 104 |
| 98 void OfflineAudioDestinationHandler::stopRendering() | 105 void OfflineAudioDestinationHandler::stopRendering() |
| 99 { | 106 { |
| 100 ASSERT_NOT_REACHED(); | 107 ASSERT_NOT_REACHED(); |
| 101 } | 108 } |
| 102 | 109 |
| 103 void OfflineAudioDestinationHandler::offlineRender() | 110 size_t OfflineAudioDestinationHandler::quantizeTimeToRenderQuantum(double when)
const |
| 104 { | 111 { |
| 105 offlineRenderInternal(); | 112 ASSERT(when >= 0); |
| 106 context()->handlePostRenderTasks(); | 113 |
| 114 size_t whenAsFrame = when * sampleRate(); |
| 115 return whenAsFrame - (whenAsFrame % renderQuantumSize); |
| 107 } | 116 } |
| 108 | 117 |
| 109 void OfflineAudioDestinationHandler::offlineRenderInternal() | 118 WebThread* OfflineAudioDestinationHandler::offlineRenderThread() const |
| 119 { |
| 120 ASSERT(m_renderThread); |
| 121 return m_renderThread; |
| 122 } |
| 123 |
| 124 void OfflineAudioDestinationHandler::startOfflineRendering() |
| 110 { | 125 { |
| 111 ASSERT(!isMainThread()); | 126 ASSERT(!isMainThread()); |
| 127 ASSERT(context()->isOfflineContext()); |
| 128 |
| 112 ASSERT(m_renderBus); | 129 ASSERT(m_renderBus); |
| 113 if (!m_renderBus) | 130 if (!m_renderBus) |
| 114 return; | 131 return; |
| 115 | 132 |
| 116 bool isAudioContextInitialized = context()->isInitialized(); | 133 bool isAudioContextInitialized = context()->isInitialized(); |
| 117 ASSERT(isAudioContextInitialized); | 134 ASSERT(isAudioContextInitialized); |
| 118 if (!isAudioContextInitialized) | 135 if (!isAudioContextInitialized) |
| 119 return; | 136 return; |
| 120 | 137 |
| 121 bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numb
erOfChannels(); | 138 bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numb
erOfChannels(); |
| 122 ASSERT(channelsMatch); | 139 ASSERT(channelsMatch); |
| 123 if (!channelsMatch) | 140 if (!channelsMatch) |
| 124 return; | 141 return; |
| 125 | 142 |
| 126 bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize; | 143 bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize; |
| 127 ASSERT(isRenderBusAllocated); | 144 ASSERT(isRenderBusAllocated); |
| 128 if (!isRenderBusAllocated) | 145 if (!isRenderBusAllocated) |
| 129 return; | 146 return; |
| 130 | 147 |
| 131 // Break up the render target into smaller "render quantize" sized pieces. | 148 m_framesToProcess = m_renderTarget->length(); |
| 132 // Render until we're finished. | 149 |
| 133 size_t framesToProcess = m_renderTarget->length(); | 150 // Start rendering. |
| 151 runOfflineRendering(); |
| 152 } |
| 153 |
| 154 void OfflineAudioDestinationHandler::runOfflineRendering() |
| 155 { |
| 156 ASSERT(!isMainThread()); |
| 157 ASSERT(context()->isOfflineContext()); |
| 158 |
| 134 unsigned numberOfChannels = m_renderTarget->numberOfChannels(); | 159 unsigned numberOfChannels = m_renderTarget->numberOfChannels(); |
| 135 | 160 |
| 136 unsigned n = 0; | 161 // If there is more to process and there is no suspension at the moment, |
| 137 while (framesToProcess > 0) { | 162 // do continue to render quanta. If there is a suspend scheduled at the |
| 138 // Render one render quantum. | 163 // current sample frame, stop the render loop and put the context into the |
| 164 // suspended state. Then calling OfflineAudioContext.resume() will pick up |
| 165 // the render loop again from where it was suspended. |
| 166 while (m_framesToProcess > 0 && !context()->shouldSuspendNow()) { |
| 167 |
| 168 // Render one render quantum. Note that this includes pre/post render |
| 169 // tasks from the online audio context. |
| 139 render(0, m_renderBus.get(), renderQuantumSize); | 170 render(0, m_renderBus.get(), renderQuantumSize); |
| 140 | 171 |
| 141 size_t framesAvailableToCopy = std::min(framesToProcess, renderQuantumSi
ze); | 172 size_t framesAvailableToCopy = std::min(m_framesToProcess, renderQuantum
Size); |
| 142 | 173 |
| 143 for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++chann
elIndex) { | 174 for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++chann
elIndex) { |
| 144 const float* source = m_renderBus->channel(channelIndex)->data(); | 175 const float* source = m_renderBus->channel(channelIndex)->data(); |
| 145 float* destination = m_renderTarget->getChannelData(channelIndex)->d
ata(); | 176 float* destination = m_renderTarget->getChannelData(channelIndex)->d
ata(); |
| 146 memcpy(destination + n, source, sizeof(float) * framesAvailableToCop
y); | 177 memcpy(destination + m_framesProcessed, source, sizeof(float) * fram
esAvailableToCopy); |
| 147 } | 178 } |
| 148 | 179 |
| 149 n += framesAvailableToCopy; | 180 m_framesProcessed += framesAvailableToCopy; |
| 150 framesToProcess -= framesAvailableToCopy; | 181 m_framesToProcess -= framesAvailableToCopy; |
| 151 } | 182 } |
| 152 | 183 |
| 184 // Finish up the rendering loop if there is no more to process. |
| 185 if (m_framesToProcess <= 0) { |
| 186 ASSERT(m_framesToProcess == 0); |
| 187 finishOfflineRendering(); |
| 188 return; |
| 189 } |
| 190 |
| 191 // Otherwise resolve pending suspend promises. |
| 192 context()->resolvePendingSuspendPromises(); |
| 193 } |
| 194 |
| 195 void OfflineAudioDestinationHandler::finishOfflineRendering() |
| 196 { |
| 197 ASSERT(!isMainThread()); |
| 198 ASSERT(context()->isOfflineContext()); |
| 199 |
| 153 // Our work is done. Let the AudioContext know. | 200 // Our work is done. Let the AudioContext know. |
| 154 if (context()->executionContext()) | 201 if (context()->executionContext()) |
| 155 context()->executionContext()->postTask(FROM_HERE, createCrossThreadTask
(&OfflineAudioDestinationHandler::notifyComplete, PassRefPtr<OfflineAudioDestina
tionHandler>(this))); | 202 context()->executionContext()->postTask(FROM_HERE, createCrossThreadTask
(&OfflineAudioDestinationHandler::notifyComplete, this)); |
| 156 } | 203 } |
| 157 | 204 |
| 158 void OfflineAudioDestinationHandler::notifyComplete() | 205 void OfflineAudioDestinationHandler::notifyComplete() |
| 159 { | 206 { |
| 160 // The AudioContext might be gone. | 207 // The OfflineAudioContext might be gone. |
| 161 if (context()) | 208 if (context()) |
| 162 context()->fireCompletionEvent(); | 209 context()->fireCompletionEvent(); |
| 163 } | 210 } |
| 164 | 211 |
| 165 // ---------------------------------------------------------------- | 212 // ---------------------------------------------------------------- |
| 166 | 213 |
| 167 OfflineAudioDestinationNode::OfflineAudioDestinationNode(AudioContext& context,
AudioBuffer* renderTarget) | 214 OfflineAudioDestinationNode::OfflineAudioDestinationNode(AudioContext& context,
AudioBuffer* renderTarget) |
| 168 : AudioDestinationNode(context) | 215 : AudioDestinationNode(context) |
| 169 { | 216 { |
| 170 setHandler(OfflineAudioDestinationHandler::create(*this, renderTarget)); | 217 setHandler(OfflineAudioDestinationHandler::create(*this, renderTarget)); |
| 171 } | 218 } |
| 172 | 219 |
| 173 OfflineAudioDestinationNode* OfflineAudioDestinationNode::create(AudioContext* c
ontext, AudioBuffer* renderTarget) | 220 OfflineAudioDestinationNode* OfflineAudioDestinationNode::create(AudioContext* c
ontext, AudioBuffer* renderTarget) |
| 174 { | 221 { |
| 175 return new OfflineAudioDestinationNode(*context, renderTarget); | 222 return new OfflineAudioDestinationNode(*context, renderTarget); |
| 176 } | 223 } |
| 177 | 224 |
| 178 } // namespace blink | 225 } // namespace blink |
| 179 | 226 |
| 180 #endif // ENABLE(WEB_AUDIO) | 227 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |