22 #include "mozzi_fixmath.h" 45 template <
unsigned int CONTROL_UPDATE_RATE,
unsigned int LERP_RATE>
50 const unsigned int LERPS_PER_CONTROL;
52 unsigned int update_step_counter;
53 unsigned int num_update_steps;
55 enum {ATTACK,DECAY,SUSTAIN,RELEASE,IDLE};
60 unsigned int update_steps;
63 }attack,decay,sustain,release,idle;
65 phase * current_phase;
72 unsigned int convertMsecToControlUpdateSteps(
unsigned int msec){
73 return (uint16_t) (((uint32_t)msec*CONTROL_UPDATE_RATE)>>10);
78 void setPhase(phase * next_phase) {
79 update_step_counter = 0;
80 num_update_steps = next_phase->update_steps;
82 current_phase = next_phase;
87 void checkForAndSetNextPhase(phase * next_phase) {
88 if (++update_step_counter >= num_update_steps){
96 void setTime(phase * p,
unsigned int msec)
98 p->update_steps = convertMsecToControlUpdateSteps(msec);
99 p->lerp_steps = (long) p->update_steps * LERPS_PER_CONTROL;
104 void setUpdateSteps(phase * p,
unsigned int steps)
106 p->update_steps = steps;
107 p->lerp_steps = (long) steps * LERPS_PER_CONTROL;
116 ADSR():LERPS_PER_CONTROL(LERP_RATE/CONTROL_UPDATE_RATE)
118 attack.phase_type = ATTACK;
119 decay.phase_type = DECAY;
120 sustain.phase_type = SUSTAIN;
121 release.phase_type = RELEASE;
122 idle.phase_type = IDLE;
133 switch(current_phase->phase_type) {
136 checkForAndSetNextPhase(&decay);
140 checkForAndSetNextPhase(&sustain);
144 checkForAndSetNextPhase(&release);
148 checkForAndSetNextPhase(&idle);
152 adsr_playing =
false;
166 unsigned char out = 0;
236 void setIdleLevel(byte value)
265 void setLevels(byte attack, byte decay, byte sustain, byte release)
284 setTime(&attack, msec);
297 setTime(&decay, msec);
311 setTime(&sustain, msec);
325 setTime(&release, msec);
330 void setIdleTime(
unsigned int msec)
332 setTime(&idle, msec);
346 void setTimes(
unsigned int attack_ms,
unsigned int decay_ms,
unsigned int sustain_ms,
unsigned int release_ms)
363 setUpdateSteps(&attack, steps);
373 setUpdateSteps(&decay, steps);
383 setUpdateSteps(&sustain, steps);
393 setUpdateSteps(&release, steps);
398 void setIdleUpdateSteps(
unsigned int steps)
400 setUpdateSteps(&idle, steps);
410 void setAllUpdateSteps(
unsigned int attack_steps,
unsigned int decay_steps,
unsigned int sustain_steps,
unsigned int release_steps)
416 setIdleUpdateSteps(65535);
void set(T value)
Set the current value of the line.
unsigned char next()
Advances one audio step along the ADSR and returns the level.
void setSustainLevel(byte value)
Set the sustain level of the ADSR.
void setDecayLevel(byte value)
Set the decay level of the ADSR.
void setReleaseUpdateSteps(unsigned int steps)
Set the release time of the ADSR, expressed as the number of update steps (not ADSR::next() interpola...
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 setAttackUpdateSteps(unsigned int steps)
Set the attack time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolat...
void setLevels(byte attack, byte decay, byte sustain, byte release)
Set the attack, decay, sustain and release levels.
void setReleaseTime(unsigned int msec)
Set the release time of the ADSR in milliseconds.
Q8n0 Q15n16_to_Q8n0(Q15n16 a)
Convert Q15n16 fixed to Q8n0 uint8_t.
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 setSustainTime(unsigned int msec)
Set the sustain time of the ADSR in milliseconds.
void noteOff()
Start the release phase of the ADSR.
void setAttackTime(unsigned int msec)
Set the attack time of the ADSR in milliseconds.
Q15n16 Q8n0_to_Q15n16(Q8n0 a)
Convert Q8n0 uint8_t to Q15n16 fix.
void setDecayTime(unsigned int msec)
Set the decay 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 setReleaseLevel(byte value)
Set the release level of the ADSR.
void setSustainUpdateSteps(unsigned int steps)
Set the sustain time of the ADSR, expressed as the number of update steps (not ADSR::next() interpola...
void update()
Updates the internal controls of the ADSR.
void setADLevels(byte attack, byte decay)
Set the attack and decay levels of the ADSR.
uint8_t Q8n0
normal uint8_t with 0 fractional bits, represents 0.0 to 255.0
void noteOn()
Start the attack phase of the ADSR.
void setAttackLevel(byte value)
Set the attack level of the ADSR.
A simple ADSR envelope generator.
T next()
Increments one step along the line.
bool playing()
Tells if the envelope is currently playing.