/* Application PCM16LE/24kHz contract. The context must actually run at 24kHz. */ class Capture extends AudioWorkletProcessor { constructor() { super(); this.samples = []; this.active = true; this.pending = 0; this.pendingBytes = 0; this.port.onmessage = e => { if (e.data.type === 'stop') this.active = false; else if (e.data.type === 'accepted') { this.pending = Math.max(0, this.pending - 1); this.pendingBytes = Math.max(0, this.pendingBytes - (e.data.bytes || 480)); } }; } process(inputs) { if (!this.active) return false; const channels = inputs[0]; if (!channels?.length) return true; for (let i = 0; i < channels[0].length; i++) { let mono = 0; for (const channel of channels) mono += channel[i] / channels.length; this.samples.push(Math.max(-32768, Math.min(32767, Math.round(mono * 32768)))); if (this.samples.length === 240) { if (this.pending >= 100 || this.pendingBytes + 480 > 96000) { this.port.postMessage({type: 'overflow'}); this.active = false; return false; } this.pending++; this.pendingBytes += 480; const bytes = new ArrayBuffer(480), view = new DataView(bytes); this.samples.forEach((value, index) => view.setInt16(index * 2, value, true)); this.samples.length = 0; this.port.postMessage({type: 'pcm', bytes}, [bytes]); } } return true; } } class Playback extends AudioWorkletProcessor { constructor() { super(); this.queue = []; this.refs = new Map(); this.queued = 0; this.port.onmessage = ({data}) => { if (data.type === 'start') { if (!this.refs.has(data.ref)) this.refs.set(data.ref, {played: 0, sealed: false}); } else if (data.type === 'pcm') { const length = data.samples.byteLength / 4; if (!Number.isInteger(length) || !length || !this.refs.has(data.ref) || this.queue.length >= 100 || this.queued + length > 48000) { this.port.postMessage({type: 'overflow'}); return; } const samples = new Float32Array(data.samples); this.queue.push({ref: data.ref, id: data.packet_id, samples, position: 0}); this.queued += samples.length; } else if (data.type === 'end') { const ref = this.refs.get(data.ref); if (ref) {ref.sealed = true; this.report(data.ref);} } else if (data.type === 'flush') { const released = this.queue.map(part => part.id); this.queue.length = 0; this.queued = 0; for (const [ref] of this.refs) this.report(ref); this.refs.clear(); this.port.postMessage({type: 'flushed', id: data.id, released}); } }; } report(ref) { const state = this.refs.get(ref); if (!state) return; this.port.postMessage({type: 'progress', ref, played_ms: Math.floor(state.played / 24)}); if (state.sealed && !this.queue.some(part => part.ref === ref)) this.refs.delete(ref); } process(inputs, outputs) { const output = outputs[0]?.[0]; if (!output) return true; output.fill(0); const touched = new Set(), consumed = new Map(); for (let i = 0; i < output.length && this.queue.length; i++) { const part = this.queue[0], state = this.refs.get(part.ref); output[i] = part.samples[part.position++]; this.queued--; state.played++; touched.add(part.ref); const update = consumed.get(part.id) || {type: 'consumed', packet_id: part.id, samples: 0, complete: false}; update.samples = part.position; consumed.set(part.id, update); if (part.position === part.samples.length) {this.queue.shift(); update.complete = true;} } for (const update of consumed.values()) this.port.postMessage(update); // Reports count only samples actually consumed by this output process callback. for (const ref of touched) this.report(ref); return true; } } registerProcessor('rag-capture', Capture); registerProcessor('rag-playback', Playback);