From 5671b40707b86f9e9be5d0eb319a2bcbe2e0e4de Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Wed, 20 Dec 2023 15:24:14 -0500 Subject: [PATCH] account for phase complete --- packages/superdough/helpers.mjs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/superdough/helpers.mjs b/packages/superdough/helpers.mjs index 51361e79..b5c01b08 100644 --- a/packages/superdough/helpers.mjs +++ b/packages/superdough/helpers.mjs @@ -4,7 +4,11 @@ import { clamp } from './util.mjs'; const setRelease = (param, phase, sustain, startTime, endTime, endValue, curve = 'linear') => { const ctx = getAudioContext(); const ramp = curve === 'exponential' ? 'exponentialRampToValueAtTime' : 'linearRampToValueAtTime'; - if (param.cancelAndHoldAtTime == null) { + // if the decay stage is complete before the note event is done, we don't need to do anything special + if (phase < startTime) { + param.setValueAtTime(sustain, startTime); + param[ramp](endValue, endTime); + } else if (param.cancelAndHoldAtTime == null) { //this replicates cancelAndHoldAtTime behavior for Firefox setTimeout(() => { //sustain at current value @@ -15,9 +19,7 @@ const setRelease = (param, phase, sustain, startTime, endTime, endValue, curve = param[ramp](endValue, endTime); }, (startTime - ctx.currentTime) * 1000); } else { - if (phase < startTime) { - param.setValueAtTime(sustain, startTime); - } + //stop the envelope, hold the value, and then set the release stage param.cancelAndHoldAtTime(startTime); param[ramp](endValue, endTime); }