1
/*******************************************/
2
/* ADSR Subclass of the Envelope Class, */
3
/* by Perry R. Cook, 1995-96 */
4
/* This is the traditional ADSR (Attack */
5
/* Decay, Sustain, Release) ADSR. */
6
/* It responds to simple KeyOn and KeyOff */
7
/* messages, keeping track of it's state. */
8
/* There are two tick (update value) */
9
/* methods, one returns the value, and */
10
/* other returns the state (0 = A, 1 = D, */
12
/*******************************************/
16
ADSR :: ADSR() : Envelope()
18
target = (MY_FLOAT) 0.0;
19
value = (MY_FLOAT) 0.0;
20
attackRate = (MY_FLOAT) 0.001;
21
decayRate = (MY_FLOAT) 0.001;
22
sustainLevel = (MY_FLOAT) 0.5;
23
releaseRate = (MY_FLOAT) 0.01;
29
/* Nothing to do here */
34
target = (MY_FLOAT) 1.0;
41
target = (MY_FLOAT) 0.0;
46
void ADSR :: setAttackRate(MY_FLOAT aRate)
49
printf("negative rates not allowed!!, correcting\n");
52
else attackRate = aRate;
55
void ADSR :: setDecayRate(MY_FLOAT aRate)
58
printf("negative rates not allowed!!, correcting\n");
61
else decayRate = aRate;
64
void ADSR :: setSustainLevel(MY_FLOAT aLevel)
67
printf("Sustain level out of range!!, correcting\n");
68
sustainLevel = (MY_FLOAT) 0.0;
70
else sustainLevel = aLevel;
73
void ADSR :: setReleaseRate(MY_FLOAT aRate)
76
printf("negative rates not allowed!!, correcting\n");
79
else releaseRate = aRate;
82
void ADSR :: setAttackTime(MY_FLOAT aTime)
85
printf("negative times not allowed!!, correcting\n");
86
attackRate = ONE_OVER_SRATE / -aTime;
88
else attackRate = ONE_OVER_SRATE / aTime;
91
void ADSR :: setDecayTime(MY_FLOAT aTime)
94
printf("negative times not allowed!!, correcting\n");
95
decayRate = ONE_OVER_SRATE / -aTime;
97
else decayRate = ONE_OVER_SRATE / aTime;
100
void ADSR :: setReleaseTime(MY_FLOAT aTime)
103
printf("negative times not allowed!!, correcting\n");
104
releaseRate = ONE_OVER_SRATE / -aTime;
106
else releaseRate = ONE_OVER_SRATE / aTime;
109
void ADSR :: setAllTimes(MY_FLOAT attTime, MY_FLOAT decTime, MY_FLOAT susLevel, MY_FLOAT relTime)
111
this->setAttackTime(attTime);
112
this->setDecayTime(decTime);
113
this->setSustainLevel(susLevel);
114
this->setReleaseTime(relTime);
117
void ADSR :: setTarget(MY_FLOAT aTarget)
120
if (value < target) {
122
this->setSustainLevel(target);
125
if (value > target) {
126
this->setSustainLevel(target);
132
void ADSR :: setValue(MY_FLOAT aValue)
137
this->setSustainLevel(aValue);
138
rate = (MY_FLOAT) 0.0;
141
MY_FLOAT ADSR :: tick()
145
if (value >= target) {
148
target = sustainLevel;
152
else if (state==DECAY) {
154
if (value <= sustainLevel) {
155
value = sustainLevel;
156
rate = (MY_FLOAT) 0.0;
160
else if (state==RELEASE) {
161
value -= releaseRate;
163
value = (MY_FLOAT) 0.0;
170
int ADSR :: informTick()
176
MY_FLOAT ADSR :: lastOut()
181
/************ Test Main ************************/
188
test.setAttackRate(0.15);
190
while(test.informTick()==ATTACK) printf("%lf\n",test.tick());
191
test.setDecayRate(0.1);
192
while (test.informTick()==DECAY) printf("%lf\n",test.lastOut());
193
test.setReleaseRate(0.05);
195
while(test.informTick()==RELEASE) printf("%lf\n",test.lastOut());