You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.6KB

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