From 9acda0b8ac15fa7b876bbc8abaef172d8e748612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=85hgren?= Date: Thu, 31 Aug 2023 12:14:38 +0000 Subject: [PATCH] Add support for toggling noise suppression effect on Android MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I4868bd6531bde08c4108b0941086add210660dcb Bug: b/279738239 Change-Id: I4868bd6531bde08c4108b0941086add210660dcb Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/318320 Reviewed-by: Xavier Lepaul‎ Reviewed-by: Per Åhgren Commit-Queue: Per Åhgren Cr-Commit-Position: refs/heads/main@{#40672} --- .../api/org/webrtc/audio/AudioDeviceModule.java | 8 ++++++++ .../api/org/webrtc/audio/JavaAudioDeviceModule.java | 6 ++++++ .../java/org/webrtc/audio/WebRtcAudioEffects.java | 13 +++++++++++++ .../java/org/webrtc/audio/WebRtcAudioRecord.java | 12 ++++++++++++ 4 files changed, 39 insertions(+) diff --git a/sdk/android/api/org/webrtc/audio/AudioDeviceModule.java b/sdk/android/api/org/webrtc/audio/AudioDeviceModule.java index 502c68cc9a..c4bb45f419 100644 --- a/sdk/android/api/org/webrtc/audio/AudioDeviceModule.java +++ b/sdk/android/api/org/webrtc/audio/AudioDeviceModule.java @@ -35,4 +35,12 @@ public interface AudioDeviceModule { /** Control muting/unmuting the microphone. */ void setMicrophoneMute(boolean mute); + + /** + * Enable or disable built in noise suppressor. Returns true if the enabling was successful, + * otherwise false is returned. + */ + default boolean setNoiseSuppressorEnabled(boolean enabled) { + return false; + } } diff --git a/sdk/android/api/org/webrtc/audio/JavaAudioDeviceModule.java b/sdk/android/api/org/webrtc/audio/JavaAudioDeviceModule.java index d3d57602a8..b118843ea0 100644 --- a/sdk/android/api/org/webrtc/audio/JavaAudioDeviceModule.java +++ b/sdk/android/api/org/webrtc/audio/JavaAudioDeviceModule.java @@ -418,6 +418,12 @@ public class JavaAudioDeviceModule implements AudioDeviceModule { audioInput.setMicrophoneMute(mute); } + @Override + public boolean setNoiseSuppressorEnabled(boolean enabled) { + Logging.d(TAG, "setNoiseSuppressorEnabled: " + enabled); + return audioInput.setNoiseSuppressorEnabled(enabled); + } + /** * Start to prefer a specific {@link AudioDeviceInfo} device for recording. Typically this should * only be used if a client gives an explicit option for choosing a physical device to record diff --git a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioEffects.java b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioEffects.java index a9ff1011b6..1e80e485fb 100644 --- a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioEffects.java +++ b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioEffects.java @@ -104,6 +104,19 @@ class WebRtcAudioEffects { return true; } + // Toggles an existing NoiseSuppressor to be enabled or disabled. + // Returns true if the toggling was successful, otherwise false is returned (this is also the case + // if no NoiseSuppressor was present). + public boolean toggleNS(boolean enable) { + if (ns == null) { + Logging.e(TAG, "Attempting to enable or disable nonexistent NoiseSuppressor."); + return false; + } + Logging.d(TAG, "toggleNS(" + enable + ")"); + boolean toggling_succeeded = ns.setEnabled(enable) == AudioEffect.SUCCESS; + return toggling_succeeded; + } + public void enable(int audioSession) { Logging.d(TAG, "enable(audioSession=" + audioSession + ")"); assertTrue(aec == null); diff --git a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java index 6647e5fcbb..cfb651f6cd 100644 --- a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java +++ b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java @@ -511,6 +511,18 @@ class WebRtcAudioRecord { microphoneMute = mute; } + // Sets whether NoiseSuppressor should be enabled or disabled. + // Returns true if the enabling was successful, otherwise false is returned (this is also the case + // if the NoiseSuppressor effect is not supported). + public boolean setNoiseSuppressorEnabled(boolean enabled) { + if (!WebRtcAudioEffects.isNoiseSuppressorSupported()) { + Logging.e(TAG, "Noise suppressor is not supported."); + return false; + } + Logging.w(TAG, "SetNoiseSuppressorEnabled(" + enabled + ")"); + return effects.toggleNS(enabled); + } + // Releases the native AudioRecord resources. private void releaseAudioResources() { Logging.d(TAG, "releaseAudioResources");