From c354ee32e3d391f53c4819f8f4373e1b200c8793 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Sun, 17 Sep 2023 10:15:33 +0200 Subject: [PATCH] simplify vibrato logic --- packages/superdough/synth.mjs | 39 ++++++++++++++--------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/packages/superdough/synth.mjs b/packages/superdough/synth.mjs index 8a240340..2e2b7cb2 100644 --- a/packages/superdough/synth.mjs +++ b/packages/superdough/synth.mjs @@ -147,12 +147,6 @@ export function waveformN(partials, type) { } export function getOscillator({ s, freq, t, vib, vibmod, partials }) { - // Additional oscillator for vibrato effect - if (vib > 0) { - var vibrato_oscillator = getAudioContext().createOscillator(); - vibrato_oscillator.frequency.value = vib; - } - // Make oscillator with partial count let o; if (!partials || s === 'sine') { @@ -161,28 +155,27 @@ export function getOscillator({ s, freq, t, vib, vibmod, partials }) { } else { o = waveformN(partials, s); } + o.frequency.value = Number(freq); + o.start(t); + // Additional oscillator for vibrato effect + let vibrato_oscillator; if (vib > 0) { - o.frequency.value = Number(freq); - var gain = getAudioContext().createGain(); + vibrato_oscillator = getAudioContext().createOscillator(); + vibrato_oscillator.frequency.value = vib; + const gain = getAudioContext().createGain(); // Vibmod is the amount of vibrato, in semitones - gain.gain.value = vibmod * freq; + gain.gain.value = vibmod * 100; vibrato_oscillator.connect(gain); gain.connect(o.detune); vibrato_oscillator.start(t); - o.start(t); - return { - node: o, - stop: (time) => { - vibrato_oscillator.stop(time); - o.stop(time); - }, - }; - } else { - // Normal operation, without vibrato - o.frequency.value = Number(freq); - o.start(t); - const stop = (time) => o.stop(time); - return { node: o, stop }; } + + return { + node: o, + stop: (time) => { + vibrato_oscillator?.stop(time); + o.stop(time); + }, + }; }