diff --git a/packages/superdough/helpers.mjs b/packages/superdough/helpers.mjs index 47161ed61..de64df0e8 100644 --- a/packages/superdough/helpers.mjs +++ b/packages/superdough/helpers.mjs @@ -262,7 +262,15 @@ export function drywet(dry, wet, wetAmount = 0) { let mix = ac.createGain(); dry_gain.connect(mix); wet_gain.connect(mix); - return mix; + return { + node: mix, + onended: () => { + dry_gain.disconnect(mix); + wet_gain.disconnect(mix); + dry.disconnect(dry_gain); + wet.disconnect(wet_gain); + }, + }; } let curves = ['linear', 'exponential']; @@ -297,6 +305,10 @@ export function getVibratoOscillator(param, value, t) { gain.gain.value = vibmod * 100; vibratoOscillator.connect(gain); gain.connect(param); + vibratoOscillator.onended = () => { + gain.disconnect(param); + vibratoOscillator.disconnect(gain); + }; vibratoOscillator.start(t); return vibratoOscillator; } diff --git a/packages/superdough/noise.mjs b/packages/superdough/noise.mjs index 816dd252b..3e41515df 100644 --- a/packages/superdough/noise.mjs +++ b/packages/superdough/noise.mjs @@ -65,8 +65,9 @@ export function getNoiseOscillator(type = 'white', t, density = 0.02) { export function getNoiseMix(inputNode, wet, t) { const noiseOscillator = getNoiseOscillator('pink', t); const noiseMix = drywet(inputNode, noiseOscillator.node, wet); + noiseOscillator.node.onended = () => noiseMix.onended(); return { - node: noiseMix, + node: noiseMix.node, stop: (time) => noiseOscillator?.stop(time), }; } diff --git a/packages/superdough/synth.mjs b/packages/superdough/synth.mjs index 10df1776b..2dc9e2be8 100644 --- a/packages/superdough/synth.mjs +++ b/packages/superdough/synth.mjs @@ -48,19 +48,17 @@ export function registerSynthSounds() { [0.001, 0.05, 0.6, 0.01], ); - let sound = getOscillator(s, t, value); - let { node: o, stop, triggerRelease } = sound; - // turn down const g = gainNode(0.3); - const { duration } = value; - - o.onended = () => { - o.disconnect(); + let sound = getOscillator(s, t, value, () => { g.disconnect(); onended(); - }; + }); + + let { node: o, stop, triggerRelease } = sound; + + const { duration } = value; const envGain = gainNode(1); let node = o.connect(g).connect(envGain); @@ -460,7 +458,7 @@ export function waveformN(partials, phases, type) { } // expects one of waveforms as s -export function getOscillator(s, t, value) { +export function getOscillator(s, t, value, onended) { const { duration, noise = 0 } = value; const partials = value.partials ?? value.n; let o; @@ -482,7 +480,6 @@ export function getOscillator(s, t, value) { } // set frequency o.frequency.value = getFrequencyFromValue(value); - o.start(t); let vibratoOscillator = getVibratoOscillator(o.detune, value, t); @@ -495,6 +492,12 @@ export function getOscillator(s, t, value) { noiseMix = getNoiseMix(o, noise, t); } + o.onended = () => { + noiseMix?.node.disconnect(); + onended(); + }; + o.start(t); + return { node: noiseMix?.node || o, stop: (time) => {