This commit is contained in:
Jade (Rose) Rowland 2024-02-29 22:55:56 -05:00
parent 1690030b1d
commit a6f7c57d19

View file

@ -21,53 +21,55 @@ const fm = (osc, harmonicityRatio, modulationIndex, wave = 'sine') => {
return mod(modfreq, modgain, wave); return mod(modfreq, modgain, wave);
}; };
const waveforms = ['sine', 'square', 'triangle', 'sawtooth']; const waveforms = ['triangle', 'square', 'sawtooth', 'sine'];
const noises = ['pink', 'white', 'brown', 'crackle']; const noises = ['pink', 'white', 'brown', 'crackle'];
export function registerSynthSounds() { export function registerSynthSounds() {
registerSound('bet', (begin, value, onended) => { [...waveforms].forEach((s, i) => {
const ac = getAudioContext(); registerSound(s, (begin, value, onended) => {
let { note, freq, duration } = value; const ac = getAudioContext();
note = note || 36; let { note, freq, duration } = value;
if (typeof note === 'string') { note = note || 36;
note = noteToMidi(note); // e.g. c3 => 48 if (typeof note === 'string') {
} note = noteToMidi(note); // e.g. c3 => 48
// get frequency }
if (!freq && typeof note === 'number') { // get frequency
freq = midiToFreq(note); // + 48); if (!freq && typeof note === 'number') {
} freq = midiToFreq(note); // + 48);
}
// set frequency // set frequency
freq = Number(freq); freq = Number(freq);
const [attack, decay, sustain, release] = getADSRValues( const [attack, decay, sustain, release] = getADSRValues(
[value.attack, value.decay, value.sustain, value.release], [value.attack, value.decay, value.sustain, value.release],
'linear', 'linear',
[0.001, 0.05, 0.6, 0.01], [0.001, 0.05, 0.6, 0.01],
); );
const holdend = begin + duration; const holdend = begin + duration;
const end = holdend + release + 0.01; const end = holdend + release + 0.01;
let node = getWorklet(ac, 'better-oscillator', { frequency: freq, begin, end }); let node = getWorklet(ac, 'better-oscillator', { frequency: freq, wave: i, begin, end });
const envGain = gainNode(1); const envGain = gainNode(1);
node = node.connect(envGain); node = node.connect(envGain);
getParamADSR(node.gain, attack, decay, sustain, release, 0, 1, begin, holdend, 'exponential'); getParamADSR(node.gain, attack, decay, sustain, release, 0, 0.3, begin, holdend, 'linear');
return { return {
node, node,
stop: (time) => { stop: (time) => {
// o.stop(time); // o.stop(time);
}, },
triggerRelease: (time) => { triggerRelease: (time) => {
// envGain?.stop(time); // envGain?.stop(time);
}, },
}; };
});
}); });
[...waveforms, ...noises].forEach((s) => { [...noises].forEach((s) => {
registerSound( registerSound(
s, s,
(t, value, onended) => { (t, value, onended) => {
@ -78,12 +80,9 @@ export function registerSynthSounds() {
); );
let sound; let sound;
if (waveforms.includes(s)) {
sound = getOscillator(s, t, value); let { density } = value;
} else { sound = getNoiseOscillator(s, t, density);
let { density } = value;
sound = getNoiseOscillator(s, t, density);
}
let { node: o, stop, triggerRelease } = sound; let { node: o, stop, triggerRelease } = sound;