-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudio-processor.js
More file actions
33 lines (27 loc) · 1015 Bytes
/
audio-processor.js
File metadata and controls
33 lines (27 loc) · 1015 Bytes
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
// Audio worklet processor for capturing microphone audio
class AudioCaptureProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.bufferSize = 4096;
this.buffer = new Float32Array(this.bufferSize);
this.bufferIndex = 0;
}
process(inputs, outputs, parameters) {
const input = inputs[0];
if (input.length > 0) {
const inputChannel = input[0];
for (let i = 0; i < inputChannel.length; i++) {
this.buffer[this.bufferIndex++] = inputChannel[i];
// When buffer is full, send it to main thread
if (this.bufferIndex >= this.bufferSize) {
this.port.postMessage({
audioData: this.buffer.slice()
});
this.bufferIndex = 0;
}
}
}
return true; // Keep processor alive
}
}
registerProcessor('audio-capture-processor', AudioCaptureProcessor);