Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Envelope.h
1 #ifndef STK_ENVELOPE_H
2 #define STK_ENVELOPE_H
3 
4 #include "Generator.h"
5 
6 namespace stk {
7 
8 /***************************************************/
20 /***************************************************/
21 
22 class Envelope : public Generator
23 {
24  public:
25 
27  Envelope( void );
28 
30  ~Envelope( void );
31 
33  Envelope& operator= ( const Envelope& e );
34 
36  void keyOn( StkFloat target = 1.0 ) { this->setTarget( target ); };
37 
39  void keyOff( StkFloat target = 0.0 ) { this->setTarget( target ); };
40 
42 
45  void setRate( StkFloat rate );
46 
48 
52  void setTime( StkFloat time );
53 
55  void setTarget( StkFloat target );
56 
58  void setValue( StkFloat value );
59 
61  int getState( void ) const { return state_; };
62 
64  StkFloat lastOut( void ) const { return lastFrame_[0]; };
65 
67  StkFloat tick( void );
68 
70 
77  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
78 
79  protected:
80 
81  void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
82 
83  StkFloat value_;
84  StkFloat target_;
85  StkFloat rate_;
86  int state_;
87 };
88 
89 inline StkFloat Envelope :: tick( void )
90 {
91  if ( state_ ) {
92  if ( target_ > value_ ) {
93  value_ += rate_;
94  if ( value_ >= target_ ) {
95  value_ = target_;
96  state_ = 0;
97  }
98  }
99  else {
100  value_ -= rate_;
101  if ( value_ <= target_ ) {
102  value_ = target_;
103  state_ = 0;
104  }
105  }
106  lastFrame_[0] = value_;
107  }
108 
109  return value_;
110 }
111 
112 inline StkFrames& Envelope :: tick( StkFrames& frames, unsigned int channel )
113 {
114 #if defined(_STK_DEBUG_)
115  if ( channel >= frames.channels() ) {
116  oStream_ << "Envelope::tick(): channel and StkFrames arguments are incompatible!";
117  handleError( StkError::FUNCTION_ARGUMENT );
118  }
119 #endif
120 
121  StkFloat *samples = &frames[channel];
122  unsigned int hop = frames.channels();
123  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
124  *samples = tick();
125 
126  return frames;
127 }
128 
129 } // stk namespace
130 
131 #endif

The Synthesis ToolKit in C++ (STK)
©1995--2021 Perry R. Cook and Gary P. Scavone. All Rights Reserved.