The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

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 * 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 * PI );
119 return phase / omegaT;
120 }
121
122 } // stk namespace
123
124 #endif

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