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