Arduino STK  4.6.2
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 
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 
STK biquad (two-pole, two-zero) filter class.
Definition: BiQuad.h:21
void setA1(StkFloat a1)
Set the a[1] coefficient value.
Definition: BiQuad.h:46
void setB2(StkFloat b2)
Set the b[2] coefficient value.
Definition: BiQuad.h:43
void setB0(StkFloat b0)
Set the b[0] coefficient value.
Definition: BiQuad.h:37
void setEqualGainZeroes(void)
Sets the filter zeroes for equal resonance gain.
void setResonance(StkFloat frequency, StkFloat radius, bool normalize=false)
Sets the filter coefficients for a resonance at frequency (in Hz).
StkFloat tick(StkFloat input)
Input one sample to the filter and return a reference to one output.
Definition: BiQuad.h:119
BiQuad()
Default constructor creates a second-order pass-through filter.
void ignoreSampleRateChange(bool ignore=true)
A function to enable/disable the automatic updating of class data when the STK sample rate changes.
Definition: BiQuad.h:31
StkFloat lastOut(void) const
Return the last computed output value.
Definition: BiQuad.h:87
~BiQuad()
Class destructor.
void setB1(StkFloat b1)
Set the b[1] coefficient value.
Definition: BiQuad.h:40
void setNotch(StkFloat frequency, StkFloat radius)
Set the filter coefficients for a notch at frequency (in Hz).
void setA2(StkFloat a2)
Set the a[2] coefficient value.
Definition: BiQuad.h:49
void setCoefficients(StkFloat b0, StkFloat b1, StkFloat b2, StkFloat a1, StkFloat a2, bool clearState=false)
Set all filter coefficients.
STK abstract filter class.
Definition: Filter.h:23
An STK class to handle vectorized audio data.
Definition: Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition: ADSR.h:8