Shhhhh! Remove Background Noise in Your Video Calls
Tags: video
Published: 2024-05-15 14:49:30

We live in a connected world. Users of your web application could be taking video calls from a myriad of different locations and under various conditions, imagine:
Morning stand-ups when a neighbor decides to start mowing their lawn.
While working from a busy coffee shop, an impromptu meeting is started to discuss design changes.
Of course on the day of the big presentation, school is canceled due to snowfall.
Now, with Vonage Video Noise Suppression, there’s one less thing to worry about: background noise.

Want to give it a try in your Chrome / Chromium-based (Electron, Opera, and Edge) browser, right now? We have GitHub repos for both [Moderate](https://github.com/Vonage-Community/video-api-web-samples/tree/main/Moderate-Noise-Suppression) and [Advanced](https://github.com/Vonage-Community/video-api-web-samples/tree/main/Advanced-Noise-Suppression) sample apps that you can deploy with one click.
Once deployed, fill in the credentials that you can get from the [Vonage Video Playground](https://tools.vonage.com/video/playground/) and you can begin testing. Just join the call from two different tabs and click the buttons to enable and disable noise suppression. If you are lucky enough not to be in a noisy environment, searching for and playing videos of city noises on a cell phone will do.

Go ahead and give them a try. I’ll wait.

The Noise Suppression functionality is made possible with the [Vonage Media Processor](https://developer.vonage.com/en/video/guides/media-processor/overview), which helps access the audio data and remove any background noise.

As mentioned previously, there are two ways to implement noise suppression in your Vonage Video call. Let’s see how they work.

## Moderate Implementation

With the Moderate implementation of Noise Suppression, Vonage has wrapped the Noise Suppression Transformer, Media Processor, and Media Processor Connector into a single function called `createVonageNoiseSuppression()`.

The important bits are:

Creating an instance from the Noise Suppression library

```javascript
const noiseSuppression = await createVonageNoiseSuppression();
```

Initializing the instance

```javascript
await noiseSuppression.init();
```

Getting a connector to the Media Processor

```javascript
const mediaProcessorConnector = await noiseSuppression.getConnector();
```

Setting the connector when initializing your publisher for the video call

```javascript
publisher
  .setAudioMediaProcessorConnector(mediaProcessorConnector)
```

Now, your audio will be passed through the Noise Suppression Transformer and then into the video call. 

To enable and disable noise suppression you call some methods on the `noiseSuppression`:

```javascript
// enable Noise Suppression
noiseSuppression.enable();
// disable Noise Suppression
noiseSuppression.disable();
```

## Advanced Implementation

If you want to add multiple effects to the audio stream before going into the video call or anything else that would require more control over insertable streams and transformers, the advanced use case will be the best option.

First, create a new instance of a MediaProcessor

```javascript
const mediaProcessor = new MediaProcessor();
```

and a new Noise Suppression Transformer.

```javascript
const noiseSuppressionTransformer = new NoiseSuppressionTransformer();
```

Then, initialize the Transformer

```javascript
await noiseSuppressionTransformer.init();
```

Next, set the transformers that the media processor will be using. This is also where you would include other transformers that you would like to add to the audio stream.

```javascript
mediaProcessor.setTransformers([noiseSuppressionTransformer]);
```

Now, create a Media Connector using the `mediaProcessor`.

```javascript
const mediaProcessorConnector = new MediaProcessorConnector(mediaProcessor);
```

Just like in the Moderate implementation example, you set the connector when initializing your publisher for the video call.

```javascript
publisher
  .setAudioMediaProcessorConnector(mediaProcessorConnector)
```

Since you now have access to the Noise Suppression Transformer, that is where you can enable and disable the functionality.

```javascript
// enable Noise Suppression
noiseSuppressionTransformer.enable();
// disable Noise Suppression
noiseSuppressionTransformer.disable();
```

## More Options

When initializing Noise Suppression, you can specify a couple of options.

First is Multithreaded WebAssembly. With Wasm multithreading, using multiple CPU cores can significantly boost performance. This is enabled by default, but to work, these prerequisites must be met:
- Serving your web application over HTTPS for secure data transmission.

- Setting the 'Cross-Origin-Opener-Policy' header to 'same-origin' to restrict execution contexts to trusted sources.

- Setting the 'Cross-Origin-Embedder-Policy' header with 'require-corp' or 'credentialless' values to ensure secure usage of shared array buffers.

If you’d like to disable this option, you can pass in `disableWasmMultiThread: true` when you do the initialization.

```javascript
.init({disableWasmMultiThread: true});
```

The Web Worker, Wasm, and Noise Suppression model are loaded dynamically when the Transformer is initialized. By default, these resources are loaded from a Vonage CDN. You do have the option to do the hosting yourself. Just specify the `assetsDirBaseUrl` parameter with the host server’s URL pointing to the static assets.

```javascript
.init({assetsDirBaseUrl: "https://my-custom-server/path/to/the/resources/root"});
```

We’ve tried to make adding Noise Suppression to your video application as straightforward as possible. Give it a try! 

If you have any questions or comments, please let us know in our [Community Slack Channel](https://developer.vonage.com/en/community/slack) and follow us on [X](https://www.twitter.com/VonageDev).