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.

113 lines
2.6KB

  1. /***************************************************/
  2. /*! \class Asymp
  3. \brief STK asymptotic curve envelope class
  4. This class implements a simple envelope generator
  5. which asymptotically approaches a target value.
  6. The algorithm used is of the form:
  7. y[n] = a y[n-1] + (1-a) target,
  8. where a = exp(-T/tau), T is the sample period, and
  9. tau is a time constant. The user can set the time
  10. constant (default value = 0.3) and target value.
  11. Theoretically, this recursion never reaches its
  12. target, though the calculations in this class are
  13. stopped when the current value gets within a small
  14. threshold value of the target (at which time the
  15. current value is set to the target). It responds
  16. to \e keyOn and \e keyOff messages by ramping to
  17. 1.0 on keyOn and to 0.0 on keyOff.
  18. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  19. */
  20. /***************************************************/
  21. #include "Asymp.h"
  22. #include <cmath>
  23. namespace stk {
  24. Asymp :: Asymp( void )
  25. {
  26. value_ = 0.0;
  27. target_ = 0.0;
  28. state_ = 0;
  29. factor_ = exp( -1.0 / ( 0.3 * Stk::sampleRate() ) );
  30. constant_ = 0.0;
  31. Stk::addSampleRateAlert( this );
  32. }
  33. Asymp :: ~Asymp( void )
  34. {
  35. Stk::removeSampleRateAlert( this );
  36. }
  37. void Asymp :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
  38. {
  39. if ( !ignoreSampleRateChange_ ) {
  40. StkFloat tau = -1.0 / ( std::log( factor_ ) * oldRate );
  41. factor_ = std::exp( -1.0 / ( tau * newRate ) );
  42. }
  43. }
  44. void Asymp :: keyOn( void )
  45. {
  46. this->setTarget( 1.0 );
  47. }
  48. void Asymp :: keyOff( void )
  49. {
  50. this->setTarget( 0.0 );
  51. }
  52. void Asymp :: setTau( StkFloat tau )
  53. {
  54. if ( tau <= 0.0 ) {
  55. oStream_ << "Asymp::setTau: negative or zero tau not allowed!";
  56. handleError( StkError::WARNING ); return;
  57. }
  58. factor_ = std::exp( -1.0 / ( tau * Stk::sampleRate() ) );
  59. constant_ = ( 1.0 - factor_ ) * target_;
  60. }
  61. void Asymp :: setTime( StkFloat time )
  62. {
  63. if ( time <= 0.0 ) {
  64. oStream_ << "Asymp::setTime: negative or zero times not allowed!";
  65. handleError( StkError::WARNING ); return;
  66. }
  67. StkFloat tau = -time / std::log( TARGET_THRESHOLD );
  68. factor_ = std::exp( -1.0 / ( tau * Stk::sampleRate() ) );
  69. constant_ = ( 1.0 - factor_ ) * target_;
  70. }
  71. void Asymp :: setT60( StkFloat t60 )
  72. {
  73. if ( t60 <= 0.0 ) {
  74. oStream_ << "Asymp::setT60: negative or zero t60 not allowed!";
  75. handleError( StkError::WARNING ); return;
  76. }
  77. setTau( t60 / 6.91 );
  78. }
  79. void Asymp :: setTarget( StkFloat target )
  80. {
  81. target_ = target;
  82. if ( value_ != target_ ) state_ = 1;
  83. constant_ = ( 1.0 - factor_ ) * target_;
  84. }
  85. void Asymp :: setValue( StkFloat value )
  86. {
  87. state_ = 0;
  88. target_ = value;
  89. value_ = value;
  90. }
  91. } // stk namespace