Arduino STK  4.6.2
ADSR.h
1 
2 
3 #ifndef STK_ADSR_H
4 #define STK_ADSR_H
5 
6 #include "Generator.h"
7 
8 namespace stk {
9 
10 /***************************************************/
24 /***************************************************/
25 
26 class ADSR : public Generator
27 {
28  public:
29 
31  enum {
36  IDLE
37  };
38 
40  ADSR( void );
41 
43  ~ADSR( void );
44 
46  void keyOn( void );
47 
49  void keyOff( void );
50 
52  void setAttackRate( StkFloat rate );
53 
55  void setAttackTarget( StkFloat target );
56 
58  void setDecayRate( StkFloat rate );
59 
61  void setSustainLevel( StkFloat level );
62 
64  void setReleaseRate( StkFloat rate );
65 
67  void setAttackTime( StkFloat time );
68 
70  void setDecayTime( StkFloat time );
71 
73  void setReleaseTime( StkFloat time );
74 
76  void setAllTimes( StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime );
77 
79  void setTarget( StkFloat target );
80 
82  int getState( void ) const { return state_; };
83 
85  void setValue( StkFloat value );
86 
88  StkFloat lastOut( void ) const { return lastFrame_[0]; };
89 
91  StkFloat tick( void );
92 
94 
101  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
102 
103  protected:
104 
105  void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
106 
107  int state_;
108  StkFloat value_;
109  StkFloat target_;
110  StkFloat attackRate_;
111  StkFloat decayRate_;
112  StkFloat releaseRate_;
113  StkFloat releaseTime_;
114  StkFloat sustainLevel_;
115 };
116 
117 inline StkFloat ADSR :: tick( void )
118 {
119  switch ( state_ ) {
120 
121  case ATTACK:
122  value_ += attackRate_;
123  if ( value_ >= target_ ) {
124  value_ = target_;
125  target_ = sustainLevel_;
126  state_ = DECAY;
127  }
128  lastFrame_[0] = value_;
129  break;
130 
131  case DECAY:
132  if ( value_ > sustainLevel_ ) {
133  value_ -= decayRate_;
134  if ( value_ <= sustainLevel_ ) {
135  value_ = sustainLevel_;
136  state_ = SUSTAIN;
137  }
138  }
139  else {
140  value_ += decayRate_; // attack target < sustain level
141  if ( value_ >= sustainLevel_ ) {
142  value_ = sustainLevel_;
143  state_ = SUSTAIN;
144  }
145  }
146  lastFrame_[0] = value_;
147  break;
148 
149  case RELEASE:
150  value_ -= releaseRate_;
151  if ( value_ <= 0.0 ) {
152  value_ = 0.0;
153  state_ = IDLE;
154  }
155  lastFrame_[0] = value_;
156 
157  }
158 
159  return value_;
160 }
161 
162 inline StkFrames& ADSR :: tick( StkFrames& frames, unsigned int channel )
163 {
164 #if defined(_STK_DEBUG_)
165  if ( channel >= frames.channels() ) {
166  oStream_ << "ADSR::tick(): channel and StkFrames arguments are incompatible!";
167  handleError( StkError::FUNCTION_ARGUMENT );
168  }
169 #endif
170 
171  StkFloat *samples = &frames[channel];
172  unsigned int hop = frames.channels();
173  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
174  *samples = ADSR::tick();
175 
176  return frames;
177 }
178 
179 } // stk namespace
180 
181 #endif
STK ADSR envelope class.
Definition: ADSR.h:27
void setDecayRate(StkFloat rate)
Set the decay rate (gain / sample).
void setReleaseRate(StkFloat rate)
Set the release rate (gain / sample).
void setAllTimes(StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime)
Set sustain level and attack, decay, and release time durations (seconds).
void setTarget(StkFloat target)
Set a sustain target value and attack or decay from current value to target.
void setDecayTime(StkFloat time)
Set the decay rate based on a time duration (seconds).
void setAttackTime(StkFloat time)
Set the attack rate based on a time duration (seconds).
StkFloat lastOut(void) const
Return the last computed output value.
Definition: ADSR.h:88
~ADSR(void)
Class destructor.
void setSustainLevel(StkFloat level)
Set the sustain level.
void setReleaseTime(StkFloat time)
Set the release rate based on a time duration (seconds).
void keyOff(void)
Set target = 0, state = ADSR::RELEASE.
void setValue(StkFloat value)
Set to state = ADSR::SUSTAIN with current and target values of value.
void keyOn(void)
Set target = 1, state = ADSR::ATTACK.
ADSR(void)
Default constructor.
void setAttackTarget(StkFloat target)
Set the target value for the attack (default = 1.0).
@ SUSTAIN
Definition: ADSR.h:34
@ IDLE
Definition: ADSR.h:36
@ DECAY
Definition: ADSR.h:33
@ RELEASE
Definition: ADSR.h:35
@ ATTACK
Definition: ADSR.h:32
StkFloat tick(void)
Compute and return one output sample.
Definition: ADSR.h:117
int getState(void) const
Return the current envelope state (ATTACK, DECAY, SUSTAIN, RELEASE, IDLE).
Definition: ADSR.h:82
void setAttackRate(StkFloat rate)
Set the attack rate (gain / sample).
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