Audio plugin host https://kx.studio/carla
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.

135 lines
4.4KB

  1. #ifndef STK_ONEPOLE_H
  2. #define STK_ONEPOLE_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class OnePole
  7. \brief STK one-pole filter class.
  8. This class implements a one-pole digital filter. A method is
  9. provided for setting the pole position along the real axis of the
  10. z-plane while maintaining a constant peak filter gain.
  11. by Perry R. Cook and Gary P. Scavone, 1995-2011.
  12. */
  13. /***************************************************/
  14. class OnePole : public Filter
  15. {
  16. public:
  17. //! The default constructor creates a low-pass filter (pole at z = 0.9).
  18. OnePole( StkFloat thePole = 0.9 );
  19. //! Class destructor.
  20. ~OnePole();
  21. //! Set the b[0] coefficient value.
  22. void setB0( StkFloat b0 ) { b_[0] = b0; };
  23. //! Set the a[1] coefficient value.
  24. void setA1( StkFloat a1 ) { a_[1] = a1; };
  25. //! Set all filter coefficients.
  26. void setCoefficients( StkFloat b0, StkFloat a1, bool clearState = false );
  27. //! Set the pole position in the z-plane.
  28. /*!
  29. This method sets the pole position along the real-axis of the
  30. z-plane and normalizes the coefficients for a maximum gain of one.
  31. A positive pole value produces a low-pass filter, while a negative
  32. pole value produces a high-pass filter. This method does not
  33. affect the filter \e gain value. The argument magnitude should be
  34. less than one to maintain filter stability.
  35. */
  36. void setPole( StkFloat thePole );
  37. //! Return the last computed output value.
  38. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  39. //! Input one sample to the filter and return one output.
  40. StkFloat tick( StkFloat input );
  41. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  42. /*!
  43. The StkFrames argument reference is returned. The \c channel
  44. argument must be less than the number of channels in the
  45. StkFrames argument (the first channel is specified by 0).
  46. However, range checking is only performed if _STK_DEBUG_ is
  47. defined during compilation, in which case an out-of-range value
  48. will trigger an StkError exception.
  49. */
  50. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  51. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  52. /*!
  53. The \c iFrames object reference is returned. Each channel
  54. argument must be less than the number of channels in the
  55. corresponding StkFrames argument (the first channel is specified
  56. by 0). However, range checking is only performed if _STK_DEBUG_
  57. is defined during compilation, in which case an out-of-range value
  58. will trigger an StkError exception.
  59. */
  60. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  61. };
  62. inline StkFloat OnePole :: tick( StkFloat input )
  63. {
  64. inputs_[0] = gain_ * input;
  65. lastFrame_[0] = b_[0] * inputs_[0] - a_[1] * outputs_[1];
  66. outputs_[1] = lastFrame_[0];
  67. return lastFrame_[0];
  68. }
  69. inline StkFrames& OnePole :: tick( StkFrames& frames, unsigned int channel )
  70. {
  71. #if defined(_STK_DEBUG_)
  72. if ( channel >= frames.channels() ) {
  73. oStream_ << "OnePole::tick(): channel and StkFrames arguments are incompatible!";
  74. handleError( StkError::FUNCTION_ARGUMENT );
  75. }
  76. #endif
  77. StkFloat *samples = &frames[channel];
  78. unsigned int hop = frames.channels();
  79. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  80. inputs_[0] = gain_ * *samples;
  81. *samples = b_[0] * inputs_[0] - a_[1] * outputs_[1];
  82. outputs_[1] = *samples;
  83. }
  84. lastFrame_[0] = outputs_[1];
  85. return frames;
  86. }
  87. inline StkFrames& OnePole :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  88. {
  89. #if defined(_STK_DEBUG_)
  90. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  91. oStream_ << "OnePole::tick(): channel and StkFrames arguments are incompatible!";
  92. handleError( StkError::FUNCTION_ARGUMENT );
  93. }
  94. #endif
  95. StkFloat *iSamples = &iFrames[iChannel];
  96. StkFloat *oSamples = &oFrames[oChannel];
  97. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  98. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  99. inputs_[0] = gain_ * *iSamples;
  100. *oSamples = b_[0] * inputs_[0] - a_[1] * outputs_[1];
  101. outputs_[1] = *oSamples;
  102. }
  103. lastFrame_[0] = outputs_[1];
  104. return iFrames;
  105. }
  106. } // stk namespace
  107. #endif