@vonage/noise-suppression
v1.0.0
Published
Noise-suppression library for web.
Downloads
856
Maintainers
Keywords
Readme
@vonage/noise-suppression
Noise-suppression library for web.
Installation
npm i @vonage/noise-suppression
Example
cd example/create-vonage-noise-suppression
# or
cd example/insertable-stream
npm i
npm run start
Usage
// Basic example
import { createVonageNoiseSuppression } from "@vonage/noise-suppression";
/**
* Process an audio stream in order to remove noise.
* Return a new audio stream with processed source.
*/
async function suppressNoiseFromAudioStream(source: MediaStream): MediaStream {
const processor = createVonageNoiseSuppression();
await processor.init();
const connector = await processor.getConnector();
const track = await connector.setTrack(source.getAudioTracks()[0]);
const output = new MediaStream();
output.addTrack(track);
return output;
}
// Using the transformer with opentok
import { NoiseSuppressionTransformer } from "@vonage/noise-suppression";
import { MediaProcessor, MediaProcessorConnector } from "@vonage/media-processor";
/**
* Create an OpenTok session with noise suppression audio processor piped
*/
async function initSessionWithNoiseSuppression() {
// Processor init
const processor = createVonageNoiseSuppression();
await processor.init();
const connector = await processor.getConnector();
// OT init
const session = OT.initSession(apiKey, sessionId);
const publisher = OT.initPublisher('publisher', {}, handleError);
await publisher.setAudioMediaProcessorConnector(noiseSuppressionConnector);
session.connect(token, (error) => {
if (error) {
handleError(error);
} else {
publisher.setAudioMediaProcessorConnector(noiseSuppressionConnector);
}
});
}
// Using the transformer with @vonage/media-processor
import { NoiseSuppressionTransformer } from "@vonage/noise-suppression";
import { MediaProcessor, MediaProcessorConnector } from "@vonage/media-processor";
/**
* Process a audio stream in order to remove noise.
* Return a new audio stream with processed source.
*/
async function suppressNoiseFromAudioStream(source: MediaStream): MediaStream {
const processor = new MediaProcessor();
const transformer = new NoiseSuppressionTransformer();
await transformer.init();
await processor.setTransformers([
transformer,
// my other audio transformers
])
const connector = new MediaProcessorConnector(processor);
const track = await connector.setTrack(source.getAudioTracks()[0]);
const output = new MediaStream();
output.addTrack(track);
return output;
}
Multithreaded wasm
Enabling WebAssembly multithreading can significantly boost the performance by harnessing the power of multiple CPU cores. To enable this feature, users must allow shared array buffers, which come with specific security prerequisites.
These prerequisites include:
- 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.
By default, the library will try to start the wasm in multithreaded mode and will print a warning message if the security conditions did not match. You can disable this message by disabling multithreading manually using the proper option.
// Using the helper
const processor = createVonageNoiseSuppression();
await processor.init({
disableWasmMultiThread: true
});
// Using the transformer
const transformer = new NoiseSuppressionTransformer();
await transformer.init({
disableWasmMultiThread: true
});
Hosting the static resources
In order to reduce the impact of the library until it is used, worker, wasm and model are loaded dynamically when the transformer is created.
These resources are served by default by Vonage CDN, but could be provided from a custom server.
You can specify the server url as an option of the transformer.
// Using the helper
const processor = createVonageNoiseSuppression();
processor.init({
assetsDirBaseUrl: "https://my-custom-server/path/to/the/resources/root"
});
// Using the transformer
const transformer = new NoiseSuppressionTransformer();
transformer.init({
assetsDirBaseUrl: "https://my-custom-server/path/to/the/resources/root"
});
Documentation
Replace version with the expected version documentation: https://vonage.github.io/client-media-processing-docs/noise-suppression/{VERSION}/