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 |
(...skipping 12 matching lines...) Expand all Loading... |
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/ScriptProcessorNode.h" | 27 #include "modules/webaudio/ScriptProcessorNode.h" |
28 | 28 |
29 #include "bindings/core/v8/ExceptionState.h" | 29 #include "bindings/core/v8/ExceptionState.h" |
30 #include "core/dom/CrossThreadTask.h" | 30 #include "core/dom/CrossThreadTask.h" |
31 #include "core/dom/ExceptionCode.h" | 31 #include "core/dom/ExceptionCode.h" |
32 #include "core/dom/ExecutionContext.h" | 32 #include "core/dom/ExecutionContext.h" |
| 33 #include "modules/webaudio/AbstractAudioContext.h" |
33 #include "modules/webaudio/AudioBuffer.h" | 34 #include "modules/webaudio/AudioBuffer.h" |
34 #include "modules/webaudio/AudioContext.h" | |
35 #include "modules/webaudio/AudioNodeInput.h" | 35 #include "modules/webaudio/AudioNodeInput.h" |
36 #include "modules/webaudio/AudioNodeOutput.h" | 36 #include "modules/webaudio/AudioNodeOutput.h" |
37 #include "modules/webaudio/AudioProcessingEvent.h" | 37 #include "modules/webaudio/AudioProcessingEvent.h" |
38 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
39 | 39 |
40 namespace blink { | 40 namespace blink { |
41 | 41 |
42 ScriptProcessorHandler::ScriptProcessorHandler(AudioNode& node, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) | 42 ScriptProcessorHandler::ScriptProcessorHandler(AudioNode& node, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) |
43 : AudioHandler(NodeTypeJavaScript, node, sampleRate) | 43 : AudioHandler(NodeTypeJavaScript, node, sampleRate) |
44 , m_doubleBufferIndex(0) | 44 , m_doubleBufferIndex(0) |
45 , m_doubleBufferIndexForEvent(0) | 45 , m_doubleBufferIndexForEvent(0) |
46 , m_bufferSize(bufferSize) | 46 , m_bufferSize(bufferSize) |
47 , m_bufferReadWriteIndex(0) | 47 , m_bufferReadWriteIndex(0) |
48 , m_numberOfInputChannels(numberOfInputChannels) | 48 , m_numberOfInputChannels(numberOfInputChannels) |
49 , m_numberOfOutputChannels(numberOfOutputChannels) | 49 , m_numberOfOutputChannels(numberOfOutputChannels) |
50 , m_internalInputBus(AudioBus::create(numberOfInputChannels, ProcessingSizeI
nFrames, false)) | 50 , m_internalInputBus(AudioBus::create(numberOfInputChannels, ProcessingSizeI
nFrames, false)) |
51 { | 51 { |
52 // Regardless of the allowed buffer sizes, we still need to process at the g
ranularity of the AudioNode. | 52 // Regardless of the allowed buffer sizes, we still need to process at the g
ranularity of the AudioNode. |
53 if (m_bufferSize < ProcessingSizeInFrames) | 53 if (m_bufferSize < ProcessingSizeInFrames) |
54 m_bufferSize = ProcessingSizeInFrames; | 54 m_bufferSize = ProcessingSizeInFrames; |
55 | 55 |
56 ASSERT(numberOfInputChannels <= AudioContext::maxNumberOfChannels()); | 56 ASSERT(numberOfInputChannels <= AbstractAudioContext::maxNumberOfChannels())
; |
57 | 57 |
58 addInput(); | 58 addInput(); |
59 addOutput(numberOfOutputChannels); | 59 addOutput(numberOfOutputChannels); |
60 | 60 |
61 m_channelCount = numberOfInputChannels; | 61 m_channelCount = numberOfInputChannels; |
62 m_channelCountMode = Explicit; | 62 m_channelCountMode = Explicit; |
63 | 63 |
64 initialize(); | 64 initialize(); |
65 } | 65 } |
66 | 66 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 } | 210 } |
211 | 211 |
212 double ScriptProcessorHandler::latencyTime() const | 212 double ScriptProcessorHandler::latencyTime() const |
213 { | 213 { |
214 return std::numeric_limits<double>::infinity(); | 214 return std::numeric_limits<double>::infinity(); |
215 } | 215 } |
216 | 216 |
217 void ScriptProcessorHandler::setChannelCount(unsigned long channelCount, Excepti
onState& exceptionState) | 217 void ScriptProcessorHandler::setChannelCount(unsigned long channelCount, Excepti
onState& exceptionState) |
218 { | 218 { |
219 ASSERT(isMainThread()); | 219 ASSERT(isMainThread()); |
220 AudioContext::AutoLocker locker(context()); | 220 AbstractAudioContext::AutoLocker locker(context()); |
221 | 221 |
222 if (channelCount != m_channelCount) { | 222 if (channelCount != m_channelCount) { |
223 exceptionState.throwDOMException( | 223 exceptionState.throwDOMException( |
224 NotSupportedError, | 224 NotSupportedError, |
225 "channelCount cannot be changed from " + String::number(m_channelCou
nt) + " to " + String::number(channelCount)); | 225 "channelCount cannot be changed from " + String::number(m_channelCou
nt) + " to " + String::number(channelCount)); |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 void ScriptProcessorHandler::setChannelCountMode(const String& mode, ExceptionSt
ate& exceptionState) | 229 void ScriptProcessorHandler::setChannelCountMode(const String& mode, ExceptionSt
ate& exceptionState) |
230 { | 230 { |
231 ASSERT(isMainThread()); | 231 ASSERT(isMainThread()); |
232 AudioContext::AutoLocker locker(context()); | 232 AbstractAudioContext::AutoLocker locker(context()); |
233 | 233 |
234 if ((mode == "max") || (mode == "clamped-max")) { | 234 if ((mode == "max") || (mode == "clamped-max")) { |
235 exceptionState.throwDOMException( | 235 exceptionState.throwDOMException( |
236 NotSupportedError, | 236 NotSupportedError, |
237 "channelCountMode cannot be changed from 'explicit' to '" + mode + "
'"); | 237 "channelCountMode cannot be changed from 'explicit' to '" + mode + "
'"); |
238 } | 238 } |
239 } | 239 } |
240 | 240 |
241 // ---------------------------------------------------------------- | 241 // ---------------------------------------------------------------- |
242 | 242 |
243 ScriptProcessorNode::ScriptProcessorNode(AudioContext& context, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) | 243 ScriptProcessorNode::ScriptProcessorNode(AbstractAudioContext& context, float sa
mpleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOu
tputChannels) |
244 : AudioNode(context) | 244 : AudioNode(context) |
245 { | 245 { |
246 setHandler(ScriptProcessorHandler::create(*this, sampleRate, bufferSize, num
berOfInputChannels, numberOfOutputChannels)); | 246 setHandler(ScriptProcessorHandler::create(*this, sampleRate, bufferSize, num
berOfInputChannels, numberOfOutputChannels)); |
247 } | 247 } |
248 | 248 |
249 static size_t chooseBufferSize() | 249 static size_t chooseBufferSize() |
250 { | 250 { |
251 // Choose a buffer size based on the audio hardware buffer size. Arbitarily
make it a power of | 251 // Choose a buffer size based on the audio hardware buffer size. Arbitarily
make it a power of |
252 // two that is 4 times greater than the hardware buffer size. | 252 // two that is 4 times greater than the hardware buffer size. |
253 // FIXME: What is the best way to choose this? | 253 // FIXME: What is the best way to choose this? |
254 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); | 254 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); |
255 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize)
+ 0.5); | 255 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize)
+ 0.5); |
256 | 256 |
257 if (bufferSize < 256) | 257 if (bufferSize < 256) |
258 return 256; | 258 return 256; |
259 if (bufferSize > 16384) | 259 if (bufferSize > 16384) |
260 return 16384; | 260 return 16384; |
261 | 261 |
262 return bufferSize; | 262 return bufferSize; |
263 } | 263 } |
264 | 264 |
265 ScriptProcessorNode* ScriptProcessorNode::create(AudioContext& context, float sa
mpleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOu
tputChannels) | 265 ScriptProcessorNode* ScriptProcessorNode::create(AbstractAudioContext& context,
float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned nu
mberOfOutputChannels) |
266 { | 266 { |
267 // Check for valid buffer size. | 267 // Check for valid buffer size. |
268 switch (bufferSize) { | 268 switch (bufferSize) { |
269 case 0: | 269 case 0: |
270 bufferSize = chooseBufferSize(); | 270 bufferSize = chooseBufferSize(); |
271 break; | 271 break; |
272 case 256: | 272 case 256: |
273 case 512: | 273 case 512: |
274 case 1024: | 274 case 1024: |
275 case 2048: | 275 case 2048: |
276 case 4096: | 276 case 4096: |
277 case 8192: | 277 case 8192: |
278 case 16384: | 278 case 16384: |
279 break; | 279 break; |
280 default: | 280 default: |
281 return nullptr; | 281 return nullptr; |
282 } | 282 } |
283 | 283 |
284 if (!numberOfInputChannels && !numberOfOutputChannels) | 284 if (!numberOfInputChannels && !numberOfOutputChannels) |
285 return nullptr; | 285 return nullptr; |
286 | 286 |
287 if (numberOfInputChannels > AudioContext::maxNumberOfChannels()) | 287 if (numberOfInputChannels > AbstractAudioContext::maxNumberOfChannels()) |
288 return nullptr; | 288 return nullptr; |
289 | 289 |
290 if (numberOfOutputChannels > AudioContext::maxNumberOfChannels()) | 290 if (numberOfOutputChannels > AbstractAudioContext::maxNumberOfChannels()) |
291 return nullptr; | 291 return nullptr; |
292 | 292 |
293 return new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInpu
tChannels, numberOfOutputChannels); | 293 return new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInpu
tChannels, numberOfOutputChannels); |
294 } | 294 } |
295 | 295 |
296 size_t ScriptProcessorNode::bufferSize() const | 296 size_t ScriptProcessorNode::bufferSize() const |
297 { | 297 { |
298 return static_cast<ScriptProcessorHandler&>(handler()).bufferSize(); | 298 return static_cast<ScriptProcessorHandler&>(handler()).bufferSize(); |
299 } | 299 } |
300 | 300 |
301 } // namespace blink | 301 } // namespace blink |
302 | 302 |
303 #endif // ENABLE(WEB_AUDIO) | 303 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |