# @twilio/krisp-audio-plugin

This plugin can be used with [twilio-video.js](https://github.com/twilio/twilio-video.js) to add custom noise cancellation
to LocalAudioTracks in Group Rooms. In Peer-to-Peer and Go Rooms, Krisp noise cancellation will not be enabled even when
requested by the application code. Please follow the steps below to use the plugin in your application:

1. Install the plugin as a dependency by running `npm install @twilio/krisp-audio-plugin`.
2. Host the `node_modules/@twilio/krisp-audio-plugin/dist/` folder on your application server. We recommend that you
   include the plugin's version in the path where the files are hosted in order to avoid browser caching issues when
   updating the plugin to a newer version.
4. In your application code, you can now enable Krisp noise cancellation as shown below.

```js
const { connect, createLocalAudioTrack } = require('twilio-video');

// Create a LocalAudioTrack with Krisp noise cancellation enabled.
const localAudioTrack = await createLocalAudioTrack({
  noiseCancellationOptions: {
    sdkAssetsPath: 'path/to/hosted/twilio/krisp/audio/plugin/1.0.0/dist',
    vendor: 'krisp'
  }
});

if (!localAudioTrack.noiseCancellation) {
  // If the Krisp audio plugin fails to load, then a warning message will be logged
  // in the browser console, and the "noiseCancellation" property will be set to null.
  // You can still use the LocalAudioTrack to join a Room. However, it will use the
  // browser's noise suppression instead of the Krisp noise cancellation. Make sure
  // the "sdkAssetsPath" provided in "noiseCancellationOptions" points to the correct
  // hosted path of the plugin assets. 
} else {
  // Join a Room with the LocalAudioTrack.
  const room = await connect('token', {
    name: 'my-cool-room',
    tracks: [localAudioTrack]
  });

  if (!localAudioTrack.noiseCancellation.isEnabled) {
    // Krisp noise cancellation is permanently disabled in Peer-to-Peer and Go Rooms.
  }
}

/**
 * Enable/disable noise cancellation.
 * @param {boolean} enable - whether noise cancellation should be enabled
 */
function setNoiseCancellation(enable) {
  const { noiseCancellation } = localAudioTrack;
  if (noiseCancellation) {
    if (enable) {
      // If enabled, then the LocalAudioTrack will use the Krisp noise
      // cancellation instead of the browser's noise suppression.
      noiseCancellation.enable();
    } else {
      // If disabled, then the LocalAudioTrack will use the browser's
      // noise suppression instead of the Krisp noise cancellation.
      noiseCancellation.disable();
    }
  }
}
```
