diff --git a/packages/soundfonts/fontloader.mjs b/packages/soundfonts/fontloader.mjs index fced1c28..4be29fa1 100644 --- a/packages/soundfonts/fontloader.mjs +++ b/packages/soundfonts/fontloader.mjs @@ -5,6 +5,7 @@ import { getParamADSR, getADSRValues, getPitchEnvelope, + getVibratoOscillator, } from '@strudel.cycles/webaudio'; import gm from './gm.mjs'; @@ -155,6 +156,8 @@ export function registerSoundfonts() { getParamADSR(node.gain, attack, decay, sustain, release, 0, 0.3, time, holdEnd, 'linear'); let envEnd = holdEnd + release + 0.01; + // vibrato + let vibratoOscillator = getVibratoOscillator(bufferSource.detune, value, time); // pitch envelope if (value.penv) { getPitchEnvelope(bufferSource.detune, value, time, holdEnd); @@ -164,6 +167,7 @@ export function registerSoundfonts() { const stop = (releaseTime) => {}; bufferSource.onended = () => { bufferSource.disconnect(); + vibratoOscillator?.stop(); node.disconnect(); onended(); }; diff --git a/packages/superdough/helpers.mjs b/packages/superdough/helpers.mjs index d69afd36..7a11d84d 100644 --- a/packages/superdough/helpers.mjs +++ b/packages/superdough/helpers.mjs @@ -157,3 +157,19 @@ export function getPitchEnvelope(param, value, t, holdEnd) { getParamADSR(param, pattack, pdecay, psustain, prelease, 0, cents, t, holdEnd, 'linear'); } } + +export function getVibratoOscillator(param, value, t) { + const { vibmod = 0.5, vib } = value; + let vibratoOscillator; + if (vib > 0) { + vibratoOscillator = getAudioContext().createOscillator(); + vibratoOscillator.frequency.value = vib; + const gain = getAudioContext().createGain(); + // Vibmod is the amount of vibrato, in semitones + gain.gain.value = vibmod * 100; + vibratoOscillator.connect(gain); + gain.connect(param); + vibratoOscillator.start(t); + return vibratoOscillator; + } +} diff --git a/packages/superdough/sampler.mjs b/packages/superdough/sampler.mjs index 5e842da5..67ee06c0 100644 --- a/packages/superdough/sampler.mjs +++ b/packages/superdough/sampler.mjs @@ -1,6 +1,6 @@ import { noteToMidi, valueToMidi, getSoundIndex } from './util.mjs'; import { getAudioContext, registerSound } from './index.mjs'; -import { getADSRValues, getParamADSR, getPitchEnvelope } from './helpers.mjs'; +import { getADSRValues, getParamADSR, getPitchEnvelope, getVibratoOscillator } from './helpers.mjs'; import { logger } from './logger.mjs'; const bufferCache = {}; // string: Promise @@ -244,8 +244,6 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) { loopEnd = 1, end = 1, duration, - vib, - vibmod = 0.5, } = value; // load sample if (speed === 0) { @@ -263,17 +261,7 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) { const bufferSource = await getSampleBufferSource(s, n, note, speed, freq, bank, resolveUrl); // vibrato - let vibratoOscillator; - if (vib > 0) { - vibratoOscillator = getAudioContext().createOscillator(); - vibratoOscillator.frequency.value = vib; - const gain = getAudioContext().createGain(); - // Vibmod is the amount of vibrato, in semitones - gain.gain.value = vibmod * 100; - vibratoOscillator.connect(gain); - gain.connect(bufferSource.detune); - vibratoOscillator.start(0); - } + let vibratoOscillator = getVibratoOscillator(bufferSource.detune, value, t); // asny stuff above took too long? if (ac.currentTime > t) { diff --git a/packages/superdough/synth.mjs b/packages/superdough/synth.mjs index 9d70e037..7bde4d3a 100644 --- a/packages/superdough/synth.mjs +++ b/packages/superdough/synth.mjs @@ -1,6 +1,6 @@ import { midiToFreq, noteToMidi } from './util.mjs'; import { registerSound, getAudioContext } from './superdough.mjs'; -import { gainNode, getADSRValues, getParamADSR, getPitchEnvelope } from './helpers.mjs'; +import { gainNode, getADSRValues, getParamADSR, getPitchEnvelope, getVibratoOscillator } from './helpers.mjs'; import { getNoiseMix, getNoiseOscillator } from './noise.mjs'; const mod = (freq, range = 1, type = 'sine') => { @@ -110,8 +110,6 @@ export function getOscillator(s, t, value) { n: partials, note, freq, - vib = 0, - vibmod = 0.5, noise = 0, // fm fmh: fmHarmonicity = 1, @@ -181,17 +179,7 @@ export function getOscillator(s, t, value) { } // Additional oscillator for vibrato effect - let vibratoOscillator; - if (vib > 0) { - vibratoOscillator = getAudioContext().createOscillator(); - vibratoOscillator.frequency.value = vib; - const gain = getAudioContext().createGain(); - // Vibmod is the amount of vibrato, in semitones - gain.gain.value = vibmod * 100; - vibratoOscillator.connect(gain); - gain.connect(o.detune); - vibratoOscillator.start(t); - } + let vibratoOscillator = getVibratoOscillator(o.detune, value, t); // pitch envelope if (value.penv) {