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 /***************************************************/
19 /***************************************************/
20 
21 class Envelope : public Generator
22 {
23  public:
24 
26  Envelope( void );
27 
29  ~Envelope( void );
30 
32  Envelope& operator= ( const Envelope& e );
33 
35  void keyOn( void ) { this->setTarget( 1.0 ); };
36 
38  void keyOff( void ) { this->setTarget( 0.0 ); };
39 
41 
44  void setRate( StkFloat rate );
45 
47 
51  void setTime( StkFloat time );
52 
54  void setTarget( StkFloat target );
55 
57  void setValue( StkFloat value );
58 
60  int getState( void ) const { return state_; };
61 
63  StkFloat lastOut( void ) const { return lastFrame_[0]; };
64 
66  StkFloat tick( void );
67 
69 
76  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
77 
78  protected:
79 
80  void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
81 
82  StkFloat value_;
83  StkFloat target_;
84  StkFloat rate_;
85  int state_;
86 };
87 
88 inline StkFloat Envelope :: tick( void )
89 {
90  if ( state_ ) {
91  if ( target_ > value_ ) {
92  value_ += rate_;
93  if ( value_ >= target_ ) {
94  value_ = target_;
95  state_ = 0;
96  }
97  }
98  else {
99  value_ -= rate_;
100  if ( value_ <= target_ ) {
101  value_ = target_;
102  state_ = 0;
103  }
104  }
105  lastFrame_[0] = value_;
106  }
107 
108  return value_;
109 }
110 
111 inline StkFrames& Envelope :: tick( StkFrames& frames, unsigned int channel )
112 {
113 #if defined(_STK_DEBUG_)
114  if ( channel >= frames.channels() ) {
115  oStream_ << "Envelope::tick(): channel and StkFrames arguments are incompatible!";
116  handleError( StkError::FUNCTION_ARGUMENT );
117  }
118 #endif
119 
120  StkFloat *samples = &frames[channel];
121  unsigned int hop = frames.channels();
122  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
123  *samples = tick();
124 
125  return frames;
126 }
127 
128 } // stk namespace
129 
130 #endif

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