From 7813ae54fef9323dc369b01a72e1fe7ecbd948d9 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) 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..0fbd46a 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) { @@ -206,6 +206,7 @@ class AdssrEnvelope Value = 1.0f; state = DEC; coef = coef_dec(decay); + sustain_asymptote = calc_sustain_asymptote(); dir = 1; } break;