template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
class ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >
A simple ADSR envelope generator.
This implementation has separate update() and next() methods, where next() interpolates values between each update(). The "normal" way to use this would be with update() in updateControl(), where it calculates a new internal state each control step, and then next() is in updateAudio(), called much more often, where it interpolates between the control values. This also allows the ADSR updates to be made even more sparsely if desired, eg. every 3rd control update.
- Template Parameters
-
CONTROL_UPDATE_RATE | The frequency of control updates. Ordinarily this will be MOZZI_CONTROL_RATE, but an alternative (amongst others) is to set this as well as the LERP_RATE parameter to MOZZI_AUDIO_RATE, and call both update() and next() in updateAudio(). Such a use would allow accurate envelopes with finer resolution of the control points than MOZZI_CONTROL_RATE. |
LERP_RATE | Sets how often next() will be called, to interpolate between updates set by CONTROL_UPDATE_RATE. This will produce the smoothest results if it's set to MOZZI_AUDIO_RATE, but if you need to save processor time and your envelope changes slowly or controls something like a filter where there may not be problems with glitchy or clicking transitions, LERP_RATE could be set to MOZZI_CONTROL_RATE (for instance). Then update() and next() could both be called in updateControl(), greatly reducing the amount of processing required compared to calling next() in updateAudio(). |
Definition at line 41 of file ADSR.h.
|
| ADSR () |
| Constructor.
|
|
void | update () |
| Updates the internal controls of the ADSR. More...
|
|
unsigned char | next () |
| Advances one audio step along the ADSR and returns the level. More...
|
|
void | noteOn (bool reset=false) |
| Start the attack phase of the ADSR. More...
|
|
void | noteOff () |
| Start the release phase of the ADSR. More...
|
|
void | setAttackLevel (byte value) |
| Set the attack level of the ADSR. More...
|
|
void | setDecayLevel (byte value) |
| Set the decay level of the ADSR. More...
|
|
void | setSustainLevel (byte value) |
| Set the sustain level of the ADSR. More...
|
|
void | setReleaseLevel (byte value) |
| Set the release level of the ADSR. More...
|
|
void | setIdleLevel (byte value) |
|
void | setADLevels (byte attack, byte decay) |
| Set the attack and decay levels of the ADSR. More...
|
|
void | setLevels (byte attack, byte decay, byte sustain, byte release) |
| Set the attack, decay, sustain and release levels. More...
|
|
void | setAttackTime (unsigned int msec) |
| Set the attack time of the ADSR in milliseconds. More...
|
|
void | setDecayTime (unsigned int msec) |
| Set the decay time of the ADSR in milliseconds. More...
|
|
void | setSustainTime (unsigned int msec) |
| Set the sustain time of the ADSR in milliseconds. More...
|
|
void | setReleaseTime (unsigned int msec) |
| Set the release time of the ADSR in milliseconds. More...
|
|
void | setIdleTime (unsigned int msec) |
|
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. More...
|
|
void | setAttackUpdateSteps (unsigned int steps) |
| Set the attack time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the attack phase. More...
|
|
void | setDecayUpdateSteps (unsigned int steps) |
| Set the decay time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the decay phase. More...
|
|
void | setSustainUpdateSteps (unsigned int steps) |
| Set the sustain time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the sustain phase. More...
|
|
void | setReleaseUpdateSteps (unsigned int steps) |
| Set the release time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the release phase. More...
|
|
void | setIdleUpdateSteps (unsigned int steps) |
|
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() interpolation steps). More...
|
|
bool | playing () |
| Tells if the envelope is currently playing. More...
|
|
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
unsigned char ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::next |
( |
| ) |
|
|
inline |
Advances one audio step along the ADSR and returns the level.
Call this in updateAudio().
- Returns
- the next value, as an unsigned char.
Definition at line 161 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::noteOn |
( |
bool |
reset = false | ) |
|
|
inline |
Start the attack phase of the ADSR.
This will restart the ADSR no matter what phase it is up to.
- Parameters
-
reset | If true, the envelope will start from 0, even if it is still playing (often useful for effect envelopes). If false (default if omitted), the envelope will start rising from the current level, which could be non-zero, if it is still playing (most useful for note envelopes). |
Definition at line 176 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setADLevels |
( |
byte |
attack, |
|
|
byte |
decay |
|
) |
| |
|
inline |
Set the attack and decay levels of the ADSR.
This assumes a conventional ADSR where the sustain continues at the same level as the decay, till the release ramps to 0.
- Parameters
-
attack | the new attack level. |
decay | the new decay level. |
Definition at line 249 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setAllUpdateSteps |
( |
unsigned int |
attack_steps, |
|
|
unsigned int |
decay_steps, |
|
|
unsigned int |
sustain_steps, |
|
|
unsigned int |
release_steps |
|
) |
| |
|
inline |
Set the attack, decay and release times of the ADSR, expressed in update steps (not ADSR::next() interpolation steps).
- Parameters
-
attack_steps | the number of update steps in the attack phase |
decay_steps | the number of update steps in the decay phase |
sustain_steps | the number of update steps in the sustain phase |
release_steps | the number of update steps in the release phase |
Definition at line 411 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setAttackTime |
( |
unsigned int |
msec | ) |
|
|
inline |
Set the attack time of the ADSR in milliseconds.
The actual time taken will be resolved within the resolution of MOZZI_CONTROL_RATE.
- Parameters
-
msec | the unsigned int attack time in milliseconds. |
- Note
- Beware of low values (less than 20 or so, depending on how many steps are being taken), in case internal step size gets calculated as 0, which would mean nothing happens.
Definition at line 283 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setDecayTime |
( |
unsigned int |
msec | ) |
|
|
inline |
Set the decay time of the ADSR in milliseconds.
The actual time taken will be resolved within the resolution of MOZZI_CONTROL_RATE.
- Parameters
-
msec | the unsigned int decay time in milliseconds. |
- Note
- Beware of low values (less than 20 or so, depending on how many steps are being taken), in case internal step size gets calculated as 0, which would mean nothing happens.
Definition at line 296 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setLevels |
( |
byte |
attack, |
|
|
byte |
decay, |
|
|
byte |
sustain, |
|
|
byte |
release |
|
) |
| |
|
inline |
Set the attack, decay, sustain and release levels.
- Parameters
-
attack | the new attack level. |
decay | the new sustain level. |
attack | the new sustain level. |
decay | the new release level. |
Definition at line 266 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setReleaseLevel |
( |
byte |
value | ) |
|
|
inline |
Set the release level of the ADSR.
Normally you'd make this 0, but you have the option of some other value.
- Parameters
-
value | the release level (usually 0). |
Definition at line 230 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setReleaseTime |
( |
unsigned int |
msec | ) |
|
|
inline |
Set the release time of the ADSR in milliseconds.
The actual time taken will be resolved within the resolution of MOZZI_CONTROL_RATE.
- Parameters
-
msec | the unsigned int release time in milliseconds. |
- Note
- Beware of low values (less than 20 or so, depending on how many steps are being taken), in case internal step size gets calculated as 0, which would mean nothing happens.
Definition at line 324 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setSustainTime |
( |
unsigned int |
msec | ) |
|
|
inline |
Set the sustain time of the ADSR in milliseconds.
The actual time taken will be resolved within the resolution of MOZZI_CONTROL_RATE. The sustain phase will finish if the ADSR recieves a noteOff().
- Parameters
-
msec | the unsigned int sustain time in milliseconds. |
- Note
- Beware of low values (less than 20 or so, depending on how many steps are being taken), in case internal step size gets calculated as 0, which would mean nothing happens.
Definition at line 310 of file ADSR.h.
template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setTimes |
( |
unsigned int |
attack_ms, |
|
|
unsigned int |
decay_ms, |
|
|
unsigned int |
sustain_ms, |
|
|
unsigned int |
release_ms |
|
) |
| |
|
inline |
Set the attack, decay and release times of the ADSR in milliseconds.
The actual times will be resolved within the resolution of MOZZI_CONTROL_RATE.
- Parameters
-
attack_ms | the new attack time in milliseconds. |
decay_ms | the new decay time in milliseconds. |
sustain_ms | the new sustain time in milliseconds. |
release_ms | the new release time in milliseconds. |
- Note
- Beware of low values (less than 20 or so, depending on how many steps are being taken), in case internal step size gets calculated as 0, which would mean nothing happens.
Definition at line 347 of file ADSR.h.