|
|
class PCMProcessor extends AudioWorkletProcessor { |
|
|
constructor() { |
|
|
super(); |
|
|
this.buffer = new Float32Array(24000 * 30); |
|
|
this.writeIndex = 0; |
|
|
this.readIndex = 0; |
|
|
this.pendingBytes = new Uint8Array(0); |
|
|
this.volume = 1.0; |
|
|
this.port.onmessage = (event) => { |
|
|
if (event.data.pcmData) { |
|
|
|
|
|
const newData = new Uint8Array(event.data.pcmData); |
|
|
const combined = new Uint8Array(this.pendingBytes.length + newData.length); |
|
|
combined.set(this.pendingBytes); |
|
|
combined.set(newData, this.pendingBytes.length); |
|
|
|
|
|
|
|
|
const completeSamples = Math.floor(combined.length / 2); |
|
|
const bytesToProcess = completeSamples * 2; |
|
|
|
|
|
if (completeSamples > 0) { |
|
|
|
|
|
const int16Array = new Int16Array(combined.buffer.slice(0, bytesToProcess)); |
|
|
|
|
|
|
|
|
for (let i = 0; i < int16Array.length; i++) { |
|
|
|
|
|
if (this.writeIndex >= this.buffer.length) { |
|
|
const newBuffer = new Float32Array(this.buffer.length * 2); |
|
|
|
|
|
let sourceIndex = this.readIndex; |
|
|
let targetIndex = 0; |
|
|
while (sourceIndex !== this.writeIndex) { |
|
|
newBuffer[targetIndex++] = this.buffer[sourceIndex]; |
|
|
sourceIndex = (sourceIndex + 1) % this.buffer.length; |
|
|
} |
|
|
this.buffer = newBuffer; |
|
|
this.readIndex = 0; |
|
|
this.writeIndex = targetIndex; |
|
|
} |
|
|
|
|
|
this.buffer[this.writeIndex] = int16Array[i] / 32768.0; |
|
|
this.writeIndex = (this.writeIndex + 1) % this.buffer.length; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (combined.length > bytesToProcess) { |
|
|
this.pendingBytes = combined.slice(bytesToProcess); |
|
|
} else { |
|
|
this.pendingBytes = new Uint8Array(0); |
|
|
} |
|
|
} else if (event.data.volume !== undefined) { |
|
|
|
|
|
this.volume = Math.max(0, event.data.volume); |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
process(inputs, outputs, parameters) { |
|
|
const output = outputs[0]; |
|
|
if (output.length > 0 && this.readIndex !== this.writeIndex) { |
|
|
const channelData = output[0]; |
|
|
for (let i = 0; i < channelData.length && this.readIndex !== this.writeIndex; i++) { |
|
|
channelData[i] = this.buffer[this.readIndex] * this.volume; |
|
|
this.readIndex = (this.readIndex + 1) % this.buffer.length; |
|
|
} |
|
|
} |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
registerProcessor('pcm-processor', PCMProcessor); |
|
|
|