-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathStreamProcessor.js
More file actions
35 lines (33 loc) · 1.17 KB
/
StreamProcessor.js
File metadata and controls
35 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// audio-processor.js (loaded by audioContext.audioWorklet.addModule)
class RecorderProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
const bufferSize = options.bufferSize || 4096;
// Prepare an interleaved stereo buffer [L,R,L,R,...]
this.buffer = new Float32Array(bufferSize * 2);
this.bufferIndex = 0;
}
process(inputs, outputs) {
const input = inputs[0];
if (input && input[0]) {
const left = input[0];
const right = input[1] || left; // if mono input, duplicate for right
for (let i = 0; i < left.length; i++) {
this.buffer[this.bufferIndex++] = left[i];
this.buffer[this.bufferIndex++] = right[i];
if (this.bufferIndex >= this.buffer.length) {
// Buffer full: send a copy to the main thread
this.port.postMessage(new Float32Array(this.buffer));
this.bufferIndex = 0;
}
}
}
// Optionally pass the audio through unchanged
if (outputs[0] && inputs[0]) {
outputs[0][0].set(inputs[0][0]);
if (inputs[0][1]) outputs[0][1].set(inputs[0][1]);
}
return true;
}
}
registerProcessor('recorder-processor', RecorderProcessor);