1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/DynamicsCompressor.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +/* 1.5 + * Copyright (C) 2011 Google Inc. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.17 + * its contributors may be used to endorse or promote products derived 1.18 + * from this software without specific prior written permission. 1.19 + * 1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#ifndef DynamicsCompressor_h 1.33 +#define DynamicsCompressor_h 1.34 + 1.35 +#include "DynamicsCompressorKernel.h" 1.36 +#include "ZeroPole.h" 1.37 + 1.38 +#include "nsTArray.h" 1.39 +#include "nsAutoPtr.h" 1.40 +#include "mozilla/MemoryReporting.h" 1.41 + 1.42 +namespace mozilla { 1.43 +struct AudioChunk; 1.44 +} 1.45 + 1.46 +namespace WebCore { 1.47 + 1.48 +using mozilla::AudioChunk; 1.49 + 1.50 +// DynamicsCompressor implements a flexible audio dynamics compression effect such as 1.51 +// is commonly used in musical production and game audio. It lowers the volume 1.52 +// of the loudest parts of the signal and raises the volume of the softest parts, 1.53 +// making the sound richer, fuller, and more controlled. 1.54 + 1.55 +class DynamicsCompressor { 1.56 +public: 1.57 + enum { 1.58 + ParamThreshold, 1.59 + ParamKnee, 1.60 + ParamRatio, 1.61 + ParamAttack, 1.62 + ParamRelease, 1.63 + ParamPreDelay, 1.64 + ParamReleaseZone1, 1.65 + ParamReleaseZone2, 1.66 + ParamReleaseZone3, 1.67 + ParamReleaseZone4, 1.68 + ParamPostGain, 1.69 + ParamFilterStageGain, 1.70 + ParamFilterStageRatio, 1.71 + ParamFilterAnchor, 1.72 + ParamEffectBlend, 1.73 + ParamReduction, 1.74 + ParamLast 1.75 + }; 1.76 + 1.77 + DynamicsCompressor(float sampleRate, unsigned numberOfChannels); 1.78 + 1.79 + void process(const AudioChunk* sourceChunk, AudioChunk* destinationChunk, unsigned framesToProcess); 1.80 + void reset(); 1.81 + void setNumberOfChannels(unsigned); 1.82 + unsigned numberOfChannels() const { return m_numberOfChannels; } 1.83 + 1.84 + void setParameterValue(unsigned parameterID, float value); 1.85 + float parameterValue(unsigned parameterID); 1.86 + 1.87 + float sampleRate() const { return m_sampleRate; } 1.88 + float nyquist() const { return m_sampleRate / 2; } 1.89 + 1.90 + double tailTime() const { return 0; } 1.91 + double latencyTime() const { return m_compressor.latencyFrames() / static_cast<double>(sampleRate()); } 1.92 + 1.93 + size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.94 + 1.95 +protected: 1.96 + unsigned m_numberOfChannels; 1.97 + 1.98 + // m_parameters holds the tweakable compressor parameters. 1.99 + float m_parameters[ParamLast]; 1.100 + void initializeParameters(); 1.101 + 1.102 + float m_sampleRate; 1.103 + 1.104 + // Emphasis filter controls. 1.105 + float m_lastFilterStageRatio; 1.106 + float m_lastAnchor; 1.107 + float m_lastFilterStageGain; 1.108 + 1.109 + typedef struct { 1.110 + ZeroPole filters[4]; 1.111 + size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const 1.112 + { 1.113 + return aMallocSizeOf(this); 1.114 + } 1.115 + } ZeroPoleFilterPack4; 1.116 + 1.117 + // Per-channel emphasis filters. 1.118 + nsTArray<nsAutoPtr<ZeroPoleFilterPack4> > m_preFilterPacks; 1.119 + nsTArray<nsAutoPtr<ZeroPoleFilterPack4> > m_postFilterPacks; 1.120 + 1.121 + nsAutoArrayPtr<const float*> m_sourceChannels; 1.122 + nsAutoArrayPtr<float*> m_destinationChannels; 1.123 + 1.124 + void setEmphasisStageParameters(unsigned stageIndex, float gain, float normalizedFrequency /* 0 -> 1 */); 1.125 + void setEmphasisParameters(float gain, float anchorFreq, float filterStageRatio); 1.126 + 1.127 + // The core compressor. 1.128 + DynamicsCompressorKernel m_compressor; 1.129 +}; 1.130 + 1.131 +} // namespace WebCore 1.132 + 1.133 +#endif // DynamicsCompressor_h