forked from GoogleCloudPlatform/generative-ai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcm-processor.js
30 lines (25 loc) · 925 Bytes
/
pcm-processor.js
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
class PCMProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.buffer = new Float32Array();
// Correct way to handle messages in AudioWorklet
this.port.onmessage = (e) => {
const newData = e.data;
const newBuffer = new Float32Array(this.buffer.length + newData.length);
newBuffer.set(this.buffer);
newBuffer.set(newData, this.buffer.length);
this.buffer = newBuffer;
};
}
process(inputs, outputs, parameters) {
const output = outputs[0];
const channelData = output[0];
if (this.buffer.length >= channelData.length) {
channelData.set(this.buffer.slice(0, channelData.length));
this.buffer = this.buffer.slice(channelData.length);
return true;
}
return true;
}
}
registerProcessor('pcm-processor', PCMProcessor);