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.

150 lines
5.1KB

  1. #ifndef STK_TWOZERO_H
  2. #define STK_TWOZERO_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class TwoZero
  7. \brief STK two-zero filter class.
  8. This class implements a two-zero digital filter. A method is
  9. provided for creating a "notch" in the frequency response while
  10. maintaining a constant filter gain.
  11. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  12. */
  13. /***************************************************/
  14. class TwoZero : public Filter
  15. {
  16. public:
  17. //! Default constructor creates a second-order pass-through filter.
  18. TwoZero();
  19. //! Class destructor.
  20. ~TwoZero();
  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 the b[0] coefficient value.
  24. void setB0( StkFloat b0 ) { b_[0] = b0; };
  25. //! Set the b[1] coefficient value.
  26. void setB1( StkFloat b1 ) { b_[1] = b1; };
  27. //! Set the b[2] coefficient value.
  28. void setB2( StkFloat b2 ) { b_[2] = b2; };
  29. //! Set all filter coefficients.
  30. void setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, bool clearState = false );
  31. //! Sets the filter coefficients for a "notch" at \e frequency (in Hz).
  32. /*!
  33. This method determines the filter coefficients corresponding to
  34. two complex-conjugate zeros with the given \e frequency (in Hz)
  35. and \e radius from the z-plane origin. The coefficients are then
  36. normalized to produce a maximum filter gain of one (independent of
  37. the filter \e gain parameter). The resulting filter frequency
  38. response has a "notch" or anti-resonance at the given \e
  39. frequency. The closer the zeros are to the unit-circle (\e radius
  40. close to or equal to one), the narrower the resulting notch width.
  41. The \e frequency value should be between zero and half the sample
  42. rate. The \e radius value should be positive.
  43. */
  44. void setNotch( StkFloat frequency, StkFloat radius );
  45. //! Return the last computed output value.
  46. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  47. //! Input one sample to the filter and return one output.
  48. StkFloat tick( StkFloat input );
  49. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  50. /*!
  51. The StkFrames argument reference is returned. The \c channel
  52. argument must be less than the number of channels in the
  53. StkFrames argument (the first channel is specified by 0).
  54. However, range checking is only performed if _STK_DEBUG_ is
  55. defined during compilation, in which case an out-of-range value
  56. will trigger an StkError exception.
  57. */
  58. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  59. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  60. /*!
  61. The \c iFrames object reference is returned. Each channel
  62. argument must be less than the number of channels in the
  63. corresponding StkFrames argument (the first channel is specified
  64. by 0). However, range checking is only performed if _STK_DEBUG_
  65. is defined during compilation, in which case an out-of-range value
  66. will trigger an StkError exception.
  67. */
  68. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  69. protected:
  70. void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
  71. };
  72. inline StkFloat TwoZero :: tick( StkFloat input )
  73. {
  74. inputs_[0] = gain_ * input;
  75. lastFrame_[0] = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
  76. inputs_[2] = inputs_[1];
  77. inputs_[1] = inputs_[0];
  78. return lastFrame_[0];
  79. }
  80. inline StkFrames& TwoZero :: tick( StkFrames& frames, unsigned int channel )
  81. {
  82. #if defined(_STK_DEBUG_)
  83. if ( channel >= frames.channels() ) {
  84. oStream_ << "TwoZero::tick(): channel and StkFrames arguments are incompatible!";
  85. handleError( StkError::FUNCTION_ARGUMENT );
  86. }
  87. #endif
  88. StkFloat *samples = &frames[channel];
  89. unsigned int hop = frames.channels();
  90. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  91. inputs_[0] = gain_ * *samples;
  92. *samples = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
  93. inputs_[2] = inputs_[1];
  94. inputs_[1] = inputs_[0];
  95. }
  96. lastFrame_[0] = *(samples-hop);
  97. return frames;
  98. }
  99. inline StkFrames& TwoZero :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  100. {
  101. #if defined(_STK_DEBUG_)
  102. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  103. oStream_ << "TwoZero::tick(): channel and StkFrames arguments are incompatible!";
  104. handleError( StkError::FUNCTION_ARGUMENT );
  105. }
  106. #endif
  107. StkFloat *iSamples = &iFrames[iChannel];
  108. StkFloat *oSamples = &oFrames[oChannel];
  109. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  110. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  111. inputs_[0] = gain_ * *iSamples;
  112. *oSamples = b_[2] * inputs_[2] + b_[1] * inputs_[1] + b_[0] * inputs_[0];
  113. inputs_[2] = inputs_[1];
  114. inputs_[1] = inputs_[0];
  115. }
  116. lastFrame_[0] = *(oSamples-oHop);
  117. return iFrames;
  118. }
  119. } // stk namespace
  120. #endif