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.3KB

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