Async  1.5.0
AsyncAudioCompressor.h
Go to the documentation of this file.
1 
27 #ifndef ASYNC_AUDIO_COMPRESSOR_INCLUDED
28 #define ASYNC_AUDIO_COMPRESSOR_INCLUDED
29 
30 
31 /****************************************************************************
32  *
33  * System Includes
34  *
35  ****************************************************************************/
36 
37 #include <cmath>
38 
39 
40 /****************************************************************************
41  *
42  * Project Includes
43  *
44  ****************************************************************************/
45 
46 #include <AsyncAudioProcessor.h>
47 
48 
49 
50 /****************************************************************************
51  *
52  * Local Includes
53  *
54  ****************************************************************************/
55 
56 
57 
58 /****************************************************************************
59  *
60  * Forward declarations
61  *
62  ****************************************************************************/
63 
64 
65 
66 /****************************************************************************
67  *
68  * Namespace
69  *
70  ****************************************************************************/
71 
72 namespace Async
73 {
74 
75 
76 /****************************************************************************
77  *
78  * Forward declarations of classes inside of the declared namespace
79  *
80  ****************************************************************************/
81 
82 
83 
84 /****************************************************************************
85  *
86  * Defines & typedefs
87  *
88  ****************************************************************************/
89 
90 
91 
92 /****************************************************************************
93  *
94  * Exported Global Variables
95  *
96  ****************************************************************************/
97 
98 
99 
100 /****************************************************************************
101  *
102  * Class definitions
103  *
104  ****************************************************************************/
105 
107 {
108  public:
109  EnvelopeDetector( double ms = 1.0, double sampleRate = INTERNAL_SAMPLE_RATE )
110  : sampleRate_( sampleRate ), ms_( ms ), coef_( 0.0 )
111  {
112  setCoef();
113  }
114 
115  virtual ~EnvelopeDetector() {}
116 
117  // time constant
118  virtual void setTc( double ms )
119  {
120  ms_ = ms;
121  setCoef();
122  }
123 
124  virtual double getTc( void ) { return ms_; }
125 
126  // sample rate
127  virtual void setSampleRate( double sampleRate )
128  {
129  sampleRate_ = sampleRate;
130  setCoef();
131  }
132 
133  virtual double getSampleRate( void ) { return sampleRate_; }
134 
135  // runtime function
136  inline void run( double in, double &state )
137  {
138  state = in + coef_ * ( state - in );
139  }
140 
141  private:
142  double sampleRate_; // sample rate
143  double ms_; // time constant in ms
144  double coef_; // runtime coefficient
145 
146  void setCoef( void ) // coef algorithm
147  {
148  coef_ = exp( -1.0 / ( 0.001 * ms_ * sampleRate_ ) );
149  }
150 
151 }; // end EnvelopeDetector class
152 
153 
154 
155 
156 
170 {
171  public:
175  AudioCompressor(void);
176 
180  ~AudioCompressor(void);
181 
189  void setThreshold(double thresh_db) { threshdB_ = thresh_db; }
190 
195  void setRatio(double ratio) { ratio_ = ratio; }
196 
201  void setAttack(double attack_ms) { att_.setTc(attack_ms);}
202 
207  void setDecay(double decay_ms) { rel_.setTc(decay_ms); }
208 
217  void setOutputGain(float gain);
218 
222  void reset(void);
223 
224 
225  protected:
226  virtual void processSamples(float *dest, const float *src, int count);
227 
228 
229  private:
230  // transfer function
231  double threshdB_; // threshold (dB)
232  double ratio_; // ratio (compression: < 1 ; expansion: > 1)
233  double output_gain;
234 
235  // attack/release
236  EnvelopeDetector att_; // attack
237  EnvelopeDetector rel_; // release
238 
239  // runtime variables
240  double envdB_; // over-threshold envelope (dB)
241 
243  AudioCompressor& operator=(const AudioCompressor&);
244 
245 }; /* class AudioCompressor */
246 
247 
248 } /* namespace */
249 
250 #endif /* ASYNC_AUDIO_COMPRESSOR_INCLUDED */
251 
252 
253 
254 /*
255  * This file has not been truncated
256  */
257 
virtual double getSampleRate(void)
virtual void processSamples(float *dest, const float *src, int count)
Process incoming samples and put them into the output buffer.
void setRatio(double ratio)
Set the compression ratio.
The base class for an audio processor.
void setOutputGain(float gain)
Set the output gain.
~AudioCompressor(void)
Destructor.
void reset(void)
Reset the compressor.
void setDecay(double decay_ms)
Set the compressor decay time.
void setAttack(double attack_ms)
Set the compressor attack time.
virtual void setTc(double ms)
virtual double getTc(void)
A class to do audio compression/limiting.
virtual void setSampleRate(double sampleRate)
Namespace for the asynchronous programming classes.
AudioCompressor(void)
Default constuctor.
The base class for an audio processor class.
void setThreshold(double thresh_db)
Set the compression threshold.
void run(double in, double &state)
EnvelopeDetector(double ms=1.0, double sampleRate=INTERNAL_SAMPLE_RATE)