Next: Transfer functions for amplitude Up: Examples Previous: Examples Contents Index

## ADSR envelope generator

Example D01.envelope.gen.pd (Figure 4.12) shows how the `line~` object may be used to generate an ADSR envelope to control a synthesis patch (only the ADSR envelope is shown in the figure). The "attack" button, when pressed, has two effects. The first (leftmost in the figure) is to set the `line~` object on its attack segment, with a target of 10 (the peak amplitude) over 200 msec (the attack time). Second, the attack button sets a `delay 200` object, so that after the attack segment is done, the decay segment can start. The decay segment falls to a target of 1 (the sustain level) after another 2500 msec (the decay time).

Figure 4.12: Using a `line~` object to generate an ADSR envelope.

The "release" button sends the same `line~` object back to zero over 500 more milliseconds (the release time). Also, in case the `delay 200` object happens to be set at the moment the "release" button is pressed, a "stop" message is sent to it. This prevents the ADSR generator from launching its decay segment after launching its release segment.

In Example D02.adsr.pd (Figure 4.13) we encapsulate the ADSR generator in a Pd abstraction (named `adsr`) so that it can easily be replicated. The design of the `adsr` abstraction makes it possible to control the five ADSR parameters either by supplying creation arguments or by connecting control streams to its inlets.

Figure 4.13: Invoking the `adsr` abstraction.

In this example the five creation arguments (1, 100, 200, 50, and 300) specify the peak level, attack time, decay time, sustain level (as a percentage of peak level), and release time. There are six control inlets: the first to trigger the ADSR generator, and the others to update the values of the five parameters. The output of the abstraction is an audio signal.

This abstraction is realized as shown in Figure 4.14. (You can open this subpatch by clicking on the `adsr` object in the patch.) The only signal objects are `line~` and `outlet~`. The three `pack` objects correspond to the three message objects from the earlier Figure 4.12. From left to right, they take care of the attack, decay, and release segments.

Figure 4.14: Inside the `adsr` abstraction.

The attack segment goes to a target specified as "$1" (the first creation argument of the abstraction) over "$2" milliseconds; these values may be overwritten by sending numbers to the "peak level" and "attack" inlets. The release segment is similar, but simpler, since the target is always zero. The hard part is the decay segment, which again must be set off after a delay equal to the attack time (the `del $2` object). The sustain level is calculated from the peak level and the sustain percentage (multiplying the two and dividing by 100).

The trigger inlet, if sent a number other than zero, triggers an onset (the attack and decay segments), and if sent zero, triggers the release segment. Furthermore, the ADSR generator may be reset to zero by sending a negative trigger (which also generates an onset).

Next: Transfer functions for amplitude Up: Examples Previous: Examples Contents Index