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 
6 namespace 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

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