Adding vibrato to synth oscillator

This commit is contained in:
Raphael Forment 2023-09-04 06:03:05 +02:00
parent abaeb52b7c
commit fdc201a799

View File

@ -40,6 +40,8 @@ export function registerSynthSounds() {
fmrelease: fmRelease, fmrelease: fmRelease,
fmvelocity: fmVelocity, fmvelocity: fmVelocity,
fmwave: fmWaveform = 'sine', fmwave: fmWaveform = 'sine',
vibrato = 0,
vdepth = 100,
} = value; } = value;
let { n, note, freq } = value; let { n, note, freq } = value;
// with synths, n and note are the same thing // with synths, n and note are the same thing
@ -53,7 +55,7 @@ export function registerSynthSounds() {
} }
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default) // maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
// make oscillator // make oscillator
const { node: o, stop } = getOscillator({ t, s: wave, freq, partials: n }); const { node: o, stop } = getOscillator({ t, s: wave, freq, vibrato, vdepth, partials: n });
// FM + FM envelope // FM + FM envelope
let stopFm, fmEnvelope; let stopFm, fmEnvelope;
@ -137,8 +139,14 @@ export function waveformN(partials, type) {
return osc; return osc;
} }
export function getOscillator({ s, freq, t, partials }) { export function getOscillator({ s, freq, t, vibrato, vdepth, partials }) {
// make oscillator // Additional oscillator for vibrato effect
if (vibrato > 0) {
var vibrato_oscillator = getAudioContext().createOscillator();
vibrato_oscillator.frequency.value = vibrato;
}
// Make oscillator with partial count
let o; let o;
if (!partials || s === 'sine') { if (!partials || s === 'sine') {
o = getAudioContext().createOscillator(); o = getAudioContext().createOscillator();
@ -146,9 +154,28 @@ export function getOscillator({ s, freq, t, partials }) {
} else { } else {
o = waveformN(partials, s); o = waveformN(partials, s);
} }
o.frequency.value = Number(freq);
o.start(t); if (vibrato > 0) {
//o.stop(t + duration + release); // Vibrato by creating a gain node
const stop = (time) => o.stop(time); o.frequency.value = Number(freq);
return { node: o, stop }; var gain = getAudioContext().createGain();
gain.gain.value = vdepth * 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 };
}
} }