michael@0: /* michael@0: * Copyright (C) 2010 Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of michael@0: * its contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include "Reverb.h" michael@0: #include "ReverbConvolverStage.h" michael@0: michael@0: #include michael@0: #include "ReverbConvolver.h" michael@0: #include "mozilla/FloatingPoint.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: namespace WebCore { michael@0: michael@0: // Empirical gain calibration tested across many impulse responses to ensure perceived volume is same as dry (unprocessed) signal michael@0: const float GainCalibration = -58; michael@0: const float GainCalibrationSampleRate = 44100; michael@0: michael@0: // A minimum power value to when normalizing a silent (or very quiet) impulse response michael@0: const float MinPower = 0.000125f; michael@0: michael@0: static float calculateNormalizationScale(ThreadSharedFloatArrayBufferList* response, size_t aLength, float sampleRate) michael@0: { michael@0: // Normalize by RMS power michael@0: size_t numberOfChannels = response->GetChannels(); michael@0: michael@0: float power = 0; michael@0: michael@0: for (size_t i = 0; i < numberOfChannels; ++i) { michael@0: float channelPower = AudioBufferSumOfSquares(static_cast(response->GetData(i)), aLength); michael@0: power += channelPower; michael@0: } michael@0: michael@0: power = sqrt(power / (numberOfChannels * aLength)); michael@0: michael@0: // Protect against accidental overload michael@0: if (!IsFinite(power) || IsNaN(power) || power < MinPower) michael@0: power = MinPower; michael@0: michael@0: float scale = 1 / power; michael@0: michael@0: scale *= powf(10, GainCalibration * 0.05f); // calibrate to make perceived volume same as unprocessed michael@0: michael@0: // Scale depends on sample-rate. michael@0: if (sampleRate) michael@0: scale *= GainCalibrationSampleRate / sampleRate; michael@0: michael@0: // True-stereo compensation michael@0: if (response->GetChannels() == 4) michael@0: scale *= 0.5f; michael@0: michael@0: return scale; michael@0: } michael@0: michael@0: Reverb::Reverb(ThreadSharedFloatArrayBufferList* impulseResponse, size_t impulseResponseBufferLength, size_t renderSliceSize, size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads, bool normalize, float sampleRate) michael@0: { michael@0: float scale = 1; michael@0: michael@0: nsAutoTArray irChannels; michael@0: for (size_t i = 0; i < impulseResponse->GetChannels(); ++i) { michael@0: irChannels.AppendElement(impulseResponse->GetData(i)); michael@0: } michael@0: nsAutoTArray tempBuf; michael@0: michael@0: if (normalize) { michael@0: scale = calculateNormalizationScale(impulseResponse, impulseResponseBufferLength, sampleRate); michael@0: michael@0: if (scale) { michael@0: tempBuf.SetLength(irChannels.Length()*impulseResponseBufferLength); michael@0: for (uint32_t i = 0; i < irChannels.Length(); ++i) { michael@0: float* buf = &tempBuf[i*impulseResponseBufferLength]; michael@0: AudioBufferCopyWithScale(irChannels[i], scale, buf, michael@0: impulseResponseBufferLength); michael@0: irChannels[i] = buf; michael@0: } michael@0: } michael@0: } michael@0: michael@0: initialize(irChannels, impulseResponseBufferLength, renderSliceSize, michael@0: maxFFTSize, numberOfChannels, useBackgroundThreads); michael@0: } michael@0: michael@0: size_t Reverb::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t amount = aMallocSizeOf(this); michael@0: amount += m_convolvers.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < m_convolvers.Length(); i++) { michael@0: if (m_convolvers[i]) { michael@0: amount += m_convolvers[i]->sizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: } michael@0: michael@0: amount += m_tempBuffer.SizeOfExcludingThis(aMallocSizeOf, false); michael@0: return amount; michael@0: } michael@0: michael@0: michael@0: void Reverb::initialize(const nsTArray& impulseResponseBuffer, michael@0: size_t impulseResponseBufferLength, size_t renderSliceSize, michael@0: size_t maxFFTSize, size_t numberOfChannels, bool useBackgroundThreads) michael@0: { michael@0: m_impulseResponseLength = impulseResponseBufferLength; michael@0: michael@0: // The reverb can handle a mono impulse response and still do stereo processing michael@0: size_t numResponseChannels = impulseResponseBuffer.Length(); michael@0: m_convolvers.SetCapacity(numberOfChannels); michael@0: michael@0: int convolverRenderPhase = 0; michael@0: for (size_t i = 0; i < numResponseChannels; ++i) { michael@0: const float* channel = impulseResponseBuffer[i]; michael@0: size_t length = impulseResponseBufferLength; michael@0: michael@0: nsAutoPtr convolver(new ReverbConvolver(channel, length, renderSliceSize, maxFFTSize, convolverRenderPhase, useBackgroundThreads)); michael@0: m_convolvers.AppendElement(convolver.forget()); michael@0: michael@0: convolverRenderPhase += renderSliceSize; michael@0: } michael@0: michael@0: // For "True" stereo processing we allocate a temporary buffer to avoid repeatedly allocating it in the process() method. michael@0: // It can be bad to allocate memory in a real-time thread. michael@0: if (numResponseChannels == 4) { michael@0: AllocateAudioBlock(2, &m_tempBuffer); michael@0: WriteZeroesToAudioBlock(&m_tempBuffer, 0, WEBAUDIO_BLOCK_SIZE); michael@0: } michael@0: } michael@0: michael@0: void Reverb::process(const AudioChunk* sourceBus, AudioChunk* destinationBus, size_t framesToProcess) michael@0: { michael@0: // Do a fairly comprehensive sanity check. michael@0: // If these conditions are satisfied, all of the source and destination pointers will be valid for the various matrixing cases. michael@0: bool isSafeToProcess = sourceBus && destinationBus && sourceBus->mChannelData.Length() > 0 && destinationBus->mChannelData.Length() > 0 michael@0: && framesToProcess <= MaxFrameSize && framesToProcess <= size_t(sourceBus->mDuration) && framesToProcess <= size_t(destinationBus->mDuration); michael@0: michael@0: MOZ_ASSERT(isSafeToProcess); michael@0: if (!isSafeToProcess) michael@0: return; michael@0: michael@0: // For now only handle mono or stereo output michael@0: MOZ_ASSERT(destinationBus->mChannelData.Length() <= 2); michael@0: michael@0: float* destinationChannelL = static_cast(const_cast(destinationBus->mChannelData[0])); michael@0: const float* sourceBusL = static_cast(sourceBus->mChannelData[0]); michael@0: michael@0: // Handle input -> output matrixing... michael@0: size_t numInputChannels = sourceBus->mChannelData.Length(); michael@0: size_t numOutputChannels = destinationBus->mChannelData.Length(); michael@0: size_t numReverbChannels = m_convolvers.Length(); michael@0: michael@0: if (numInputChannels == 2 && numReverbChannels == 2 && numOutputChannels == 2) { michael@0: // 2 -> 2 -> 2 michael@0: const float* sourceBusR = static_cast(sourceBus->mChannelData[1]); michael@0: float* destinationChannelR = static_cast(const_cast(destinationBus->mChannelData[1])); michael@0: m_convolvers[0]->process(sourceBusL, sourceBus->mDuration, destinationChannelL, destinationBus->mDuration, framesToProcess); michael@0: m_convolvers[1]->process(sourceBusR, sourceBus->mDuration, destinationChannelR, destinationBus->mDuration, framesToProcess); michael@0: } else if (numInputChannels == 1 && numOutputChannels == 2 && numReverbChannels == 2) { michael@0: // 1 -> 2 -> 2 michael@0: for (int i = 0; i < 2; ++i) { michael@0: float* destinationChannel = static_cast(const_cast(destinationBus->mChannelData[i])); michael@0: m_convolvers[i]->process(sourceBusL, sourceBus->mDuration, destinationChannel, destinationBus->mDuration, framesToProcess); michael@0: } michael@0: } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 2) { michael@0: // 1 -> 1 -> 2 michael@0: m_convolvers[0]->process(sourceBusL, sourceBus->mDuration, destinationChannelL, destinationBus->mDuration, framesToProcess); michael@0: michael@0: // simply copy L -> R michael@0: float* destinationChannelR = static_cast(const_cast(destinationBus->mChannelData[1])); michael@0: bool isCopySafe = destinationChannelL && destinationChannelR && size_t(destinationBus->mDuration) >= framesToProcess && size_t(destinationBus->mDuration) >= framesToProcess; michael@0: MOZ_ASSERT(isCopySafe); michael@0: if (!isCopySafe) michael@0: return; michael@0: PodCopy(destinationChannelR, destinationChannelL, framesToProcess); michael@0: } else if (numInputChannels == 1 && numReverbChannels == 1 && numOutputChannels == 1) { michael@0: // 1 -> 1 -> 1 michael@0: m_convolvers[0]->process(sourceBusL, sourceBus->mDuration, destinationChannelL, destinationBus->mDuration, framesToProcess); michael@0: } else if (numInputChannels == 2 && numReverbChannels == 4 && numOutputChannels == 2) { michael@0: // 2 -> 4 -> 2 ("True" stereo) michael@0: const float* sourceBusR = static_cast(sourceBus->mChannelData[1]); michael@0: float* destinationChannelR = static_cast(const_cast(destinationBus->mChannelData[1])); michael@0: michael@0: float* tempChannelL = static_cast(const_cast(m_tempBuffer.mChannelData[0])); michael@0: float* tempChannelR = static_cast(const_cast(m_tempBuffer.mChannelData[1])); michael@0: michael@0: // Process left virtual source michael@0: m_convolvers[0]->process(sourceBusL, sourceBus->mDuration, destinationChannelL, destinationBus->mDuration, framesToProcess); michael@0: m_convolvers[1]->process(sourceBusL, sourceBus->mDuration, destinationChannelR, destinationBus->mDuration, framesToProcess); michael@0: michael@0: // Process right virtual source michael@0: m_convolvers[2]->process(sourceBusR, sourceBus->mDuration, tempChannelL, m_tempBuffer.mDuration, framesToProcess); michael@0: m_convolvers[3]->process(sourceBusR, sourceBus->mDuration, tempChannelR, m_tempBuffer.mDuration, framesToProcess); michael@0: michael@0: AudioBufferAddWithScale(tempChannelL, 1.0f, destinationChannelL, sourceBus->mDuration); michael@0: AudioBufferAddWithScale(tempChannelR, 1.0f, destinationChannelR, sourceBus->mDuration); michael@0: } else if (numInputChannels == 1 && numReverbChannels == 4 && numOutputChannels == 2) { michael@0: // 1 -> 4 -> 2 (Processing mono with "True" stereo impulse response) michael@0: // This is an inefficient use of a four-channel impulse response, but we should handle the case. michael@0: float* destinationChannelR = static_cast(const_cast(destinationBus->mChannelData[1])); michael@0: michael@0: float* tempChannelL = static_cast(const_cast(m_tempBuffer.mChannelData[0])); michael@0: float* tempChannelR = static_cast(const_cast(m_tempBuffer.mChannelData[1])); michael@0: michael@0: // Process left virtual source michael@0: m_convolvers[0]->process(sourceBusL, sourceBus->mDuration, destinationChannelL, destinationBus->mDuration, framesToProcess); michael@0: m_convolvers[1]->process(sourceBusL, sourceBus->mDuration, destinationChannelR, destinationBus->mDuration, framesToProcess); michael@0: michael@0: // Process right virtual source michael@0: m_convolvers[2]->process(sourceBusL, sourceBus->mDuration, tempChannelL, m_tempBuffer.mDuration, framesToProcess); michael@0: m_convolvers[3]->process(sourceBusL, sourceBus->mDuration, tempChannelR, m_tempBuffer.mDuration, framesToProcess); michael@0: michael@0: AudioBufferAddWithScale(tempChannelL, 1.0f, destinationChannelL, sourceBus->mDuration); michael@0: AudioBufferAddWithScale(tempChannelR, 1.0f, destinationChannelR, sourceBus->mDuration); michael@0: } else { michael@0: // Handle gracefully any unexpected / unsupported matrixing michael@0: // FIXME: add code for 5.1 support... michael@0: destinationBus->SetNull(destinationBus->mDuration); michael@0: } michael@0: } michael@0: michael@0: void Reverb::reset() michael@0: { michael@0: for (size_t i = 0; i < m_convolvers.Length(); ++i) michael@0: m_convolvers[i]->reset(); michael@0: } michael@0: michael@0: size_t Reverb::latencyFrames() const michael@0: { michael@0: return !m_convolvers.IsEmpty() ? m_convolvers[0]->latencyFrames() : 0; michael@0: } michael@0: michael@0: } // namespace WebCore