2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "AudioFileReader.h"
37 #include "ReverbConvolver.h"
39 #include <wtf/MathExtras.h>
40 #include <wtf/OwnPtr.h>
41 #include <wtf/PassOwnPtr.h>
49 // Empirical gain calibration tested across many impulse responses to ensure perceived volume is same as dry (unprocessed) signal
50 const double GainCalibration = -58.0;
52 // A minimum power value to when normalizing a silent (or very quiet) impulse response
53 const double MinPower = 0.000125;
55 static double calculateNormalizationScale(AudioBus* response)
57 // Normalize by RMS power
58 size_t numberOfChannels = response->numberOfChannels();
59 size_t length = response->length();
63 for (size_t i = 0; i < numberOfChannels; ++i) {
65 float* p = response->channel(i)->data();
69 power += sample * sample;
73 power = sqrt(power / (numberOfChannels * length));
75 // Protect against accidental overload
76 if (isinf(power) || isnan(power) || power < MinPower)
79 double scale = 1.0 / power;
81 scale *= pow(10.0, GainCalibration * 0.05); // calibrate to make perceived volume same as unprocessed
83 // True-stereo compensation
84 if (response->numberOfChannels() == 4)
90 Reverb::Reverb(AudioBus* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads, bool normalize)
95 scale = calculateNormalizationScale(impulseResponse);
98 impulseResponse->scale(scale);
101 initialize(impulseResponse, renderSliceSize, maxFFTSize, numberOfChannels, useBackgroundThreads);
103 // Undo scaling since this shouldn't be a destructive operation on impulseResponse.
104 // FIXME: What about roundoff? Perhaps consider making a temporary scaled copy
105 // instead of scaling and unscaling in place.
106 if (normalize && scale)
107 impulseResponse->scale(1.0 / scale);
110 void Reverb::initialize(AudioBus* impulseResponseBuffer, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads)
112 m_impulseResponseLength = impulseResponseBuffer->length();
114 // The reverb can handle a mono impulse response and still do stereo processing
115 size_t numResponseChannels = impulseResponseBuffer->numberOfChannels();
116 m_convolvers.reserveCapacity(numberOfChannels);
118 int convolverRenderPhase = 0;
119 for (size_t i = 0; i < numResponseChannels; ++i) {
120 AudioChannel* channel = impulseResponseBuffer->channel(i);
122 OwnPtr<ReverbConvolver> convolver = adoptPtr(new ReverbConvolver(channel, renderSliceSize, maxFFTSize, convolverRenderPhase, useBackgroundThreads));
123 m_convolvers.append(convolver.release());
125 convolverRenderPhase += renderSliceSize;
128 // For "True" stereo processing we allocate a temporary buffer to avoid repeatedly allocating it in the process() method.
129 // It can be bad to allocate memory in a real-time thread.
130 if (numResponseChannels == 4)
131 m_tempBuffer = adoptPtr(new AudioBus(2, MaxFrameSize));
134 void Reverb::process(AudioBus* sourceBus, AudioBus* destinationBus, size_t framesToProcess)
136 // Do a fairly comprehensive sanity check.
137 // If these conditions are satisfied, all of the source and destination pointers will be valid for the various matrixing cases.
138 bool isSafeToProcess = sourceBus && destinationBus && sourceBus->numberOfChannels() > 0 && destinationBus->numberOfChannels() > 0
139 && framesToProcess <= MaxFrameSize && framesToProcess <= sourceBus->length() && framesToProcess <= destinationBus->length();
141 ASSERT(isSafeToProcess);
142 if (!isSafeToProcess)
145 // For now only handle mono or stereo output
146 if (destinationBus->numberOfChannels() > 2) {
147 destinationBus->zero();
151 AudioChannel* destinationChannelL = destinationBus->channel(0);
152 AudioChannel* sourceChannelL = sourceBus->channel(0);
154 // Handle input -> output matrixing...
155 size_t numInputChannels = sourceBus->numberOfChannels();
156 size_t numOutputChannels = destinationBus->numberOfChannels();
157 size_t numReverbChannels = m_convolvers.size();
159 if (numInputChannels == 2 && numReverbChannels == 2 && numOutputChannels == 2) {
161 AudioChannel* sourceChannelR = sourceBus->channel(1);
162 AudioChannel* destinationChannelR = destinationBus->channel(1);
163 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
164 m_convolvers[1]->process(sourceChannelR, destinationChannelR, framesToProcess);
165 } else if (numInputChannels == 1 && numOutputChannels == 2 && numReverbChannels == 2) {
167 for (int i = 0; i < 2; ++i) {
168 AudioChannel* destinationChannel = destinationBus->channel(i);
169 m_convolvers[i]->process(sourceChannelL, destinationChannel, framesToProcess);
171 } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 2) {
173 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
175 // simply copy L -> R
176 AudioChannel* destinationChannelR = destinationBus->channel(1);
177 bool isCopySafe = destinationChannelL->data() && destinationChannelR->data() && destinationChannelL->length() >= framesToProcess && destinationChannelR->length() >= framesToProcess;
181 memcpy(destinationChannelR->data(), destinationChannelL->data(), sizeof(float) * framesToProcess);
182 } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 1) {
184 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
185 } else if (numInputChannels == 2 && numReverbChannels == 4 && numOutputChannels == 2) {
186 // 2 -> 4 -> 2 ("True" stereo)
187 AudioChannel* sourceChannelR = sourceBus->channel(1);
188 AudioChannel* destinationChannelR = destinationBus->channel(1);
190 AudioChannel* tempChannelL = m_tempBuffer->channel(0);
191 AudioChannel* tempChannelR = m_tempBuffer->channel(1);
193 // Process left virtual source
194 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
195 m_convolvers[1]->process(sourceChannelL, destinationChannelR, framesToProcess);
197 // Process right virtual source
198 m_convolvers[2]->process(sourceChannelR, tempChannelL, framesToProcess);
199 m_convolvers[3]->process(sourceChannelR, tempChannelR, framesToProcess);
201 destinationBus->sumFrom(*m_tempBuffer);
202 } else if (numInputChannels == 1 && numReverbChannels == 4 && numOutputChannels == 2) {
203 // 1 -> 4 -> 2 (Processing mono with "True" stereo impulse response)
204 // This is an inefficient use of a four-channel impulse response, but we should handle the case.
205 AudioChannel* destinationChannelR = destinationBus->channel(1);
207 AudioChannel* tempChannelL = m_tempBuffer->channel(0);
208 AudioChannel* tempChannelR = m_tempBuffer->channel(1);
210 // Process left virtual source
211 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
212 m_convolvers[1]->process(sourceChannelL, destinationChannelR, framesToProcess);
214 // Process right virtual source
215 m_convolvers[2]->process(sourceChannelL, tempChannelL, framesToProcess);
216 m_convolvers[3]->process(sourceChannelL, tempChannelR, framesToProcess);
218 destinationBus->sumFrom(*m_tempBuffer);
220 // Handle gracefully any unexpected / unsupported matrixing
221 // FIXME: add code for 5.1 support...
222 destinationBus->zero();
228 for (size_t i = 0; i < m_convolvers.size(); ++i)
229 m_convolvers[i]->reset();
232 } // namespace WebCore
234 #endif // ENABLE(WEB_AUDIO)