mirror of
https://github.com/hex248/tsos.git
synced 2026-02-07 18:23:05 +00:00
synth setup with tone.js
This commit is contained in:
47
src/lib/audio/synth.ts
Normal file
47
src/lib/audio/synth.ts
Normal file
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user