1// AudioWorklet that plays audio rendered elsewhere (render workers), on the audio clock.
2// Chunks carry absolute frame numbers; the player outputs whatever covers the current frames
3// (silence if nothing has arrived: an underrun) and reports how far it has played, so the
4// renderer can stay a fixed distance ahead. An underrun fades out from the last sample instead of
5// cutting to silence, and the audio after it fades back in, so a gap is a soft dip, not a click.
7import type { Chunk, Consumed } from "./protocol.ts";
9declare const currentFrame: number;
10declare const sampleRate: number;
11declare class AudioWorkletProcessor {
12 readonly port: MessagePort;
13 constructor();
14}
15declare function registerProcessor(name: string, ctor: new () => AudioWorkletProcessor): void;
17const FADE = 256; // samples to fade back in after a gap
18const TAIL = 0.985; // per-sample decay of the last sample during a gap (≈ 5 ms to −40 dB)
20class PcmPlayer extends AudioWorkletProcessor {
21 private queue: Chunk[] = [];
22 private src: MessagePort | null = null;
23 private underruns = 0;
24 private started = false;
25 private lastReport = 0;
26 private tailL = 0; // what a gap fades out from (the last sample played, decaying)
27 private tailR = 0;
28 private fadeIn = FADE; // samples since audio resumed after a gap (FADE = fully in)
30 constructor() {
31 super();
32 this.port.onmessage = (e: MessageEvent<{ type: "port"; port: MessagePort }>) => {
33 if (e.data?.type !== "port") return;
34 this.src = e.data.port;
35 this.src.onmessage = (ev: MessageEvent<Chunk>) => this.queue.push(ev.data);
36 };
37 }
39 process(_in: Float32Array[][], outputs: Float32Array[][]): boolean {
40 const out = outputs[0];
41 const L = out[0], R = out[1] ?? out[0];
42 const n = L.length;
43 const F = currentFrame;
44 const q = this.queue;
45 while (q.length && q[0].frame + q[0].L.length <= F) q.shift();
46 let i = 0;
47 while (i < n) {
48 const f = F + i;
49 const c = q.find((x) => x.frame <= f && f < x.frame + x.L.length);
50 if (!c) {
51 // nothing for this frame: silence (counts as an underrun once audio has started)
52 const next = q.find((x) => x.frame > f);
53 const end = next ? Math.min(n, next.frame - F) : n;
54 if (this.started) this.underruns++;
55 const stop = Math.max(end, i + 1);
56 for (; i < stop; i++) {
57 this.tailL *= TAIL;
58 this.tailR *= TAIL;
59 L[i] = this.tailL;
60 if (R !== L) R[i] = this.tailR;
61 }
62 if (this.started) this.fadeIn = 0;
63 continue;
64 }
65 this.started = true;
66 const off = f - c.frame;
67 const len = Math.min(n - i, c.L.length - off);
68 if (this.fadeIn >= FADE) {
69 L.set(c.L.subarray(off, off + len), i);
70 if (R !== L) R.set(c.R.subarray(off, off + len), i);
71 } else {
72 // crossfade from the decaying tail into the resumed audio
73 for (let j = 0; j < len; j++) {
74 const g = Math.min(1, this.fadeIn++ / FADE);
75 this.tailL *= TAIL;
76 this.tailR *= TAIL;
77 L[i + j] = g * c.L[off + j] + (1 - g) * this.tailL;
78 if (R !== L) R[i + j] = g * c.R[off + j] + (1 - g) * this.tailR;
79 }
80 }
81 i += len;
82 this.tailL = L[i - 1];
83 this.tailR = R[i - 1];
84 }
85 if (this.src && F + n - this.lastReport >= sampleRate / 50) {
86 this.lastReport = F + n;
87 const m: Consumed = { consumed: F + n, underruns: this.underruns };
88 this.src.postMessage(m);
89 }
90 return true;
91 }
92}
94registerProcessor("pcm-player", PcmPlayer);