The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

TwoPole.h
1#ifndef STK_TWOPOLE_H
2#define STK_TWOPOLE_H
3
4#include "Filter.h"
5
6namespace stk {
7
8/***************************************************/
18/***************************************************/
19
20 class TwoPole : public Filter
21{
22 public:
23
25 TwoPole( void );
26
28 ~TwoPole();
29
31 void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
32
34 void setB0( StkFloat b0 ) { b_[0] = b0; };
35
37 void setA1( StkFloat a1 ) { a_[1] = a1; };
38
40 void setA2( StkFloat a2 ) { a_[2] = a2; };
41
43 void setCoefficients( StkFloat b0, StkFloat a1, StkFloat a2, bool clearState = false );
44
46
61 void setResonance(StkFloat frequency, StkFloat radius, bool normalize = false);
62
64 StkFloat lastOut( void ) const { return lastFrame_[0]; };
65
67 StkFloat tick( StkFloat input );
68
70
78 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
79
81
89 StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
90
91 protected:
92
93 virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
94};
95
96 inline StkFloat TwoPole :: tick( StkFloat input )
97{
98 inputs_[0] = gain_ * input;
99 lastFrame_[0] = b_[0] * inputs_[0] - a_[1] * outputs_[1] - a_[2] * outputs_[2];
100 outputs_[2] = outputs_[1];
101 outputs_[1] = lastFrame_[0];
102
103 return lastFrame_[0];
104}
105
106 inline StkFrames& TwoPole :: tick( StkFrames& frames, unsigned int channel )
107{
108#if defined(_STK_DEBUG_)
109 if ( channel >= frames.channels() ) {
110 oStream_ << "TwoPole::tick(): channel and StkFrames arguments are incompatible!";
111 handleError( StkError::FUNCTION_ARGUMENT );
112 }
113#endif
114
115 StkFloat *samples = &frames[channel];
116 unsigned int hop = frames.channels();
117 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
118 inputs_[0] = gain_ * *samples;
119 *samples = b_[0] * inputs_[0] - a_[1] * outputs_[1] - a_[2] * outputs_[2];
120 outputs_[2] = outputs_[1];
121 outputs_[1] = *samples;
122 }
123
124 lastFrame_[0] = outputs_[1];
125 return frames;
126}
127
128 inline StkFrames& TwoPole :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
129{
130#if defined(_STK_DEBUG_)
131 if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
132 oStream_ << "TwoPole::tick(): channel and StkFrames arguments are incompatible!";
133 handleError( StkError::FUNCTION_ARGUMENT );
134 }
135#endif
136
137 StkFloat *iSamples = &iFrames[iChannel];
138 StkFloat *oSamples = &oFrames[oChannel];
139 unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
140 for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
141 inputs_[0] = gain_ * *iSamples;
142 *oSamples = b_[0] * inputs_[0] - a_[1] * outputs_[1] - a_[2] * outputs_[2];
143 outputs_[2] = outputs_[1];
144 outputs_[1] = *oSamples;
145 }
146
147 lastFrame_[0] = outputs_[1];
148 return iFrames;
149}
150
151} // stk namespace
152
153#endif
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::TwoPole
STK two-pole filter class.
Definition TwoPole.h:21
stk::TwoPole::setResonance
void setResonance(StkFloat frequency, StkFloat radius, bool normalize=false)
Sets the filter coefficients for a resonance at frequency (in Hz).
stk::TwoPole::TwoPole
TwoPole(void)
Default constructor creates a second-order pass-through filter.
stk::TwoPole::setB0
void setB0(StkFloat b0)
Set the b[0] coefficient value.
Definition TwoPole.h:34
stk::TwoPole::setA2
void setA2(StkFloat a2)
Set the a[2] coefficient value.
Definition TwoPole.h:40
stk::TwoPole::~TwoPole
~TwoPole()
Class destructor.
stk::TwoPole::ignoreSampleRateChange
void ignoreSampleRateChange(bool ignore=true)
A function to enable/disable the automatic updating of class data when the STK sample rate changes.
Definition TwoPole.h:31
stk::TwoPole::setA1
void setA1(StkFloat a1)
Set the a[1] coefficient value.
Definition TwoPole.h:37
stk::TwoPole::tick
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition TwoPole.h:96
stk::TwoPole::lastOut
StkFloat lastOut(void) const
Return the last computed output value.
Definition TwoPole.h:64
stk::TwoPole::setCoefficients
void setCoefficients(StkFloat b0, StkFloat a1, StkFloat a2, bool clearState=false)
Set all filter coefficients.
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.