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.

154 lines
5.4KB

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