Add pink, white and brown oscillators

This commit is contained in:
Raphael Forment 2023-09-30 14:07:33 +02:00
parent 8a741952df
commit c2481e460b

View File

@ -20,100 +20,102 @@ const fm = (osc, harmonicityRatio, modulationIndex, wave = 'sine') => {
return mod(modfreq, modgain, wave); return mod(modfreq, modgain, wave);
}; };
export function registerSynthSounds() { export function registerSynthSounds() {
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => { ['sine', 'square', 'triangle',
registerSound( 'sawtooth', 'pink', 'white',
wave, 'brown'].forEach((wave) => {
(t, value, onended) => { registerSound(
// destructure adsr here, because the default should be different for synths and samples wave,
let { (t, value, onended) => {
attack = 0.001, // destructure adsr here, because the default should be different for synths and samples
decay = 0.05, let {
sustain = 0.6, attack = 0.001,
release = 0.01, decay = 0.05,
fmh: fmHarmonicity = 1, sustain = 0.6,
fmi: fmModulationIndex, release = 0.01,
fmenv: fmEnvelopeType = 'lin', fmh: fmHarmonicity = 1,
fmattack: fmAttack, fmi: fmModulationIndex,
fmdecay: fmDecay, fmenv: fmEnvelopeType = 'lin',
fmsustain: fmSustain, fmattack: fmAttack,
fmrelease: fmRelease, fmdecay: fmDecay,
fmvelocity: fmVelocity, fmsustain: fmSustain,
fmwave: fmWaveform = 'sine', fmrelease: fmRelease,
vib = 0, fmvelocity: fmVelocity,
vibmod = 0.5, fmwave: fmWaveform = 'sine',
} = value; vib = 0,
let { n, note, freq } = value; vibmod = 0.5,
// with synths, n and note are the same thing } = value;
note = note || 36; let { n, note, freq } = value;
if (typeof note === 'string') { // with synths, n and note are the same thing
note = noteToMidi(note); // e.g. c3 => 48 note = note || 36;
} if (typeof note === 'string') {
// get frequency note = noteToMidi(note); // e.g. c3 => 48
if (!freq && typeof note === 'number') { }
freq = midiToFreq(note); // + 48); // get frequency
} if (!freq && typeof note === 'number') {
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default) freq = midiToFreq(note); // + 48);
// make oscillator }
const { node: o, stop } = getOscillator({ // maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
t, // make oscillator
s: wave, const { node: o, stop } = getOscillator({
freq, t,
vib, s: wave,
vibmod, freq,
partials: n, vib,
}); vibmod,
partials: n,
// FM + FM envelope });
let stopFm, fmEnvelope; // FM + FM envelope
if (fmModulationIndex) { let stopFm, fmEnvelope;
const { node: modulator, stop } = fm(o, fmHarmonicity, fmModulationIndex, fmWaveform); if (fmModulationIndex) {
if (![fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity].find((v) => v !== undefined)) { const { node: modulator, stop } = fm(o, fmHarmonicity, fmModulationIndex, fmWaveform);
// no envelope by default if (![fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity].find((v) => v !== undefined)) {
modulator.connect(o.frequency); // no envelope by default
} else { modulator.connect(o.frequency);
fmAttack = fmAttack ?? 0.001; } else {
fmDecay = fmDecay ?? 0.001; fmAttack = fmAttack ?? 0.001;
fmSustain = fmSustain ?? 1; fmDecay = fmDecay ?? 0.001;
fmRelease = fmRelease ?? 0.001; fmSustain = fmSustain ?? 1;
fmVelocity = fmVelocity ?? 1; fmRelease = fmRelease ?? 0.001;
fmEnvelope = getEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t); fmVelocity = fmVelocity ?? 1;
if (fmEnvelopeType === 'exp') { fmEnvelope = getEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
fmEnvelope = getExpEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t); if (fmEnvelopeType === 'exp') {
fmEnvelope.node.maxValue = fmModulationIndex * 2; fmEnvelope = getExpEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
fmEnvelope.node.minValue = 0.00001; fmEnvelope.node.maxValue = fmModulationIndex * 2;
} fmEnvelope.node.minValue = 0.00001;
modulator.connect(fmEnvelope.node); }
fmEnvelope.node.connect(o.frequency); modulator.connect(fmEnvelope.node);
fmEnvelope.node.connect(o.frequency);
}
stopFm = stop;
} }
stopFm = stop;
}
// turn down // turn down
const g = gainNode(0.3); const g = gainNode(0.3);
// gain envelope // gain envelope
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t); const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t);
o.onended = () => { o.onended = () => {
o.disconnect(); o.disconnect();
g.disconnect(); g.disconnect();
onended(); onended();
}; };
return { return {
node: o.connect(g).connect(envelope), node: o.connect(g).connect(envelope),
stop: (releaseTime) => { stop: (releaseTime) => {
releaseEnvelope(releaseTime); releaseEnvelope(releaseTime);
fmEnvelope?.stop(releaseTime); fmEnvelope?.stop(releaseTime);
let end = releaseTime + release; let end = releaseTime + release;
stop(end); stop(end);
stopFm?.(end); stopFm?.(end);
}, },
}; };
}, },
{ type: 'synth', prebake: true }, { type: 'synth', prebake: true },
); );
}); });
} }
export function waveformN(partials, type) { export function waveformN(partials, type) {
@ -146,36 +148,86 @@ export function waveformN(partials, type) {
return osc; return osc;
} }
export function getOscillator({ s, freq, t, vib, vibmod, partials }) { export function getNoiseOscillator({ t, ac, type = 'white' }) {
// Make oscillator with partial count const bufferSize = 2 * ac.sampleRate;
let o; const noiseBuffer = ac.createBuffer(1, bufferSize, ac.sampleRate);
if (!partials || s === 'sine') { const output = noiseBuffer.getChannelData(0);
o = getAudioContext().createOscillator(); let lastOut = 0;
o.type = s || 'triangle'; let b0, b1, b2, b3, b4, b5, b6;
} else { b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;
o = waveformN(partials, s);
}
o.frequency.value = Number(freq);
o.start(t);
// Additional oscillator for vibrato effect for (let i = 0; i < bufferSize; i++) {
let vibrato_oscillator; if (type === 'white') {
if (vib > 0) { output[i] = Math.random() * 2 - 1;
vibrato_oscillator = getAudioContext().createOscillator(); } else if (type === 'brown') {
vibrato_oscillator.frequency.value = vib; let white = Math.random() * 2 - 1;
const gain = getAudioContext().createGain(); output[i] = (lastOut + (0.02 * white)) / 1.02;
// Vibmod is the amount of vibrato, in semitones lastOut = output[i];
gain.gain.value = vibmod * 100; } else if (type === 'pink') {
vibrato_oscillator.connect(gain); let white = Math.random() * 2 - 1;
gain.connect(o.detune); b0 = 0.99886 * b0 + white * 0.0555179;
vibrato_oscillator.start(t); b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.96900 * b2 + white * 0.1538520;
b3 = 0.86650 * b3 + white * 0.3104856;
b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980;
output[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
output[i] *= 0.11;
b6 = white * 0.115926;
}
} }
const o = ac.createBufferSource();
o.buffer = noiseBuffer;
o.loop = true;
o.start(t);
return { return {
node: o, node: o,
stop: (time) => { stop: (time) => o.stop(time)
vibrato_oscillator?.stop(time);
o.stop(time);
},
}; };
} }
export function getOscillator({ s, freq, t, vib, vibmod, partials }) {
// Make oscillator with partial count
let o;
if (['pink', 'white', 'brown'].includes(s)) {
let noiseOscillator = getNoiseOscillator({ t: t, ac: getAudioContext(), type: s })
return {
node: noiseOscillator.node,
stop: noiseOscillator.stop
}
} else {
if (!partials || s === 'sine') {
o = getAudioContext().createOscillator();
o.type = s || 'triangle';
} else {
o = waveformN(partials, s);
}
o.frequency.value = Number(freq);
o.start(t);
// Additional oscillator for vibrato effect
let vibrato_oscillator;
if (vib > 0) {
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 * 100;
vibrato_oscillator.connect(gain);
gain.connect(o.detune);
vibrato_oscillator.start(t);
}
return {
node: o,
stop: (time) => {
vibrato_oscillator?.stop(time);
o.stop(time);
},
};
}
}