/* * RNNoise AudioWorklet Processor * Handles frame chunking (480 samples / 10ms frame size required by RNNoise) * and bridges PCM audio buffers with the WebAssembly RNNoise engine. */ class RNNoiseWorkletProcessor extends AudioWorkletProcessor { constructor() { super(); this.FRAME_SIZE = 480; // 10ms at 48kHz this.inputBuffer = new Float32Array(0); this.outputBuffer = new Float32Array(0); this.isWasmReady = false; // Listen for WASM binary or module initialization from the main thread this.port.onmessage = (event) => { if (event.data.type === 'INIT_WASM') { this.initWasm(event.data.wasmBytes); } }; } initWasm(wasmBytes) { // Instantiate WASM module directly inside the AudioWorklet scope WebAssembly.instantiate(wasmBytes, { env: { memory: new WebAssembly.Memory({ initial: 256 }), abort: () => console.error('RNNoise WASM Aborted') } }).then(result => { this.wasmInstance = result.instance; this.denoiseState = this.wasmInstance.exports.rnnoise_create(null); this.wasmInputPtr = this.wasmInstance.exports.malloc(this.FRAME_SIZE * 4); this.wasmOutputPtr = this.wasmInstance.exports.malloc(this.FRAME_SIZE * 4); this.isWasmReady = true; }).catch(err => { console.error('Failed to initialize RNNoise WASM in AudioWorklet:', err); }); } process(inputs, outputs, parameters) { const input = inputs[0]; const output = outputs[0]; if (!input || !input[0]) return true; const inputChannel = input[0]; const outputChannel = output[0]; // Append incoming Web Audio rendering quantum (typically 128 samples) to buffer this.inputBuffer = this.concatFloat32(this.inputBuffer, inputChannel); // Process full 480-sample frames when available while (this.inputBuffer.length >= this.FRAME_SIZE) { const frame = this.inputBuffer.subarray(0, this.FRAME_SIZE); this.inputBuffer = this.inputBuffer.subarray(this.FRAME_SIZE); if (this.isWasmReady && this.denoiseState) { // Copy frame to WASM heap const heapF32 = new Float32Array( this.wasmInstance.exports.memory.buffer, this.wasmInputPtr, this.FRAME_SIZE ); heapF32.set(frame); // Run RNNoise frame processing this.wasmInstance.exports.rnnoise_process_frame( this.denoiseState, this.wasmOutputPtr, this.wasmInputPtr ); // Extract processed frame from WASM heap const processedFrame = new Float32Array( this.wasmInstance.exports.memory.buffer, this.wasmOutputPtr, this.FRAME_SIZE ); this.outputBuffer = this.concatFloat32(this.outputBuffer, processedFrame); } else { // Pass-through fallback until WASM module is fully loaded this.outputBuffer = this.concatFloat32(this.outputBuffer, frame); } } // Deliver processed audio back to Web Audio graph (128 samples at a time) if (this.outputBuffer.length >= outputChannel.length) { outputChannel.set(this.outputBuffer.subarray(0, outputChannel.length)); this.outputBuffer = this.outputBuffer.subarray(outputChannel.length); } else { // Silence padding if buffer is temporarily underrun outputChannel.fill(0); } return true; } concatFloat32(a, b) { const c = new Float32Array(a.length + b.length); c.set(a, 0); c.set(b, a.length); return c; } destroy() { if (this.isWasmReady && this.wasmInstance) { this.wasmInstance.exports.rnnoise_destroy(this.denoiseState); this.wasmInstance.exports.free(this.wasmInputPtr); this.wasmInstance.exports.free(this.wasmOutputPtr); } } } registerProcessor('rnnoise-worklet', RNNoiseWorkletProcessor);