Line data Source code
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 DynamicsCompressor_h
30 : #define DynamicsCompressor_h
31 :
32 : #include "DynamicsCompressorKernel.h"
33 : #include "ZeroPole.h"
34 :
35 : #include "nsTArray.h"
36 : #include "nsAutoPtr.h"
37 : #include "mozilla/MemoryReporting.h"
38 : #include "mozilla/UniquePtr.h"
39 :
40 : namespace mozilla {
41 : class AudioBlock;
42 : } // namespace mozilla
43 :
44 : namespace WebCore {
45 :
46 : using mozilla::AudioBlock;
47 :
48 : // DynamicsCompressor implements a flexible audio dynamics compression effect such as
49 : // is commonly used in musical production and game audio. It lowers the volume
50 : // of the loudest parts of the signal and raises the volume of the softest parts,
51 : // making the sound richer, fuller, and more controlled.
52 :
53 0 : class DynamicsCompressor {
54 : public:
55 : enum {
56 : ParamThreshold,
57 : ParamKnee,
58 : ParamRatio,
59 : ParamAttack,
60 : ParamRelease,
61 : ParamPreDelay,
62 : ParamReleaseZone1,
63 : ParamReleaseZone2,
64 : ParamReleaseZone3,
65 : ParamReleaseZone4,
66 : ParamPostGain,
67 : ParamFilterStageGain,
68 : ParamFilterStageRatio,
69 : ParamFilterAnchor,
70 : ParamEffectBlend,
71 : ParamReduction,
72 : ParamLast
73 : };
74 :
75 : DynamicsCompressor(float sampleRate, unsigned numberOfChannels);
76 :
77 : void process(const AudioBlock* sourceChunk, AudioBlock* destinationChunk, unsigned framesToProcess);
78 : void reset();
79 : void setNumberOfChannels(unsigned);
80 0 : unsigned numberOfChannels() const { return m_numberOfChannels; }
81 :
82 : void setParameterValue(unsigned parameterID, float value);
83 : float parameterValue(unsigned parameterID);
84 :
85 : float sampleRate() const { return m_sampleRate; }
86 0 : float nyquist() const { return m_sampleRate / 2; }
87 :
88 : double tailTime() const { return 0; }
89 : double latencyTime() const { return m_compressor.latencyFrames() / static_cast<double>(sampleRate()); }
90 :
91 : size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
92 :
93 : protected:
94 : unsigned m_numberOfChannels;
95 :
96 : // m_parameters holds the tweakable compressor parameters.
97 : float m_parameters[ParamLast];
98 : void initializeParameters();
99 :
100 : float m_sampleRate;
101 :
102 : // Emphasis filter controls.
103 : float m_lastFilterStageRatio;
104 : float m_lastAnchor;
105 : float m_lastFilterStageGain;
106 :
107 : typedef struct {
108 : ZeroPole filters[4];
109 0 : size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
110 : {
111 0 : return aMallocSizeOf(this);
112 : }
113 0 : } ZeroPoleFilterPack4;
114 :
115 : // Per-channel emphasis filters.
116 : nsTArray<nsAutoPtr<ZeroPoleFilterPack4> > m_preFilterPacks;
117 : nsTArray<nsAutoPtr<ZeroPoleFilterPack4> > m_postFilterPacks;
118 :
119 : mozilla::UniquePtr<const float*[]> m_sourceChannels;
120 : mozilla::UniquePtr<float*[]> m_destinationChannels;
121 :
122 : void setEmphasisStageParameters(unsigned stageIndex, float gain, float normalizedFrequency /* 0 -> 1 */);
123 : void setEmphasisParameters(float gain, float anchorFreq, float filterStageRatio);
124 :
125 : // The core compressor.
126 : DynamicsCompressorKernel m_compressor;
127 : };
128 :
129 : } // namespace WebCore
130 :
131 : #endif // DynamicsCompressor_h
|