michael@0: /* michael@0: * Copyright (C) 2011 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 "DynamicsCompressor.h" michael@0: #include "AudioSegment.h" michael@0: michael@0: #include michael@0: #include "AudioNodeEngine.h" michael@0: #include "nsDebug.h" michael@0: michael@0: using mozilla::WEBAUDIO_BLOCK_SIZE; michael@0: using mozilla::AudioBlockCopyChannelWithScale; michael@0: michael@0: namespace WebCore { michael@0: michael@0: DynamicsCompressor::DynamicsCompressor(float sampleRate, unsigned numberOfChannels) michael@0: : m_numberOfChannels(numberOfChannels) michael@0: , m_sampleRate(sampleRate) michael@0: , m_compressor(sampleRate, numberOfChannels) michael@0: { michael@0: // Uninitialized state - for parameter recalculation. michael@0: m_lastFilterStageRatio = -1; michael@0: m_lastAnchor = -1; michael@0: m_lastFilterStageGain = -1; michael@0: michael@0: setNumberOfChannels(numberOfChannels); michael@0: initializeParameters(); michael@0: } michael@0: michael@0: size_t DynamicsCompressor::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t amount = aMallocSizeOf(this); michael@0: amount += m_preFilterPacks.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < m_preFilterPacks.Length(); i++) { michael@0: if (m_preFilterPacks[i]) { michael@0: amount += m_preFilterPacks[i]->sizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: } michael@0: michael@0: amount += m_postFilterPacks.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < m_postFilterPacks.Length(); i++) { michael@0: if (m_postFilterPacks[i]) { michael@0: amount += m_postFilterPacks[i]->sizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: } michael@0: michael@0: amount += m_sourceChannels.SizeOfExcludingThis(aMallocSizeOf); michael@0: amount += m_destinationChannels.SizeOfExcludingThis(aMallocSizeOf); michael@0: amount += m_compressor.sizeOfExcludingThis(aMallocSizeOf); michael@0: return amount; michael@0: } michael@0: michael@0: void DynamicsCompressor::setParameterValue(unsigned parameterID, float value) michael@0: { michael@0: MOZ_ASSERT(parameterID < ParamLast); michael@0: if (parameterID < ParamLast) michael@0: m_parameters[parameterID] = value; michael@0: } michael@0: michael@0: void DynamicsCompressor::initializeParameters() michael@0: { michael@0: // Initializes compressor to default values. michael@0: michael@0: m_parameters[ParamThreshold] = -24; // dB michael@0: m_parameters[ParamKnee] = 30; // dB michael@0: m_parameters[ParamRatio] = 12; // unit-less michael@0: m_parameters[ParamAttack] = 0.003f; // seconds michael@0: m_parameters[ParamRelease] = 0.250f; // seconds michael@0: m_parameters[ParamPreDelay] = 0.006f; // seconds michael@0: michael@0: // Release zone values 0 -> 1. michael@0: m_parameters[ParamReleaseZone1] = 0.09f; michael@0: m_parameters[ParamReleaseZone2] = 0.16f; michael@0: m_parameters[ParamReleaseZone3] = 0.42f; michael@0: m_parameters[ParamReleaseZone4] = 0.98f; michael@0: michael@0: m_parameters[ParamFilterStageGain] = 4.4f; // dB michael@0: m_parameters[ParamFilterStageRatio] = 2; michael@0: m_parameters[ParamFilterAnchor] = 15000 / nyquist(); michael@0: michael@0: m_parameters[ParamPostGain] = 0; // dB michael@0: m_parameters[ParamReduction] = 0; // dB michael@0: michael@0: // Linear crossfade (0 -> 1). michael@0: m_parameters[ParamEffectBlend] = 1; michael@0: } michael@0: michael@0: float DynamicsCompressor::parameterValue(unsigned parameterID) michael@0: { michael@0: MOZ_ASSERT(parameterID < ParamLast); michael@0: return m_parameters[parameterID]; michael@0: } michael@0: michael@0: void DynamicsCompressor::setEmphasisStageParameters(unsigned stageIndex, float gain, float normalizedFrequency /* 0 -> 1 */) michael@0: { michael@0: float gk = 1 - gain / 20; michael@0: float f1 = normalizedFrequency * gk; michael@0: float f2 = normalizedFrequency / gk; michael@0: float r1 = expf(-f1 * M_PI); michael@0: float r2 = expf(-f2 * M_PI); michael@0: michael@0: MOZ_ASSERT(m_numberOfChannels == m_preFilterPacks.Length()); michael@0: michael@0: for (unsigned i = 0; i < m_numberOfChannels; ++i) { michael@0: // Set pre-filter zero and pole to create an emphasis filter. michael@0: ZeroPole& preFilter = m_preFilterPacks[i]->filters[stageIndex]; michael@0: preFilter.setZero(r1); michael@0: preFilter.setPole(r2); michael@0: michael@0: // Set post-filter with zero and pole reversed to create the de-emphasis filter. michael@0: // If there were no compressor kernel in between, they would cancel each other out (allpass filter). michael@0: ZeroPole& postFilter = m_postFilterPacks[i]->filters[stageIndex]; michael@0: postFilter.setZero(r2); michael@0: postFilter.setPole(r1); michael@0: } michael@0: } michael@0: michael@0: void DynamicsCompressor::setEmphasisParameters(float gain, float anchorFreq, float filterStageRatio) michael@0: { michael@0: setEmphasisStageParameters(0, gain, anchorFreq); michael@0: setEmphasisStageParameters(1, gain, anchorFreq / filterStageRatio); michael@0: setEmphasisStageParameters(2, gain, anchorFreq / (filterStageRatio * filterStageRatio)); michael@0: setEmphasisStageParameters(3, gain, anchorFreq / (filterStageRatio * filterStageRatio * filterStageRatio)); michael@0: } michael@0: michael@0: void DynamicsCompressor::process(const AudioChunk* sourceChunk, AudioChunk* destinationChunk, unsigned framesToProcess) michael@0: { michael@0: // Though numberOfChannels is retrived from destinationBus, we still name it numberOfChannels instead of numberOfDestinationChannels. michael@0: // It's because we internally match sourceChannels's size to destinationBus by channel up/down mix. Thus we need numberOfChannels michael@0: // to do the loop work for both m_sourceChannels and m_destinationChannels. michael@0: michael@0: unsigned numberOfChannels = destinationChunk->mChannelData.Length(); michael@0: unsigned numberOfSourceChannels = sourceChunk->mChannelData.Length(); michael@0: michael@0: MOZ_ASSERT(numberOfChannels == m_numberOfChannels && numberOfSourceChannels); michael@0: michael@0: if (numberOfChannels != m_numberOfChannels || !numberOfSourceChannels) { michael@0: destinationChunk->SetNull(WEBAUDIO_BLOCK_SIZE); michael@0: return; michael@0: } michael@0: michael@0: switch (numberOfChannels) { michael@0: case 2: // stereo michael@0: m_sourceChannels[0] = static_cast(sourceChunk->mChannelData[0]); michael@0: michael@0: if (numberOfSourceChannels > 1) michael@0: m_sourceChannels[1] = static_cast(sourceChunk->mChannelData[1]); michael@0: else michael@0: // Simply duplicate mono channel input data to right channel for stereo processing. michael@0: m_sourceChannels[1] = m_sourceChannels[0]; michael@0: michael@0: break; michael@0: default: michael@0: // FIXME : support other number of channels. michael@0: NS_WARNING("Support other number of channels"); michael@0: destinationChunk->SetNull(WEBAUDIO_BLOCK_SIZE); michael@0: return; michael@0: } michael@0: michael@0: for (unsigned i = 0; i < numberOfChannels; ++i) michael@0: m_destinationChannels[i] = const_cast(static_cast( michael@0: destinationChunk->mChannelData[i])); michael@0: michael@0: float filterStageGain = parameterValue(ParamFilterStageGain); michael@0: float filterStageRatio = parameterValue(ParamFilterStageRatio); michael@0: float anchor = parameterValue(ParamFilterAnchor); michael@0: michael@0: if (filterStageGain != m_lastFilterStageGain || filterStageRatio != m_lastFilterStageRatio || anchor != m_lastAnchor) { michael@0: m_lastFilterStageGain = filterStageGain; michael@0: m_lastFilterStageRatio = filterStageRatio; michael@0: m_lastAnchor = anchor; michael@0: michael@0: setEmphasisParameters(filterStageGain, anchor, filterStageRatio); michael@0: } michael@0: michael@0: float sourceWithVolume[WEBAUDIO_BLOCK_SIZE]; michael@0: michael@0: // Apply pre-emphasis filter. michael@0: // Note that the final three stages are computed in-place in the destination buffer. michael@0: for (unsigned i = 0; i < numberOfChannels; ++i) { michael@0: const float* sourceData; michael@0: if (sourceChunk->mVolume == 1.0f) { michael@0: // Fast path, the volume scale doesn't need to get taken into account michael@0: sourceData = m_sourceChannels[i]; michael@0: } else { michael@0: AudioBlockCopyChannelWithScale(m_sourceChannels[i], michael@0: sourceChunk->mVolume, michael@0: sourceWithVolume); michael@0: sourceData = sourceWithVolume; michael@0: } michael@0: michael@0: float* destinationData = m_destinationChannels[i]; michael@0: ZeroPole* preFilters = m_preFilterPacks[i]->filters; michael@0: michael@0: preFilters[0].process(sourceData, destinationData, framesToProcess); michael@0: preFilters[1].process(destinationData, destinationData, framesToProcess); michael@0: preFilters[2].process(destinationData, destinationData, framesToProcess); michael@0: preFilters[3].process(destinationData, destinationData, framesToProcess); michael@0: } michael@0: michael@0: float dbThreshold = parameterValue(ParamThreshold); michael@0: float dbKnee = parameterValue(ParamKnee); michael@0: float ratio = parameterValue(ParamRatio); michael@0: float attackTime = parameterValue(ParamAttack); michael@0: float releaseTime = parameterValue(ParamRelease); michael@0: float preDelayTime = parameterValue(ParamPreDelay); michael@0: michael@0: // This is effectively a master volume on the compressed signal (pre-blending). michael@0: float dbPostGain = parameterValue(ParamPostGain); michael@0: michael@0: // Linear blending value from dry to completely processed (0 -> 1) michael@0: // 0 means the signal is completely unprocessed. michael@0: // 1 mixes in only the compressed signal. michael@0: float effectBlend = parameterValue(ParamEffectBlend); michael@0: michael@0: float releaseZone1 = parameterValue(ParamReleaseZone1); michael@0: float releaseZone2 = parameterValue(ParamReleaseZone2); michael@0: float releaseZone3 = parameterValue(ParamReleaseZone3); michael@0: float releaseZone4 = parameterValue(ParamReleaseZone4); michael@0: michael@0: // Apply compression to the pre-filtered signal. michael@0: // The processing is performed in place. michael@0: m_compressor.process(m_destinationChannels.get(), michael@0: m_destinationChannels.get(), michael@0: numberOfChannels, michael@0: framesToProcess, michael@0: michael@0: dbThreshold, michael@0: dbKnee, michael@0: ratio, michael@0: attackTime, michael@0: releaseTime, michael@0: preDelayTime, michael@0: dbPostGain, michael@0: effectBlend, michael@0: michael@0: releaseZone1, michael@0: releaseZone2, michael@0: releaseZone3, michael@0: releaseZone4 michael@0: ); michael@0: michael@0: // Update the compression amount. michael@0: setParameterValue(ParamReduction, m_compressor.meteringGain()); michael@0: michael@0: // Apply de-emphasis filter. michael@0: for (unsigned i = 0; i < numberOfChannels; ++i) { michael@0: float* destinationData = m_destinationChannels[i]; michael@0: ZeroPole* postFilters = m_postFilterPacks[i]->filters; michael@0: michael@0: postFilters[0].process(destinationData, destinationData, framesToProcess); michael@0: postFilters[1].process(destinationData, destinationData, framesToProcess); michael@0: postFilters[2].process(destinationData, destinationData, framesToProcess); michael@0: postFilters[3].process(destinationData, destinationData, framesToProcess); michael@0: } michael@0: } michael@0: michael@0: void DynamicsCompressor::reset() michael@0: { michael@0: m_lastFilterStageRatio = -1; // for recalc michael@0: m_lastAnchor = -1; michael@0: m_lastFilterStageGain = -1; michael@0: michael@0: for (unsigned channel = 0; channel < m_numberOfChannels; ++channel) { michael@0: for (unsigned stageIndex = 0; stageIndex < 4; ++stageIndex) { michael@0: m_preFilterPacks[channel]->filters[stageIndex].reset(); michael@0: m_postFilterPacks[channel]->filters[stageIndex].reset(); michael@0: } michael@0: } michael@0: michael@0: m_compressor.reset(); michael@0: } michael@0: michael@0: void DynamicsCompressor::setNumberOfChannels(unsigned numberOfChannels) michael@0: { michael@0: if (m_preFilterPacks.Length() == numberOfChannels) michael@0: return; michael@0: michael@0: m_preFilterPacks.Clear(); michael@0: m_postFilterPacks.Clear(); michael@0: for (unsigned i = 0; i < numberOfChannels; ++i) { michael@0: m_preFilterPacks.AppendElement(new ZeroPoleFilterPack4()); michael@0: m_postFilterPacks.AppendElement(new ZeroPoleFilterPack4()); michael@0: } michael@0: michael@0: m_sourceChannels = new const float* [numberOfChannels]; michael@0: m_destinationChannels = new float* [numberOfChannels]; michael@0: michael@0: m_compressor.setNumberOfChannels(numberOfChannels); michael@0: m_numberOfChannels = numberOfChannels; michael@0: } michael@0: michael@0: } // namespace WebCore