Files
client/app/renderer/js/audio/capture-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

43 lines
1.3 KiB
JavaScript

// Microphone → 20 ms frames of 960 signed 16-bit mono samples at 48 kHz (bridge §4.9).
const FRAME = 960;
class CaptureProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.frame = new Int16Array(FRAME);
this.pos = 0;
this.sumSq = 0;
this.peak = 0;
if (sampleRate !== 48000) this.port.postMessage({ type: 'error', message: `sample rate ${sampleRate}` });
}
process(inputs, outputs) {
const input = inputs[0];
const out = outputs[0] && outputs[0][0];
if (out) out.fill(0);
if (!input || input.length === 0) return true;
const ch = input[0];
for (let i = 0; i < ch.length; i += 1) {
let s = ch[i];
if (s > 1) s = 1;
else if (s < -1) s = -1;
this.frame[this.pos] = s < 0 ? Math.round(s * 0x8000) : Math.round(s * 0x7fff);
this.pos += 1;
this.sumSq += s * s;
const a = s < 0 ? -s : s;
if (a > this.peak) this.peak = a;
if (this.pos === FRAME) {
const pcm = this.frame.buffer;
this.port.postMessage({ type: 'frame', pcm, rms: Math.sqrt(this.sumSq / FRAME), peak: this.peak }, [pcm]);
this.frame = new Int16Array(FRAME);
this.pos = 0;
this.sumSq = 0;
this.peak = 0;
}
}
return true;
}
}
registerProcessor('tp-capture', CaptureProcessor);