1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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.
16 *
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.
27 */
28
29#include "config.h"
30
31#if ENABLE(WEB_AUDIO)
32
33#include "Reverb.h"
34
35#include "AudioBus.h"
36#include "AudioFileReader.h"
37#include "ReverbConvolver.h"
38#include <math.h>
39#include <wtf/MathExtras.h>
40#include <wtf/OwnPtr.h>
41#include <wtf/PassOwnPtr.h>
42
43#if OS(DARWIN)
44using namespace std;
45#endif
46
47namespace WebCore {
48
49// Empirical gain calibration tested across many impulse responses to ensure perceived volume is same as dry (unprocessed) signal
50const double GainCalibration = -58.0;
51
52// A minimum power value to when normalizing a silent (or very quiet) impulse response
53const double MinPower = 0.000125;
54
55static double calculateNormalizationScale(AudioBus* response)
56{
57    // Normalize by RMS power
58    size_t numberOfChannels = response->numberOfChannels();
59    size_t length = response->length();
60
61    double power = 0.0;
62
63    for (size_t i = 0; i < numberOfChannels; ++i) {
64        int n = length;
65        float* p = response->channel(i)->data();
66
67        while (n--) {
68            float sample = *p++;
69            power += sample * sample;
70        }
71    }
72
73    power = sqrt(power / (numberOfChannels * length));
74
75    // Protect against accidental overload
76    if (isinf(power) || isnan(power) || power < MinPower)
77        power = MinPower;
78
79    double scale = 1.0 / power;
80
81    scale *= pow(10.0, GainCalibration * 0.05); // calibrate to make perceived volume same as unprocessed
82
83    // True-stereo compensation
84    if (response->numberOfChannels() == 4)
85        scale *= 0.5;
86
87    return scale;
88}
89
90Reverb::Reverb(AudioBus* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads)
91{
92    double scale = calculateNormalizationScale(impulseResponse);
93    if (scale)
94        impulseResponse->scale(scale);
95
96    initialize(impulseResponse, renderSliceSize, maxFFTSize, numberOfChannels, useBackgroundThreads);
97
98    // Undo scaling since this shouldn't be a destructive operation on impulseResponse
99    if (scale)
100        impulseResponse->scale(1.0 / scale);
101}
102
103void Reverb::initialize(AudioBus* impulseResponseBuffer, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads)
104{
105    m_impulseResponseLength = impulseResponseBuffer->length();
106
107    // The reverb can handle a mono impulse response and still do stereo processing
108    size_t numResponseChannels = impulseResponseBuffer->numberOfChannels();
109    m_convolvers.reserveCapacity(numberOfChannels);
110
111    int convolverRenderPhase = 0;
112    for (size_t i = 0; i < numResponseChannels; ++i) {
113        AudioChannel* channel = impulseResponseBuffer->channel(i);
114
115        OwnPtr<ReverbConvolver> convolver = adoptPtr(new ReverbConvolver(channel, renderSliceSize, maxFFTSize, convolverRenderPhase, useBackgroundThreads));
116        m_convolvers.append(convolver.release());
117
118        convolverRenderPhase += renderSliceSize;
119    }
120
121    // For "True" stereo processing we allocate a temporary buffer to avoid repeatedly allocating it in the process() method.
122    // It can be bad to allocate memory in a real-time thread.
123    if (numResponseChannels == 4)
124        m_tempBuffer = new AudioBus(2, MaxFrameSize);
125}
126
127void Reverb::process(AudioBus* sourceBus, AudioBus* destinationBus, size_t framesToProcess)
128{
129    // Do a fairly comprehensive sanity check.
130    // If these conditions are satisfied, all of the source and destination pointers will be valid for the various matrixing cases.
131    bool isSafeToProcess = sourceBus && destinationBus && sourceBus->numberOfChannels() > 0 && destinationBus->numberOfChannels() > 0
132        && framesToProcess <= MaxFrameSize && framesToProcess <= sourceBus->length() && framesToProcess <= destinationBus->length();
133
134    ASSERT(isSafeToProcess);
135    if (!isSafeToProcess)
136        return;
137
138    // For now only handle mono or stereo output
139    if (destinationBus->numberOfChannels() > 2) {
140        destinationBus->zero();
141        return;
142    }
143
144    AudioChannel* destinationChannelL = destinationBus->channel(0);
145    AudioChannel* sourceChannelL = sourceBus->channel(0);
146
147    // Handle input -> output matrixing...
148    size_t numInputChannels = sourceBus->numberOfChannels();
149    size_t numOutputChannels = destinationBus->numberOfChannels();
150    size_t numReverbChannels = m_convolvers.size();
151
152    if (numInputChannels == 2 && numReverbChannels == 2 && numOutputChannels == 2) {
153        // 2 -> 2 -> 2
154        AudioChannel* sourceChannelR = sourceBus->channel(1);
155        AudioChannel* destinationChannelR = destinationBus->channel(1);
156        m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
157        m_convolvers[1]->process(sourceChannelR, destinationChannelR, framesToProcess);
158    } else  if (numInputChannels == 1 && numOutputChannels == 2 && numReverbChannels == 2) {
159        // 1 -> 2 -> 2
160        for (int i = 0; i < 2; ++i) {
161            AudioChannel* destinationChannel = destinationBus->channel(i);
162            m_convolvers[i]->process(sourceChannelL, destinationChannel, framesToProcess);
163        }
164    } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 2) {
165        // 1 -> 1 -> 2
166        m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
167
168        // simply copy L -> R
169        AudioChannel* destinationChannelR = destinationBus->channel(1);
170        bool isCopySafe = destinationChannelL->data() && destinationChannelR->data() && destinationChannelL->length() >= framesToProcess && destinationChannelR->length() >= framesToProcess;
171        ASSERT(isCopySafe);
172        if (!isCopySafe)
173            return;
174        memcpy(destinationChannelR->data(), destinationChannelL->data(), sizeof(float) * framesToProcess);
175    } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 1) {
176        // 1 -> 1 -> 1
177        m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
178    } else if (numInputChannels == 2 && numReverbChannels == 4 && numOutputChannels == 2) {
179        // 2 -> 4 -> 2 ("True" stereo)
180        AudioChannel* sourceChannelR = sourceBus->channel(1);
181        AudioChannel* destinationChannelR = destinationBus->channel(1);
182
183        AudioChannel* tempChannelL = m_tempBuffer->channel(0);
184        AudioChannel* tempChannelR = m_tempBuffer->channel(1);
185
186        // Process left virtual source
187        m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
188        m_convolvers[1]->process(sourceChannelL, destinationChannelR, framesToProcess);
189
190        // Process right virtual source
191        m_convolvers[2]->process(sourceChannelR, tempChannelL, framesToProcess);
192        m_convolvers[3]->process(sourceChannelR, tempChannelR, framesToProcess);
193
194        destinationBus->sumFrom(*m_tempBuffer);
195    } else if (numInputChannels == 1 && numReverbChannels == 4 && numOutputChannels == 2) {
196        // 1 -> 4 -> 2 (Processing mono with "True" stereo impulse response)
197        // This is an inefficient use of a four-channel impulse response, but we should handle the case.
198        AudioChannel* destinationChannelR = destinationBus->channel(1);
199
200        AudioChannel* tempChannelL = m_tempBuffer->channel(0);
201        AudioChannel* tempChannelR = m_tempBuffer->channel(1);
202
203        // Process left virtual source
204        m_convolvers[0]->process(sourceChannelL, destinationChannelL, framesToProcess);
205        m_convolvers[1]->process(sourceChannelL, destinationChannelR, framesToProcess);
206
207        // Process right virtual source
208        m_convolvers[2]->process(sourceChannelL, tempChannelL, framesToProcess);
209        m_convolvers[3]->process(sourceChannelL, tempChannelR, framesToProcess);
210
211        destinationBus->sumFrom(*m_tempBuffer);
212    } else {
213        // Handle gracefully any unexpected / unsupported matrixing
214        // FIXME: add code for 5.1 support...
215        destinationBus->zero();
216    }
217}
218
219void Reverb::reset()
220{
221    for (size_t i = 0; i < m_convolvers.size(); ++i)
222        m_convolvers[i]->reset();
223}
224
225} // namespace WebCore
226
227#endif // ENABLE(WEB_AUDIO)
228