1 module stk.twozero;
2 
3 import stk.filter;
4 
5 /***************************************************/
6 /*! \class TwoZero
7 \brief STK two-zero filter class.
8 
9 This class implements a two-zero digital filter.  A method is
10 provided for creating a "notch" in the frequency response while
11 maintaining a constant filter gain.
12 
13 by Perry R. Cook and Gary P. Scavone, 1995--2016.
14 */
15 /***************************************************/
16 
17 class TwoZero : Filter
18 {
19 public:
20     //! Default constructor creates a second-order pass-through filter.
21     this() {
22         b_.length = 3;
23         b_[0 .. $] = 0;
24         inputs_.resize( 3, 1, 0.0 );
25         b_[0] = 1.0;
26         Stk.addSampleRateAlert( this );
27     }
28 
29     //! Class destructor.
30     ~this() {
31         Stk.removeSampleRateAlert( this );
32     }
33 
34     //! A function to enable/disable the automatic updating of class data when the STK sample rate changes.
35     override void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
36 
37     //! Set the b[0] coefficient value.
38     void setB0( StkFloat b0 ) { b_[0] = b0; };
39 
40     //! Set the b[1] coefficient value.
41     void setB1( StkFloat b1 ) { b_[1] = b1; };
42 
43     //! Set the b[2] coefficient value.
44     void setB2( StkFloat b2 ) { b_[2] = b2; };
45 
46     //! Set all filter coefficients.
47     void setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, bool clearState = false ) {
48         b_[0] = b0;
49         b_[1] = b1;
50         b_[2] = b2;
51 
52         if ( clearState ) this.clear();
53     }
54 
55     //! Sets the filter coefficients for a "notch" at \e frequency (in Hz).
56     /*!
57     This method determines the filter coefficients corresponding to
58     two complex-conjugate zeros with the given \e frequency (in Hz)
59     and \e radius from the z-plane origin.  The coefficients are then
60     normalized to produce a maximum filter gain of one (independent of
61     the filter \e gain parameter).  The resulting filter frequency
62     response has a "notch" or anti-resonance at the given \e
63     frequency.  The closer the zeros are to the unit-circle (\e radius
64     close to or equal to one), the narrower the resulting notch width.
65     The \e frequency value should be between zero and half the sample
66     rate.  The \e radius value should be positive.
67     */
68     void setNotch( StkFloat frequency, StkFloat radius ) {
69         b_[2] = radius * radius;
70         b_[1] = -2.0 * radius * cos(TWO_PI * frequency / Stk.sampleRate());
71 
72         // Normalize the filter gain.
73         if ( b_[1] > 0.0 ) // Maximum at z = 0.
74             b_[0] = 1.0 / ( 1.0 + b_[1] + b_[2] );
75         else            // Maximum at z = -1.
76             b_[0] = 1.0 / ( 1.0 - b_[1] + b_[2] );
77         b_[1] *= b_[0];
78         b_[2] *= b_[0];
79     }
80 
81     //! Return the last computed output value.
82     StkFloat lastOut() const { return lastFrame_[0]; };
83 
84     //! Input one sample to the filter and return one output.
85     StkFloat tick( StkFloat input ) {
86         inputs_[0] = gain_ * input;
87         lastFrame_[0] = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
88         inputs_[2] = inputs_[1];
89         inputs_[1] = inputs_[0];
90 
91         return lastFrame_[0];
92     }
93 
94     //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
95     /*!
96     The StkFrames argument reference is returned.  The \c channel
97     argument must be less than the number of channels in the
98     StkFrames argument (the first channel is specified by 0).
99     However, range checking is only performed if _STK_DEBUG_ is
100     defined during compilation, in which case an out-of-range value
101     will trigger an StkError exception.
102     */
103     ref StkFrames tick( ref StkFrames frames, uint channel = 0 ) {
104         StkFloat *samples = &frames[channel];
105         uint hop = frames.channels();
106         for ( uint i=0; i<frames.frames(); i++, samples += hop ) {
107             inputs_[0] = gain_ * *samples;
108             *samples = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
109             inputs_[2] = inputs_[1];
110             inputs_[1] = inputs_[0];
111         }
112 
113         lastFrame_[0] = *(samples-hop);
114         return frames;
115     }
116 
117     //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
118     /*!
119     The \c iFrames object reference is returned.  Each channel
120     argument must be less than the number of channels in the
121     corresponding StkFrames argument (the first channel is specified
122     by 0).  However, range checking is only performed if _STK_DEBUG_
123     is defined during compilation, in which case an out-of-range value
124     will trigger an StkError exception.
125     */
126     ref StkFrames tick( ref StkFrames iFrames, ref StkFrames oFrames, uint iChannel = 0, uint oChannel = 0 ) {
127         StkFloat *iSamples = &iFrames[iChannel];
128         StkFloat *oSamples = &oFrames[oChannel];
129         uint iHop = iFrames.channels(), oHop = oFrames.channels();
130         for ( uint i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
131             inputs_[0] = gain_ * *iSamples;
132             *oSamples = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
133             inputs_[2] = inputs_[1];
134             inputs_[1] = inputs_[0];
135         }
136 
137         lastFrame_[0] = *(oSamples-oHop);
138         return iFrames;
139     }
140 
141 protected:
142 
143     override void sampleRateChanged( StkFloat newRate, StkFloat oldRate ) {
144         if ( !ignoreSampleRateChange_ ) {
145             //oStream_ << "TwoZero::sampleRateChanged: you may need to recompute filter coefficients!";
146             handleError( StkErrorType.WARNING );
147         }
148     }
149 };
150