44 template <
unsigned int CONTROL_UPDATE_RATE,
unsigned int LERP_RATE,
typename T =
unsigned int>
49 const unsigned int LERPS_PER_CONTROL;
51 T update_step_counter;
54 enum {ATTACK,DECAY,SUSTAIN,RELEASE,IDLE};
62 }attack,decay,sustain,release,idle;
64 phase * current_phase;
71 T convertMsecToControlUpdateSteps(
unsigned int msec){
72 return (T) (((uint32_t)msec*CONTROL_UPDATE_RATE)>>10);
77 void setPhase(phase * next_phase) {
78 update_step_counter = 0;
79 num_update_steps = next_phase->update_steps;
80 transition.set(Q8n0_to_Q15n16(next_phase->level),next_phase->lerp_steps);
81 current_phase = next_phase;
86 void checkForAndSetNextPhase(phase * next_phase) {
87 if (++update_step_counter >= num_update_steps){
95 void setTime(phase * p,
unsigned int msec)
97 p->update_steps = convertMsecToControlUpdateSteps(msec);
98 p->lerp_steps = (
long) p->update_steps * LERPS_PER_CONTROL;
103 void setUpdateSteps(phase * p,
unsigned int steps)
105 p->update_steps = steps;
106 p->lerp_steps = (
long) steps * LERPS_PER_CONTROL;
115 ADSR():LERPS_PER_CONTROL(LERP_RATE/CONTROL_UPDATE_RATE)
117 attack.phase_type = ATTACK;
118 decay.phase_type = DECAY;
119 sustain.phase_type = SUSTAIN;
120 release.phase_type = RELEASE;
121 idle.phase_type = IDLE;
123 adsr_playing =
false;
124 current_phase = &idle;
134 switch(current_phase->phase_type) {
137 checkForAndSetNextPhase(&decay);
141 checkForAndSetNextPhase(&sustain);
145 checkForAndSetNextPhase(&release);
149 checkForAndSetNextPhase(&idle);
153 adsr_playing =
false;
167 unsigned char out = 0;
168 if (adsr_playing) out = Q15n16_to_Q8n0(transition.next());
181 if (reset) transition.set(0);
241 void setIdleLevel(byte value)
255 setAttackLevel(attack);
256 setDecayLevel(decay);
257 setSustainLevel(decay);
270 void setLevels(byte attack, byte decay, byte sustain, byte release)
272 setAttackLevel(attack);
273 setDecayLevel(decay);
274 setSustainLevel(sustain);
275 setReleaseLevel(release);
289 setTime(&attack, msec);
302 setTime(&decay, msec);
316 setTime(&sustain, msec);
330 setTime(&release, msec);
335 void setIdleTime(
unsigned int msec)
337 setTime(&idle, msec);
351 void setTimes(
unsigned int attack_ms,
unsigned int decay_ms,
unsigned int sustain_ms,
unsigned int release_ms)
368 setUpdateSteps(&attack, steps);
378 setUpdateSteps(&decay, steps);
388 setUpdateSteps(&sustain, steps);
398 setUpdateSteps(&release, steps);
403 void setIdleUpdateSteps(
unsigned int steps)
405 setUpdateSteps(&idle, steps);
415 void setAllUpdateSteps(
unsigned int attack_steps,
unsigned int decay_steps,
unsigned int sustain_steps,
unsigned int release_steps)
421 setIdleUpdateSteps(65535);
void setDecayLevel(byte value)
Set the decay level of the ADSR.
void noteOn(bool reset=false)
Start the attack phase of the ADSR.
void setAttackUpdateSteps(unsigned int steps)
Set the attack time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolat...
void setSustainLevel(byte value)
Set the sustain level of the ADSR.
int32_t Q15n16
signed fractional number using 15 integer bits and 16 fractional bits, represents -32767...
void setLevels(byte attack, byte decay, byte sustain, byte release)
Set the attack, decay, sustain and release levels.
unsigned char next()
Advances one audio step along the ADSR and returns the level.
void setSustainUpdateSteps(unsigned int steps)
Set the sustain time of the ADSR, expressed as the number of update steps (not ADSR::next() interpola...
void setReleaseUpdateSteps(unsigned int steps)
Set the release time of the ADSR, expressed as the number of update steps (not ADSR::next() interpola...
void setReleaseTime(unsigned int msec)
Set the release time of the ADSR in milliseconds.
void setDecayUpdateSteps(unsigned int steps)
Set the decay time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolati...
void setAllUpdateSteps(unsigned int attack_steps, unsigned int decay_steps, unsigned int sustain_steps, unsigned int release_steps)
Set the attack, decay and release times of the ADSR, expressed in update steps (not ADSR::next() inte...
void noteOff()
Start the release phase of the ADSR.
For linear changes with a minimum of calculation at each step.
void setAttackLevel(byte value)
Set the attack level of the ADSR.
void setADLevels(byte attack, byte decay)
Set the attack and decay levels of the ADSR.
bool playing()
Tells if the envelope is currently playing.
void setAttackTime(unsigned int msec)
Set the attack time of the ADSR in milliseconds.
void setTimes(unsigned int attack_ms, unsigned int decay_ms, unsigned int sustain_ms, unsigned int release_ms)
Set the attack, decay and release times of the ADSR in milliseconds.
void setReleaseLevel(byte value)
Set the release level of the ADSR.
void setDecayTime(unsigned int msec)
Set the decay time of the ADSR in milliseconds.
uint8_t Q8n0
normal uint8_t with 0 fractional bits, represents 0.0 to 255.0
void setSustainTime(unsigned int msec)
Set the sustain time of the ADSR in milliseconds.
A simple ADSR envelope generator.
void update()
Updates the internal controls of the ADSR.