Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
feat(noise-suppression) add support for Krisp
Saúl Ibarra Corretgéde3b6d2a9fe8
diff --git a/config.js b/config.jsindex adf78730c1..6b4a36809e 100644--- a/config.js+++ b/config.js@@ -197,6 +197,22 @@ var config = {// enableOpusDtx: false,// },+ // Noise suppression configuration. By default rnnoise is used. Optionally Krisp+ // can be used by enabling it below, but the Krisp JS SDK files must be supplied in your+ // installation. Specifically, these files are needed:+ // - https://meet.example.com/libs/krisp/krisp.mjs+ // - https://meet.example.com/libs/krisp/models/model_8.kw+ // - https://meet.example.com/libs/krisp/models/model_16.kw+ // - https://meet.example.com/libs/krisp/models/model_32.kw+ // NOTE: Krisp JS SDK v1.0.9 was tested.+ // noiseSuppression: {+ // krisp: {+ // enabled: false,+ // logProcessStats: false,+ // debugLogs: false,+ // },+ // },+// Video// Sets the preferred resolution (height) for local video. Defaults to 720.diff --git a/react/features/base/config/configType.ts b/react/features/base/config/configType.tsindex e2bbf9109e..06fb311a07 100644--- a/react/features/base/config/configType.ts+++ b/react/features/base/config/configType.ts@@ -117,6 +117,14 @@ export interface IDeeplinkingConfig {ios?: IDeeplinkingMobileConfig;}+export interface INoiseSuppressionConfig {+ krisp?: {+ debugLogs?: boolean;+ enabled?: boolean;+ logProcessStats?: boolean;+ };+}+export interface IConfig {_desktopSharingSourceDevice?: string;analytics?: {@@ -416,6 +424,7 @@ export interface IConfig {microsoftApiApplicationClientID?: string;moderatedRoomServiceUrl?: string;mouseMoveCallbackInterval?: number;+ noiseSuppression?: INoiseSuppressionConfig;noticeMessage?: string;notificationTimeouts?: {long?: number;diff --git a/react/features/noise-suppression/actions.ts b/react/features/noise-suppression/actions.tsindex 151c45a593..18a3251eb9 100644--- a/react/features/noise-suppression/actions.ts+++ b/react/features/noise-suppression/actions.ts@@ -50,6 +50,7 @@ export function setNoiseSuppressionEnabled(enabled: boolean): any {return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {const state = getState();+ const { noiseSuppression: nsOptions } = state['features/base/config'];const localAudio = getLocalJitsiAudioTrack(state);const noiseSuppressionEnabled = isNoiseSuppressionEnabled(state);@@ -61,7 +62,7 @@ export function setNoiseSuppressionEnabled(enabled: boolean): any {return;}- await localAudio.setEffect(new NoiseSuppressionEffect());+ await localAudio.setEffect(new NoiseSuppressionEffect(nsOptions));dispatch(setNoiseSuppressionEnabledState(true));logger.info('Noise suppression enabled.');diff --git a/react/features/stream-effects/noise-suppression/NoiseSuppressionEffect.ts b/react/features/stream-effects/noise-suppression/NoiseSuppressionEffect.tsindex 8fe8fe36bb..9b99648266 100644--- a/react/features/stream-effects/noise-suppression/NoiseSuppressionEffect.ts+++ b/react/features/stream-effects/noise-suppression/NoiseSuppressionEffect.ts@@ -1,18 +1,30 @@+import { INoiseSuppressionConfig } from '../../base/config/configType';import { getBaseUrl } from '../../base/util/helpers';import logger from './logger';+interface IKrispState {+ filterNode?: AudioWorkletNode;+ filterNodeReady: boolean;+ sdk: any;+ sdkInitialized: boolean;+}++const krispState: IKrispState = {+ filterNode: undefined,+ filterNodeReady: false,+ sdk: undefined,+ sdkInitialized: false+};++let audioContext: AudioContext;+/*** Class Implementing the effect interface expected by a JitsiLocalTrack.* Effect applies rnnoise denoising on a audio JitsiLocalTrack.*/export class NoiseSuppressionEffect {- /**- * Web audio context.- */- private _audioContext: AudioContext;-/*** Source that will be attached to the track affected by the effect.*/@@ -26,7 +38,7 @@ export class NoiseSuppressionEffect {/*** `AudioWorkletProcessor` associated node.*/- private _noiseSuppressorNode: AudioWorkletNode;+ private _noiseSuppressorNode?: AudioWorkletNode;/*** Audio track extracted from the original MediaStream to which the effect is applied.@@ -38,6 +50,24 @@ export class NoiseSuppressionEffect {*/private _outputMediaTrack: MediaStreamTrack;+ /**+ * Configured options for noise suppression.+ */+ private _options?: INoiseSuppressionConfig;++ /**+ * Instantiates a noise suppressor audio effect which will use either rnnoise or krisp.+ *+ * @param {INoiseSuppressionConfig} options - Configured options.+ */+ constructor(options?: INoiseSuppressionConfig) {+ this._options = options;++ const useKrisp = options?.krisp?.enabled;++ logger.info(`NoiseSuppressionEffect created with ${useKrisp ? 'Krisp' : 'RNNoise'}`);+ }+/*** Effect interface called by source JitsiLocalTrack.* Applies effect that uses a {@code NoiseSuppressor} service initialized with {@code RnnoiseProcessor}@@ -48,23 +78,39 @@ export class NoiseSuppressionEffect {*/startEffect(audioStream: MediaStream): MediaStream {this._originalMediaTrack = audioStream.getAudioTracks()[0];- this._audioContext = new AudioContext();- this._audioSource = this._audioContext.createMediaStreamSource(audioStream);- this._audioDestination = this._audioContext.createMediaStreamDestination();++ if (!audioContext) {+ audioContext = new AudioContext();+ }++ this._audioSource = audioContext.createMediaStreamSource(audioStream);+ this._audioDestination = audioContext.createMediaStreamDestination();this._outputMediaTrack = this._audioDestination.stream.getAudioTracks()[0];- const baseUrl = `${getBaseUrl()}libs/`;- const workletUrl = `${baseUrl}noise-suppressor-worklet.min.js`;+ let init;++ if (this._options?.krisp?.enabled) {+ init = _initializeKrisp(this._options).then(filterNode => {+ this._noiseSuppressorNode = filterNode;++ if (krispState.filterNodeReady) {+ // @ts-ignore+ krispState.filterNode?.enable();+ }+ });+ } else {+ init = _initializeKRnnoise().then(filterNode => {+ this._noiseSuppressorNode = filterNode;+ });+ }// Connect the audio processing graph MediaStream -> AudioWorkletNode -> MediaStreamAudioDestinationNode- this._audioContext.audioWorklet.addModule(workletUrl)- .then(() => {- // After the resolution of module loading, an AudioWorkletNode can be constructed.- this._noiseSuppressorNode = new AudioWorkletNode(this._audioContext, 'NoiseSuppressorWorklet');- this._audioSource.connect(this._noiseSuppressorNode).connect(this._audioDestination);- })- .catch(error => {- logger.error('Error while adding audio worklet module: ', error);++ init.then(() => {+ if (this._noiseSuppressorNode) {+ this._audioSource.connect(this._noiseSuppressorNode);+ this._noiseSuppressorNode.connect(this._audioDestination);+ }});// Sync the effect track muted state with the original track state.@@ -97,13 +143,103 @@ export class NoiseSuppressionEffect {// Sync original track muted state with effect state before removing the effect.this._originalMediaTrack.enabled = this._outputMediaTrack.enabled;- // Technically after this process the Audio Worklet along with it's resources should be garbage collected,- // however on chrome there seems to be a problem as described here:- // https://bugs.chromium.org/p/chromium/issues/detail?id=1298955- this._noiseSuppressorNode?.port?.close();+ if (this._options?.krisp?.enabled) {+ // When using Krisp we'll just disable the filter which we'll keep reusing.++ // @ts-ignore+ this._noiseSuppressorNode?.disable();+ } else {+ // Technically after this process the Audio Worklet along with it's resources should be garbage collected,+ // however on chrome there seems to be a problem as described here:+ // https://bugs.chromium.org/p/chromium/issues/detail?id=1298955+ this._noiseSuppressorNode?.port?.close();+ }+this._audioDestination?.disconnect();this._noiseSuppressorNode?.disconnect();this._audioSource?.disconnect();- this._audioContext?.close();++ audioContext.suspend();+ }+}++/**+ * Initializes the Krisp SDK and creates the filter node.+ *+ * @param {INoiseSuppressionConfig} options - Krisp options.+ *+ * @returns {Promise<AudioWorkletNode | undefined>}+ */+async function _initializeKrisp(options: INoiseSuppressionConfig): Promise<AudioWorkletNode | undefined> {+ await audioContext.resume();++ if (!krispState.sdk) {+ const baseUrl = `${getBaseUrl()}libs/krisp`;+ const { default: KrispSDK } = await import(/* webpackIgnore: true */ `${baseUrl}/krispsdk.mjs`);++ krispState.sdk = new KrispSDK({+ params: {+ models: {+ model8: `${baseUrl}/models/model_8.kw`,+ model16: `${baseUrl}/models/model_16.kw`,+ model32: `${baseUrl}/models/model_32.kw`+ },+ logProcessStats: options?.krisp?.logProcessStats,+ debugLogs: options?.krisp?.debugLogs+ },+ callbacks: {}+ });+ }++ if (!krispState.sdkInitialized) {+ // @ts-ignore+ await krispState.sdk?.init();++ krispState.sdkInitialized = true;+ }++ if (!krispState.filterNode) {+ try {+ // @ts-ignore+ krispState.filterNode = await krispState.sdk?.createNoiseFilter(audioContext, () => {+ logger.info('Krisp audio filter ready');++ // Enable audio filtering.+ // @ts-ignore+ krispState.filterNode?.enable();+ krispState.filterNodeReady = true;+ });+ } catch (e) {+ logger.error('Failed to create Krisp noise filter', e);++ krispState.filterNode = undefined;+ krispState.filterNodeReady = false;+ }}++ return krispState.filterNode;+}++/**+ * Initializes the RNNoise audio worklet and creates the filter node.+ *+ * @returns {Promise<AudioWorkletNode | undefined>}+ */+async function _initializeKRnnoise(): Promise<AudioWorkletNode | undefined> {+ await audioContext.resume();++ const baseUrl = `${getBaseUrl()}libs/`;+ const workletUrl = `${baseUrl}noise-suppressor-worklet.min.js`;++ try {+ await audioContext.audioWorklet.addModule(workletUrl);+ } catch (e) {+ logger.error('Error while adding audio worklet module: ', e);++ return;+ }++ // After the resolution of module loading, an AudioWorkletNode can be constructed.++ return new AudioWorkletNode(audioContext, 'NoiseSuppressorWorklet');}