Mozzi  version v2.0
sound synthesis library for Arduino
ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T > Class Template Reference

A simple ADSR envelope generator. More...

#include <ADSR.h>

Detailed Description

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_RATEThe 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_RATESets 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.

Public Member Functions

 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...
 

Public Attributes

bool adsr_playing
 

Member Function Documentation

◆ next()

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.

◆ noteOff()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::noteOff ( )
inline

Start the release phase of the ADSR.

Definition at line 188 of file ADSR.h.

◆ noteOn()

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
resetIf 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.

◆ playing()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
bool ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::playing ( )
inline

Tells if the envelope is currently playing.

Returns
true if playing, false if in IDLE state

Definition at line 427 of file ADSR.h.

◆ setADLevels()

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
attackthe new attack level.
decaythe new decay level.

Definition at line 249 of file ADSR.h.

◆ setAllUpdateSteps()

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_stepsthe number of update steps in the attack phase
decay_stepsthe number of update steps in the decay phase
sustain_stepsthe number of update steps in the sustain phase
release_stepsthe number of update steps in the release phase

Definition at line 411 of file ADSR.h.

◆ setAttackLevel()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setAttackLevel ( byte  value)
inline

Set the attack level of the ADSR.

Parameters
valuethe attack level.

Definition at line 198 of file ADSR.h.

◆ setAttackTime()

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
msecthe 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.

◆ setAttackUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setAttackUpdateSteps ( unsigned int  steps)
inline

Set the attack time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the attack phase.

Parameters
stepsthe number of times ADSR::update() will be called in the attack phase.

Definition at line 362 of file ADSR.h.

◆ setDecayLevel()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setDecayLevel ( byte  value)
inline

Set the decay level of the ADSR.

Parameters
valuethe decay level.

Definition at line 209 of file ADSR.h.

◆ setDecayTime()

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
msecthe 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.

◆ setDecayUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setDecayUpdateSteps ( unsigned int  steps)
inline

Set the decay time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the decay phase.

Parameters
stepsthe number of times ADSR::update() will be called in the decay phase.

Definition at line 372 of file ADSR.h.

◆ setLevels()

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
attackthe new attack level.
decaythe new sustain level.
attackthe new sustain level.
decaythe new release level.

Definition at line 266 of file ADSR.h.

◆ setReleaseLevel()

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
valuethe release level (usually 0).

Definition at line 230 of file ADSR.h.

◆ setReleaseTime()

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
msecthe 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.

◆ setReleaseUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setReleaseUpdateSteps ( unsigned int  steps)
inline

Set the release time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the release phase.

Parameters
stepsthe number of times ADSR::update() will be called in the release phase.

Definition at line 392 of file ADSR.h.

◆ setSustainLevel()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setSustainLevel ( byte  value)
inline

Set the sustain level of the ADSR.

Parameters
valuethe sustain level. Usually the same as the decay level, for a steady sustained note.

Definition at line 220 of file ADSR.h.

◆ setSustainTime()

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
msecthe 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.

◆ setSustainUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::setSustainUpdateSteps ( unsigned int  steps)
inline

Set the sustain time of the ADSR, expressed as the number of update steps (not ADSR::next() interpolation steps) in the sustain phase.

Parameters
stepsthe number of times ADSR::update() will be called in the sustain phase.

Definition at line 382 of file ADSR.h.

◆ setTimes()

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_msthe new attack time in milliseconds.
decay_msthe new decay time in milliseconds.
sustain_msthe new sustain time in milliseconds.
release_msthe 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.

◆ update()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE, typename T = unsigned int>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE, T >::update ( )
inline

Updates the internal controls of the ADSR.

Call this in updateControl().

Definition at line 128 of file ADSR.h.