Skip to content

Commit

Permalink
AdssrEnvelope: Fix division-by-zero failure when going into decay (#5,#9
Browse files Browse the repository at this point in the history
)

If the default values (ADSR mode on, Sustain level zero) are first
loaded, and then a patch setting ADSR mode, depending on which
order the parameters are set, this can result in a
division-by-zero in calculate_sustain_asymptote().

Fix this by recalculating sustain_asymptote before every
usage.

(The envelope code should really be rewritten to avoid
recalculating coef at every phase transition; coefs should
be set when the parameters are set, separately for
each envelope stage).
  • Loading branch information
Ricard Wanderlof committed Apr 5, 2024
1 parent 04b98fa commit 6847d83
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion plugins/mimid/Engine/AdssrEnvelope.h
Expand Up @@ -146,8 +146,8 @@ class AdssrEnvelope
void setSustain(float sus)
{
sustain = sus;
sustain_asymptote = calc_sustain_asymptote();
if (state == DEC || state == SUS) {
sustain_asymptote = calc_sustain_asymptote();
// Chase sustain level at decay rate, if sustain
// level changed in ADSR mode
if (Value > sustain) {
Expand Down Expand Up @@ -205,6 +205,7 @@ class AdssrEnvelope
if (Value > 1.0f) {
Value = 1.0f;
state = DEC;
sustain_asymptote = calc_sustain_asymptote();
coef = coef_dec(decay);
dir = 1;
}
Expand Down

0 comments on commit 6847d83

Please sign in to comment.