Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


BiQuad.h

00001 #ifndef STK_BIQUAD_H
00002 #define STK_BIQUAD_H
00003 
00004 #include "Filter.h"
00005 
00006 namespace stk {
00007 
00008 /***************************************************/
00018 /***************************************************/
00019 
00020 class BiQuad : public Filter
00021 {
00022 public:
00023 
00025   BiQuad();
00026 
00028   ~BiQuad();
00029 
00031   void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
00032 
00034   void setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, StkFloat a1, StkFloat a2, bool clearState = false );
00035 
00037   void setB0( StkFloat b0 ) { b_[0] = b0; };
00038 
00040   void setB1( StkFloat b1 ) { b_[1] = b1; };
00041 
00043   void setB2( StkFloat b2 ) { b_[2] = b2; };
00044 
00046   void setA1( StkFloat a1 ) { a_[1] = a1; };
00047 
00049   void setA2( StkFloat a2 ) { a_[2] = a2; };
00050 
00052 
00065   void setResonance( StkFloat frequency, StkFloat radius, bool normalize = false );
00066 
00068 
00075   void setNotch( StkFloat frequency, StkFloat radius );
00076 
00078 
00084   void setEqualGainZeroes( void );
00085 
00087   StkFloat lastOut( void ) const { return lastFrame_[0]; };
00088 
00090   StkFloat tick( StkFloat input );
00091 
00093 
00101   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
00102 
00104 
00112   StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
00113 
00114  protected:
00115 
00116   virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
00117 };
00118 
00119 inline StkFloat BiQuad :: tick( StkFloat input )
00120 {
00121   inputs_[0] = gain_ * input;
00122   lastFrame_[0] = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
00123   lastFrame_[0] -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
00124   inputs_[2] = inputs_[1];
00125   inputs_[1] = inputs_[0];
00126   outputs_[2] = outputs_[1];
00127   outputs_[1] = lastFrame_[0];
00128 
00129   return lastFrame_[0];
00130 }
00131 
00132 inline StkFrames& BiQuad :: tick( StkFrames& frames, unsigned int channel )
00133 {
00134 #if defined(_STK_DEBUG_)
00135   if ( channel >= frames.channels() ) {
00136     oStream_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
00137     handleError( StkError::FUNCTION_ARGUMENT );
00138   }
00139 #endif
00140 
00141   StkFloat *samples = &frames[channel];
00142   unsigned int hop = frames.channels();
00143   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00144     inputs_[0] = gain_ * *samples;
00145     *samples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
00146     *samples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
00147     inputs_[2] = inputs_[1];
00148     inputs_[1] = inputs_[0];
00149     outputs_[2] = outputs_[1];
00150     outputs_[1] = *samples;
00151   }
00152 
00153   lastFrame_[0] = outputs_[1];
00154   return frames;
00155 }
00156 
00157 inline StkFrames& BiQuad :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
00158 {
00159 #if defined(_STK_DEBUG_)
00160   if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
00161     oStream_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
00162     handleError( StkError::FUNCTION_ARGUMENT );
00163   }
00164 #endif
00165 
00166   StkFloat *iSamples = &iFrames[iChannel];
00167   StkFloat *oSamples = &oFrames[oChannel];
00168   unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
00169   for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
00170     inputs_[0] = gain_ * *iSamples;
00171     *oSamples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
00172     *oSamples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
00173     inputs_[2] = inputs_[1];
00174     inputs_[1] = inputs_[0];
00175     outputs_[2] = outputs_[1];
00176     outputs_[1] = *oSamples;
00177   }
00178 
00179   lastFrame_[0] = outputs_[1];
00180   return iFrames;
00181 }
00182 
00183 } // stk namespace
00184 
00185 #endif
00186 

The Synthesis ToolKit in C++ (STK)
©1995-2012 Perry R. Cook and Gary P. Scavone. All Rights Reserved.