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 
00063   void setResonance( StkFloat frequency, StkFloat radius, bool normalize = false );
00064 
00066 
00072   void setNotch( StkFloat frequency, StkFloat radius );
00073 
00075 
00081   void setEqualGainZeroes( void );
00082 
00084   StkFloat lastOut( void ) const { return lastFrame_[0]; };
00085 
00087   StkFloat tick( StkFloat input );
00088 
00090 
00098   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
00099 
00101 
00109   StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
00110 
00111  protected:
00112 
00113   virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
00114 };
00115 
00116 inline StkFloat BiQuad :: tick( StkFloat input )
00117 {
00118   inputs_[0] = gain_ * input;
00119   lastFrame_[0] = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
00120   lastFrame_[0] -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
00121   inputs_[2] = inputs_[1];
00122   inputs_[1] = inputs_[0];
00123   outputs_[2] = outputs_[1];
00124   outputs_[1] = lastFrame_[0];
00125 
00126   return lastFrame_[0];
00127 }
00128 
00129 inline StkFrames& BiQuad :: tick( StkFrames& frames, unsigned int channel )
00130 {
00131 #if defined(_STK_DEBUG_)
00132   if ( channel >= frames.channels() ) {
00133     errorString_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
00134     handleError( StkError::FUNCTION_ARGUMENT );
00135   }
00136 #endif
00137 
00138   StkFloat *samples = &frames[channel];
00139   unsigned int hop = frames.channels();
00140   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00141     inputs_[0] = gain_ * *samples;
00142     *samples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
00143     *samples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
00144     inputs_[2] = inputs_[1];
00145     inputs_[1] = inputs_[0];
00146     outputs_[2] = outputs_[1];
00147     outputs_[1] = *samples;
00148   }
00149 
00150   lastFrame_[0] = outputs_[1];
00151   return frames;
00152 }
00153 
00154 inline StkFrames& BiQuad :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
00155 {
00156 #if defined(_STK_DEBUG_)
00157   if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
00158     errorString_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
00159     handleError( StkError::FUNCTION_ARGUMENT );
00160   }
00161 #endif
00162 
00163   StkFloat *iSamples = &iFrames[iChannel];
00164   StkFloat *oSamples = &oFrames[oChannel];
00165   unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
00166   for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
00167     inputs_[0] = gain_ * *iSamples;
00168     *oSamples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
00169     *oSamples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
00170     inputs_[2] = inputs_[1];
00171     inputs_[1] = inputs_[0];
00172     outputs_[2] = outputs_[1];
00173     outputs_[1] = *oSamples;
00174   }
00175 
00176   lastFrame_[0] = outputs_[1];
00177   return iFrames;
00178 }
00179 
00180 } // stk namespace
00181 
00182 #endif
00183 

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