I’m trying to program my own ADSR (because why not). I’ve done a little math on paper and figured out some formulas. Then I read through the code of the Shruthi ADSR and was surprised to find a completely different solution. Basically I’m wondering if my solution has flaws I can’t yet see.
Here’s what I did:
For the Attack phase: The goal is to have something like U (t) = t/A with A being the attack time. The derivative is: U_dot(t) = 1/A. Approximating U_dot(t) by (U (t+h) - U (t))/h I get to the solution: U (t+h) = U (t) + h/A.
For the Decay/Release phase: The goal is to have something like U (t) = U_max*e^(-t/R) with R beeing the release time. The derivative is: U_dot(t) = -U_max/R*e^(
t/R). Again, approximating U_dot(t) by (U (t+h) U (t))/h I get to the solution: U (t+h) = U (t)*(1-h/R).
This formula gets unstable for any R < h, but you won’t want that anyways, because R = h is already instantly zero.
Looking at those formulas, I can see that they are easy to implement, don’t suffer from any serious numerical instabilities or errors. And they can even be modulated without problems. I don’t see any obvious problems here.
In contrast, the Shruthi code basically has a exponential curve prepared in memory and uses a phase accumulator to “play” through that curve. I wonder what’s the benefit of this approach. (I’m silently assuming that pichenettes code simply must be superiour to what I figured out in a few minutes…)