You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { IStore } from '../app/types';
  2. import { getLocalJitsiAudioTrack } from '../base/tracks/functions';
  3. import { showErrorNotification } from '../notifications/actions';
  4. import { NoiseSuppressionEffect } from '../stream-effects/noise-suppression/NoiseSuppressionEffect';
  5. import { SET_NOISE_SUPPRESSION_ENABLED } from './actionTypes';
  6. import { canEnableNoiseSuppression, isNoiseSuppressionEnabled } from './functions';
  7. import logger from './logger';
  8. /**
  9. * Updates the noise suppression active state.
  10. *
  11. * @param {boolean} enabled - Is noise suppression enabled.
  12. * @returns {{
  13. * type: SET_NOISE_SUPPRESSION_STATE,
  14. * enabled: boolean
  15. * }}
  16. */
  17. export function setNoiseSuppressionEnabledState(enabled: boolean): any {
  18. return {
  19. type: SET_NOISE_SUPPRESSION_ENABLED,
  20. enabled
  21. };
  22. }
  23. /**
  24. * Enabled/disable noise suppression depending on the current state.
  25. *
  26. * @returns {Function}
  27. */
  28. export function toggleNoiseSuppression(): any {
  29. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  30. if (isNoiseSuppressionEnabled(getState())) {
  31. dispatch(setNoiseSuppressionEnabled(false));
  32. } else {
  33. dispatch(setNoiseSuppressionEnabled(true));
  34. }
  35. };
  36. }
  37. /**
  38. * Attempt to enable or disable noise suppression using the {@link NoiseSuppressionEffect}.
  39. *
  40. * @param {boolean} enabled - Enable or disable noise suppression.
  41. *
  42. * @returns {Function}
  43. */
  44. export function setNoiseSuppressionEnabled(enabled: boolean): any {
  45. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  46. const state = getState();
  47. const { noiseSuppression: nsOptions } = state['features/base/config'];
  48. const localAudio = getLocalJitsiAudioTrack(state);
  49. const noiseSuppressionEnabled = isNoiseSuppressionEnabled(state);
  50. logger.info(`Attempting to set noise suppression enabled state: ${enabled}`);
  51. if (enabled === noiseSuppressionEnabled) {
  52. logger.warn(`Noise suppression enabled state already: ${enabled}`);
  53. return;
  54. }
  55. // If there is no local audio, simply set the enabled state. Once an audio track is created
  56. // the effects list will be applied.
  57. if (!localAudio) {
  58. dispatch(setNoiseSuppressionEnabledState(enabled));
  59. return;
  60. }
  61. try {
  62. if (enabled) {
  63. if (!canEnableNoiseSuppression(state, dispatch, localAudio)) {
  64. return;
  65. }
  66. await localAudio.setEffect(new NoiseSuppressionEffect(nsOptions));
  67. dispatch(setNoiseSuppressionEnabledState(true));
  68. logger.info('Noise suppression enabled.');
  69. } else {
  70. await localAudio.setEffect(undefined);
  71. dispatch(setNoiseSuppressionEnabledState(false));
  72. logger.info('Noise suppression disabled.');
  73. }
  74. } catch (error) {
  75. logger.error(
  76. `Failed to set noise suppression enabled to: ${enabled}`,
  77. error
  78. );
  79. dispatch(showErrorNotification({
  80. titleKey: 'notify.noiseSuppressionFailedTitle'
  81. }));
  82. }
  83. };
  84. }