Reusable AudioWorklet for streaming PCM16 audio playback. Place in public/ folder and load via audioContext.audioWorklet.addModule() /
/**
* Reusable AudioWorklet for streaming PCM16 audio playback.
* Place in public/ folder and load via audioContext.audioWorklet.addModule()
*/
class RingBuffer {
constructor(initialCapacity) {
this.capacity = initialCapacity;
this.buffer = new Float32Array(initialCapacity);
this.readIndex = 0;
this.writeIndex = 0;
this.availableData = 0;
}
push(data) {
const len = data.length;
// Auto-grow if needed
while (this.availableData + len > this.capacity) {
this.grow();
}
for (let i = 0; i < len; i++) {
this.buffer[this.writeIndex] = data[i];
this.writeIndex = (this.writeIndex + 1) % this.capacity;
this.availableData++;
}
}
grow() {
const newCapacity = this.capacity * 2;
const newBuffer = new Float32Array(n
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key