Async
0.18.0
|
00001 00027 #ifndef ASYNC_AUDIO_COMPRESSOR_INCLUDED 00028 #define ASYNC_AUDIO_COMPRESSOR_INCLUDED 00029 00030 00031 /**************************************************************************** 00032 * 00033 * System Includes 00034 * 00035 ****************************************************************************/ 00036 00037 #include <cmath> 00038 00039 00040 /**************************************************************************** 00041 * 00042 * Project Includes 00043 * 00044 ****************************************************************************/ 00045 00046 #include <AsyncAudioProcessor.h> 00047 00048 00049 00050 /**************************************************************************** 00051 * 00052 * Local Includes 00053 * 00054 ****************************************************************************/ 00055 00056 00057 00058 /**************************************************************************** 00059 * 00060 * Forward declarations 00061 * 00062 ****************************************************************************/ 00063 00064 00065 00066 /**************************************************************************** 00067 * 00068 * Namespace 00069 * 00070 ****************************************************************************/ 00071 00072 namespace Async 00073 { 00074 00075 00076 /**************************************************************************** 00077 * 00078 * Forward declarations of classes inside of the declared namespace 00079 * 00080 ****************************************************************************/ 00081 00082 00083 00084 /**************************************************************************** 00085 * 00086 * Defines & typedefs 00087 * 00088 ****************************************************************************/ 00089 00090 00091 00092 /**************************************************************************** 00093 * 00094 * Exported Global Variables 00095 * 00096 ****************************************************************************/ 00097 00098 00099 00100 /**************************************************************************** 00101 * 00102 * Class definitions 00103 * 00104 ****************************************************************************/ 00105 00106 class EnvelopeDetector 00107 { 00108 public: 00109 EnvelopeDetector( double ms = 1.0, double sampleRate = INTERNAL_SAMPLE_RATE ) 00110 : sampleRate_( sampleRate ), ms_( ms ), coef_( 0.0 ) 00111 { 00112 setCoef(); 00113 } 00114 00115 virtual ~EnvelopeDetector() {} 00116 00117 // time constant 00118 virtual void setTc( double ms ) 00119 { 00120 ms_ = ms; 00121 setCoef(); 00122 } 00123 00124 virtual double getTc( void ) { return ms_; } 00125 00126 // sample rate 00127 virtual void setSampleRate( double sampleRate ) 00128 { 00129 sampleRate_ = sampleRate; 00130 setCoef(); 00131 } 00132 00133 virtual double getSampleRate( void ) { return sampleRate_; } 00134 00135 // runtime function 00136 inline void run( double in, double &state ) 00137 { 00138 state = in + coef_ * ( state - in ); 00139 } 00140 00141 private: 00142 double sampleRate_; // sample rate 00143 double ms_; // time constant in ms 00144 double coef_; // runtime coefficient 00145 00146 void setCoef( void ) // coef algorithm 00147 { 00148 coef_ = exp( -1.0 / ( 0.001 * ms_ * sampleRate_ ) ); 00149 } 00150 00151 }; // end EnvelopeDetector class 00152 00153 00154 00155 00156 00169 class AudioCompressor : public AudioProcessor 00170 { 00171 public: 00175 AudioCompressor(void); 00176 00180 ~AudioCompressor(void); 00181 00189 void setThreshold(double thresh_db) { threshdB_ = thresh_db; } 00190 00195 void setRatio(double ratio) { ratio_ = ratio; } 00196 00201 void setAttack(double attack_ms) { att_.setTc(attack_ms);} 00202 00207 void setDecay(double decay_ms) { rel_.setTc(decay_ms); } 00208 00217 void setOutputGain(float gain); 00218 00222 void reset(void); 00223 00224 00225 protected: 00226 virtual void processSamples(float *dest, const float *src, int count); 00227 00228 00229 private: 00230 // transfer function 00231 double threshdB_; // threshold (dB) 00232 double ratio_; // ratio (compression: < 1 ; expansion: > 1) 00233 double output_gain; 00234 00235 // attack/release 00236 EnvelopeDetector att_; // attack 00237 EnvelopeDetector rel_; // release 00238 00239 // runtime variables 00240 double envdB_; // over-threshold envelope (dB) 00241 00242 AudioCompressor(const AudioCompressor&); 00243 AudioCompressor& operator=(const AudioCompressor&); 00244 00245 }; /* class AudioCompressor */ 00246 00247 00248 } /* namespace */ 00249 00250 #endif /* ASYNC_AUDIO_COMPRESSOR_INCLUDED */ 00251 00252 00253 00254 /* 00255 * This file has not been truncated 00256 */ 00257