The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

TwoZero.h
1#ifndef STK_TWOZERO_H
2#define STK_TWOZERO_H
3
4#include "Filter.h"
5
6namespace stk {
7
8/***************************************************/
18/***************************************************/
19
20 class TwoZero : public Filter
21{
22 public:
24 TwoZero();
25
27 ~TwoZero();
28
30 void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
31
33 void setB0( StkFloat b0 ) { b_[0] = b0; };
34
36 void setB1( StkFloat b1 ) { b_[1] = b1; };
37
39 void setB2( StkFloat b2 ) { b_[2] = b2; };
40
42 void setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, bool clearState = false );
43
45
57 void setNotch( StkFloat frequency, StkFloat radius );
58
60 StkFloat lastOut( void ) const { return lastFrame_[0]; };
61
63 StkFloat tick( StkFloat input );
64
66
74 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
75
77
85 StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
86
87 protected:
88
89 void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
90};
91
92 inline StkFloat TwoZero :: tick( StkFloat input )
93{
94 inputs_[0] = gain_ * input;
95 lastFrame_[0] = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
96 inputs_[2] = inputs_[1];
97 inputs_[1] = inputs_[0];
98
99 return lastFrame_[0];
100}
101
102 inline StkFrames& TwoZero :: tick( StkFrames& frames, unsigned int channel )
103{
104#if defined(_STK_DEBUG_)
105 if ( channel >= frames.channels() ) {
106 oStream_ << "TwoZero::tick(): channel and StkFrames arguments are incompatible!";
107 handleError( StkError::FUNCTION_ARGUMENT );
108 }
109#endif
110
111 StkFloat *samples = &frames[channel];
112 unsigned int hop = frames.channels();
113 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
114 inputs_[0] = gain_ * *samples;
115 *samples = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
116 inputs_[2] = inputs_[1];
117 inputs_[1] = inputs_[0];
118 }
119
120 lastFrame_[0] = *(samples-hop);
121 return frames;
122}
123
124 inline StkFrames& TwoZero :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
125{
126#if defined(_STK_DEBUG_)
127 if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
128 oStream_ << "TwoZero::tick(): channel and StkFrames arguments are incompatible!";
129 handleError( StkError::FUNCTION_ARGUMENT );
130 }
131#endif
132
133 StkFloat *iSamples = &iFrames[iChannel];
134 StkFloat *oSamples = &oFrames[oChannel];
135 unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
136 for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
137 inputs_[0] = gain_ * *iSamples;
138 *oSamples = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
139 inputs_[2] = inputs_[1];
140 inputs_[1] = inputs_[0];
141 }
142
143 lastFrame_[0] = *(oSamples-oHop);
144 return iFrames;
145}
146
147} // stk namespace
148
149#endif
stk::Filter::Filter
Filter(void)
Class constructor.
Definition Filter.h:26
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::Stk::handleError
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
stk::TwoZero::setB1
void setB1(StkFloat b1)
Set the b[1] coefficient value.
Definition TwoZero.h:36
stk::TwoZero::setNotch
void setNotch(StkFloat frequency, StkFloat radius)
Sets the filter coefficients for a "notch" at frequency (in Hz).
stk::TwoZero::setCoefficients
void setCoefficients(StkFloat b0, StkFloat b1, StkFloat b2, bool clearState=false)
Set all filter coefficients.
stk::TwoZero::setB0
void setB0(StkFloat b0)
Set the b[0] coefficient value.
Definition TwoZero.h:33
stk::TwoZero::TwoZero
TwoZero()
Default constructor creates a second-order pass-through filter.
stk::TwoZero::lastOut
StkFloat lastOut(void) const
Return the last computed output value.
Definition TwoZero.h:60
stk::TwoZero::setB2
void setB2(StkFloat b2)
Set the b[2] coefficient value.
Definition TwoZero.h:39
stk::TwoZero::ignoreSampleRateChange
void ignoreSampleRateChange(bool ignore=true)
A function to enable/disable the automatic updating of class data when the STK sample rate changes.
Definition TwoZero.h:30
stk::TwoZero::~TwoZero
~TwoZero()
Class destructor.
stk::TwoZero::tick
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition TwoZero.h:92
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.