17 #include "mozzi_fixmath.h"
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;
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;
177 if (reset) transition.
set(0);
237 void setIdleLevel(
byte value)
266 void setLevels(
byte attack,
byte decay,
byte sustain,
byte 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);
A simple ADSR envelope generator.
void setAttackUpdateSteps(unsigned int steps)
Set the attack time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolat...
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 setDecayTime(unsigned int msec)
Set the decay time of the ADSR in milliseconds.
void update()
Updates the internal controls of the ADSR.
void noteOff()
Start the release phase of the ADSR.
void setADLevels(byte attack, byte decay)
Set the attack and decay levels of the ADSR.
void setLevels(byte attack, byte decay, byte sustain, byte release)
Set the attack, decay, sustain and release levels.
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 setAttackLevel(byte value)
Set the attack level of the ADSR.
void setReleaseLevel(byte value)
Set the release level of the ADSR.
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...
unsigned char next()
Advances one audio step along the ADSR and returns the level.
void setReleaseTime(unsigned int msec)
Set the release time of the ADSR in milliseconds.
void setAttackTime(unsigned int msec)
Set the attack time of the ADSR in milliseconds.
bool playing()
Tells if the envelope is currently playing.
void setSustainTime(unsigned int msec)
Set the sustain 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 setSustainLevel(byte value)
Set the sustain 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 noteOn(bool reset=false)
Start the attack phase of the ADSR.
T next()
Increments one step along the line.
void set(T value)
Set the current value of the line.
Q8n0 Q15n16_to_Q8n0(Q15n16 a)
Convert Q15n16 fixed to Q8n0 uint8_t.
Q15n16 Q8n0_to_Q15n16(Q8n0 a)
Convert Q8n0 uint8_t to Q15n16 fix.
uint8_t Q8n0
normal uint8_t with 0 fractional bits, represents 0.0 to 255.0