The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

ADSR.h
1#ifndef STK_ADSR_H
2#define STK_ADSR_H
3
4#include "Generator.h"
5
6 namespace stk {
7
8/***************************************************/
22/***************************************************/
23
24 class ADSR : public Generator
25{
26 public:
27
29 enum {
30 ATTACK,
31 DECAY,
32 SUSTAIN,
33 RELEASE,
34 IDLE
35 };
36
38 ADSR( void );
39
41 ~ADSR( void );
42
44 void keyOn( void );
45
47 void keyOff( void );
48
50 void setAttackRate( StkFloat rate );
51
53 void setAttackTarget( StkFloat target );
54
56 void setDecayRate( StkFloat rate );
57
59 void setSustainLevel( StkFloat level );
60
62 void setReleaseRate( StkFloat rate );
63
65 void setAttackTime( StkFloat time );
66
68 void setDecayTime( StkFloat time );
69
71 void setReleaseTime( StkFloat time );
72
74 void setAllTimes( StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime );
75
77 void setTarget( StkFloat target );
78
80 int getState( void ) const { return state_; };
81
83 void setValue( StkFloat value );
84
86 StkFloat lastOut( void ) const { return lastFrame_[0]; };
87
89 StkFloat tick( void );
90
92
99 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
100
101 protected:
102
103 void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
104
105 int state_;
106 StkFloat value_;
107 StkFloat target_;
108 StkFloat attackRate_;
109 StkFloat decayRate_;
110 StkFloat releaseRate_;
111 StkFloat releaseTime_;
112 StkFloat sustainLevel_;
113};
114
115 inline StkFloat ADSR :: tick( void )
116{
117 switch ( state_ ) {
118
119 case ATTACK:
120 value_ += attackRate_;
121 if ( value_ >= target_ ) {
122 value_ = target_;
123 target_ = sustainLevel_;
124 state_ = DECAY;
125 }
126 lastFrame_[0] = value_;
127 break;
128
129 case DECAY:
130 if ( value_ > sustainLevel_ ) {
131 value_ -= decayRate_;
132 if ( value_ <= sustainLevel_ ) {
133 value_ = sustainLevel_;
134 state_ = SUSTAIN;
135 }
136 }
137 else {
138 value_ += decayRate_; // attack target < sustain level
139 if ( value_ >= sustainLevel_ ) {
140 value_ = sustainLevel_;
141 state_ = SUSTAIN;
142 }
143 }
144 lastFrame_[0] = value_;
145 break;
146
147 case RELEASE:
148 value_ -= releaseRate_;
149 if ( value_ <= 0.0 ) {
150 value_ = 0.0;
151 state_ = IDLE;
152 }
153 lastFrame_[0] = value_;
154
155 }
156
157 return value_;
158}
159
160 inline StkFrames& ADSR :: tick( StkFrames& frames, unsigned int channel )
161{
162#if defined(_STK_DEBUG_)
163 if ( channel >= frames.channels() ) {
164 oStream_ << "ADSR::tick(): channel and StkFrames arguments are incompatible!";
165 handleError( StkError::FUNCTION_ARGUMENT );
166 }
167#endif
168
169 StkFloat *samples = &frames[channel];
170 unsigned int hop = frames.channels();
171 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
172 *samples = ADSR::tick();
173
174 return frames;
175}
176
177} // stk namespace
178
179#endif
stk::ADSR
STK ADSR envelope class.
Definition ADSR.h:25
stk::ADSR::setDecayRate
void setDecayRate(StkFloat rate)
Set the decay rate (gain / sample).
stk::ADSR::setReleaseRate
void setReleaseRate(StkFloat rate)
Set the release rate (gain / sample).
stk::ADSR::setAllTimes
void setAllTimes(StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime)
Set sustain level and attack, decay, and release time durations (seconds).
stk::ADSR::setTarget
void setTarget(StkFloat target)
Set a sustain target value and attack or decay from current value to target.
stk::ADSR::setDecayTime
void setDecayTime(StkFloat time)
Set the decay rate based on a time duration (seconds).
stk::ADSR::setAttackTime
void setAttackTime(StkFloat time)
Set the attack rate based on a time duration (seconds).
stk::ADSR::lastOut
StkFloat lastOut(void) const
Return the last computed output value.
Definition ADSR.h:86
stk::ADSR::~ADSR
~ADSR(void)
Class destructor.
stk::ADSR::setSustainLevel
void setSustainLevel(StkFloat level)
Set the sustain level.
stk::ADSR::setReleaseTime
void setReleaseTime(StkFloat time)
Set the release rate based on a time duration (seconds).
stk::ADSR::keyOff
void keyOff(void)
Set target = 0, state = ADSR::RELEASE.
stk::ADSR::setValue
void setValue(StkFloat value)
Set to state = ADSR::SUSTAIN with current and target values of value.
stk::ADSR::keyOn
void keyOn(void)
Set target = 1, state = ADSR::ATTACK.
stk::ADSR::ADSR
ADSR(void)
Default constructor.
stk::ADSR::setAttackTarget
void setAttackTarget(StkFloat target)
Set the target value for the attack (default = 1.0).
stk::ADSR::SUSTAIN
@ SUSTAIN
Definition ADSR.h:32
stk::ADSR::IDLE
@ IDLE
Definition ADSR.h:34
stk::ADSR::DECAY
@ DECAY
Definition ADSR.h:31
stk::ADSR::RELEASE
@ RELEASE
Definition ADSR.h:33
stk::ADSR::ATTACK
@ ATTACK
Definition ADSR.h:30
stk::ADSR::tick
StkFloat tick(void)
Compute and return one output sample.
Definition ADSR.h:115
stk::ADSR::getState
int getState(void) const
Return the current envelope state (ATTACK, DECAY, SUSTAIN, RELEASE, IDLE).
Definition ADSR.h:80
stk::ADSR::setAttackRate
void setAttackRate(StkFloat rate)
Set the attack rate (gain / sample).
stk::Generator
STK abstract unit generator parent class.
Definition Generator.h:21
stk::StkFrames
An STK class to handle vectorized audio data.
Definition Stk.h:279
stk::StkFrames::channels
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition Stk.h:416
stk::StkFrames::frames
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition Stk.h:419
stk
The STK namespace.
Definition ADSR.h:6

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