Files
alice-backend/server/public/room-mic-worklet.js
T

33 lines
830 B
JavaScript

class RoomMic extends AudioWorkletProcessor {
constructor() {
super();
this.remaining = sampleRate * 15;
this.block = new Float32Array(4096);
this.offset = 0;
}
flush() {
const block = this.block.slice(0, this.offset);
this.port.postMessage(block, [block.buffer]);
this.offset = 0;
}
process(inputs) {
const mono = inputs[0]?.[0];
if (!mono || !this.remaining) return true;
const count = Math.min(mono.length, this.remaining);
for (let i = 0; i < count; i++) {
this.block[this.offset++] = mono[i];
if (this.offset === this.block.length) this.flush();
}
this.remaining -= count;
if (this.remaining === 0) {
if (this.offset) this.flush();
this.port.postMessage('done');
}
return true;
}
}
registerProcessor('room-mic', RoomMic);