From 6847d83e67631c5e1932524b34400e3c4d3e1e88 Mon Sep 17 00:00:00 2001 From: Ricard Wanderlof Date: Fri, 22 Mar 2024 22:04:29 +0100 Subject: [PATCH] AdssrEnvelope: Fix division-by-zero failure when going into decay (#5,#9) 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). --- plugins/mimid/Engine/AdssrEnvelope.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/mimid/Engine/AdssrEnvelope.h b/plugins/mimid/Engine/AdssrEnvelope.h index 902e7ea..1ce481c 100644 --- a/plugins/mimid/Engine/AdssrEnvelope.h +++ b/plugins/mimid/Engine/AdssrEnvelope.h @@ -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) { @@ -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; }