1 module stk.filter; 2 3 public import stk.stk; 4 5 /***************************************************/ 6 /*! \class Filter 7 \brief STK abstract filter class. 8 9 This class provides limited common functionality for STK digital 10 filter subclasses. It is general enough to support both 11 monophonic and polyphonic input/output classes. 12 13 by Perry R. Cook and Gary P. Scavone, 1995--2016. 14 */ 15 /***************************************************/ 16 17 class Filter : Stk 18 { 19 //! Class constructor. 20 this() { gain_ = 1.0; channelsIn_ = 1; lastFrame_.resize( 1, 1, 0.0 ); }; 21 22 //! Return the number of input channels for the class. 23 uint channelsIn() const { return channelsIn_; }; 24 25 //! Return the number of output channels for the class. 26 uint channelsOut() const { return lastFrame_.channels(); }; 27 28 //! Clears all internal states of the filter. 29 void clear() { 30 31 inputs_.fill(0.0); 32 outputs_.fill(0.0); 33 lastFrame_.fill(0.0); 34 } 35 36 //! Set the filter gain. 37 /*! 38 The gain is applied at the filter input and does not affect the 39 coefficient values. The default gain value is 1.0. 40 */ 41 void setGain( StkFloat gain ) { gain_ = gain; } 42 43 //! Return the current filter gain. 44 StkFloat getGain() const { return gain_; } 45 46 //! Return the filter phase delay at the specified frequency. 47 /*! 48 Note that the phase delay calculation accounts for the filter 49 gain. The frequency value should be greater than 0.0 and less 50 than or equal to one-half the sample rate. 51 */ 52 StkFloat phaseDelay( StkFloat frequency ) { 53 import std.math : sin, cos, PI, atan2, fmod; 54 if ( frequency <= 0.0 || frequency > 0.5 * Stk.sampleRate() ) { 55 //oStream_ << "Filter::phaseDelay: argument (" << frequency << ") is out of range!"; 56 handleError( StkErrorType.WARNING ); 57 return 0.0; 58 } 59 60 StkFloat omegaT = 2 * PI * frequency / Stk.sampleRate(); 61 StkFloat realpart = 0.0, imagpart = 0.0; 62 for ( uint i=0; i<b_.length; i++ ) { 63 realpart += b_[i] * cos( i * omegaT ); 64 imagpart -= b_[i] * sin( i * omegaT ); 65 } 66 realpart *= gain_; 67 imagpart *= gain_; 68 69 StkFloat phase = atan2( imagpart, realpart ); 70 71 realpart = 0.0, imagpart = 0.0; 72 for ( uint i=0; i<a_.length; i++ ) { 73 realpart += a_[i] * cos( i * omegaT ); 74 realpart -= a_[i] * sin( i * omegaT ); 75 } 76 77 phase -= atan2( imagpart, realpart ); 78 phase = fmod( -phase, 2 * PI ); 79 return phase / omegaT; 80 } 81 82 //! Return an StkFrames reference to the last output sample frame. 83 ref StkFrames lastFrame() { return lastFrame_; } 84 85 //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs. 86 /*! 87 The StkFrames argument reference is returned. The \c channel 88 argument must be less than the number of channels in the 89 StkFrames argument (the first channel is specified by 0). 90 However, range checking is only performed if _STK_DEBUG_ is 91 defined during compilation, in which case an out-of-range value 92 will trigger an StkError exception. 93 */ 94 //ref StkFrames tick( ref StkFrames frames, uint channel = 0 ) = 0; 95 96 protected: 97 98 StkFloat gain_; 99 uint channelsIn_; 100 StkFrames lastFrame_; 101 102 StkFloat[] b_; 103 StkFloat[] a_; 104 StkFrames outputs_; 105 StkFrames inputs_; 106 }