| 1 | use libwebrtc::native::apm::AudioProcessingModule; |
| 2 | |
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 4 | pub enum NoiseSuppression { |
| 5 | #[default] |
| 6 | Auto, |
| 7 | Low, |
| 8 | Off, |
| 9 | } |
| 10 | |
| 11 | impl NoiseSuppression { |
| 12 | pub const ALL: [NoiseSuppression; 3] = [ |
| 13 | NoiseSuppression::Auto, |
| 14 | NoiseSuppression::Low, |
| 15 | NoiseSuppression::Off, |
| 16 | ]; |
| 17 | |
| 18 | pub fn label(self) -> &'static str { |
| 19 | match self { |
| 20 | NoiseSuppression::Auto => "Auto", |
| 21 | NoiseSuppression::Low => "Low", |
| 22 | NoiseSuppression::Off => "Off", |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn key(self) -> &'static str { |
| 27 | match self { |
| 28 | NoiseSuppression::Auto => "auto", |
| 29 | NoiseSuppression::Low => "low", |
| 30 | NoiseSuppression::Off => "off", |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn from_key(key: &str) -> Option<NoiseSuppression> { |
| 35 | NoiseSuppression::ALL |
| 36 | .into_iter() |
| 37 | .find(|level| level.key() == key) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 42 | pub struct MicSettings { |
| 43 | pub noise_suppression: NoiseSuppression, |
| 44 | pub music_mode: bool, |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 48 | pub struct ApmConfig { |
| 49 | pub echo_canceller: bool, |
| 50 | pub gain_controller: bool, |
| 51 | pub high_pass_filter: bool, |
| 52 | pub noise_suppression: bool, |
| 53 | } |
| 54 | |
| 55 | impl ApmConfig { |
| 56 | pub fn is_passthrough(self) -> bool { |
| 57 | self == ApmConfig { |
| 58 | echo_canceller: false, |
| 59 | gain_controller: false, |
| 60 | high_pass_filter: false, |
| 61 | noise_suppression: false, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn front_module(self) -> Option<AudioProcessingModule> { |
| 66 | (self.echo_canceller || self.noise_suppression).then(|| { |
| 67 | AudioProcessingModule::new(self.echo_canceller, false, false, self.noise_suppression) |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl MicSettings { |
| 73 | pub fn apm_config(self) -> ApmConfig { |
| 74 | if self.music_mode { |
| 75 | return ApmConfig { |
| 76 | echo_canceller: true, |
| 77 | gain_controller: false, |
| 78 | high_pass_filter: false, |
| 79 | noise_suppression: false, |
| 80 | }; |
| 81 | } |
| 82 | let on = self.noise_suppression != NoiseSuppression::Off; |
| 83 | let apm_noise_suppression = self.noise_suppression == NoiseSuppression::Low; |
| 84 | ApmConfig { |
| 85 | echo_canceller: on, |
| 86 | gain_controller: on, |
| 87 | high_pass_filter: on, |
| 88 | noise_suppression: apm_noise_suppression, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub fn uses_device_track(self) -> bool { |
| 93 | self.uses_device_track_on(cfg!(windows)) |
| 94 | } |
| 95 | |
| 96 | /// On Linux and macOS, Auto and Low keep the platform ADM device track |
| 97 | /// (Pulse / CoreAudio — same path Web Teams uses). Music mode and Off still |
| 98 | /// run the software chain; Windows always does. |
| 99 | pub fn uses_device_track_on(self, windows: bool) -> bool { |
| 100 | !windows |
| 101 | && !self.music_mode |
| 102 | && matches!( |
| 103 | self.noise_suppression, |
| 104 | NoiseSuppression::Auto | NoiseSuppression::Low |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | pub fn ml_noise_suppression(self) -> bool { |
| 109 | !self.music_mode && self.noise_suppression == NoiseSuppression::Auto |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #[cfg(test)] |
| 114 | mod tests { |
| 115 | use super::*; |
| 116 | |
| 117 | #[test] |
| 118 | fn auto_uses_the_device_track_on_linux_and_macos_and_keeps_ml_for_the_windows_chain() { |
| 119 | let settings = MicSettings::default(); |
| 120 | // Non-Windows (Pulse on Linux, CoreAudio on macOS) keeps the ADM track. |
| 121 | assert!(settings.uses_device_track_on(false)); |
| 122 | assert!(!settings.uses_device_track_on(true)); |
| 123 | assert!(settings.ml_noise_suppression()); |
| 124 | assert_eq!( |
| 125 | settings.apm_config(), |
| 126 | ApmConfig { |
| 127 | echo_canceller: true, |
| 128 | gain_controller: true, |
| 129 | high_pass_filter: true, |
| 130 | noise_suppression: false |
| 131 | } |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn low_keeps_the_coreaudio_or_pulse_device_track_with_the_apm_noise_suppression() { |
| 137 | let settings = MicSettings { |
| 138 | noise_suppression: NoiseSuppression::Low, |
| 139 | music_mode: false, |
| 140 | }; |
| 141 | assert!(settings.uses_device_track_on(false)); |
| 142 | assert!(!settings.ml_noise_suppression()); |
| 143 | assert_eq!( |
| 144 | settings.apm_config(), |
| 145 | ApmConfig { |
| 146 | echo_canceller: true, |
| 147 | gain_controller: true, |
| 148 | high_pass_filter: true, |
| 149 | noise_suppression: true |
| 150 | } |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn only_auto_outside_music_mode_runs_the_ml_stage() { |
| 156 | assert!(MicSettings::default().ml_noise_suppression()); |
| 157 | assert!( |
| 158 | !MicSettings { |
| 159 | noise_suppression: NoiseSuppression::Off, |
| 160 | music_mode: false |
| 161 | } |
| 162 | .ml_noise_suppression() |
| 163 | ); |
| 164 | assert!( |
| 165 | !MicSettings { |
| 166 | noise_suppression: NoiseSuppression::Auto, |
| 167 | music_mode: true |
| 168 | } |
| 169 | .ml_noise_suppression() |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn windows_never_uses_the_device_track() { |
| 175 | for noise_suppression in NoiseSuppression::ALL { |
| 176 | for music_mode in [false, true] { |
| 177 | let settings = MicSettings { |
| 178 | noise_suppression, |
| 179 | music_mode, |
| 180 | }; |
| 181 | assert!(!settings.uses_device_track_on(true)); |
| 182 | let non_windows_device = !music_mode |
| 183 | && matches!( |
| 184 | noise_suppression, |
| 185 | NoiseSuppression::Auto | NoiseSuppression::Low |
| 186 | ); |
| 187 | assert_eq!(settings.uses_device_track_on(false), non_windows_device); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn off_leaves_the_device_track_and_processes_nothing() { |
| 194 | let settings = MicSettings { |
| 195 | noise_suppression: NoiseSuppression::Off, |
| 196 | music_mode: false, |
| 197 | }; |
| 198 | assert!(!settings.uses_device_track()); |
| 199 | assert!(settings.apm_config().is_passthrough()); |
| 200 | assert!(settings.apm_config().front_module().is_none()); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() { |
| 205 | for noise_suppression in NoiseSuppression::ALL { |
| 206 | let settings = MicSettings { |
| 207 | noise_suppression, |
| 208 | music_mode: true, |
| 209 | }; |
| 210 | assert!(!settings.uses_device_track()); |
| 211 | assert_eq!( |
| 212 | settings.apm_config(), |
| 213 | ApmConfig { |
| 214 | echo_canceller: true, |
| 215 | gain_controller: false, |
| 216 | high_pass_filter: false, |
| 217 | noise_suppression: false |
| 218 | } |
| 219 | ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | #[test] |
| 224 | fn noise_levels_round_trip_through_their_keys() { |
| 225 | for level in NoiseSuppression::ALL { |
| 226 | assert_eq!(NoiseSuppression::from_key(level.key()), Some(level)); |
| 227 | } |
| 228 | assert_eq!(NoiseSuppression::from_key("high"), None); |
| 229 | assert_eq!(NoiseSuppression::Low.key(), "low"); |
| 230 | assert_eq!(NoiseSuppression::Low.label(), "Low"); |
| 231 | } |
| 232 | } |