/ music / acoustic-base
Sign in
music / acoustic-base
acoustic-base / src / app / synth.ts
74 lines · 3.0 KBBlameHistoryRaw
1// Audio: an AudioContext shared by the rendered-buffer playback (the simulation, the reference
2// renders) and a spessasynth_lib worklet synth for playing SoundFonts live from the keyboard and
3// MIDI (the SF2 built here, or the reference SF2).
4import { WorkletSynthesizer } from "spessasynth_lib";
5import processorUrl from "spessasynth_lib/dist/spessasynth_processor.min.js?url";
7export class Audio {
8 ctx: AudioContext | null = null;
9 synth: WorkletSynthesizer | null = null;
10 private master: GainNode | null = null;
11 private starting: Promise<void> | null = null;
12 private bankKey = "";
13 private current: AudioBufferSourceNode | null = null;
14 volume = 0.8;
16 start(): Promise<void> {
17 if (this.ctx && this.ctx.state === "suspended") void this.ctx.resume();
18 if (!this.starting) {
19 this.starting = (async () => {
20 const ctx = new AudioContext({ latencyHint: "interactive" });
21 this.ctx = ctx;
22 void ctx.resume();
23 this.master = ctx.createGain();
24 this.master.gain.value = this.volume;
25 this.master.connect(ctx.destination);
26 await ctx.audioWorklet.addModule(processorUrl);
27 const synth = new WorkletSynthesizer(ctx);
28 synth.connect(this.master);
29 await synth.isReady;
30 this.synth = synth;
31 })();
32 }
33 return this.starting;
34 }
36 setVolume(v: number) { this.volume = v; if (this.master) this.master.gain.value = v; }
38 /** play a mono buffer (stops the previous one); resolves when it ends */
39 async play(x: Float32Array, fs: number, gain = 1): Promise<void> {
40 await this.start();
41 this.stop();
42 const ctx = this.ctx!;
43 const buf = ctx.createBuffer(1, Math.max(1, x.length), fs);
44 const y = new Float32Array(x.length);
45 for (let i = 0; i < x.length; i++) y[i] = x[i] * gain;
46 buf.copyToChannel(y, 0);
47 const src = ctx.createBufferSource();
48 src.buffer = buf;
49 src.connect(this.master!);
50 this.current = src;
51 return new Promise((res) => { src.onended = () => { if (this.current === src) this.current = null; res(); }; src.start(); });
52 }
54 stop() { try { this.current?.stop(); } catch { /* already stopped */ } this.current = null; }
56 /** load a SoundFont into the live synth (key: to avoid reloading the same one) and select a preset */
57 async loadBank(key: string, data: ArrayBuffer, bank: number, program: number): Promise<void> {
58 await this.start();
59 const s = this.synth!;
60 if (this.bankKey !== key) {
61 s.stopAll(true);
62 await s.soundBankManager.addSoundBank(data.slice(0), "main");
63 this.bankKey = key;
64 }
65 s.controllerChange(0, 0 as never, bank & 0x7f);
66 s.controllerChange(0, 32 as never, 0);
67 s.programChange(0, program);
68 s.controllerChange(0, 91 as never, 30);
69 }
71 noteOn(note: number, vel: number) { this.synth?.noteOn(0, note, Math.max(1, Math.min(127, Math.round(vel)))); }
72 noteOff(note: number) { this.synth?.noteOff(0, note); }
73 sustain(on: boolean) { this.synth?.controllerChange(0, 64 as never, on ? 127 : 0); }
↑↓ move↵openescclose