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"
38 #include "VectorMath.h"
40 #include <wtf/MathExtras.h>
41 #include <wtf/OwnPtr.h>
42 #include <wtf/PassOwnPtr.h>
50 using namespace VectorMath;
52 // Empirical gain calibration tested across many impulse responses to ensure perceived volume is same as dry (unprocessed) signal
53 const float GainCalibration = -58;
55 // A minimum power value to when normalizing a silent (or very quiet) impulse response
56 const float MinPower = 0.000125f;
58 static float calculateNormalizationScale(AudioBus* response)
60 // Normalize by RMS power
61 size_t numberOfChannels = response->numberOfChannels();
62 size_t length = response->length();
66 for (size_t i = 0; i < numberOfChannels; ++i) {
67 float channelPower = 0;
68 vsvesq(response->channel(i)->data(), 1, &channelPower, length);
69 power += channelPower;
72 power = sqrt(power / (numberOfChannels * length));
74 // Protect against accidental overload
75 if (isinf(power) || isnan(power) || power < MinPower)
78 float scale = 1 / power;
80 scale *= powf(10, GainCalibration * 0.05f); // calibrate to make perceived volume same as unprocessed
82 // True-stereo compensation
83 if (response->numberOfChannels() == 4)
89 Reverb::Reverb(AudioBus* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads, bool normalize)
94 scale = calculateNormalizationScale(impulseResponse);
97 impulseResponse->scale(scale);
100 initialize(impulseResponse, renderSliceSize, maxFFTSize, numberOfChannels, useBackgroundThreads);
102 // Undo scaling since this shouldn't be a destructive operation on impulseResponse.
103 // FIXME: What about roundoff? Perhaps consider making a temporary scaled copy
104 // instead of scaling and unscaling in place.
105 if (normalize && scale)
106 impulseResponse->scale(1 / scale);
109 void Reverb::initialize(AudioBus* impulseResponseBuffer, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads)
111 m_impulseResponseLength = impulseResponseBuffer->length();
113 // The reverb can handle a mono impulse response and still do stereo processing
114 size_t numResponseChannels = impulseResponseBuffer->numberOfChannels();
115 m_convolvers.reserveCapacity(numberOfChannels);
117 int convolverRenderPhase = 0;
118 for (size_t i = 0; i < numResponseChannels; ++i) {
119 AudioChannel* channel = impulseResponseBuffer->channel(i);
121 OwnPtr<ReverbConvolver> convolver = adoptPtr(new ReverbConvolver(channel, renderSliceSize, maxFFTSize, convolverRenderPhase, useBackgroundThreads));
122 m_convolvers.append(convolver.release());
124 convolverRenderPhase += renderSliceSize;
127 // For "True" stereo processing we allocate a temporary buffer to avoid repeatedly allocating it in the process() method.
128 // It can be bad to allocate memory in a real-time thread.
129 if (numResponseChannels == 4)
130 m_tempBuffer = adoptPtr(new AudioBus(2, MaxFrameSize));
133 void Reverb::process(AudioBus* sourceBus, AudioBus* destinationBus, size_t framesToProcess)
135 // Do a fairly comprehensive sanity check.
136 // If these conditions are satisfied, all of the source and destination pointers will be valid for the various matrixing cases.
137 bool isSafeToProcess = sourceBus && destinationBus && sourceBus->numberOfChannels() > 0 && destinationBus->numberOfChannels() > 0
138 && framesToProcess <= MaxFrameSize && framesToProcess <= sourceBus->length() && framesToProcess <= destinationBus->length();
140 ASSERT(isSafeToProcess);
141 if (!isSafeToProcess)
144 // For now only handle mono or stereo output
145 if (destinationBus->numberOfChannels() > 2) {
146 destinationBus->zero();
150 AudioChannel* destinationChannelL = destinationBus->channel(0);
151 AudioChannel* sourceChannelL = sourceBus->channel(0);
153 // Handle input -> output matrixing...
154 size_t numInputChannels = sourceBus->numberOfChannels();
155 size_t numOutputChannels = destinationBus->numberOfChannels();
156 size_t numReverbChannels = m_convolvers.size();
158 if (numInputChannels == 2 && numReverbChannels == 2 && numOutputChannels == 2) {
160 AudioChannel* sourceChannelR = sourceBus->channel(1);
161 AudioChannel* destinationChannelR = destinationBus->channel(1);
162 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
163 m_convolvers[1]->process(sourceChannelR, destinationChannelR, framesToProcess);
164 } else if (numInputChannels == 1 && numOutputChannels == 2 && numReverbChannels == 2) {
166 for (int i = 0; i < 2; ++i) {
167 AudioChannel* destinationChannel = destinationBus->channel(i);
168 m_convolvers[i]->process(sourceChannelL, destinationChannel, framesToProcess);
170 } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 2) {
172 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
174 // simply copy L -> R
175 AudioChannel* destinationChannelR = destinationBus->channel(1);
176 bool isCopySafe = destinationChannelL->data() && destinationChannelR->data() && destinationChannelL->length() >= framesToProcess && destinationChannelR->length() >= framesToProcess;
180 memcpy(destinationChannelR->data(), destinationChannelL->data(), sizeof(float) * framesToProcess);
181 } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 1) {
183 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
184 } else if (numInputChannels == 2 && numReverbChannels == 4 && numOutputChannels == 2) {
185 // 2 -> 4 -> 2 ("True" stereo)
186 AudioChannel* sourceChannelR = sourceBus->channel(1);
187 AudioChannel* destinationChannelR = destinationBus->channel(1);
189 AudioChannel* tempChannelL = m_tempBuffer->channel(0);
190 AudioChannel* tempChannelR = m_tempBuffer->channel(1);
192 // Process left virtual source
193 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
194 m_convolvers[1]->process(sourceChannelL, destinationChannelR, framesToProcess);
196 // Process right virtual source
197 m_convolvers[2]->process(sourceChannelR, tempChannelL, framesToProcess);
198 m_convolvers[3]->process(sourceChannelR, tempChannelR, framesToProcess);
200 destinationBus->sumFrom(*m_tempBuffer);
201 } else if (numInputChannels == 1 && numReverbChannels == 4 && numOutputChannels == 2) {
202 // 1 -> 4 -> 2 (Processing mono with "True" stereo impulse response)
203 // This is an inefficient use of a four-channel impulse response, but we should handle the case.
204 AudioChannel* destinationChannelR = destinationBus->channel(1);
206 AudioChannel* tempChannelL = m_tempBuffer->channel(0);
207 AudioChannel* tempChannelR = m_tempBuffer->channel(1);
209 // Process left virtual source
210 m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
211 m_convolvers[1]->process(sourceChannelL, destinationChannelR, framesToProcess);
213 // Process right virtual source
214 m_convolvers[2]->process(sourceChannelL, tempChannelL, framesToProcess);
215 m_convolvers[3]->process(sourceChannelL, tempChannelR, framesToProcess);
217 destinationBus->sumFrom(*m_tempBuffer);
219 // Handle gracefully any unexpected / unsupported matrixing
220 // FIXME: add code for 5.1 support...
221 destinationBus->zero();
227 for (size_t i = 0; i < m_convolvers.size(); ++i)
228 m_convolvers[i]->reset();
231 } // namespace WebCore
233 #endif // ENABLE(WEB_AUDIO)