1 /* 2 * Copyright (C) 2010, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 23 */ 24 25 #include "config.h" 26 27 #if ENABLE(WEB_AUDIO) 28 29 #include "modules/webaudio/ScriptProcessorNode.h" 30 31 #include "core/dom/Document.h" 32 #include "core/platform/audio/AudioBus.h" 33 #include "modules/webaudio/AudioBuffer.h" 34 #include "modules/webaudio/AudioContext.h" 35 #include "modules/webaudio/AudioNodeInput.h" 36 #include "modules/webaudio/AudioNodeOutput.h" 37 #include "modules/webaudio/AudioProcessingEvent.h" 38 #include "wtf/Float32Array.h" 39 #include "wtf/MainThread.h" 40 41 namespace WebCore { 42 43 const size_t DefaultBufferSize = 4096; 44 45 PassRefPtr<ScriptProcessorNode> ScriptProcessorNode::create(AudioContext* context, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels) 46 { 47 // Check for valid buffer size. 48 switch (bufferSize) { 49 case 256: 50 case 512: 51 case 1024: 52 case 2048: 53 case 4096: 54 case 8192: 55 case 16384: 56 break; 57 default: 58 return 0; 59 } 60 61 if (!numberOfInputChannels && !numberOfOutputChannels) 62 return 0; 63 64 if (numberOfInputChannels > AudioContext::maxNumberOfChannels()) 65 return 0; 66 67 if (numberOfOutputChannels > AudioContext::maxNumberOfChannels()) 68 return 0; 69 70 return adoptRef(new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInputChannels, numberOfOutputChannels)); 71 } 72 73 ScriptProcessorNode::ScriptProcessorNode(AudioContext* context, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels) 74 : AudioNode(context, sampleRate) 75 , m_doubleBufferIndex(0) 76 , m_doubleBufferIndexForEvent(0) 77 , m_bufferSize(bufferSize) 78 , m_bufferReadWriteIndex(0) 79 , m_isRequestOutstanding(false) 80 , m_numberOfInputChannels(numberOfInputChannels) 81 , m_numberOfOutputChannels(numberOfOutputChannels) 82 , m_internalInputBus(AudioBus::create(numberOfInputChannels, AudioNode::ProcessingSizeInFrames, false)) 83 { 84 ScriptWrappable::init(this); 85 // Regardless of the allowed buffer sizes, we still need to process at the granularity of the AudioNode. 86 if (m_bufferSize < AudioNode::ProcessingSizeInFrames) 87 m_bufferSize = AudioNode::ProcessingSizeInFrames; 88 89 ASSERT(numberOfInputChannels <= AudioContext::maxNumberOfChannels()); 90 91 addInput(adoptPtr(new AudioNodeInput(this))); 92 addOutput(adoptPtr(new AudioNodeOutput(this, numberOfOutputChannels))); 93 94 setNodeType(NodeTypeJavaScript); 95 96 initialize(); 97 } 98 99 ScriptProcessorNode::~ScriptProcessorNode() 100 { 101 uninitialize(); 102 } 103 104 void ScriptProcessorNode::initialize() 105 { 106 if (isInitialized()) 107 return; 108 109 float sampleRate = context()->sampleRate(); 110 111 // Create double buffers on both the input and output sides. 112 // These AudioBuffers will be directly accessed in the main thread by JavaScript. 113 for (unsigned i = 0; i < 2; ++i) { 114 RefPtr<AudioBuffer> inputBuffer = m_numberOfInputChannels ? AudioBuffer::create(m_numberOfInputChannels, bufferSize(), sampleRate) : 0; 115 RefPtr<AudioBuffer> outputBuffer = m_numberOfOutputChannels ? AudioBuffer::create(m_numberOfOutputChannels, bufferSize(), sampleRate) : 0; 116 117 m_inputBuffers.append(inputBuffer); 118 m_outputBuffers.append(outputBuffer); 119 } 120 121 AudioNode::initialize(); 122 } 123 124 void ScriptProcessorNode::uninitialize() 125 { 126 if (!isInitialized()) 127 return; 128 129 m_inputBuffers.clear(); 130 m_outputBuffers.clear(); 131 132 AudioNode::uninitialize(); 133 } 134 135 void ScriptProcessorNode::process(size_t framesToProcess) 136 { 137 // Discussion about inputs and outputs: 138 // As in other AudioNodes, ScriptProcessorNode uses an AudioBus for its input and output (see inputBus and outputBus below). 139 // Additionally, there is a double-buffering for input and output which is exposed directly to JavaScript (see inputBuffer and outputBuffer below). 140 // This node is the producer for inputBuffer and the consumer for outputBuffer. 141 // The JavaScript code is the consumer of inputBuffer and the producer for outputBuffer. 142 143 // Get input and output busses. 144 AudioBus* inputBus = this->input(0)->bus(); 145 AudioBus* outputBus = this->output(0)->bus(); 146 147 // Get input and output buffers. We double-buffer both the input and output sides. 148 unsigned doubleBufferIndex = this->doubleBufferIndex(); 149 bool isDoubleBufferIndexGood = doubleBufferIndex < 2 && doubleBufferIndex < m_inputBuffers.size() && doubleBufferIndex < m_outputBuffers.size(); 150 ASSERT(isDoubleBufferIndexGood); 151 if (!isDoubleBufferIndexGood) 152 return; 153 154 AudioBuffer* inputBuffer = m_inputBuffers[doubleBufferIndex].get(); 155 AudioBuffer* outputBuffer = m_outputBuffers[doubleBufferIndex].get(); 156 157 // Check the consistency of input and output buffers. 158 unsigned numberOfInputChannels = m_internalInputBus->numberOfChannels(); 159 bool buffersAreGood = outputBuffer && bufferSize() == outputBuffer->length() && m_bufferReadWriteIndex + framesToProcess <= bufferSize(); 160 161 // If the number of input channels is zero, it's ok to have inputBuffer = 0. 162 if (m_internalInputBus->numberOfChannels()) 163 buffersAreGood = buffersAreGood && inputBuffer && bufferSize() == inputBuffer->length(); 164 165 ASSERT(buffersAreGood); 166 if (!buffersAreGood) 167 return; 168 169 // We assume that bufferSize() is evenly divisible by framesToProcess - should always be true, but we should still check. 170 bool isFramesToProcessGood = framesToProcess && bufferSize() >= framesToProcess && !(bufferSize() % framesToProcess); 171 ASSERT(isFramesToProcessGood); 172 if (!isFramesToProcessGood) 173 return; 174 175 unsigned numberOfOutputChannels = outputBus->numberOfChannels(); 176 177 bool channelsAreGood = (numberOfInputChannels == m_numberOfInputChannels) && (numberOfOutputChannels == m_numberOfOutputChannels); 178 ASSERT(channelsAreGood); 179 if (!channelsAreGood) 180 return; 181 182 for (unsigned i = 0; i < numberOfInputChannels; i++) 183 m_internalInputBus->setChannelMemory(i, inputBuffer->getChannelData(i)->data() + m_bufferReadWriteIndex, framesToProcess); 184 185 if (numberOfInputChannels) 186 m_internalInputBus->copyFrom(*inputBus); 187 188 // Copy from the output buffer to the output. 189 for (unsigned i = 0; i < numberOfOutputChannels; ++i) 190 memcpy(outputBus->channel(i)->mutableData(), outputBuffer->getChannelData(i)->data() + m_bufferReadWriteIndex, sizeof(float) * framesToProcess); 191 192 // Update the buffering index. 193 m_bufferReadWriteIndex = (m_bufferReadWriteIndex + framesToProcess) % bufferSize(); 194 195 // m_bufferReadWriteIndex will wrap back around to 0 when the current input and output buffers are full. 196 // When this happens, fire an event and swap buffers. 197 if (!m_bufferReadWriteIndex) { 198 // Avoid building up requests on the main thread to fire process events when they're not being handled. 199 // This could be a problem if the main thread is very busy doing other things and is being held up handling previous requests. 200 if (m_isRequestOutstanding) { 201 // We're late in handling the previous request. The main thread must be very busy. 202 // The best we can do is clear out the buffer ourself here. 203 outputBuffer->zero(); 204 } else { 205 // Reference ourself so we don't accidentally get deleted before fireProcessEvent() gets called. 206 ref(); 207 208 // Fire the event on the main thread, not this one (which is the realtime audio thread). 209 m_doubleBufferIndexForEvent = m_doubleBufferIndex; 210 m_isRequestOutstanding = true; 211 callOnMainThread(fireProcessEventDispatch, this); 212 } 213 214 swapBuffers(); 215 } 216 } 217 218 void ScriptProcessorNode::fireProcessEventDispatch(void* userData) 219 { 220 ScriptProcessorNode* jsAudioNode = static_cast<ScriptProcessorNode*>(userData); 221 ASSERT(jsAudioNode); 222 if (!jsAudioNode) 223 return; 224 225 jsAudioNode->fireProcessEvent(); 226 227 // De-reference to match the ref() call in process(). 228 jsAudioNode->deref(); 229 } 230 231 void ScriptProcessorNode::fireProcessEvent() 232 { 233 ASSERT(isMainThread() && m_isRequestOutstanding); 234 235 bool isIndexGood = m_doubleBufferIndexForEvent < 2; 236 ASSERT(isIndexGood); 237 if (!isIndexGood) 238 return; 239 240 AudioBuffer* inputBuffer = m_inputBuffers[m_doubleBufferIndexForEvent].get(); 241 AudioBuffer* outputBuffer = m_outputBuffers[m_doubleBufferIndexForEvent].get(); 242 ASSERT(outputBuffer); 243 if (!outputBuffer) 244 return; 245 246 // Avoid firing the event if the document has already gone away. 247 if (context()->scriptExecutionContext()) { 248 // Let the audio thread know we've gotten to the point where it's OK for it to make another request. 249 m_isRequestOutstanding = false; 250 251 // Call the JavaScript event handler which will do the audio processing. 252 dispatchEvent(AudioProcessingEvent::create(inputBuffer, outputBuffer)); 253 } 254 } 255 256 void ScriptProcessorNode::reset() 257 { 258 m_bufferReadWriteIndex = 0; 259 m_doubleBufferIndex = 0; 260 261 for (unsigned i = 0; i < 2; ++i) { 262 m_inputBuffers[i]->zero(); 263 m_outputBuffers[i]->zero(); 264 } 265 } 266 267 double ScriptProcessorNode::tailTime() const 268 { 269 return std::numeric_limits<double>::infinity(); 270 } 271 272 double ScriptProcessorNode::latencyTime() const 273 { 274 return std::numeric_limits<double>::infinity(); 275 } 276 277 } // namespace WebCore 278 279 #endif // ENABLE(WEB_AUDIO) 280