2022-10-11 10:47:54 +00:00
|
|
|
import { IStore } from '../app/types';
|
|
|
|
import { getLocalJitsiAudioTrack } from '../base/tracks/functions';
|
|
|
|
import { showErrorNotification } from '../notifications/actions';
|
2022-09-06 17:32:20 +00:00
|
|
|
import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
|
2022-07-20 12:31:17 +00:00
|
|
|
import { NoiseSuppressionEffect } from '../stream-effects/noise-suppression/NoiseSuppressionEffect';
|
|
|
|
|
|
|
|
import { SET_NOISE_SUPPRESSION_ENABLED } from './actionTypes';
|
|
|
|
import { canEnableNoiseSuppression, isNoiseSuppressionEnabled } from './functions';
|
|
|
|
import logger from './logger';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the noise suppression active state.
|
|
|
|
*
|
|
|
|
* @param {boolean} enabled - Is noise suppression enabled.
|
|
|
|
* @returns {{
|
|
|
|
* type: SET_NOISE_SUPPRESSION_STATE,
|
|
|
|
* enabled: boolean
|
|
|
|
* }}
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
export function setNoiseSuppressionEnabledState(enabled: boolean): any {
|
2022-07-20 12:31:17 +00:00
|
|
|
return {
|
|
|
|
type: SET_NOISE_SUPPRESSION_ENABLED,
|
|
|
|
enabled
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enabled/disable noise suppression depending on the current state.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
export function toggleNoiseSuppression(): any {
|
2022-10-11 10:47:54 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2022-07-20 12:31:17 +00:00
|
|
|
if (isNoiseSuppressionEnabled(getState())) {
|
|
|
|
dispatch(setNoiseSuppressionEnabled(false));
|
|
|
|
} else {
|
|
|
|
dispatch(setNoiseSuppressionEnabled(true));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to enable or disable noise suppression using the {@link NoiseSuppressionEffect}.
|
|
|
|
*
|
|
|
|
* @param {boolean} enabled - Enable or disable noise suppression.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
export function setNoiseSuppressionEnabled(enabled: boolean): any {
|
2022-10-11 10:47:54 +00:00
|
|
|
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2022-07-20 12:31:17 +00:00
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
const localAudio = getLocalJitsiAudioTrack(state);
|
|
|
|
const noiseSuppressionEnabled = isNoiseSuppressionEnabled(state);
|
|
|
|
|
|
|
|
logger.info(`Attempting to set noise suppression enabled state: ${enabled}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (enabled && !noiseSuppressionEnabled) {
|
|
|
|
if (!canEnableNoiseSuppression(state, dispatch, localAudio)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await localAudio.setEffect(new NoiseSuppressionEffect());
|
|
|
|
dispatch(setNoiseSuppressionEnabledState(true));
|
|
|
|
logger.info('Noise suppression enabled.');
|
|
|
|
|
|
|
|
} else if (!enabled && noiseSuppressionEnabled) {
|
|
|
|
await localAudio.setEffect(undefined);
|
|
|
|
dispatch(setNoiseSuppressionEnabledState(false));
|
|
|
|
logger.info('Noise suppression disabled.');
|
|
|
|
} else {
|
|
|
|
logger.warn(`Noise suppression enabled state already: ${enabled}`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Failed to set noise suppression enabled to: ${enabled}`,
|
|
|
|
error
|
|
|
|
);
|
|
|
|
|
2022-10-11 10:47:54 +00:00
|
|
|
// @ts-ignore
|
2022-07-20 12:31:17 +00:00
|
|
|
dispatch(showErrorNotification({
|
|
|
|
titleKey: 'notify.noiseSuppressionFailedTitle'
|
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|