# [sdiy] Fast envelope generation

Tom Wiltshire tom at electricdruid.net
Wed Dec 19 02:06:01 CET 2012

```Hi Don,

This is in serious danger of getting a bit religious, which is far from my intention, but here goes anyway. Please consider it an explanation rather than a attack or criticism (anything that works is good with me!) - further discussion of the philosophical position, if you will.

> Certainly true that the lookup table provides any curve that you care to set up.  But it doesn't give you an easy or natural way to make changes to the curve, or dynamically change parameters during the envelope.

How not? The primary parameter is the speed/time of the segment, and that can be altered dynamically for a NCO-based design as easily as for a IIR-based one. You either modulate per-sample or you don't, in either case. Example:

http://www.modcan.com/bseries/ahdbdsr.html

This uses the design we're discussing - NCO plus linear interp, at a sample rate of 25KHz. All parameters are dynamically modulatable whilst the envelope runs and you can stretch it and bounce it like elastic. Altering the time during a segment alters the shape, in exactly the same way as FM alters a waveform.
As to natural - yeah, it sounds natural - pingy, bloopy, poppy, liquid, natural. I was surprised. Pleasantly.

> The table lookup is more computationally intensive because you have to scale the curve for the size of your step, and you have to perform the interpolation, and that requires some accuracy.

I can see that it might be possible to do some IIR RC sims cheaper than some lookup+interps schemes, but I don't think that's always the case.

You can read the curve as a interpolation index x, and then do the following linear interp calc:

y = ((1-x) * start) + (x * end)

This gives you the curve between the two points 'start' and 'end' and saves the scaling, for the cost of two multiplies. Since you can read any lookup table, you can insert any curve between the two points for no more work.
The accuracy required is *less* than is required for the IIR, since the result 'y' is not used for any subsequent calculations, unlike the IIR. y is only required to whatever the output resolution is - usually 16-bits or less. The phase is stored to a reasonable accuracy, I grant you (24 or 32-bits is enough) but that never needs multiplying - a 'frequency increment' (addition) is the only operation required. This keeps the "hard sums" (multiply!) to the low accuracy parts of the calculation.
The worst part of envelopes is definitely the logic since that varies from one stage to another, whereas the calculation above is the same for all of them!

Regards,
Tom

```