in conversational-webgpu/src/vad-processor.js [6:34]
process(inputs, outputs, parameters) {
const buffer = inputs[0][0];
if (!buffer) return; // buffer is null when the stream ends
if (buffer.length > MIN_CHUNK_SIZE) {
// If the buffer is larger than the minimum chunk size, send the entire buffer
this.port.postMessage({ buffer });
} else {
const remaining = MIN_CHUNK_SIZE - globalPointer;
if (buffer.length >= remaining) {
// If the buffer is larger than (or equal to) the remaining space in the global buffer, copy the remaining space
globalBuffer.set(buffer.subarray(0, remaining), globalPointer);
// Send the global buffer
this.port.postMessage({ buffer: globalBuffer });
// Reset the global buffer and set the remaining buffer
globalBuffer.fill(0);
globalBuffer.set(buffer.subarray(remaining), 0);
globalPointer = buffer.length - remaining;
} else {
// If the buffer is smaller than the remaining space in the global buffer, copy the buffer to the global buffer
globalBuffer.set(buffer, globalPointer);
globalPointer += buffer.length;
}
}
return true; // Keep the processor alive
}