| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010, Google Inc. All rights reserved. | 2 * Copyright (C) 2010, 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 AN
Y | 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y |
| 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 O
N | 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N |
| 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 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 | 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 "modules/webaudio/ScriptProcessorNode.h" | 25 #include "modules/webaudio/ScriptProcessorNode.h" |
| 26 #include "bindings/core/v8/ExceptionState.h" | 26 #include "bindings/core/v8/ExceptionState.h" |
| 27 #include "core/dom/CrossThreadTask.h" | 27 #include "core/dom/CrossThreadTask.h" |
| 28 #include "core/dom/ExceptionCode.h" | 28 #include "core/dom/ExceptionCode.h" |
| 29 #include "core/dom/ExecutionContext.h" | 29 #include "core/dom/ExecutionContext.h" |
| 30 #include "modules/webaudio/AbstractAudioContext.h" | |
| 31 #include "modules/webaudio/AudioBuffer.h" | 30 #include "modules/webaudio/AudioBuffer.h" |
| 32 #include "modules/webaudio/AudioNodeInput.h" | 31 #include "modules/webaudio/AudioNodeInput.h" |
| 33 #include "modules/webaudio/AudioNodeOutput.h" | 32 #include "modules/webaudio/AudioNodeOutput.h" |
| 34 #include "modules/webaudio/AudioProcessingEvent.h" | 33 #include "modules/webaudio/AudioProcessingEvent.h" |
| 34 #include "modules/webaudio/BaseAudioContext.h" |
| 35 #include "public/platform/Platform.h" | 35 #include "public/platform/Platform.h" |
| 36 | 36 |
| 37 namespace blink { | 37 namespace blink { |
| 38 | 38 |
| 39 ScriptProcessorHandler::ScriptProcessorHandler(AudioNode& node, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) | 39 ScriptProcessorHandler::ScriptProcessorHandler(AudioNode& node, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) |
| 40 : AudioHandler(NodeTypeJavaScript, node, sampleRate) | 40 : AudioHandler(NodeTypeJavaScript, node, sampleRate) |
| 41 , m_doubleBufferIndex(0) | 41 , m_doubleBufferIndex(0) |
| 42 , m_bufferSize(bufferSize) | 42 , m_bufferSize(bufferSize) |
| 43 , m_bufferReadWriteIndex(0) | 43 , m_bufferReadWriteIndex(0) |
| 44 , m_numberOfInputChannels(numberOfInputChannels) | 44 , m_numberOfInputChannels(numberOfInputChannels) |
| 45 , m_numberOfOutputChannels(numberOfOutputChannels) | 45 , m_numberOfOutputChannels(numberOfOutputChannels) |
| 46 , m_internalInputBus(AudioBus::create(numberOfInputChannels, ProcessingSizeI
nFrames, false)) | 46 , m_internalInputBus(AudioBus::create(numberOfInputChannels, ProcessingSizeI
nFrames, false)) |
| 47 { | 47 { |
| 48 // Regardless of the allowed buffer sizes, we still need to process at the g
ranularity of the AudioNode. | 48 // Regardless of the allowed buffer sizes, we still need to process at the g
ranularity of the AudioNode. |
| 49 if (m_bufferSize < ProcessingSizeInFrames) | 49 if (m_bufferSize < ProcessingSizeInFrames) |
| 50 m_bufferSize = ProcessingSizeInFrames; | 50 m_bufferSize = ProcessingSizeInFrames; |
| 51 | 51 |
| 52 ASSERT(numberOfInputChannels <= AbstractAudioContext::maxNumberOfChannels())
; | 52 DCHECK_LE(numberOfInputChannels, BaseAudioContext::maxNumberOfChannels()); |
| 53 | 53 |
| 54 addInput(); | 54 addInput(); |
| 55 addOutput(numberOfOutputChannels); | 55 addOutput(numberOfOutputChannels); |
| 56 | 56 |
| 57 m_channelCount = numberOfInputChannels; | 57 m_channelCount = numberOfInputChannels; |
| 58 m_channelCountMode = Explicit; | 58 m_channelCountMode = Explicit; |
| 59 | 59 |
| 60 initialize(); | 60 initialize(); |
| 61 } | 61 } |
| 62 | 62 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 } | 206 } |
| 207 | 207 |
| 208 double ScriptProcessorHandler::latencyTime() const | 208 double ScriptProcessorHandler::latencyTime() const |
| 209 { | 209 { |
| 210 return std::numeric_limits<double>::infinity(); | 210 return std::numeric_limits<double>::infinity(); |
| 211 } | 211 } |
| 212 | 212 |
| 213 void ScriptProcessorHandler::setChannelCount(unsigned long channelCount, Excepti
onState& exceptionState) | 213 void ScriptProcessorHandler::setChannelCount(unsigned long channelCount, Excepti
onState& exceptionState) |
| 214 { | 214 { |
| 215 ASSERT(isMainThread()); | 215 ASSERT(isMainThread()); |
| 216 AbstractAudioContext::AutoLocker locker(context()); | 216 BaseAudioContext::AutoLocker locker(context()); |
| 217 | 217 |
| 218 if (channelCount != m_channelCount) { | 218 if (channelCount != m_channelCount) { |
| 219 exceptionState.throwDOMException( | 219 exceptionState.throwDOMException( |
| 220 NotSupportedError, | 220 NotSupportedError, |
| 221 "channelCount cannot be changed from " + String::number(m_channelCou
nt) + " to " + String::number(channelCount)); | 221 "channelCount cannot be changed from " + String::number(m_channelCou
nt) + " to " + String::number(channelCount)); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 void ScriptProcessorHandler::setChannelCountMode(const String& mode, ExceptionSt
ate& exceptionState) | 225 void ScriptProcessorHandler::setChannelCountMode(const String& mode, ExceptionSt
ate& exceptionState) |
| 226 { | 226 { |
| 227 ASSERT(isMainThread()); | 227 ASSERT(isMainThread()); |
| 228 AbstractAudioContext::AutoLocker locker(context()); | 228 BaseAudioContext::AutoLocker locker(context()); |
| 229 | 229 |
| 230 if ((mode == "max") || (mode == "clamped-max")) { | 230 if ((mode == "max") || (mode == "clamped-max")) { |
| 231 exceptionState.throwDOMException( | 231 exceptionState.throwDOMException( |
| 232 NotSupportedError, | 232 NotSupportedError, |
| 233 "channelCountMode cannot be changed from 'explicit' to '" + mode + "
'"); | 233 "channelCountMode cannot be changed from 'explicit' to '" + mode + "
'"); |
| 234 } | 234 } |
| 235 } | 235 } |
| 236 | 236 |
| 237 // ---------------------------------------------------------------- | 237 // ---------------------------------------------------------------- |
| 238 | 238 |
| 239 ScriptProcessorNode::ScriptProcessorNode(AbstractAudioContext& context, float sa
mpleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOu
tputChannels) | 239 ScriptProcessorNode::ScriptProcessorNode(BaseAudioContext& context, float sample
Rate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutput
Channels) |
| 240 : AudioNode(context) | 240 : AudioNode(context) |
| 241 , ActiveScriptWrappable(this) | 241 , ActiveScriptWrappable(this) |
| 242 { | 242 { |
| 243 setHandler(ScriptProcessorHandler::create(*this, sampleRate, bufferSize, num
berOfInputChannels, numberOfOutputChannels)); | 243 setHandler(ScriptProcessorHandler::create(*this, sampleRate, bufferSize, num
berOfInputChannels, numberOfOutputChannels)); |
| 244 } | 244 } |
| 245 | 245 |
| 246 static size_t chooseBufferSize() | 246 static size_t chooseBufferSize() |
| 247 { | 247 { |
| 248 // Choose a buffer size based on the audio hardware buffer size. Arbitarily
make it a power of | 248 // Choose a buffer size based on the audio hardware buffer size. Arbitarily
make it a power of |
| 249 // two that is 4 times greater than the hardware buffer size. | 249 // two that is 4 times greater than the hardware buffer size. |
| 250 // FIXME: What is the best way to choose this? | 250 // FIXME: What is the best way to choose this? |
| 251 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); | 251 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); |
| 252 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize)
+ 0.5); | 252 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize)
+ 0.5); |
| 253 | 253 |
| 254 if (bufferSize < 256) | 254 if (bufferSize < 256) |
| 255 return 256; | 255 return 256; |
| 256 if (bufferSize > 16384) | 256 if (bufferSize > 16384) |
| 257 return 16384; | 257 return 16384; |
| 258 | 258 |
| 259 return bufferSize; | 259 return bufferSize; |
| 260 } | 260 } |
| 261 | 261 |
| 262 ScriptProcessorNode* ScriptProcessorNode::create( | 262 ScriptProcessorNode* ScriptProcessorNode::create( |
| 263 AbstractAudioContext& context, | 263 BaseAudioContext& context, |
| 264 ExceptionState& exceptionState) | 264 ExceptionState& exceptionState) |
| 265 { | 265 { |
| 266 DCHECK(isMainThread()); | 266 DCHECK(isMainThread()); |
| 267 | 267 |
| 268 // Default buffer size is 0 (let WebAudio choose) with 2 inputs and 2 | 268 // Default buffer size is 0 (let WebAudio choose) with 2 inputs and 2 |
| 269 // outputs. | 269 // outputs. |
| 270 return create(context, 0, 2, 2, exceptionState); | 270 return create(context, 0, 2, 2, exceptionState); |
| 271 } | 271 } |
| 272 | 272 |
| 273 ScriptProcessorNode* ScriptProcessorNode::create( | 273 ScriptProcessorNode* ScriptProcessorNode::create( |
| 274 AbstractAudioContext& context, | 274 BaseAudioContext& context, |
| 275 size_t bufferSize, | 275 size_t bufferSize, |
| 276 ExceptionState& exceptionState) | 276 ExceptionState& exceptionState) |
| 277 { | 277 { |
| 278 DCHECK(isMainThread()); | 278 DCHECK(isMainThread()); |
| 279 | 279 |
| 280 // Default is 2 inputs and 2 outputs. | 280 // Default is 2 inputs and 2 outputs. |
| 281 return create(context, bufferSize, 2, 2, exceptionState); | 281 return create(context, bufferSize, 2, 2, exceptionState); |
| 282 } | 282 } |
| 283 | 283 |
| 284 ScriptProcessorNode* ScriptProcessorNode::create( | 284 ScriptProcessorNode* ScriptProcessorNode::create( |
| 285 AbstractAudioContext& context, | 285 BaseAudioContext& context, |
| 286 size_t bufferSize, | 286 size_t bufferSize, |
| 287 unsigned numberOfInputChannels, | 287 unsigned numberOfInputChannels, |
| 288 ExceptionState& exceptionState) | 288 ExceptionState& exceptionState) |
| 289 { | 289 { |
| 290 DCHECK(isMainThread()); | 290 DCHECK(isMainThread()); |
| 291 | 291 |
| 292 // Default is 2 outputs. | 292 // Default is 2 outputs. |
| 293 return create(context, bufferSize, numberOfInputChannels, 2, exceptionState)
; | 293 return create(context, bufferSize, numberOfInputChannels, 2, exceptionState)
; |
| 294 } | 294 } |
| 295 | 295 |
| 296 ScriptProcessorNode* ScriptProcessorNode::create( | 296 ScriptProcessorNode* ScriptProcessorNode::create( |
| 297 AbstractAudioContext& context, | 297 BaseAudioContext& context, |
| 298 size_t bufferSize, | 298 size_t bufferSize, |
| 299 unsigned numberOfInputChannels, | 299 unsigned numberOfInputChannels, |
| 300 unsigned numberOfOutputChannels, | 300 unsigned numberOfOutputChannels, |
| 301 ExceptionState& exceptionState) | 301 ExceptionState& exceptionState) |
| 302 { | 302 { |
| 303 DCHECK(isMainThread()); | 303 DCHECK(isMainThread()); |
| 304 | 304 |
| 305 if (context.isContextClosed()) { | 305 if (context.isContextClosed()) { |
| 306 context.throwExceptionForClosedState(exceptionState); | 306 context.throwExceptionForClosedState(exceptionState); |
| 307 return nullptr; | 307 return nullptr; |
| 308 } | 308 } |
| 309 | 309 |
| 310 if (numberOfInputChannels == 0 && numberOfOutputChannels == 0) { | 310 if (numberOfInputChannels == 0 && numberOfOutputChannels == 0) { |
| 311 exceptionState.throwDOMException( | 311 exceptionState.throwDOMException( |
| 312 IndexSizeError, | 312 IndexSizeError, |
| 313 "number of input channels and output channels cannot both be zero.")
; | 313 "number of input channels and output channels cannot both be zero.")
; |
| 314 return nullptr; | 314 return nullptr; |
| 315 } | 315 } |
| 316 | 316 |
| 317 if (numberOfInputChannels > AbstractAudioContext::maxNumberOfChannels()) { | 317 if (numberOfInputChannels > BaseAudioContext::maxNumberOfChannels()) { |
| 318 exceptionState.throwDOMException( | 318 exceptionState.throwDOMException( |
| 319 IndexSizeError, | 319 IndexSizeError, |
| 320 "number of input channels (" + String::number(numberOfInputChannels) | 320 "number of input channels (" + String::number(numberOfInputChannels) |
| 321 + ") exceeds maximum (" | 321 + ") exceeds maximum (" |
| 322 + String::number(AbstractAudioContext::maxNumberOfChannels()) + ")."
); | 322 + String::number(BaseAudioContext::maxNumberOfChannels()) + ")."); |
| 323 return nullptr; | 323 return nullptr; |
| 324 } | 324 } |
| 325 | 325 |
| 326 if (numberOfOutputChannels > AbstractAudioContext::maxNumberOfChannels()) { | 326 if (numberOfOutputChannels > BaseAudioContext::maxNumberOfChannels()) { |
| 327 exceptionState.throwDOMException( | 327 exceptionState.throwDOMException( |
| 328 IndexSizeError, | 328 IndexSizeError, |
| 329 "number of output channels (" + String::number(numberOfInputChannels
) | 329 "number of output channels (" + String::number(numberOfInputChannels
) |
| 330 + ") exceeds maximum (" | 330 + ") exceeds maximum (" |
| 331 + String::number(AbstractAudioContext::maxNumberOfChannels()) + ")."
); | 331 + String::number(BaseAudioContext::maxNumberOfChannels()) + ")."); |
| 332 return nullptr; | 332 return nullptr; |
| 333 } | 333 } |
| 334 | 334 |
| 335 // Check for valid buffer size. | 335 // Check for valid buffer size. |
| 336 switch (bufferSize) { | 336 switch (bufferSize) { |
| 337 case 0: | 337 case 0: |
| 338 bufferSize = chooseBufferSize(); | 338 bufferSize = chooseBufferSize(); |
| 339 break; | 339 break; |
| 340 case 256: | 340 case 256: |
| 341 case 512: | 341 case 512: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 // If |onaudioprocess| event handler is defined, the node should not be | 378 // If |onaudioprocess| event handler is defined, the node should not be |
| 379 // GCed even if it is out of scope. | 379 // GCed even if it is out of scope. |
| 380 if (hasEventListeners(EventTypeNames::audioprocess)) | 380 if (hasEventListeners(EventTypeNames::audioprocess)) |
| 381 return true; | 381 return true; |
| 382 | 382 |
| 383 return false; | 383 return false; |
| 384 } | 384 } |
| 385 | 385 |
| 386 } // namespace blink | 386 } // namespace blink |
| 387 | 387 |
| OLD | NEW |