API
DHDenoiser
We currently only support processing a single stream; creating more than one instance is not supported.
Create a DHDenoiser instance for noise reduction.
- Javascript
import { DHDenoiser } from 'dh-denoiser';const denoiser = new DHDenoiser();
init()
async init(context: AudioContext, processorUrl?: string): void
context
(required): An AudioContext instance to handle the audio.processorUrl
(optional): This parameter specifies the location ofdh-denoiser-processor.js
. If theprocessorUrl
parameter is not set, the library assumes thatdh-denoiser-processor.js
exists in the same path asdh-denoiser.js
. Ifdh-denoiser-processor.js
is located in a different path, you must specify that path using this parameter.
Calling this method initializes the SDK, prepares an AudioContext
, and can start the denoising process.
- Javascript
const denoiser = new DHDenoiser();const context = new AudioContext({ sampleRate: 48000 });await denoiser.init(context);// If dh-denoiser-processor.js is located in a different path:// await denoiser.init(context, './path-to/dh-denoiser-processor.js');
appendDenoiserToStream()
async appendDenoiserToStream(stream: MediaStream): Promise<MediaStream>
Processes the MediaStream of the WebAudio API.
This method connects AudioWorkletNode
for noise reduction to MediaStream
passed in. This allows you to add denoising to the original audio stream.
Before calling appendDenoiserToStream
, you must initialize the SDK through init()
method.
Here is an example of using this method:
- Javascript
// Getting MediaStreamnavigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => { // With the code below, we get a new MediaStream with noise cancellation added. const denoisedStream = await denoiser.appendDenoiserToStream(stream); // You can use denoisedStream any way you like.});
This will give you a new 'MediaStream' with noise reduction added to the original 'MediaStream'. You can use this new MediaStream
to perform various tasks such as audio output and audio recording.
isEnabled()
isEnabled(): boolean
This method checks whether denoising is currently enabled. Return true
if denoising is enabled, false
otherwise. The default state is false
, i.e. noise reduction is disabled by default.
Here is an example of using this method:
- Javascript
if (denoiser.isEnabled()) { console.log('Noise reduction is enabled.');} else { console.log('Noise reduction is disabled.');}
setEnable()
setEnable(): boolean
Activate the noise reduction function. Calling this method outputs the audio stream with denoising applied. If DHDenoiser
has not been properly initialized, this method will not work.
Here is an example of using this method:
- Javascript
denoiser.setEnable();
setDisable()
setDisable(): boolean
Deactivate the noise reduction function. Calling this method disable the noise reduction function and output the original audio stream as it is. If DHDenoiser
has not been properly initialized, this method will not work.
Here is an example of using this method:
- Javascript
denoiser.setDisable();
Using the setEnable()
and setDisable()
methods, you can easily enable or disable the noise reduction function at the desired time. This gives the user direct control over the use of the noise reduction feature.
getConstraints()
getConstraints(): Object
This method returns the media track constraints used to optimize denoising. These constraints are used to minimize the impact of other audio signal processing algorithms on denoising performance.
- Javascript
const constraints = denoiser.getConstraints();
Example Code
<!doctype html><html> <head> <title>Deep Hearing Denoiser Demo</title> </head> <body> <button id="startButton">Start Stream</button> <button id="denoiseButton">Toggle Denoise</button> <script type="module"> import { DHDenoiser } from './dh-denoiser.js'; const startStream = async (denoiser) => { const constraints = denoiser.getConstraints(); const context = new AudioContext({ sampleRate: 48000 }); await denoiser.init(context, './dh-denoiser-processor.js'); try { const stream = await navigator.mediaDevices.getUserMedia({ audio: constraints, }); const denoisedStream = await denoiser.appendDenoiserToStream(stream); // Loopback the denoised audio to the system output. const audio = new Audio(); audio.srcObject = denoisedStream; audio.play(); } catch (err) { console.error('Failed to get user media', err); } }; document.addEventListener('DOMContentLoaded', () => { const denoiser = new DHDenoiser(); const startButton = document.getElementById('startButton'); const denoiseButton = document.getElementById('denoiseButton'); startButton.addEventListener('click', () => { startStream(denoiser); }); denoiseButton.addEventListener('click', () => { if (denoiser.isEnabled()) { denoiser.setDisable(); // Disable denoiser } else { denoiser.setEnable(); // Enable denoiser } }); }); </script> </body></html>