Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


BiQuad.h
1 #ifndef STK_BIQUAD_H
2 #define STK_BIQUAD_H
3 
4 #include "Filter.h"
5 
6 namespace stk {
7 
8 /***************************************************/
18 /***************************************************/
19 
20 class BiQuad : public Filter
21 {
22 public:
23 
25  BiQuad();
26 
28  ~BiQuad();
29 
31  void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
32 
34  void setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, StkFloat a1, StkFloat a2, bool clearState = false );
35 
37  void setB0( StkFloat b0 ) { b_[0] = b0; };
38 
40  void setB1( StkFloat b1 ) { b_[1] = b1; };
41 
43  void setB2( StkFloat b2 ) { b_[2] = b2; };
44 
46  void setA1( StkFloat a1 ) { a_[1] = a1; };
47 
49  void setA2( StkFloat a2 ) { a_[2] = a2; };
50 
52 
65  void setResonance( StkFloat frequency, StkFloat radius, bool normalize = false );
66 
68 
75  void setNotch( StkFloat frequency, StkFloat radius );
76 
78 
84  void setEqualGainZeroes( void );
85 
87  StkFloat lastOut( void ) const { return lastFrame_[0]; };
88 
90  StkFloat tick( StkFloat input );
91 
93 
101  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
102 
104 
112  StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
113 
114  protected:
115 
116  virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
117 };
118 
119 inline StkFloat BiQuad :: tick( StkFloat input )
120 {
121  inputs_[0] = gain_ * input;
122  lastFrame_[0] = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
123  lastFrame_[0] -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
124  inputs_[2] = inputs_[1];
125  inputs_[1] = inputs_[0];
126  outputs_[2] = outputs_[1];
127  outputs_[1] = lastFrame_[0];
128 
129  return lastFrame_[0];
130 }
131 
132 inline StkFrames& BiQuad :: tick( StkFrames& frames, unsigned int channel )
133 {
134 #if defined(_STK_DEBUG_)
135  if ( channel >= frames.channels() ) {
136  oStream_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
137  handleError( StkError::FUNCTION_ARGUMENT );
138  }
139 #endif
140 
141  StkFloat *samples = &frames[channel];
142  unsigned int hop = frames.channels();
143  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
144  inputs_[0] = gain_ * *samples;
145  *samples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
146  *samples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
147  inputs_[2] = inputs_[1];
148  inputs_[1] = inputs_[0];
149  outputs_[2] = outputs_[1];
150  outputs_[1] = *samples;
151  }
152 
153  lastFrame_[0] = outputs_[1];
154  return frames;
155 }
156 
157 inline StkFrames& BiQuad :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
158 {
159 #if defined(_STK_DEBUG_)
160  if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
161  oStream_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
162  handleError( StkError::FUNCTION_ARGUMENT );
163  }
164 #endif
165 
166  StkFloat *iSamples = &iFrames[iChannel];
167  StkFloat *oSamples = &oFrames[oChannel];
168  unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
169  for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
170  inputs_[0] = gain_ * *iSamples;
171  *oSamples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
172  *oSamples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
173  inputs_[2] = inputs_[1];
174  inputs_[1] = inputs_[0];
175  outputs_[2] = outputs_[1];
176  outputs_[1] = *oSamples;
177  }
178 
179  lastFrame_[0] = outputs_[1];
180  return iFrames;
181 }
182 
183 } // stk namespace
184 
185 #endif
186 

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