Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
ADSR< CONTROL_UPDATE_RATE, LERP_RATE > Class Template Reference

A simple ADSR envelope generator. More...

#include <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 ()
 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
 

Detailed Description

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
class ADSR< CONTROL_UPDATE_RATE, LERP_RATE >

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 CONTROL_RATE, but an alternative (amongst others) is to set this as well as the LERP_RATE parameter to 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 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 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 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 46 of file ADSR.h.

Member Function Documentation

§ next()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
unsigned char ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 164 of file ADSR.h.

§ noteOff()

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

Start the release phase of the ADSR.

Definition at line 187 of file ADSR.h.

§ noteOn()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::noteOn ( )
inline

Start the attack phase of the ADSR.

THis will restart the ADSR no matter what phase it is up to.

Definition at line 176 of file ADSR.h.

§ playing()

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

Tells if the envelope is currently playing.

Returns
true if playing, false if in IDLE state

Definition at line 426 of file ADSR.h.

§ setADLevels()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 248 of file ADSR.h.

§ setAllUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 410 of file ADSR.h.

§ setAttackLevel()

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

Set the attack level of the ADSR.

Parameters
valuethe attack level.

Definition at line 197 of file ADSR.h.

§ setAttackTime()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 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 282 of file ADSR.h.

§ setAttackUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 361 of file ADSR.h.

§ setDecayLevel()

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

Set the decay level of the ADSR.

Parameters
valuethe decay level.

Definition at line 208 of file ADSR.h.

§ setDecayTime()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 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 295 of file ADSR.h.

§ setDecayUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 371 of file ADSR.h.

§ setLevels()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 265 of file ADSR.h.

§ setReleaseLevel()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 229 of file ADSR.h.

§ setReleaseTime()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 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 323 of file ADSR.h.

§ setReleaseUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 391 of file ADSR.h.

§ setSustainLevel()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 219 of file ADSR.h.

§ setSustainTime()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 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 309 of file ADSR.h.

§ setSustainUpdateSteps()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 381 of file ADSR.h.

§ setTimes()

template<unsigned int CONTROL_UPDATE_RATE, unsigned int LERP_RATE>
void ADSR< CONTROL_UPDATE_RATE, LERP_RATE >::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 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 346 of file ADSR.h.

§ update()

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

Updates the internal controls of the ADSR.

Call this in updateControl().

Definition at line 131 of file ADSR.h.