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.

191 lines
6.4KB

  1. #ifndef STK_FORMSWEP_H
  2. #define STK_FORMSWEP_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class FormSwep
  7. \brief STK sweepable formant filter class.
  8. This class implements a formant (resonance) which can be "swept"
  9. over time from one frequency setting to another. It provides
  10. methods for controlling the sweep rate and target frequency.
  11. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  12. */
  13. /***************************************************/
  14. class FormSwep : public Filter
  15. {
  16. public:
  17. //! Default constructor creates a second-order pass-through filter.
  18. FormSwep( void );
  19. //! Class destructor.
  20. ~FormSwep();
  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. //! Sets the filter coefficients for a resonance at \e frequency (in Hz).
  24. /*!
  25. This method determines the filter coefficients corresponding to
  26. two complex-conjugate poles with the given \e frequency (in Hz)
  27. and \e radius from the z-plane origin. The filter zeros are
  28. placed at z = 1, z = -1, and the coefficients are then normalized
  29. to produce a constant unity gain (independent of the filter \e
  30. gain parameter). The resulting filter frequency response has a
  31. resonance at the given \e frequency. The closer the poles are to
  32. the unit-circle (\e radius close to one), the narrower the
  33. resulting resonance width. An unstable filter will result for \e
  34. radius >= 1.0. The \e frequency value should be between zero and
  35. half the sample rate.
  36. */
  37. void setResonance( StkFloat frequency, StkFloat radius );
  38. //! Set both the current and target resonance parameters.
  39. void setStates( StkFloat frequency, StkFloat radius, StkFloat gain = 1.0 );
  40. //! Set target resonance parameters.
  41. void setTargets( StkFloat frequency, StkFloat radius, StkFloat gain = 1.0 );
  42. //! Set the sweep rate (between 0.0 - 1.0).
  43. /*!
  44. The formant parameters are varied in increments of the
  45. sweep rate between their current and target values.
  46. A sweep rate of 1.0 will produce an immediate change in
  47. resonance parameters from their current values to the
  48. target values. A sweep rate of 0.0 will produce no
  49. change in resonance parameters.
  50. */
  51. void setSweepRate( StkFloat rate );
  52. //! Set the sweep rate in terms of a time value in seconds.
  53. /*!
  54. This method adjusts the sweep rate based on a
  55. given time for the formant parameters to reach
  56. their target values.
  57. */
  58. void setSweepTime( StkFloat time );
  59. //! Return the last computed output value.
  60. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  61. //! Input one sample to the filter and return a reference to one output.
  62. StkFloat tick( StkFloat input );
  63. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  64. /*!
  65. The StkFrames argument reference is returned. The \c channel
  66. argument must be less than the number of channels in the
  67. StkFrames argument (the first channel is specified by 0).
  68. However, range checking is only performed if _STK_DEBUG_ is
  69. defined during compilation, in which case an out-of-range value
  70. will trigger an StkError exception.
  71. */
  72. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  73. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  74. /*!
  75. The \c iFrames object reference is returned. Each channel
  76. argument must be less than the number of channels in the
  77. corresponding StkFrames argument (the first channel is specified
  78. by 0). However, range checking is only performed if _STK_DEBUG_
  79. is defined during compilation, in which case an out-of-range value
  80. will trigger an StkError exception.
  81. */
  82. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  83. protected:
  84. virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
  85. bool dirty_;
  86. StkFloat frequency_;
  87. StkFloat radius_;
  88. StkFloat startFrequency_;
  89. StkFloat startRadius_;
  90. StkFloat startGain_;
  91. StkFloat targetFrequency_;
  92. StkFloat targetRadius_;
  93. StkFloat targetGain_;
  94. StkFloat deltaFrequency_;
  95. StkFloat deltaRadius_;
  96. StkFloat deltaGain_;
  97. StkFloat sweepState_;
  98. StkFloat sweepRate_;
  99. };
  100. inline StkFloat FormSwep :: tick( StkFloat input )
  101. {
  102. if ( dirty_ ) {
  103. sweepState_ += sweepRate_;
  104. if ( sweepState_ >= 1.0 ) {
  105. sweepState_ = 1.0;
  106. dirty_ = false;
  107. radius_ = targetRadius_;
  108. frequency_ = targetFrequency_;
  109. gain_ = targetGain_;
  110. }
  111. else {
  112. radius_ = startRadius_ + (deltaRadius_ * sweepState_);
  113. frequency_ = startFrequency_ + (deltaFrequency_ * sweepState_);
  114. gain_ = startGain_ + (deltaGain_ * sweepState_);
  115. }
  116. this->setResonance( frequency_, radius_ );
  117. }
  118. inputs_[0] = gain_ * input;
  119. lastFrame_[0] = b_[0] * inputs_[0] + b_[1] * inputs_[1] + b_[2] * inputs_[2];
  120. lastFrame_[0] -= a_[2] * outputs_[2] + a_[1] * outputs_[1];
  121. inputs_[2] = inputs_[1];
  122. inputs_[1] = inputs_[0];
  123. outputs_[2] = outputs_[1];
  124. outputs_[1] = lastFrame_[0];
  125. return lastFrame_[0];
  126. }
  127. inline StkFrames& FormSwep :: tick( StkFrames& frames, unsigned int channel )
  128. {
  129. #if defined(_STK_DEBUG_)
  130. if ( channel >= frames.channels() ) {
  131. oStream_ << "FormSwep::tick(): channel and StkFrames arguments are incompatible!";
  132. handleError( StkError::FUNCTION_ARGUMENT );
  133. }
  134. #endif
  135. StkFloat *samples = &frames[channel];
  136. unsigned int hop = frames.channels();
  137. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  138. *samples = tick( *samples );
  139. return frames;
  140. }
  141. inline StkFrames& FormSwep :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  142. {
  143. #if defined(_STK_DEBUG_)
  144. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  145. oStream_ << "FormSwep::tick(): channel and StkFrames arguments are incompatible!";
  146. handleError( StkError::FUNCTION_ARGUMENT );
  147. }
  148. #endif
  149. StkFloat *iSamples = &iFrames[iChannel];
  150. StkFloat *oSamples = &oFrames[oChannel];
  151. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  152. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop )
  153. *oSamples = tick( *iSamples );
  154. return iFrames;
  155. }
  156. } // stk namespace
  157. #endif