Arduino STK  4.6.2
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 
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
STK linear line envelope class.
Definition: Envelope.h:22
void keyOn(void)
Set target = 1.
Definition: Envelope.h:35
Envelope & operator=(const Envelope &e)
Assignment operator.
void setTarget(StkFloat target)
Set the target value.
~Envelope(void)
Class destructor.
int getState(void) const
Return the current envelope state (0 = at target, 1 otherwise).
Definition: Envelope.h:60
Envelope(void)
Default constructor.
StkFloat tick(void)
Compute and return one output sample.
Definition: Envelope.h:88
void keyOff(void)
Set target = 0.
Definition: Envelope.h:38
void setValue(StkFloat value)
Set current and target values to value.
void setRate(StkFloat rate)
Set the rate.
void setTime(StkFloat time)
Set the rate based on a positive time duration (seconds).
StkFloat lastOut(void) const
Return the last computed output value.
Definition: Envelope.h:63
STK abstract unit generator parent class.
Definition: Generator.h:21
An STK class to handle vectorized audio data.
Definition: Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition: ADSR.h:8