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.

147 lines
3.8KB

  1. #ifndef STK_ASYMP_H
  2. #define STK_ASYMP_H
  3. #include "Generator.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class Asymp
  7. \brief STK asymptotic curve envelope class
  8. This class implements a simple envelope generator
  9. which asymptotically approaches a target value.
  10. The algorithm used is of the form:
  11. y[n] = a y[n-1] + (1-a) target,
  12. where a = exp(-T/tau), T is the sample period, and
  13. tau is a time constant. The user can set the time
  14. constant (default value = 0.3) and target value.
  15. Theoretically, this recursion never reaches its
  16. target, though the calculations in this class are
  17. stopped when the current value gets within a small
  18. threshold value of the target (at which time the
  19. current value is set to the target). It responds
  20. to \e keyOn and \e keyOff messages by ramping to
  21. 1.0 on keyOn and to 0.0 on keyOff.
  22. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  23. */
  24. /***************************************************/
  25. const StkFloat TARGET_THRESHOLD = 0.000001;
  26. class Asymp : public Generator
  27. {
  28. public:
  29. //! Default constructor.
  30. Asymp( void );
  31. //! Class destructor.
  32. ~Asymp( void );
  33. //! Set target = 1.
  34. void keyOn( void );
  35. //! Set target = 0.
  36. void keyOff( void );
  37. //! Set the asymptotic rate via the time factor \e tau (must be > 0).
  38. /*!
  39. The rate is computed as described above. The value of \e tau
  40. must be greater than zero. Values of \e tau close to zero produce
  41. fast approach rates, while values greater than 1.0 produce rather
  42. slow rates.
  43. */
  44. void setTau( StkFloat tau );
  45. //! Set the asymptotic rate based on a time duration (must be > 0).
  46. void setTime( StkFloat time );
  47. //! Set the asymptotic rate such that the target value is perceptually reached (to within -60dB of the target) in \e t60 seconds.
  48. void setT60( StkFloat t60 );
  49. //! Set the target value.
  50. void setTarget( StkFloat target );
  51. //! Set current and target values to \e value.
  52. void setValue( StkFloat value );
  53. //! Return the current envelope \e state (0 = at target, 1 otherwise).
  54. int getState( void ) const { return state_; };
  55. //! Return the last computed output value.
  56. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  57. //! Compute and return one output sample.
  58. StkFloat tick( void );
  59. //! Fill a channel of the StkFrames object with computed outputs.
  60. /*!
  61. The \c channel argument must be less than the number of
  62. channels in the StkFrames argument (the first channel is specified
  63. by 0). However, range checking is only performed if _STK_DEBUG_
  64. is defined during compilation, in which case an out-of-range value
  65. will trigger an StkError exception.
  66. */
  67. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  68. protected:
  69. void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
  70. StkFloat value_;
  71. StkFloat target_;
  72. StkFloat factor_;
  73. StkFloat constant_;
  74. int state_;
  75. };
  76. inline StkFloat Asymp :: tick( void )
  77. {
  78. if ( state_ ) {
  79. value_ = factor_ * value_ + constant_;
  80. // Check threshold.
  81. if ( target_ > value_ ) {
  82. if ( target_ - value_ <= TARGET_THRESHOLD ) {
  83. value_ = target_;
  84. state_ = 0;
  85. }
  86. }
  87. else {
  88. if ( value_ - target_ <= TARGET_THRESHOLD ) {
  89. value_ = target_;
  90. state_ = 0;
  91. }
  92. }
  93. lastFrame_[0] = value_;
  94. }
  95. return value_;
  96. }
  97. inline StkFrames& Asymp :: tick( StkFrames& frames, unsigned int channel )
  98. {
  99. #if defined(_STK_DEBUG_)
  100. if ( channel >= frames.channels() ) {
  101. oStream_ << "Asymp::tick(): channel and StkFrames arguments are incompatible!";
  102. handleError( StkError::FUNCTION_ARGUMENT );
  103. }
  104. #endif
  105. StkFloat *samples = &frames[channel];
  106. unsigned int hop = frames.channels();
  107. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  108. *samples = Asymp::tick();
  109. return frames;
  110. }
  111. } // stk namespace
  112. #endif