From c2df8aa6b9d5bfb9b5358cf6c535b8b8e8125d87 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 4 Mar 2024 00:43:03 -0500 Subject: [PATCH] cleaning --- packages/superdough/synth.mjs | 48 ++++++++++++++++++++++++++++++-- packages/superdough/worklets.mjs | 7 +++-- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/packages/superdough/synth.mjs b/packages/superdough/synth.mjs index e3362cec..a0865103 100644 --- a/packages/superdough/synth.mjs +++ b/packages/superdough/synth.mjs @@ -25,11 +25,51 @@ const waveforms = ['triangle', 'square', 'sawtooth', 'sine']; const noises = ['pink', 'white', 'brown', 'crackle']; export function registerSynthSounds() { + [...waveforms].forEach((s) => { + registerSound( + s, + (t, value, onended) => { + const [attack, decay, sustain, release] = getADSRValues( + [value.attack, value.decay, value.sustain, value.release], + 'linear', + [0.001, 0.05, 0.6, 0.01], + ); + + let sound; + sound = getOscillator(s, t, value); + let { node: o, stop, triggerRelease } = sound; + + // turn down + const g = gainNode(0.3); + + const { duration } = value; + + o.onended = () => { + o.disconnect(); + g.disconnect(); + onended(); + }; + + const envGain = gainNode(1); + let node = o.connect(g).connect(envGain); + const holdEnd = t + duration; + getParamADSR(node.gain, attack, decay, sustain, release, 0, 1, t, holdEnd, 'linear'); + const envEnd = holdEnd + release + 0.01; + triggerRelease?.(envEnd); + stop(envEnd); + return { + node, + stop: (releaseTime) => {}, + }; + }, + { type: 'synth', prebake: true }, + ); + }); registerSound( 'supersaw', (begin, value, onended) => { const ac = getAudioContext(); - let { note, freq, duration } = value; + let { note, freq, duration, n = 2 } = value; note = note || 36; if (typeof note === 'string') { note = noteToMidi(note); // e.g. c3 => 48 @@ -58,7 +98,9 @@ export function registerSynthSounds() { frequency: freq, begin, end, - detune: 0, + detune: Math.min(200, Math.max(0, n * 0.1)), + // voices: n, + // spread: 0, }, { outputChannelCount: [2], @@ -68,7 +110,7 @@ export function registerSynthSounds() { const envGain = gainNode(1); node = node.connect(envGain); - getParamADSR(node.gain, attack, decay, sustain, release, 0, 0.1, begin, holdend, 'linear'); + getParamADSR(node.gain, attack, decay, sustain, release, 0, 0.3, begin, holdend, 'linear'); return { node, diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 00a3be94..add1f607 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -170,7 +170,7 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor { { name: 'spread', - defaultValue: 0.6, + defaultValue: 0.4, min: 0, max: 1, }, @@ -202,6 +202,7 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor { const detune = params.detune[0]; let spread = params.spread[0]; spread = spread * 0.5 + 0.5; + const gainAdjustment = Math.max(0.75, 1 - (voices - 1) * 0.05); for (let n = 0; n < voices; n++) { let adj = 0; @@ -217,8 +218,8 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor { this.phase[n] = this.phase[n] ?? Math.random(); const v = saw(this.phase[n], dt); - output[0][i] = output[0][i] + v * (1 - balance); - output[1][i] = output[1][i] + v * balance; + output[0][i] = (output[0][i] + v * (1 - balance)) * gainAdjustment; + output[1][i] = (output[1][i] + v * balance) * gainAdjustment; this.phase[n] += dt;