40 template <
unsigned int CONTROL_UPDATE_RATE,
unsigned int LERP_RATE,
typename T =
unsigned int>
45 const unsigned int LERPS_PER_CONTROL;
47 T update_step_counter;
50 enum {ATTACK,DECAY,SUSTAIN,RELEASE,IDLE};
58 }attack,decay,sustain,release,idle;
60 phase * current_phase;
67 T convertMsecToControlUpdateSteps(
unsigned int msec){
68 return (T) (((uint32_t)msec*CONTROL_UPDATE_RATE)>>10);
73 void setPhase(phase * next_phase) {
74 update_step_counter = 0;
75 num_update_steps = next_phase->update_steps;
76 transition.set(Q8n0_to_Q15n16(next_phase->level),next_phase->lerp_steps);
77 current_phase = next_phase;
82 void checkForAndSetNextPhase(phase * next_phase) {
83 if (++update_step_counter >= num_update_steps){
91 void setTime(phase * p,
unsigned int msec)
93 p->update_steps = convertMsecToControlUpdateSteps(msec);
94 p->lerp_steps = (
long) p->update_steps * LERPS_PER_CONTROL;
99 void setUpdateSteps(phase * p,
unsigned int steps)
101 p->update_steps = steps;
102 p->lerp_steps = (
long) steps * LERPS_PER_CONTROL;
111 ADSR():LERPS_PER_CONTROL(LERP_RATE/CONTROL_UPDATE_RATE)
113 attack.phase_type = ATTACK;
114 decay.phase_type = DECAY;
115 sustain.phase_type = SUSTAIN;
116 release.phase_type = RELEASE;
117 idle.phase_type = IDLE;
119 adsr_playing =
false;
120 current_phase = &idle;
130 switch(current_phase->phase_type) {
133 checkForAndSetNextPhase(&decay);
137 checkForAndSetNextPhase(&sustain);
141 checkForAndSetNextPhase(&release);
145 checkForAndSetNextPhase(&idle);
149 adsr_playing =
false;
163 unsigned char out = 0;
164 if (adsr_playing) out = Q15n16_to_Q8n0(transition.next());
177 if (reset) transition.set(0);
237 void setIdleLevel(byte value)
251 setAttackLevel(attack);
252 setDecayLevel(decay);
253 setSustainLevel(decay);
266 void setLevels(byte attack, byte decay, byte sustain, byte release)
268 setAttackLevel(attack);
269 setDecayLevel(decay);
270 setSustainLevel(sustain);
271 setReleaseLevel(release);
285 setTime(&attack, msec);
298 setTime(&decay, msec);
312 setTime(&sustain, msec);
326 setTime(&release, msec);
331 void setIdleTime(
unsigned int msec)
333 setTime(&idle, msec);
347 void setTimes(
unsigned int attack_ms,
unsigned int decay_ms,
unsigned int sustain_ms,
unsigned int release_ms)
364 setUpdateSteps(&attack, steps);
374 setUpdateSteps(&decay, steps);
384 setUpdateSteps(&sustain, steps);
394 setUpdateSteps(&release, steps);
399 void setIdleUpdateSteps(
unsigned int steps)
401 setUpdateSteps(&idle, steps);
411 void setAllUpdateSteps(
unsigned int attack_steps,
unsigned int decay_steps,
unsigned int sustain_steps,
unsigned int release_steps)
417 setIdleUpdateSteps(65535);