Files
client/app/renderer/js/audio/playback-worklet.js
T
omar 7287fdb63e
ci / linux-x64 (push) Failing after 1m26s
ci / linux-arm64-cross (push) Failing after 2m20s
TeamsPeach desktop client: Electron UI, Rust core bridge, TS6 stream viewer, CI
2026-10-24 03:31:54 +03:00

79 lines
2.0 KiB
JavaScript

// Mixed voice from the core → speakers. Bounded ring: 200 ms capacity (oldest dropped),
// 40 ms prebuffer after every underrun, silence while empty (bridge §5 `audio`).
const CAPACITY = 9600;
const PREBUFFER = 1920;
class PlaybackProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.ring = new Float32Array(CAPACITY);
this.read = 0;
this.write = 0;
this.size = 0;
this.playing = false;
this.framesIn = 0;
this.dropped = 0;
this.underruns = 0;
this.port.onmessage = (event) => {
const data = event.data;
if (data instanceof ArrayBuffer) {
this.push(new Int16Array(data));
} else if (data && data.type === 'flush') {
this.read = 0;
this.write = 0;
this.size = 0;
this.playing = false;
} else if (data && data.type === 'stats') {
this.port.postMessage({
type: 'stats',
framesIn: this.framesIn,
dropped: this.dropped,
underruns: this.underruns,
bufferedMs: Math.round((this.size / 48000) * 1000),
});
}
};
}
push(pcm) {
this.framesIn += 1;
for (let i = 0; i < pcm.length; i += 1) {
if (this.size === CAPACITY) {
this.read = (this.read + 1) % CAPACITY;
this.size -= 1;
this.dropped += 1;
}
this.ring[this.write] = pcm[i] / 32768;
this.write = (this.write + 1) % CAPACITY;
this.size += 1;
}
}
process(_inputs, outputs) {
const out = outputs[0][0];
if (!this.playing) {
if (this.size < PREBUFFER) {
out.fill(0);
return true;
}
this.playing = true;
}
for (let i = 0; i < out.length; i += 1) {
if (this.size > 0) {
out[i] = this.ring[this.read];
this.read = (this.read + 1) % CAPACITY;
this.size -= 1;
} else {
out[i] = 0;
if (this.playing) {
this.playing = false;
this.underruns += 1;
}
}
}
return true;
}
}
registerProcessor('tp-playback', PlaybackProcessor);