mirror of
https://github.com/block/buzz.git
synced 2026-10-29 21:41:33 +00:00
Signed-off-by: Meli <5aaa86bce934fc3445fc254aab560a40923f10252f92107e665073dede0e04d3@buzz.block.builderlab.xyz>
32 lines
857 B
JavaScript
32 lines
857 B
JavaScript
// Bound latency/memory, not scheduler jitter. Never drop or reorder microphone samples.
|
|
export class CaptureQueue {
|
|
constructor() {
|
|
this.frames = [];
|
|
this.samples = 0;
|
|
}
|
|
push(pcm) {
|
|
if (this.samples + pcm.length > 120000)
|
|
throw Error("microphone transport stalled for over five seconds");
|
|
this.frames.push(pcm);
|
|
this.samples += pcm.length;
|
|
}
|
|
shift() {
|
|
const count = Math.min(this.samples, 2400),
|
|
out = new Int16Array(count);
|
|
let used = 0;
|
|
while (used < count) {
|
|
const head = this.frames[0],
|
|
n = Math.min(head.length, count - used);
|
|
out.set(head.subarray(0, n), used);
|
|
used += n;
|
|
if (n === head.length) this.frames.shift();
|
|
else this.frames[0] = head.subarray(n);
|
|
}
|
|
this.samples -= count;
|
|
return out;
|
|
}
|
|
get length() {
|
|
return this.samples;
|
|
}
|
|
}
|