1 /* 2 * Copyright (C) 2011 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 #ifndef DynamicsCompressorKernel_h 30 #define DynamicsCompressorKernel_h 31 32 #include "nsTArray.h" 33 #include "mozilla/MemoryReporting.h" 34 #include "mozilla/UniquePtr.h" 35 36 namespace WebCore { 37 38 class DynamicsCompressorKernel { 39 public: 40 DynamicsCompressorKernel(float sampleRate, unsigned numberOfChannels); 41 42 void setNumberOfChannels(unsigned); 43 44 // Performs stereo-linked compression. 45 void process(float* sourceChannels[], float* destinationChannels[], 46 unsigned numberOfChannels, unsigned framesToProcess, 47 48 float dbThreshold, float dbKnee, float ratio, float attackTime, 49 float releaseTime, float preDelayTime, float dbPostGain, 50 float effectBlend, 51 52 float releaseZone1, float releaseZone2, float releaseZone3, 53 float releaseZone4); 54 55 void reset(); 56 latencyFrames()57 unsigned latencyFrames() const { return m_lastPreDelayFrames; } 58 sampleRate()59 float sampleRate() const { return m_sampleRate; } 60 meteringGain()61 float meteringGain() const { return m_meteringGain; } 62 63 size_t sizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 64 65 protected: 66 float m_sampleRate; 67 68 float m_detectorAverage; 69 float m_compressorGain; 70 71 // Metering 72 float m_meteringReleaseK; 73 float m_meteringGain; 74 75 // Lookahead section. 76 enum { MaxPreDelayFrames = 1024 }; 77 enum { MaxPreDelayFramesMask = MaxPreDelayFrames - 1 }; 78 enum { 79 DefaultPreDelayFrames = 256 80 }; // setPreDelayTime() will override this initial value 81 unsigned m_lastPreDelayFrames; 82 void setPreDelayTime(float); 83 84 nsTArray<mozilla::UniquePtr<float[]>> m_preDelayBuffers; 85 int m_preDelayReadIndex; 86 int m_preDelayWriteIndex; 87 88 float m_maxAttackCompressionDiffDb; 89 90 // Static compression curve. 91 float kneeCurve(float x, float k); 92 float saturate(float x, float k); 93 float slopeAt(float x, float k); 94 float kAtSlope(float desiredSlope); 95 96 float updateStaticCurveParameters(float dbThreshold, float dbKnee, 97 float ratio); 98 99 // Amount of input change in dB required for 1 dB of output change. 100 // This applies to the portion of the curve above m_kneeThresholdDb (see 101 // below). 102 float m_ratio; 103 float m_slope; // Inverse ratio. 104 105 // The input to output change below the threshold is linear 1:1. 106 float m_linearThreshold; 107 float m_dbThreshold; 108 109 // m_dbKnee is the number of dB above the threshold before we enter the 110 // "ratio" portion of the curve. m_kneeThresholdDb = m_dbThreshold + m_dbKnee 111 // The portion between m_dbThreshold and m_kneeThresholdDb is the "soft knee" 112 // portion of the curve which transitions smoothly from the linear portion to 113 // the ratio portion. 114 float m_dbKnee; 115 float m_kneeThreshold; 116 float m_kneeThresholdDb; 117 float m_ykneeThresholdDb; 118 119 // Internal parameter for the knee portion of the curve. 120 float m_K; 121 }; 122 123 } // namespace WebCore 124 125 #endif // DynamicsCompressorKernel_h 126