Arduino STK  4.6.2
Filter.h
1 #ifndef STK_FILTER_H
2 #define STK_FILTER_H
3 
4 #include "Stk.h"
5 #include <vector>
6 #include <cmath>
7 
8 namespace stk {
9 
10 /***************************************************/
20 /***************************************************/
21 
22 class Filter : public Stk
23 {
24 public:
26  Filter( void ) { gain_ = 1.0; channelsIn_ = 1; lastFrame_.resize( 1, 1, 0.0 ); };
27 
29  unsigned int channelsIn( void ) const { return channelsIn_; };
30 
32  unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
33 
35  virtual void clear( void );
36 
38 
42  void setGain( StkFloat gain ) { gain_ = gain; };
43 
45  StkFloat getGain( void ) const { return gain_; };
46 
48 
53  StkFloat phaseDelay( StkFloat frequency );
54 
56  const StkFrames& lastFrame( void ) const { return lastFrame_; };
57 
59 
67  virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 ) = 0;
68 
69 protected:
70 
71  StkFloat gain_;
72  unsigned int channelsIn_;
73  StkFrames lastFrame_;
74 
75  std::vector<StkFloat> b_;
76  std::vector<StkFloat> a_;
77  StkFrames outputs_;
78  StkFrames inputs_;
79 
80 };
81 
82 inline void Filter :: clear( void )
83 {
84  unsigned int i;
85  for ( i=0; i<inputs_.size(); i++ )
86  inputs_[i] = 0.0;
87  for ( i=0; i<outputs_.size(); i++ )
88  outputs_[i] = 0.0;
89  for ( i=0; i<lastFrame_.size(); i++ )
90  lastFrame_[i] = 0.0;
91 }
92 
93 inline StkFloat Filter :: phaseDelay( StkFloat frequency )
94 {
95  if ( frequency <= 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
96  oStream_ << "Filter::phaseDelay: argument (" << frequency << ") is out of range!";
97  handleError( StkError::WARNING ); return 0.0;
98  }
99 
100  StkFloat omegaT = 2 * STK_PI * frequency / Stk::sampleRate();
101  StkFloat real = 0.0, imag = 0.0;
102  for ( unsigned int i=0; i<b_.size(); i++ ) {
103  real += b_[i] * std::cos( i * omegaT );
104  imag -= b_[i] * std::sin( i * omegaT );
105  }
106  real *= gain_;
107  imag *= gain_;
108 
109  StkFloat phase = atan2( imag, real );
110 
111  real = 0.0, imag = 0.0;
112  for ( unsigned int i=0; i<a_.size(); i++ ) {
113  real += a_[i] * std::cos( i * omegaT );
114  imag -= a_[i] * std::sin( i * omegaT );
115  }
116 
117  phase -= std::atan2( imag, real );
118  phase = std::fmod( -phase, 2 * STK_PI );
119  return phase / omegaT;
120 }
121 
122 } // stk namespace
123 
124 #endif
STK abstract filter class.
Definition: Filter.h:23
StkFloat getGain(void) const
Return the current filter gain.
Definition: Filter.h:45
unsigned int channelsOut(void) const
Return the number of output channels for the class.
Definition: Filter.h:32
virtual StkFrames & tick(StkFrames &frames, unsigned int channel=0)=0
Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs...
unsigned int channelsIn(void) const
Return the number of input channels for the class.
Definition: Filter.h:29
StkFloat phaseDelay(StkFloat frequency)
Return the filter phase delay at the specified frequency.
Definition: Filter.h:93
virtual void clear(void)
Clears all internal states of the filter.
Definition: Filter.h:82
void setGain(StkFloat gain)
Set the filter gain.
Definition: Filter.h:42
const StkFrames & lastFrame(void) const
Return an StkFrames reference to the last output sample frame.
Definition: Filter.h:56
Filter(void)
Class constructor.
Definition: Filter.h:26
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
size_t size() const
Returns the total number of audio samples represented by the object.
Definition: Stk.h:373
void resize(size_t nFrames, unsigned int nChannels=1)
Resize self to represent the specified number of channels and frames.
STK base class.
Definition: Stk.h:144
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
static StkFloat sampleRate(void)
Static method that returns the current STK sample rate.
Definition: Stk.h:156
The STK namespace.
Definition: ADSR.h:8