|
1 | 1 | /** |
2 | 2 | * RNNoise AudioWorklet Processor |
3 | | - * Processes audio in real-time using RNNoise WASM for noise suppression |
| 3 | + * Uses the sync wasm loader so WorkletGlobalScope can initialize synchronously. |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { RNNoise } from '@jitsi/rnnoise-wasm'; |
| 6 | +import createRNNWasmModuleSync from '/rnnoise-sync.js'; |
7 | 7 |
|
8 | 8 | class RNNoiseProcessor extends AudioWorkletProcessor { |
9 | 9 | constructor() { |
10 | 10 | super(); |
11 | | - |
12 | | - this.rnnoise = null; |
| 11 | + |
| 12 | + this.module = null; |
| 13 | + this.statePtr = 0; |
| 14 | + this.inPtr = 0; |
| 15 | + this.outPtr = 0; |
13 | 16 | this.isInitialized = false; |
14 | | - this.frameSize = 480; // RNNoise frame size for 48kHz (10ms) |
| 17 | + this.enabled = true; |
| 18 | + |
| 19 | + this.frameSize = 480; // RNNoise frame size for 48kHz / 10ms |
15 | 20 | this.inputBuffer = new Float32Array(this.frameSize); |
16 | 21 | this.outputBuffer = new Float32Array(this.frameSize); |
17 | 22 | this.bufferIndex = 0; |
| 23 | + this.pendingOutput = []; |
| 24 | + this.pendingOutputReadIndex = 0; |
18 | 25 |
|
19 | | - // Initialize RNNoise |
20 | | - this.initRNNoise(); |
21 | | - |
22 | | - // Listen for messages from main thread |
23 | 26 | this.port.onmessage = (event) => { |
24 | | - if (event.data.type === 'enable') { |
| 27 | + if (event.data?.type === 'enable') { |
25 | 28 | this.enabled = event.data.enabled; |
26 | 29 | } |
27 | 30 | }; |
28 | 31 |
|
29 | | - this.enabled = true; |
| 32 | + this.initRNNoise(); |
30 | 33 | } |
31 | 34 |
|
32 | 35 | async initRNNoise() { |
33 | 36 | try { |
34 | | - // Initialize the RNNoise WASM module |
35 | | - await RNNoise.initWasm(); |
36 | | - this.rnnoise = new RNNoise(); |
| 37 | + this.module = createRNNWasmModuleSync(); |
| 38 | + await this.module.ready; |
| 39 | + |
| 40 | + this.module._rnnoise_init(); |
| 41 | + this.statePtr = this.module._rnnoise_create(); |
| 42 | + this.inPtr = this.module._malloc(this.frameSize * 4); |
| 43 | + this.outPtr = this.module._malloc(this.frameSize * 4); |
| 44 | + |
37 | 45 | this.isInitialized = true; |
38 | | - console.log('RNNoise initialized successfully'); |
39 | 46 | this.port.postMessage({ type: 'initialized' }); |
40 | 47 | } catch (error) { |
41 | 48 | console.error('Failed to initialize RNNoise:', error); |
42 | | - this.port.postMessage({ type: 'error', message: error.message }); |
| 49 | + this.port.postMessage({ type: 'error', message: error?.message || 'RNNoise init failed' }); |
43 | 50 | } |
44 | 51 | } |
45 | 52 |
|
46 | | - process(inputs, outputs, parameters) { |
47 | | - if (!this.isInitialized || !this.enabled) { |
48 | | - // Pass through if not initialized or disabled |
49 | | - if (inputs[0] && inputs[0][0]) { |
50 | | - outputs[0][0].set(inputs[0][0]); |
| 53 | + denoiseFrame(inputFrame) { |
| 54 | + const heapOffsetIn = this.inPtr >> 2; |
| 55 | + const heapOffsetOut = this.outPtr >> 2; |
| 56 | + |
| 57 | + this.module.HEAPF32.set(inputFrame, heapOffsetIn); |
| 58 | + this.module._rnnoise_process_frame(this.statePtr, this.outPtr, this.inPtr); |
| 59 | + |
| 60 | + const denoised = this.module.HEAPF32.subarray(heapOffsetOut, heapOffsetOut + this.frameSize); |
| 61 | + this.outputBuffer.set(denoised); |
| 62 | + } |
| 63 | + |
| 64 | + enqueueFrame(frame) { |
| 65 | + for (let i = 0; i < frame.length; i++) { |
| 66 | + this.pendingOutput.push(frame[i]); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + dequeueSample(fallback) { |
| 71 | + if (this.pendingOutputReadIndex < this.pendingOutput.length) { |
| 72 | + const sample = this.pendingOutput[this.pendingOutputReadIndex]; |
| 73 | + this.pendingOutputReadIndex += 1; |
| 74 | + |
| 75 | + if (this.pendingOutputReadIndex > 4096 && this.pendingOutputReadIndex * 2 > this.pendingOutput.length) { |
| 76 | + this.pendingOutput = this.pendingOutput.slice(this.pendingOutputReadIndex); |
| 77 | + this.pendingOutputReadIndex = 0; |
51 | 78 | } |
52 | | - return true; |
| 79 | + |
| 80 | + return sample; |
53 | 81 | } |
54 | 82 |
|
55 | | - const input = inputs[0][0]; |
56 | | - const output = outputs[0][0]; |
| 83 | + return fallback; |
| 84 | + } |
| 85 | + |
| 86 | + process(inputs, outputs) { |
| 87 | + const input = inputs[0]?.[0]; |
| 88 | + const output = outputs[0]?.[0]; |
57 | 89 |
|
58 | | - if (!input) { |
59 | | - output.fill(0); |
| 90 | + if (!output) return true; |
| 91 | + |
| 92 | + if (!input || !this.isInitialized || !this.enabled) { |
| 93 | + if (input) output.set(input); |
| 94 | + else output.fill(0); |
60 | 95 | return true; |
61 | 96 | } |
62 | 97 |
|
63 | | - // Process audio frame by frame (RNNoise requires fixed 480-sample frames) |
| 98 | + // First, ingest input and process complete RNNoise frames. |
64 | 99 | let inputIndex = 0; |
65 | | - |
66 | 100 | while (inputIndex < input.length) { |
67 | | - // Fill the input buffer |
68 | | - const samplesToRead = Math.min( |
69 | | - this.frameSize - this.bufferIndex, |
70 | | - input.length - inputIndex |
71 | | - ); |
72 | | - |
73 | | - this.inputBuffer.set( |
74 | | - input.subarray(inputIndex, inputIndex + samplesToRead), |
75 | | - this.bufferIndex |
76 | | - ); |
77 | | - |
| 101 | + const samplesToRead = Math.min(this.frameSize - this.bufferIndex, input.length - inputIndex); |
| 102 | + this.inputBuffer.set(input.subarray(inputIndex, inputIndex + samplesToRead), this.bufferIndex); |
78 | 103 | this.bufferIndex += samplesToRead; |
79 | 104 | inputIndex += samplesToRead; |
80 | 105 |
|
81 | | - // Process when we have a complete frame |
82 | 106 | if (this.bufferIndex === this.frameSize) { |
83 | 107 | try { |
84 | | - // Apply RNNoise denoising |
85 | | - this.rnnoise.denoise(this.inputBuffer, this.outputBuffer); |
86 | | - |
87 | | - // Write output |
88 | | - output.set(this.outputBuffer, inputIndex - this.frameSize); |
| 108 | + this.denoiseFrame(this.inputBuffer); |
| 109 | + this.enqueueFrame(this.outputBuffer); |
89 | 110 | } catch (error) { |
90 | 111 | console.error('RNNoise processing error:', error); |
91 | | - output.set(this.inputBuffer, inputIndex - this.frameSize); |
| 112 | + this.enqueueFrame(this.inputBuffer); |
92 | 113 | } |
93 | | - |
94 | | - // Reset buffer index for next frame |
95 | 114 | this.bufferIndex = 0; |
96 | 115 | } |
97 | 116 | } |
98 | 117 |
|
99 | | - // If there's remaining audio less than a full frame, pass it through |
100 | | - if (this.bufferIndex > 0) { |
101 | | - output.set(this.inputBuffer.subarray(0, this.bufferIndex), input.length - this.bufferIndex); |
| 118 | + // Then, render exactly one output quantum from the queued denoised samples. |
| 119 | + for (let i = 0; i < output.length; i++) { |
| 120 | + output[i] = this.dequeueSample(input[i] ?? 0); |
102 | 121 | } |
103 | 122 |
|
104 | 123 | return true; |
|
0 commit comments