The Synthesis ToolKit in C++ (STK)

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
6namespace stk {
7
8/***************************************************/
22/***************************************************/
23
24const StkFloat RECIP_SQRT_2 = static_cast<StkFloat>( M_SQRT1_2 );
25 class BiQuad : public Filter
26{
27public:
28
30 BiQuad();
31
33 ~BiQuad();
34
36 void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
37
39 void setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, StkFloat a1, StkFloat a2, bool clearState = false );
40
42 void setB0( StkFloat b0 ) { b_[0] = b0; };
43
45 void setB1( StkFloat b1 ) { b_[1] = b1; };
46
48 void setB2( StkFloat b2 ) { b_[2] = b2; };
49
51 void setA1( StkFloat a1 ) { a_[1] = a1; };
52
54 void setA2( StkFloat a2 ) { a_[2] = a2; };
55
57
70 void setResonance( StkFloat frequency, StkFloat radius, bool normalize = false );
71
73
80 void setNotch( StkFloat frequency, StkFloat radius );
81
83
94 void setLowPass( StkFloat fc, StkFloat Q=RECIP_SQRT_2 );
95
97
108 void setHighPass( StkFloat fc, StkFloat Q=RECIP_SQRT_2 );
109
111
120 void setBandPass( StkFloat fc, StkFloat Q );
121
123
131 void setBandReject( StkFloat fc, StkFloat Q );
132
134
141 void setAllPass( StkFloat fc, StkFloat Q );
142
144
149 void setEqualGainZeroes( void );
150
152 StkFloat lastOut( void ) const { return lastFrame_[0]; };
153
155 StkFloat tick( StkFloat input );
156
158
166 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
167
169
177 StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
178
179 protected:
180
181 virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
182
183 // Helper function to update the three intermediate values for the predefined filter types
184 // along with the feedback filter coefficients. Performs the debug check for fc and Q-factor arguments.
185 void setCommonFilterValues( StkFloat fc, StkFloat Q );
186
187 StkFloat K_;
188 StkFloat kSqr_;
189 StkFloat denom_;
190};
191
192 inline StkFloat BiQuad :: tick( StkFloat input )
193{
194 inputs_[0] = gain_ * input;
195 lastFrame_[0] = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
196 lastFrame_[0] -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
197 inputs_[2] = inputs_[1];
198 inputs_[1] = inputs_[0];
199 outputs_[2] = outputs_[1];
200 outputs_[1] = lastFrame_[0];
201
202 return lastFrame_[0];
203}
204
205 inline StkFrames& BiQuad :: tick( StkFrames& frames, unsigned int channel )
206{
207#if defined(_STK_DEBUG_)
208 if ( channel >= frames.channels() ) {
209 oStream_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
210 handleError( StkError::FUNCTION_ARGUMENT );
211 }
212#endif
213
214 StkFloat *samples = &frames[channel];
215 unsigned int hop = frames.channels();
216 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
217 inputs_[0] = gain_ * *samples;
218 *samples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
219 *samples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
220 inputs_[2] = inputs_[1];
221 inputs_[1] = inputs_[0];
222 outputs_[2] = outputs_[1];
223 outputs_[1] = *samples;
224 }
225
226 lastFrame_[0] = outputs_[1];
227 return frames;
228}
229
230 inline StkFrames& BiQuad :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
231{
232#if defined(_STK_DEBUG_)
233 if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
234 oStream_ << "BiQuad::tick(): channel and StkFrames arguments are incompatible!";
235 handleError( StkError::FUNCTION_ARGUMENT );
236 }
237#endif
238
239 StkFloat *iSamples = &iFrames[iChannel];
240 StkFloat *oSamples = &oFrames[oChannel];
241 unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
242 for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
243 inputs_[0] = gain_ * *iSamples;
244 *oSamples = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
245 *oSamples -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
246 inputs_[2] = inputs_[1];
247 inputs_[1] = inputs_[0];
248 outputs_[2] = outputs_[1];
249 outputs_[1] = *oSamples;
250 }
251
252 lastFrame_[0] = outputs_[1];
253 return iFrames;
254}
255
256} // stk namespace
257
258#endif
259
stk::BiQuad
STK biquad (two-pole, two-zero) filter class.
Definition BiQuad.h:26
stk::BiQuad::setLowPass
void setLowPass(StkFloat fc, StkFloat Q=RECIP_SQRT_2)
Set the filter coefficients for a low-pass with cutoff frequency fc (in Hz) and Q-factor Q.
stk::BiQuad::setA1
void setA1(StkFloat a1)
Set the a[1] coefficient value.
Definition BiQuad.h:51
stk::BiQuad::setB2
void setB2(StkFloat b2)
Set the b[2] coefficient value.
Definition BiQuad.h:48
stk::BiQuad::setB0
void setB0(StkFloat b0)
Set the b[0] coefficient value.
Definition BiQuad.h:42
stk::BiQuad::setEqualGainZeroes
void setEqualGainZeroes(void)
Sets the filter zeroes for equal resonance gain.
stk::BiQuad::setHighPass
void setHighPass(StkFloat fc, StkFloat Q=RECIP_SQRT_2)
Set the filter coefficients for a high-pass with cutoff frequency fc (in Hz) and Q-factor Q.
stk::BiQuad::setResonance
void setResonance(StkFloat frequency, StkFloat radius, bool normalize=false)
Sets the filter coefficients for a resonance at frequency (in Hz).
stk::BiQuad::setBandReject
void setBandReject(StkFloat fc, StkFloat Q)
Set the filter coefficients for a band-reject centered at fc (in Hz) with Q-factor Q.
stk::BiQuad::tick
StkFloat tick(StkFloat input)
Input one sample to the filter and return a reference to one output.
Definition BiQuad.h:192
stk::BiQuad::setAllPass
void setAllPass(StkFloat fc, StkFloat Q)
Set the filter coefficients for an all-pass centered at fc (in Hz) with Q-factor Q.
stk::BiQuad::setBandPass
void setBandPass(StkFloat fc, StkFloat Q)
Set the filter coefficients for a band-pass centered at fc (in Hz) with Q-factor Q.
stk::BiQuad::BiQuad
BiQuad()
Default constructor creates a second-order pass-through filter.
stk::BiQuad::ignoreSampleRateChange
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:36
stk::BiQuad::lastOut
StkFloat lastOut(void) const
Return the last computed output value.
Definition BiQuad.h:152
stk::BiQuad::~BiQuad
~BiQuad()
Class destructor.
stk::BiQuad::setB1
void setB1(StkFloat b1)
Set the b[1] coefficient value.
Definition BiQuad.h:45
stk::BiQuad::setNotch
void setNotch(StkFloat frequency, StkFloat radius)
Set the filter coefficients for a notch at frequency (in Hz).
stk::BiQuad::setA2
void setA2(StkFloat a2)
Set the a[2] coefficient value.
Definition BiQuad.h:54
stk::BiQuad::setCoefficients
void setCoefficients(StkFloat b0, StkFloat b1, StkFloat b2, StkFloat a1, StkFloat a2, bool clearState=false)
Set all filter coefficients.
stk::Filter
STK abstract filter class.
Definition Filter.h:23
stk::StkFrames
An STK class to handle vectorized audio data.
Definition Stk.h:279
stk::StkFrames::channels
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition Stk.h:416
stk::StkFrames::frames
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition Stk.h:419
stk
The STK namespace.
Definition ADSR.h:6

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