29 lines
666 B
JavaScript
29 lines
666 B
JavaScript
class PCMProcessor 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 || !input.length) return true;
|
|
|
|
const channelData = input[0];
|
|
|
|
for (let i = 0; i < channelData.length; i++) {
|
|
this.buffer[this.bufferIndex++] = channelData[i];
|
|
|
|
if (this.bufferIndex >= this.bufferSize) {
|
|
this.port.postMessage(this.buffer);
|
|
this.bufferIndex = 0;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
registerProcessor("pcm-processor", PCMProcessor);
|