Reference p5.Envelope

p5.Envelope

Envelopes are pre-defined amplitude distribution over time. Typically, envelopes are used to control the output volume of an object, a series of fades referred to as Attack, Decay, Sustain and Release ( ADSR ). Envelopes can also control other Web Audio Parameters—for example, a p5.Envelope can control an Oscillator's frequency like this: osc.freq(env).

Use <a href="/reference/p5.Envelope/setRange/">setRange</a> to change the attack/release level. Use <a href="/reference/p5.Envelope/setADSR/">setADSR</a> to change attackTime, decayTime, sustainPercent and releaseTime.

Use the <a href="/reference/p5.Envelope/play/">play</a> method to play the entire envelope, the <a href="/reference/p5.Envelope/ramp/">ramp</a> method for a pingable trigger, or <a href="/reference/p5.Envelope/triggerAttack/">triggerAttack</a>/ <a href="/reference/p5.Envelope/triggerRelease/">triggerRelease</a> to trigger noteOn/noteOff.

Examples

Fields

attackTime

Time until envelope reaches attackLevel

attackLevel

Level once attack is complete.

decayTime

Time until envelope reaches decayLevel.

decayLevel

Level after decay. The envelope will sustain here until it is released.

releaseTime

Duration of the release portion of the envelope.

releaseLevel

Level at the end of the release.

Methods

set

Reset the envelope with a series of time/value pairs.

setADSR

Set values like a traditional ADSR envelope .

setRange

Set max (attackLevel) and min (releaseLevel) of envelope.

setInput

Assign a parameter to be controlled by this envelope. If a p5.Sound object is given, then the p5.Envelope will control its output gain. If multiple inputs are provided, the env will control all of them.

setExp

Set whether the envelope ramp is linear (default) or exponential. Exponential ramps can be useful because we perceive amplitude and frequency logarithmically.

play

Play tells the envelope to start acting on a given input. If the input is a p5.sound object (i.e. AudioIn, Oscillator, SoundFile), then Envelope will control its output volume. Envelopes can also be used to control any Web Audio Audio Param.

triggerAttack

Trigger the Attack, and Decay portion of the Envelope. Similar to holding down a key on a piano, but it will hold the sustain level until you let go. Input can be any p5.sound object, or a Web Audio Param.

triggerRelease

Trigger the Release of the Envelope. This is similar to releasing the key on a piano and letting the sound fade according to the release level and release time.

ramp

Exponentially ramp to a value using the first two values from setADSR(attackTime, decayTime) as time constants for simple exponential ramps. If the value is higher than current value, it uses attackTime, while a decrease uses decayTime.

add

Add a value to the p5.Oscillator's output amplitude, and return the oscillator. Calling this method again will override the initial add() with new values.

mult

Multiply the p5.Envelope's output amplitude by a fixed value. Calling this method again will override the initial mult() with new values.

scale

Scale this envelope's amplitude values to a given range, and return the envelope. Calling this method again will override the initial scale() with new values.

Notice any errors or typos? Please let us know. Please feel free to edit lib/addons/p5.sound.js and open a pull request!

Related References