// Effects after the rendered audio, on the page (native Web Audio nodes: cheap and steady): // a sound's own modal-synth effects, or dry plus a send to one reverb shared by several outputs. import { FX } from "@modal-synth/audio/fx.ts"; import type { Params } from "@modal-synth/engine/params.ts"; export function routeEffects(ctx: AudioContext, from: AudioNode, out: AudioNode, sound: Params | undefined, effects: "own" | "none" | { send: AudioNode }) { if (!sound) { from.connect(out); return; } if (effects === "own") { const fx = new FX(ctx, out); from.connect(fx.input); fx.set(sound); return; } const vol = ctx.createGain(); vol.gain.value = Math.pow(10, (sound.volume as number) / 20); from.connect(vol).connect(out); if (effects !== "none") { const send = ctx.createGain(); send.gain.value = (sound.reverbMix as number) * vol.gain.value; from.connect(send).connect(effects.send); } } /** One convolution reverb shared by several outputs (decaying noise whose tail darkens). */ export class SharedReverb { input: GainNode; private conv: ConvolverNode; constructor(ctx: AudioContext, out: AudioNode, T60: number) { this.input = ctx.createGain(); this.conv = ctx.createConvolver(); this.conv.buffer = makeIR(ctx, T60); this.input.connect(this.conv).connect(out); } dispose() { this.input.disconnect(); this.conv.disconnect(); } } function makeIR(ctx: BaseAudioContext, T60: number): AudioBuffer { const sr = ctx.sampleRate; const n = Math.round(sr * Math.min(T60 * 1.1, 12)); const buf = ctx.createBuffer(2, n, sr); for (let ch = 0; ch < 2; ch++) { const d = buf.getChannelData(ch); let lp = 0; for (let i = 0; i < n; i++) { const t = i / sr; const fc = 9000 * Math.exp(-t / (0.6 * T60)) + 400; const a = 1 - Math.exp((-2 * Math.PI * fc) / sr); lp += a * (Math.random() * 2 - 1 - lp); d[i] = lp * Math.exp((-6.9 * t) / T60) * Math.min(1, t / 0.004) * 0.5; } } return buf; }