forked from thestk/stk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADSR.cpp
181 lines (148 loc) · 4.27 KB
/
ADSR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/***************************************************/
/*! \class ADSR
\brief STK ADSR envelope class.
This class implements a traditional ADSR (Attack, Decay, Sustain,
Release) envelope. It responds to simple keyOn and keyOff
messages, keeping track of its state. The \e state = ADSR::IDLE
before being triggered and after the envelope value reaches 0.0 in
the ADSR::RELEASE state. All rate, target and level settings must
be non-negative. All time settings must be positive.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "ADSR.h"
namespace stk {
ADSR :: ADSR( void )
{
target_ = 0.0;
value_ = 0.0;
attackRate_ = 0.001;
decayRate_ = 0.001;
releaseRate_ = 0.005;
releaseTime_ = -1.0;
sustainLevel_ = 0.5;
state_ = IDLE;
Stk::addSampleRateAlert( this );
}
ADSR :: ~ADSR( void )
{
Stk::removeSampleRateAlert( this );
}
void ADSR :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
{
if ( !ignoreSampleRateChange_ ) {
attackRate_ = oldRate * attackRate_ / newRate;
decayRate_ = oldRate * decayRate_ / newRate;
releaseRate_ = oldRate * releaseRate_ / newRate;
}
}
void ADSR :: keyOn()
{
if ( target_ <= 0.0 ) target_ = 1.0;
state_ = ATTACK;
}
void ADSR :: keyOff()
{
target_ = 0.0;
state_ = RELEASE;
// FIXED October 2010 - Nick Donaldson
// Need to make release rate relative to current value!!
// Only update if we have set a TIME rather than a RATE,
// in which case releaseTime_ will be -1
if ( releaseTime_ > 0.0 )
releaseRate_ = value_ / ( releaseTime_ * Stk::sampleRate() );
}
void ADSR :: setAttackRate( StkFloat rate )
{
if ( rate < 0.0 ) {
oStream_ << "ADSR::setAttackRate: argument must be >= 0.0!";
handleError( StkError::WARNING ); return;
}
attackRate_ = rate;
}
void ADSR :: setAttackTarget( StkFloat target )
{
if ( target < 0.0 ) {
oStream_ << "ADSR::setAttackTarget: negative target not allowed!";
handleError( StkError::WARNING ); return;
}
target_ = target;
}
void ADSR :: setDecayRate( StkFloat rate )
{
if ( rate < 0.0 ) {
oStream_ << "ADSR::setDecayRate: negative rates not allowed!";
handleError( StkError::WARNING ); return;
}
decayRate_ = rate;
}
void ADSR :: setSustainLevel( StkFloat level )
{
if ( level < 0.0 ) {
oStream_ << "ADSR::setSustainLevel: negative level not allowed!";
handleError( StkError::WARNING ); return;
}
sustainLevel_ = level;
}
void ADSR :: setReleaseRate( StkFloat rate )
{
if ( rate < 0.0 ) {
oStream_ << "ADSR::setReleaseRate: negative rates not allowed!";
handleError( StkError::WARNING ); return;
}
releaseRate_ = rate;
// Set to negative value so we don't update the release rate on keyOff()
releaseTime_ = -1.0;
}
void ADSR :: setAttackTime( StkFloat time )
{
if ( time <= 0.0 ) {
oStream_ << "ADSR::setAttackTime: negative or zero times not allowed!";
handleError( StkError::WARNING ); return;
}
attackRate_ = 1.0 / ( time * Stk::sampleRate() );
}
void ADSR :: setDecayTime( StkFloat time )
{
if ( time <= 0.0 ) {
oStream_ << "ADSR::setDecayTime: negative or zero times not allowed!";
handleError( StkError::WARNING ); return;
}
decayRate_ = (1.0 - sustainLevel_) / ( time * Stk::sampleRate() );
}
void ADSR :: setReleaseTime( StkFloat time )
{
if ( time <= 0.0 ) {
oStream_ << "ADSR::setReleaseTime: negative or zero times not allowed!";
handleError( StkError::WARNING ); return;
}
releaseRate_ = sustainLevel_ / ( time * Stk::sampleRate() );
releaseTime_ = time;
}
void ADSR :: setAllTimes( StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime )
{
this->setAttackTime( aTime );
this->setSustainLevel( sLevel );
this->setDecayTime( dTime );
this->setReleaseTime( rTime );
}
void ADSR :: setTarget( StkFloat target )
{
if ( target < 0.0 ) {
oStream_ << "ADSR::setTarget: negative target not allowed!";
handleError( StkError::WARNING ); return;
}
target_ = target;
this->setSustainLevel( target_ );
if ( value_ < target_ ) state_ = ATTACK;
if ( value_ > target_ ) state_ = DECAY;
}
void ADSR :: setValue( StkFloat value )
{
state_ = SUSTAIN;
target_ = value;
value_ = value;
this->setSustainLevel( value );
lastFrame_[0] = value;
}
} // stk namespace