Envelopes

To add envelopes to your sounds you need to create an envelope shape using Env and then play it back using EnvGen. Let's look at both stages:

Creating envelopes 1: Fixed-Duration preset envelopes

linen - trapezoid shape, arguments are attackTime, sustainTime, releaseTime, sustainLevel
triangle - triangle shape, arguments are duration and level
sine - hanning window shape (half a sine), arguments are duration and level
perc - create a percussive shape, arguments are attackTime, releaseTime, peakLevel and curve

Examples:

(
SynthDef("envelopes", { arg trig;
var e, amp;
e = Env.linen(1,3,1,0.6);
amp = EnvGen.kr(e, trig);
Out.ar(0, amp*SinOsc.ar(440));
}).writeDefFile;
s.sendSynthDef("envelopes");
)

s.sendMsg("/s_new", "envelopes", 1000, 1, 0);
s.sendMsg("/n_set", 1000, "trig", 1);
s.sendMsg("/n_free", 1000);

Creating envelopes 2: Sustaining preset envelopes

adsr - specify a regular adsr (attack, decay, sustain, release) envelope. Arguments are attackTime, decayTime, sustainLevel, releaseTime, peakLevel and curve
asr - specify an asr envelope. Arguments are attackTime, sustainLevel, releaseTime, peakLevel and curve

curve options:
	'step' - flat segments
	'linear' - linear segments, the default
	'exponential' - natural exponential growth and decay. In this case, the levels must all be nonzero
				and the have the same sign.
	'sine' - sinusoidal S shaped segments.
	'welch' - sinusoidal segments shaped like the sides of a Welch window.
	a Float - a curvature value for all segments.
	An Array of Floats - curvature values for each segments.

Creating envelopes 3: Custom envelopes

using the method .new, you can specify any type of envelope you like. The arguments are
levels - array of levels
times - array of durations of segments
curves
releaseNode - envelope will sustain at the releasenode until released
loopNode - if set, the envelope will loop from the loopnode to the releasenode until released

Using envelopes:
Envelopes are then used in the EnvGen ugen. The arguments are envelope, gate, levelScale, levelBias, timeScale and doneAction.

envelope - an instance of Env (or an array of Controls)
gate     -    a control signal that holds the EnvGen open (except Env.linen) - 0 will close the gate, >0 will open it
levelScale - scales the levels of the breakpoints.
levelBias - offsets the levels of the breakpoints.
timeScale - scales the breakpoint durations.
doneAction -the doneAction arg lets you cause the EnvGen to stop or end the synth

If you send a value of -1 or less to the gate argument, then the envelope will cut off immediately.