| 1 |
/***************************************************/ |
| 2 |
/*! \class ADSR |
| 3 |
\brief STK ADSR envelope class. |
| 4 |
|
| 5 |
This Envelope subclass implements a |
| 6 |
traditional ADSR (Attack, Decay, |
| 7 |
Sustain, Release) envelope. It |
| 8 |
responds to simple keyOn and keyOff |
| 9 |
messages, keeping track of its state. |
| 10 |
The \e state = ADSR::DONE after the |
| 11 |
envelope value reaches 0.0 in the |
| 12 |
ADSR::RELEASE state. |
| 13 |
|
| 14 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 15 |
*/ |
| 16 |
/***************************************************/ |
| 17 |
|
| 18 |
#include "ADSR.h" |
| 19 |
|
| 20 |
using namespace Nyq; |
| 21 |
|
| 22 |
ADSR :: ADSR() : Envelope() |
| 23 |
{ |
| 24 |
target_ = 0.0; |
| 25 |
value_ = 0.0; |
| 26 |
attackRate_ = 0.001; |
| 27 |
decayRate_ = 0.001; |
| 28 |
sustainLevel_ = 0.5; |
| 29 |
releaseRate_ = 0.01; |
| 30 |
state_ = ATTACK; |
| 31 |
} |
| 32 |
|
| 33 |
ADSR :: ~ADSR() |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
void ADSR :: keyOn() |
| 38 |
{ |
| 39 |
target_ = 1.0; |
| 40 |
rate_ = attackRate_; |
| 41 |
state_ = ATTACK; |
| 42 |
} |
| 43 |
|
| 44 |
void ADSR :: keyOff() |
| 45 |
{ |
| 46 |
target_ = 0.0; |
| 47 |
rate_ = releaseRate_; |
| 48 |
state_ = RELEASE; |
| 49 |
} |
| 50 |
|
| 51 |
void ADSR :: setAttackRate(StkFloat rate) |
| 52 |
{ |
| 53 |
if (rate < 0.0) { |
| 54 |
errorString_ << "ADSR::setAttackRate: negative rates not allowed ... correcting!"; |
| 55 |
handleError( StkError::WARNING ); |
| 56 |
attackRate_ = -rate; |
| 57 |
} |
| 58 |
else attackRate_ = rate; |
| 59 |
} |
| 60 |
|
| 61 |
void ADSR :: setDecayRate(StkFloat rate) |
| 62 |
{ |
| 63 |
if (rate < 0.0) { |
| 64 |
errorString_ << "ADSR::setDecayRate: negative rates not allowed ... correcting!"; |
| 65 |
handleError( StkError::WARNING ); |
| 66 |
decayRate_ = -rate; |
| 67 |
} |
| 68 |
else decayRate_ = rate; |
| 69 |
} |
| 70 |
|
| 71 |
void ADSR :: setSustainLevel(StkFloat level) |
| 72 |
{ |
| 73 |
if (level < 0.0 ) { |
| 74 |
errorString_ << "ADSR::setSustainLevel: level out of range ... correcting!"; |
| 75 |
handleError( StkError::WARNING ); |
| 76 |
sustainLevel_ = 0.0; |
| 77 |
} |
| 78 |
else sustainLevel_ = level; |
| 79 |
} |
| 80 |
|
| 81 |
void ADSR :: setReleaseRate(StkFloat rate) |
| 82 |
{ |
| 83 |
if (rate < 0.0) { |
| 84 |
errorString_ << "ADSR::setReleaseRate: negative rates not allowed ... correcting!"; |
| 85 |
handleError( StkError::WARNING ); |
| 86 |
releaseRate_ = -rate; |
| 87 |
} |
| 88 |
else releaseRate_ = rate; |
| 89 |
} |
| 90 |
|
| 91 |
void ADSR :: setAttackTime(StkFloat time) |
| 92 |
{ |
| 93 |
if (time < 0.0) { |
| 94 |
errorString_ << "ADSR::setAttackTime: negative times not allowed ... correcting!"; |
| 95 |
handleError( StkError::WARNING ); |
| 96 |
attackRate_ = 1.0 / ( -time * Stk::sampleRate() ); |
| 97 |
} |
| 98 |
else attackRate_ = 1.0 / ( time * Stk::sampleRate() ); |
| 99 |
} |
| 100 |
|
| 101 |
void ADSR :: setDecayTime(StkFloat time) |
| 102 |
{ |
| 103 |
if (time < 0.0) { |
| 104 |
errorString_ << "ADSR::setDecayTime: negative times not allowed ... correcting!"; |
| 105 |
handleError( StkError::WARNING ); |
| 106 |
decayRate_ = 1.0 / ( -time * Stk::sampleRate() ); |
| 107 |
} |
| 108 |
else decayRate_ = 1.0 / ( time * Stk::sampleRate() ); |
| 109 |
} |
| 110 |
|
| 111 |
void ADSR :: setReleaseTime(StkFloat time) |
| 112 |
{ |
| 113 |
if (time < 0.0) { |
| 114 |
errorString_ << "ADSR::setReleaseTime: negative times not allowed ... correcting!"; |
| 115 |
handleError( StkError::WARNING ); |
| 116 |
releaseRate_ = sustainLevel_ / ( -time * Stk::sampleRate() ); |
| 117 |
} |
| 118 |
else releaseRate_ = sustainLevel_ / ( time * Stk::sampleRate() ); |
| 119 |
} |
| 120 |
|
| 121 |
void ADSR :: setAllTimes(StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime) |
| 122 |
{ |
| 123 |
this->setAttackTime(aTime); |
| 124 |
this->setDecayTime(dTime); |
| 125 |
this->setSustainLevel(sLevel); |
| 126 |
this->setReleaseTime(rTime); |
| 127 |
} |
| 128 |
|
| 129 |
void ADSR :: setTarget(StkFloat target) |
| 130 |
{ |
| 131 |
target_ = target; |
| 132 |
if (value_ < target_) { |
| 133 |
state_ = ATTACK; |
| 134 |
this->setSustainLevel(target_); |
| 135 |
rate_ = attackRate_; |
| 136 |
} |
| 137 |
if (value_ > target_) { |
| 138 |
this->setSustainLevel(target_); |
| 139 |
state_ = DECAY; |
| 140 |
rate_ = decayRate_; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void ADSR :: setValue(StkFloat value) |
| 145 |
{ |
| 146 |
state_ = SUSTAIN; |
| 147 |
target_ = value; |
| 148 |
value_ = value; |
| 149 |
this->setSustainLevel(value); |
| 150 |
rate_ = (StkFloat) 0.0; |
| 151 |
} |
| 152 |
|
| 153 |
int ADSR :: getState(void) const |
| 154 |
{ |
| 155 |
return state_; |
| 156 |
} |
| 157 |
|
| 158 |
StkFloat ADSR :: computeSample() |
| 159 |
{ |
| 160 |
switch (state_) { |
| 161 |
|
| 162 |
case ATTACK: |
| 163 |
value_ += rate_; |
| 164 |
if (value_ >= target_) { |
| 165 |
value_ = target_; |
| 166 |
rate_ = decayRate_; |
| 167 |
target_ = sustainLevel_; |
| 168 |
state_ = DECAY; |
| 169 |
} |
| 170 |
break; |
| 171 |
|
| 172 |
case DECAY: |
| 173 |
value_ -= decayRate_; |
| 174 |
if (value_ <= sustainLevel_) { |
| 175 |
value_ = sustainLevel_; |
| 176 |
rate_ = (StkFloat) 0.0; |
| 177 |
state_ = SUSTAIN; |
| 178 |
} |
| 179 |
break; |
| 180 |
|
| 181 |
case RELEASE: |
| 182 |
value_ -= releaseRate_; |
| 183 |
if (value_ <= 0.0) { |
| 184 |
value_ = (StkFloat) 0.0; |
| 185 |
state_ = DONE; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
lastOutput_ = value_; |
| 190 |
return value_; |
| 191 |
} |