// One convolution reverb shared by several instruments (instead of one per instrument, which is // costly on phones). The impulse response is the same kind modal-synth's effects use: decaying // noise whose tail darkens over time. export class SharedReverb { /** send instruments' wet signal here */ 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() { const i = this.input, c = this.conv; window.setTimeout(() => { i.disconnect(); c.disconnect(); }, 1500); } } 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; }