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 22 matching lines...) Expand all Loading... |
33 #include "modules/webaudio/AudioBuffer.h" | 33 #include "modules/webaudio/AudioBuffer.h" |
34 #include "modules/webaudio/AudioContext.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 #include "wtf/Float32Array.h" | 39 #include "wtf/Float32Array.h" |
40 | 40 |
41 namespace blink { | 41 namespace blink { |
42 | 42 |
43 #if !ENABLE(OILPAN) | |
44 // We need a dedicated specialization for ScriptProcessorNode because it doesn't | |
45 // inherit from RefCounted. | |
46 template<> struct CrossThreadCopierBase<false, false, false, PassRefPtr<ScriptPr
ocessorNode> > : public CrossThreadCopierPassThrough<PassRefPtr<ScriptProcessorN
ode> > { | |
47 }; | |
48 #endif | |
49 | |
50 static size_t chooseBufferSize() | 43 static size_t chooseBufferSize() |
51 { | 44 { |
52 // Choose a buffer size based on the audio hardware buffer size. Arbitarily
make it a power of | 45 // Choose a buffer size based on the audio hardware buffer size. Arbitarily
make it a power of |
53 // two that is 4 times greater than the hardware buffer size. | 46 // two that is 4 times greater than the hardware buffer size. |
54 // FIXME: What is the best way to choose this? | 47 // FIXME: What is the best way to choose this? |
55 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); | 48 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); |
56 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize)
+ 0.5); | 49 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize)
+ 0.5); |
57 | 50 |
58 if (bufferSize < 256) | 51 if (bufferSize < 256) |
59 return 256; | 52 return 256; |
60 if (bufferSize > 16384) | 53 if (bufferSize > 16384) |
61 return 16384; | 54 return 16384; |
62 | 55 |
63 return bufferSize; | 56 return bufferSize; |
64 } | 57 } |
65 | 58 |
66 PassRefPtrWillBeRawPtr<ScriptProcessorNode> ScriptProcessorNode::create(AudioCon
text* context, float sampleRate, size_t bufferSize, unsigned numberOfInputChanne
ls, unsigned numberOfOutputChannels) | 59 ScriptProcessorNode* ScriptProcessorNode::create(AudioContext* context, float sa
mpleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOu
tputChannels) |
67 { | 60 { |
68 // Check for valid buffer size. | 61 // Check for valid buffer size. |
69 switch (bufferSize) { | 62 switch (bufferSize) { |
70 case 0: | 63 case 0: |
71 bufferSize = chooseBufferSize(); | 64 bufferSize = chooseBufferSize(); |
72 break; | 65 break; |
73 case 256: | 66 case 256: |
74 case 512: | 67 case 512: |
75 case 1024: | 68 case 1024: |
76 case 2048: | 69 case 2048: |
77 case 4096: | 70 case 4096: |
78 case 8192: | 71 case 8192: |
79 case 16384: | 72 case 16384: |
80 break; | 73 break; |
81 default: | 74 default: |
82 return nullptr; | 75 return 0; |
83 } | 76 } |
84 | 77 |
85 if (!numberOfInputChannels && !numberOfOutputChannels) | 78 if (!numberOfInputChannels && !numberOfOutputChannels) |
86 return nullptr; | 79 return 0; |
87 | 80 |
88 if (numberOfInputChannels > AudioContext::maxNumberOfChannels()) | 81 if (numberOfInputChannels > AudioContext::maxNumberOfChannels()) |
89 return nullptr; | 82 return 0; |
90 | 83 |
91 if (numberOfOutputChannels > AudioContext::maxNumberOfChannels()) | 84 if (numberOfOutputChannels > AudioContext::maxNumberOfChannels()) |
92 return nullptr; | 85 return 0; |
93 | 86 |
94 return adoptRefWillBeNoop(new ScriptProcessorNode(context, sampleRate, buffe
rSize, numberOfInputChannels, numberOfOutputChannels)); | 87 return new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInpu
tChannels, numberOfOutputChannels); |
95 } | 88 } |
96 | 89 |
97 ScriptProcessorNode::ScriptProcessorNode(AudioContext* context, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) | 90 ScriptProcessorNode::ScriptProcessorNode(AudioContext* context, float sampleRate
, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan
nels) |
98 : AudioNode(context, sampleRate) | 91 : AudioNode(context, sampleRate) |
99 , m_doubleBufferIndex(0) | 92 , m_doubleBufferIndex(0) |
100 , m_doubleBufferIndexForEvent(0) | 93 , m_doubleBufferIndexForEvent(0) |
101 , m_bufferSize(bufferSize) | 94 , m_bufferSize(bufferSize) |
102 , m_bufferReadWriteIndex(0) | 95 , m_bufferReadWriteIndex(0) |
103 , m_numberOfInputChannels(numberOfInputChannels) | 96 , m_numberOfInputChannels(numberOfInputChannels) |
104 , m_numberOfOutputChannels(numberOfOutputChannels) | 97 , m_numberOfOutputChannels(numberOfOutputChannels) |
(...skipping 28 matching lines...) Expand all Loading... |
133 void ScriptProcessorNode::initialize() | 126 void ScriptProcessorNode::initialize() |
134 { | 127 { |
135 if (isInitialized()) | 128 if (isInitialized()) |
136 return; | 129 return; |
137 | 130 |
138 float sampleRate = context()->sampleRate(); | 131 float sampleRate = context()->sampleRate(); |
139 | 132 |
140 // Create double buffers on both the input and output sides. | 133 // Create double buffers on both the input and output sides. |
141 // These AudioBuffers will be directly accessed in the main thread by JavaSc
ript. | 134 // These AudioBuffers will be directly accessed in the main thread by JavaSc
ript. |
142 for (unsigned i = 0; i < 2; ++i) { | 135 for (unsigned i = 0; i < 2; ++i) { |
143 RefPtrWillBeRawPtr<AudioBuffer> inputBuffer = m_numberOfInputChannels ?
AudioBuffer::create(m_numberOfInputChannels, bufferSize(), sampleRate) : nullptr
; | 136 AudioBuffer* inputBuffer = m_numberOfInputChannels ? AudioBuffer::create
(m_numberOfInputChannels, bufferSize(), sampleRate) : 0; |
144 RefPtrWillBeRawPtr<AudioBuffer> outputBuffer = m_numberOfOutputChannels
? AudioBuffer::create(m_numberOfOutputChannels, bufferSize(), sampleRate) : null
ptr; | 137 AudioBuffer* outputBuffer = m_numberOfOutputChannels ? AudioBuffer::crea
te(m_numberOfOutputChannels, bufferSize(), sampleRate) : 0; |
145 | 138 |
146 m_inputBuffers.append(inputBuffer); | 139 m_inputBuffers.append(inputBuffer); |
147 m_outputBuffers.append(outputBuffer); | 140 m_outputBuffers.append(outputBuffer); |
148 } | 141 } |
149 | 142 |
150 AudioNode::initialize(); | 143 AudioNode::initialize(); |
151 } | 144 } |
152 | 145 |
153 void ScriptProcessorNode::uninitialize() | 146 void ScriptProcessorNode::uninitialize() |
154 { | 147 { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 // This could be a problem if the main thread is very busy doing other t
hings and is being held up handling previous requests. | 221 // This could be a problem if the main thread is very busy doing other t
hings and is being held up handling previous requests. |
229 // The audio thread can't block on this lock, so we call tryLock() inste
ad. | 222 // The audio thread can't block on this lock, so we call tryLock() inste
ad. |
230 MutexTryLocker tryLocker(m_processEventLock); | 223 MutexTryLocker tryLocker(m_processEventLock); |
231 if (!tryLocker.locked()) { | 224 if (!tryLocker.locked()) { |
232 // We're late in handling the previous request. The main thread must
be very busy. | 225 // We're late in handling the previous request. The main thread must
be very busy. |
233 // The best we can do is clear out the buffer ourself here. | 226 // The best we can do is clear out the buffer ourself here. |
234 outputBuffer->zero(); | 227 outputBuffer->zero(); |
235 } else if (context()->executionContext()) { | 228 } else if (context()->executionContext()) { |
236 // Fire the event on the main thread, not this one (which is the rea
ltime audio thread). | 229 // Fire the event on the main thread, not this one (which is the rea
ltime audio thread). |
237 m_doubleBufferIndexForEvent = m_doubleBufferIndex; | 230 m_doubleBufferIndexForEvent = m_doubleBufferIndex; |
238 context()->executionContext()->postTask(createCrossThreadTask(&Scrip
tProcessorNode::fireProcessEvent, PassRefPtrWillBeRawPtr<ScriptProcessorNode>(th
is))); | 231 context()->executionContext()->postTask(createCrossThreadTask(&Scrip
tProcessorNode::fireProcessEvent, this)); |
239 } | 232 } |
240 | 233 |
241 swapBuffers(); | 234 swapBuffers(); |
242 } | 235 } |
243 } | 236 } |
244 | 237 |
245 void ScriptProcessorNode::fireProcessEvent() | 238 void ScriptProcessorNode::fireProcessEvent() |
246 { | 239 { |
247 ASSERT(isMainThread()); | 240 ASSERT(isMainThread()); |
248 | 241 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 void ScriptProcessorNode::trace(Visitor* visitor) | 277 void ScriptProcessorNode::trace(Visitor* visitor) |
285 { | 278 { |
286 visitor->trace(m_inputBuffers); | 279 visitor->trace(m_inputBuffers); |
287 visitor->trace(m_outputBuffers); | 280 visitor->trace(m_outputBuffers); |
288 AudioNode::trace(visitor); | 281 AudioNode::trace(visitor); |
289 } | 282 } |
290 | 283 |
291 } // namespace blink | 284 } // namespace blink |
292 | 285 |
293 #endif // ENABLE(WEB_AUDIO) | 286 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |