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