|
| 1 | +// Copyright (C) <2020> Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +/* global window, AudioContext, Float32Array */ |
| 6 | + |
| 7 | +'use strict'; |
| 8 | +// /////////////////////////////////////////////////////////////////////////////// |
| 9 | +// Handles the WebAssembly kernel for denoising raw audio files in F32Arrayformat |
| 10 | +// /////////////////////////////////////////////////////////////////////////////// |
| 11 | + |
| 12 | +import {Module, wasmMemory} from './rnn_denoise.js'; |
| 13 | + |
| 14 | +// cwrap wasm API's used here make wasm calls from JS simpler. |
| 15 | +const wasmRnndenoiseRawmem = Module.cwrap('rnnDenoise_rawmem', |
| 16 | + 'number', ['number', 'number', 'number', 'number']); |
| 17 | + |
| 18 | +const sampleRate = 44100; // Audio Sample rate can be set and controlled here. |
| 19 | +const numChannels = 1; // Current channel support limited to 1. Stereo is ToDo. |
| 20 | + |
| 21 | +/** |
| 22 | + * @function wasmDenoiseStream |
| 23 | + * @desc Apply denoising into raw audio data in F32Array format using |
| 24 | + * a WebAssembly port of RNNoise denoising algoritm. |
| 25 | + * @return {Float32Array} fProcessdArr with denoised audio data |
| 26 | + * @param {Float32Array} f32buffer |
| 27 | + */ |
| 28 | +export function wasmDenoiseStream(f32buffer) { |
| 29 | + // Create and Initialize Wasm memory with input audio data. |
| 30 | + const wasmMemPtr = Module._malloc(f32buffer.length * 4 ); |
| 31 | + const wasmMemArr = new Float32Array(wasmMemory.buffer, |
| 32 | + wasmMemPtr, f32buffer.length); |
| 33 | + wasmMemArr.set(f32buffer); |
| 34 | + |
| 35 | + // Call Wasm denoising kernel |
| 36 | + const wasmRetPtr = wasmRnndenoiseRawmem(wasmMemPtr, |
| 37 | + sampleRate, numChannels, f32buffer.length); |
| 38 | + |
| 39 | + // Create JS Array from Wasm memory with results |
| 40 | + const fProcessedArr = new Float32Array(wasmMemory.buffer, |
| 41 | + wasmRetPtr, f32buffer.length); |
| 42 | + |
| 43 | + return fProcessedArr; |
| 44 | +} |
| 45 | + |
| 46 | +// //////////////////////////////////////////////////////////////////// |
| 47 | +// Creates a WebAudio Based filter for applying audio denoising. |
| 48 | +// /////////////////////////////////////////////////////////////////// |
| 49 | + |
| 50 | +// WebAuddio context |
| 51 | +window.AudioContext = window.AudioContext || window.webkitAudioContext; |
| 52 | +export const audioContext = new AudioContext(); // new AudioContext({sampleRate: 48000}); |
| 53 | + |
| 54 | + |
| 55 | +// Audio buffer size: |
| 56 | +// Accepts powers of 2 between 0 and 16384. |
| 57 | +// Too low causes audio glitches due to buffer underruns |
| 58 | +// Too high could increase latency. |
| 59 | +// Set to 0 and WebAudio API will autopick a value. |
| 60 | +const bufferSize = 4096; |
| 61 | + |
| 62 | +export const audioDenoise = (function() { |
| 63 | + const numberOfInputChannels = 1; |
| 64 | + const numberOfOutputChannels = 1; |
| 65 | + let recorder; |
| 66 | + |
| 67 | + if (audioContext.createScriptProcessor) { |
| 68 | + recorder = audioContext.createScriptProcessor(bufferSize, |
| 69 | + numberOfInputChannels, numberOfOutputChannels); |
| 70 | + } else { |
| 71 | + recorder = audioContext.createJavaScriptNode(bufferSize, |
| 72 | + numberOfInputChannels, numberOfOutputChannels); |
| 73 | + } |
| 74 | + |
| 75 | + recorder.onaudioprocess = function(e) { |
| 76 | + const input = e.inputBuffer.getChannelData(0); |
| 77 | + |
| 78 | + const wasmOutput = wasmDenoiseStream(input); |
| 79 | + |
| 80 | + e.outputBuffer.copyToChannel(wasmOutput, 0, 0); |
| 81 | + }; |
| 82 | + return recorder; |
| 83 | +})(); |
| 84 | + |
0 commit comments