/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / core / audio.ts
31 lines · 919 BBlameHistoryRaw
1// One shared AudioContext for the whole album, created on the first user gesture.
3export interface AudioEngine {
4 ctx: AudioContext;
5 /** Compositions connect here. */
6 input: AudioNode;
7}
9let engine: AudioEngine | null = null;
11export async function getAudio(): Promise<AudioEngine> {
12 if (!engine) {
13 const ctx = new AudioContext({ latencyHint: "playback" });
14 const input = ctx.createGain();
15 // gentle safety limiter on the master bus
16 const limiter = ctx.createDynamicsCompressor();
17 limiter.threshold.value = -6;
18 limiter.knee.value = 6;
19 limiter.ratio.value = 12;
20 limiter.attack.value = 0.003;
21 limiter.release.value = 0.25;
22 input.connect(limiter).connect(ctx.destination);
23 engine = { ctx, input };
24 }
25 if (engine.ctx.state !== "running") await engine.ctx.resume();
26 return engine;
29export function dbToGain(db: number): number {
30 return Math.pow(10, db / 20);
↑↓ move↵openescclose