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.

131 lines
3.2KB

  1. #ifndef STK_ENVELOPE_H
  2. #define STK_ENVELOPE_H
  3. #include "Generator.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class Envelope
  7. \brief STK linear line envelope class.
  8. This class implements a simple linear line envelope generator
  9. which is capable of ramping to an arbitrary target value by a
  10. specified \e rate. It also responds to simple \e keyOn and \e
  11. keyOff messages, ramping to 1.0 on keyOn and to 0.0 on keyOff.
  12. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  13. */
  14. /***************************************************/
  15. class Envelope : public Generator
  16. {
  17. public:
  18. //! Default constructor.
  19. Envelope( void );
  20. //! Class destructor.
  21. ~Envelope( void );
  22. //! Assignment operator.
  23. Envelope& operator= ( const Envelope& e );
  24. //! Set target = 1.
  25. void keyOn( void ) { this->setTarget( 1.0 ); };
  26. //! Set target = 0.
  27. void keyOff( void ) { this->setTarget( 0.0 ); };
  28. //! Set the \e rate.
  29. /*!
  30. The \e rate must be positive (though a value of 0.0 is allowed).
  31. */
  32. void setRate( StkFloat rate );
  33. //! Set the \e rate based on a positive time duration (seconds).
  34. /*!
  35. The \e rate is calculated such that the envelope will ramp from
  36. a value of 0.0 to 1.0 in the specified time duration.
  37. */
  38. void setTime( StkFloat time );
  39. //! Set the target value.
  40. void setTarget( StkFloat target );
  41. //! Set current and target values to \e value.
  42. void setValue( StkFloat value );
  43. //! Return the current envelope \e state (0 = at target, 1 otherwise).
  44. int getState( void ) const { return state_; };
  45. //! Return the last computed output value.
  46. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  47. //! Compute and return one output sample.
  48. StkFloat tick( void );
  49. //! Fill a channel of the StkFrames object with computed outputs.
  50. /*!
  51. The \c channel argument must be less than the number of
  52. channels in the StkFrames argument (the first channel is specified
  53. by 0). However, range checking is only performed if _STK_DEBUG_
  54. is defined during compilation, in which case an out-of-range value
  55. will trigger an StkError exception.
  56. */
  57. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  58. protected:
  59. void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
  60. StkFloat value_;
  61. StkFloat target_;
  62. StkFloat rate_;
  63. int state_;
  64. };
  65. inline StkFloat Envelope :: tick( void )
  66. {
  67. if ( state_ ) {
  68. if ( target_ > value_ ) {
  69. value_ += rate_;
  70. if ( value_ >= target_ ) {
  71. value_ = target_;
  72. state_ = 0;
  73. }
  74. }
  75. else {
  76. value_ -= rate_;
  77. if ( value_ <= target_ ) {
  78. value_ = target_;
  79. state_ = 0;
  80. }
  81. }
  82. lastFrame_[0] = value_;
  83. }
  84. return value_;
  85. }
  86. inline StkFrames& Envelope :: tick( StkFrames& frames, unsigned int channel )
  87. {
  88. #if defined(_STK_DEBUG_)
  89. if ( channel >= frames.channels() ) {
  90. oStream_ << "Envelope::tick(): channel and StkFrames arguments are incompatible!";
  91. handleError( StkError::FUNCTION_ARGUMENT );
  92. }
  93. #endif
  94. StkFloat *samples = &frames[channel];
  95. unsigned int hop = frames.channels();
  96. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  97. *samples = tick();
  98. return frames;
  99. }
  100. } // stk namespace
  101. #endif