/** * Streaming PCM16 playback (contract: 24 kHz mono PCM16LE output, no WAV header). * * Bounded playout queue: at most BOUND_SAMPLES (96,000 bytes) of source audio is admitted. * When it fills, the processor reports `overflow` and the call must fail safely — PCM is * never silently dropped to keep the same context alive. * * Real playback progress: `played_ms` is computed from the source samples actually * consumed by `process()` per audio_ref (never an elapsed timer), kept cumulative per ref * and reported roughly 4×/s, on ref changes, and on clear/flush/idle — so the main thread * can send the contract's `playback_progress` control message. * * Messages in: { type: 'push', pcm: ArrayBuffer (Int16 mono), sampleRate, audioRef } * { type: 'clear' } barge-in: drop everything immediately * { type: 'flush' } end of turn: play what is left, then report 'idle' * Messages out: { type: 'progress', audioRef, playedMs } * { type: 'idle' } buffer ran dry after playing * { type: 'overflow' } bounded queue filled — caller fails the session safely */ const BOUND_BYTES = 96000; // contract cap for the playback queue const REPORT_INTERVAL_MS = 250; class PcmPlaybackProcessor extends AudioWorkletProcessor { constructor() { super(); this.capacity = Math.floor(BOUND_BYTES / 2); // source samples (PCM16) this.buf = new Float32Array(this.capacity); this.r = 0; this.w = 0; this.count = 0; this.prebuffer = Math.floor(sampleRate * 0.08); // jitter buffer before (re)starting this.started = false; this.prev = 0; this.pos = 1; this.ratio = 1; // source samples per output sample, set per push this.currentRef = null; this.refPlayedSamples = new Map(); // cumulative consumed source samples per audio_ref this.unreportedSamples = 0; this.overflowPosted = false; this.port.onmessage = (e) => { const m = e.data; if (!m) return; if (m.type === 'push') this.push(new Int16Array(m.pcm), m.sampleRate, m.audioRef || null); else if (m.type === 'clear') this.clear(); else if (m.type === 'flush') this.flush(); }; } clear() { this.r = this.w = this.count = 0; this.started = false; this.prev = 0; this.pos = 1; this.reportProgress(true); // final report for the interrupted ref } flush() { if (this.count === 0) { this.started = false; this.reportProgress(true); this.port.postMessage({ type: 'idle' }); } else { this.started = true; // short tail: do not wait for the prebuffer } } push(int16, srcRate, audioRef) { const n = int16.length; if (!n) return; // Bounded queue: admitting this chunk past the cap is a safe failure, not a silent drop. if (this.count + n > this.capacity) { if (!this.overflowPosted) { this.overflowPosted = true; this.port.postMessage({ type: 'overflow' }); } return; } this.ratio = srcRate / sampleRate; // source samples per output sample const src = new Float32Array(n + 1); src[0] = this.prev; for (let i = 0; i < n; i++) src[i + 1] = int16[i] / 32768; let pos = this.pos; while (pos < n) { const i = Math.floor(pos); const frac = pos - i; this.write(src[i] * (1 - frac) + src[i + 1] * frac); pos += this.ratio; } this.prev = src[n]; this.pos = pos - n; if (audioRef !== this.currentRef) { this.reportProgress(true); // final report for the ref that just ended this.currentRef = audioRef; this.reportProgress(true); } } write(v) { this.buf[this.w] = v; this.w = (this.w + 1) % this.capacity; this.count++; } process(inputs, outputs) { const out = outputs[0]; const ch0 = out[0]; if (!this.started && this.count >= this.prebuffer) this.started = true; if (this.started) { let played = 0; for (let i = 0; i < ch0.length; i++) { if (this.count > 0) { ch0[i] = this.buf[this.r]; this.r = (this.r + 1) % this.capacity; this.count--; played++; } else { ch0[i] = 0; } } if (this.currentRef && played > 0) { // Convert consumed output samples back to contract (source) samples. const consumed = played * this.ratio; const ref = this.currentRef; this.refPlayedSamples.set(ref, (this.refPlayedSamples.get(ref) ?? 0) + consumed); this.unreportedSamples += consumed; const srcRate = this.ratio * sampleRate; if ((this.unreportedSamples / srcRate) * 1000 >= REPORT_INTERVAL_MS) { this.reportProgress(false); } } if (this.count === 0) { this.started = false; this.reportProgress(true); this.port.postMessage({ type: 'idle' }); } } else { ch0.fill(0); } for (let c = 1; c < out.length; c++) out[c].set(ch0); return true; } reportProgress(force) { this.unreportedSamples = 0; const ref = this.currentRef; if (!ref) return; const srcRate = this.ratio * sampleRate; const playedSamples = this.refPlayedSamples.get(ref) ?? 0; const playedMs = Math.max(0, Math.round((playedSamples / srcRate) * 1000)); if (playedMs <= 0) return; // nothing has actually been heard for this ref yet this.port.postMessage({ type: 'progress', audioRef: ref, playedMs }); } } registerProcessor('pcm-playback', PcmPlaybackProcessor);