[sdiy] software / firmware envelopes in C (or anything else)
Gordonjcp
gordonjcp at gjcp.net
Thu Nov 6 11:45:15 CET 2025
On Thu, Nov 06, 2025 at 10:00:15AM +0100, Roman Sowa via Synth-diy wrote:
> What you say here:
> output += (target - output) * rate
> is basicaly digital representation of RC network, the core of any analog
> envelope. Who needs anything better if it's simple enough.
>
> Anyway, I do it exactly like in your equation. Target is changing between 0,
> full scale and sustain level, depending on ADSR phase.
Ah - you have to watch that!
You've got to flip to the decay phase at somewhat less than full scale because the "RC network" charges asymptotically to the rail.
If you look at a practical RC ADSR circuit, you'll see that "Attack to Decay" is triggered by a comparator set to maybe 10V. This means that the RC network is still charging fairly quickly when it flips, with a slight downward curve which is a little more pronounced on a 12V supply than a 15V supply.
I do this in mine by flipping when the envelope value exceeds 0xbfff, or 49151, being a "nice round number" in hex and half-way between the values for 12V- or 15V-supply 10V envelopes. It feels about right.
> All done in fixed point math, but 24 bits is a must, as 16-bit truncation
> errors spoil the ends of decaying phases.
Can't say I've noticed this.
> I also thought about doing it for decades, but when finally did it, I
> couldn't understand why I waited so long, as the code turned out to be
> pretty simple. All done in 8-bit PIC micro in assembler.
> I don't use any log or other advanced math, I take rate values from look up
> table.
The Juno 106 has roughly half its voice ROM full of such tables, just to make the controls work nicely ;-)
--
Gordonjcp
More information about the Synth-diy
mailing list