From 853e024bf7f98cc5783c592bfe3ffb0f5ec63ad8 Mon Sep 17 00:00:00 2001 From: Oliver Bryan Date: Sun, 25 Jan 2026 09:54:02 +0000 Subject: [PATCH] synth setup with tone.js --- src/lib/audio/synth.ts | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/lib/audio/synth.ts diff --git a/src/lib/audio/synth.ts b/src/lib/audio/synth.ts new file mode 100644 index 0000000..954b8e2 --- /dev/null +++ b/src/lib/audio/synth.ts @@ -0,0 +1,47 @@ +import * as Tone from "tone"; + +export type SynthNodes = { + oscillatorA: Tone.Oscillator; + oscillatorB: Tone.Oscillator; + crossFade: Tone.CrossFade; + noise: Tone.Noise; + gain: Tone.Gain; +}; + +export function createSynth(): SynthNodes { + const oscillatorA = new Tone.Oscillator({ type: "sawtooth" }); + const oscillatorB = new Tone.Oscillator({ type: "sine" }); + const crossFade = new Tone.CrossFade(0); + const noise = new Tone.Noise({ type: "white" }); + const gain = new Tone.Gain(0.5); + + oscillatorA.connect(crossFade.a); + oscillatorB.connect(crossFade.b); + crossFade.connect(gain); + noise.connect(gain); + gain.toDestination(); + + oscillatorA.start(); + oscillatorB.start(); + noise.start(); + + return { + oscillatorA, + oscillatorB, + crossFade, + noise, + gain, + }; +} + +export function disposeSynth(nodes: SynthNodes) { + nodes.oscillatorA.stop(); + nodes.oscillatorB.stop(); + nodes.noise.stop(); + + nodes.oscillatorA.dispose(); + nodes.oscillatorB.dispose(); + nodes.crossFade.dispose(); + nodes.noise.dispose(); + nodes.gain.dispose(); +}