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 18 matching lines...) Expand all Loading... |
29 #include "modules/webaudio/AudioNodeInput.h" | 29 #include "modules/webaudio/AudioNodeInput.h" |
30 #include "modules/webaudio/AudioNodeOutput.h" | 30 #include "modules/webaudio/AudioNodeOutput.h" |
31 #include "modules/webaudio/ConvolverNode.h" | 31 #include "modules/webaudio/ConvolverNode.h" |
32 #include "modules/webaudio/ConvolverOptions.h" | 32 #include "modules/webaudio/ConvolverOptions.h" |
33 #include "platform/audio/Reverb.h" | 33 #include "platform/audio/Reverb.h" |
34 #include "wtf/PtrUtil.h" | 34 #include "wtf/PtrUtil.h" |
35 #include <memory> | 35 #include <memory> |
36 | 36 |
37 // Note about empirical tuning: | 37 // Note about empirical tuning: |
38 // The maximum FFT size affects reverb performance and accuracy. | 38 // The maximum FFT size affects reverb performance and accuracy. |
39 // If the reverb is single-threaded and processes entirely in the real-time audi
o thread, | 39 // If the reverb is single-threaded and processes entirely in the real-time |
40 // it's important not to make this too high. In this case 8192 is a good value. | 40 // audio thread, it's important not to make this too high. In this case 8192 is |
41 // But, the Reverb object is multi-threaded, so we want this as high as possible
without losing too much accuracy. | 41 // a good value. But, the Reverb object is multi-threaded, so we want this as |
42 // Very large FFTs will have worse phase errors. Given these constraints 32768 i
s a good compromise. | 42 // high as possible without losing too much accuracy. Very large FFTs will have |
| 43 // worse phase errors. Given these constraints 32768 is a good compromise. |
43 const size_t MaxFFTSize = 32768; | 44 const size_t MaxFFTSize = 32768; |
44 | 45 |
45 namespace blink { | 46 namespace blink { |
46 | 47 |
47 ConvolverHandler::ConvolverHandler(AudioNode& node, float sampleRate) | 48 ConvolverHandler::ConvolverHandler(AudioNode& node, float sampleRate) |
48 : AudioHandler(NodeTypeConvolver, node, sampleRate), m_normalize(true) { | 49 : AudioHandler(NodeTypeConvolver, node, sampleRate), m_normalize(true) { |
49 addInput(); | 50 addInput(); |
50 addOutput(2); | 51 addOutput(2); |
51 | 52 |
52 // Node-specific default mixing rules. | 53 // Node-specific default mixing rules. |
(...skipping 17 matching lines...) Expand all Loading... |
70 AudioBus* outputBus = output(0).bus(); | 71 AudioBus* outputBus = output(0).bus(); |
71 DCHECK(outputBus); | 72 DCHECK(outputBus); |
72 | 73 |
73 // Synchronize with possible dynamic changes to the impulse response. | 74 // Synchronize with possible dynamic changes to the impulse response. |
74 MutexTryLocker tryLocker(m_processLock); | 75 MutexTryLocker tryLocker(m_processLock); |
75 if (tryLocker.locked()) { | 76 if (tryLocker.locked()) { |
76 if (!isInitialized() || !m_reverb) { | 77 if (!isInitialized() || !m_reverb) { |
77 outputBus->zero(); | 78 outputBus->zero(); |
78 } else { | 79 } else { |
79 // Process using the convolution engine. | 80 // Process using the convolution engine. |
80 // Note that we can handle the case where nothing is connected to the inpu
t, in which case we'll just feed silence into the convolver. | 81 // Note that we can handle the case where nothing is connected to the |
81 // FIXME: If we wanted to get fancy we could try to factor in the 'tail t
ime' and stop processing once the tail dies down if | 82 // input, in which case we'll just feed silence into the convolver. |
| 83 // FIXME: If we wanted to get fancy we could try to factor in the 'tail |
| 84 // time' and stop processing once the tail dies down if |
82 // we keep getting fed silence. | 85 // we keep getting fed silence. |
83 m_reverb->process(input(0).bus(), outputBus, framesToProcess); | 86 m_reverb->process(input(0).bus(), outputBus, framesToProcess); |
84 } | 87 } |
85 } else { | 88 } else { |
86 // Too bad - the tryLock() failed. We must be in the middle of setting a ne
w impulse response. | 89 // Too bad - the tryLock() failed. We must be in the middle of setting a |
| 90 // new impulse response. |
87 outputBus->zero(); | 91 outputBus->zero(); |
88 } | 92 } |
89 } | 93 } |
90 | 94 |
91 void ConvolverHandler::setBuffer(AudioBuffer* buffer, | 95 void ConvolverHandler::setBuffer(AudioBuffer* buffer, |
92 ExceptionState& exceptionState) { | 96 ExceptionState& exceptionState) { |
93 DCHECK(isMainThread()); | 97 DCHECK(isMainThread()); |
94 | 98 |
95 if (!buffer) | 99 if (!buffer) |
96 return; | 100 return; |
97 | 101 |
98 if (buffer->sampleRate() != context()->sampleRate()) { | 102 if (buffer->sampleRate() != context()->sampleRate()) { |
99 exceptionState.throwDOMException( | 103 exceptionState.throwDOMException( |
100 NotSupportedError, | 104 NotSupportedError, |
101 "The buffer sample rate of " + String::number(buffer->sampleRate()) + | 105 "The buffer sample rate of " + String::number(buffer->sampleRate()) + |
102 " does not match the context rate of " + | 106 " does not match the context rate of " + |
103 String::number(context()->sampleRate()) + " Hz."); | 107 String::number(context()->sampleRate()) + " Hz."); |
104 return; | 108 return; |
105 } | 109 } |
106 | 110 |
107 unsigned numberOfChannels = buffer->numberOfChannels(); | 111 unsigned numberOfChannels = buffer->numberOfChannels(); |
108 size_t bufferLength = buffer->length(); | 112 size_t bufferLength = buffer->length(); |
109 | 113 |
110 // The current implementation supports only 1-, 2-, or 4-channel impulse respo
nses, with the | 114 // The current implementation supports only 1-, 2-, or 4-channel impulse |
111 // 4-channel response being interpreted as true-stereo (see Reverb class). | 115 // responses, with the 4-channel response being interpreted as true-stereo |
| 116 // (see Reverb class). |
112 bool isChannelCountGood = | 117 bool isChannelCountGood = |
113 numberOfChannels == 1 || numberOfChannels == 2 || numberOfChannels == 4; | 118 numberOfChannels == 1 || numberOfChannels == 2 || numberOfChannels == 4; |
114 | 119 |
115 if (!isChannelCountGood) { | 120 if (!isChannelCountGood) { |
116 exceptionState.throwDOMException( | 121 exceptionState.throwDOMException( |
117 NotSupportedError, "The buffer must have 1, 2, or 4 channels, not " + | 122 NotSupportedError, "The buffer must have 1, 2, or 4 channels, not " + |
118 String::number(numberOfChannels)); | 123 String::number(numberOfChannels)); |
119 return; | 124 return; |
120 } | 125 } |
121 | 126 |
122 // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not
a memcpy(). | 127 // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not |
123 // This memory is simply used in the Reverb constructor and no reference to it
is kept for later use in that class. | 128 // a memcpy(). This memory is simply used in the Reverb constructor and no |
| 129 // reference to it is kept for later use in that class. |
124 RefPtr<AudioBus> bufferBus = | 130 RefPtr<AudioBus> bufferBus = |
125 AudioBus::create(numberOfChannels, bufferLength, false); | 131 AudioBus::create(numberOfChannels, bufferLength, false); |
126 for (unsigned i = 0; i < numberOfChannels; ++i) | 132 for (unsigned i = 0; i < numberOfChannels; ++i) |
127 bufferBus->setChannelMemory(i, buffer->getChannelData(i)->data(), | 133 bufferBus->setChannelMemory(i, buffer->getChannelData(i)->data(), |
128 bufferLength); | 134 bufferLength); |
129 | 135 |
130 bufferBus->setSampleRate(buffer->sampleRate()); | 136 bufferBus->setSampleRate(buffer->sampleRate()); |
131 | 137 |
132 // Create the reverb with the given impulse response. | 138 // Create the reverb with the given impulse response. |
133 std::unique_ptr<Reverb> reverb = wrapUnique( | 139 std::unique_ptr<Reverb> reverb = wrapUnique( |
(...skipping 13 matching lines...) Expand all Loading... |
147 return m_buffer.get(); | 153 return m_buffer.get(); |
148 } | 154 } |
149 | 155 |
150 double ConvolverHandler::tailTime() const { | 156 double ConvolverHandler::tailTime() const { |
151 MutexTryLocker tryLocker(m_processLock); | 157 MutexTryLocker tryLocker(m_processLock); |
152 if (tryLocker.locked()) | 158 if (tryLocker.locked()) |
153 return m_reverb | 159 return m_reverb |
154 ? m_reverb->impulseResponseLength() / | 160 ? m_reverb->impulseResponseLength() / |
155 static_cast<double>(sampleRate()) | 161 static_cast<double>(sampleRate()) |
156 : 0; | 162 : 0; |
157 // Since we don't want to block the Audio Device thread, we return a large val
ue | 163 // Since we don't want to block the Audio Device thread, we return a large |
158 // instead of trying to acquire the lock. | 164 // value instead of trying to acquire the lock. |
159 return std::numeric_limits<double>::infinity(); | 165 return std::numeric_limits<double>::infinity(); |
160 } | 166 } |
161 | 167 |
162 double ConvolverHandler::latencyTime() const { | 168 double ConvolverHandler::latencyTime() const { |
163 MutexTryLocker tryLocker(m_processLock); | 169 MutexTryLocker tryLocker(m_processLock); |
164 if (tryLocker.locked()) | 170 if (tryLocker.locked()) |
165 return m_reverb | 171 return m_reverb |
166 ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) | 172 ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) |
167 : 0; | 173 : 0; |
168 // Since we don't want to block the Audio Device thread, we return a large val
ue | 174 // Since we don't want to block the Audio Device thread, we return a large |
169 // instead of trying to acquire the lock. | 175 // value instead of trying to acquire the lock. |
170 return std::numeric_limits<double>::infinity(); | 176 return std::numeric_limits<double>::infinity(); |
171 } | 177 } |
172 | 178 |
173 // ---------------------------------------------------------------- | 179 // ---------------------------------------------------------------- |
174 | 180 |
175 ConvolverNode::ConvolverNode(BaseAudioContext& context) : AudioNode(context) { | 181 ConvolverNode::ConvolverNode(BaseAudioContext& context) : AudioNode(context) { |
176 setHandler(ConvolverHandler::create(*this, context.sampleRate())); | 182 setHandler(ConvolverHandler::create(*this, context.sampleRate())); |
177 } | 183 } |
178 | 184 |
179 ConvolverNode* ConvolverNode::create(BaseAudioContext& context, | 185 ConvolverNode* ConvolverNode::create(BaseAudioContext& context, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 227 |
222 bool ConvolverNode::normalize() const { | 228 bool ConvolverNode::normalize() const { |
223 return convolverHandler().normalize(); | 229 return convolverHandler().normalize(); |
224 } | 230 } |
225 | 231 |
226 void ConvolverNode::setNormalize(bool normalize) { | 232 void ConvolverNode::setNormalize(bool normalize) { |
227 convolverHandler().setNormalize(normalize); | 233 convolverHandler().setNormalize(normalize); |
228 } | 234 } |
229 | 235 |
230 } // namespace blink | 236 } // namespace blink |
OLD | NEW |