| 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] = [NoiseSuppression::Auto, NoiseSuppression::Low, NoiseSuppression::Off]; |
| 13 | |
| 14 | pub fn label(self) -> &'static str { |
| 15 | match self { |
| 16 | NoiseSuppression::Auto => "Auto", |
| 17 | NoiseSuppression::Low => "Low", |
| 18 | NoiseSuppression::Off => "Off", |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | pub fn key(self) -> &'static str { |
| 23 | match self { |
| 24 | NoiseSuppression::Auto => "auto", |
| 25 | NoiseSuppression::Low => "low", |
| 26 | NoiseSuppression::Off => "off", |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub fn from_key(key: &str) -> Option<NoiseSuppression> { |
| 31 | NoiseSuppression::ALL.into_iter().find(|level| level.key() == key) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 36 | pub struct MicSettings { |
| 37 | pub noise_suppression: NoiseSuppression, |
| 38 | pub music_mode: bool, |
| 39 | } |
| 40 | |
| 41 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 42 | pub struct ApmConfig { |
| 43 | pub echo_canceller: bool, |
| 44 | pub gain_controller: bool, |
| 45 | pub high_pass_filter: bool, |
| 46 | pub noise_suppression: bool, |
| 47 | } |
| 48 | |
| 49 | impl ApmConfig { |
| 50 | pub fn is_passthrough(self) -> bool { |
| 51 | self == ApmConfig { echo_canceller: false, gain_controller: false, high_pass_filter: false, noise_suppression: false } |
| 52 | } |
| 53 | |
| 54 | pub fn front_module(self) -> Option<AudioProcessingModule> { |
| 55 | (self.echo_canceller || self.high_pass_filter || self.noise_suppression) |
| 56 | .then(|| AudioProcessingModule::new(self.echo_canceller, false, self.high_pass_filter, self.noise_suppression)) |
| 57 | } |
| 58 | |
| 59 | pub fn gain_module(self) -> Option<AudioProcessingModule> { |
| 60 | self.gain_controller.then(|| AudioProcessingModule::new(false, true, false, false)) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl MicSettings { |
| 65 | pub fn apm_config(self) -> ApmConfig { |
| 66 | if self.music_mode { |
| 67 | return ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false }; |
| 68 | } |
| 69 | let on = self.noise_suppression != NoiseSuppression::Off; |
| 70 | let apm_noise_suppression = self.noise_suppression == NoiseSuppression::Low; |
| 71 | ApmConfig { echo_canceller: on, gain_controller: on, high_pass_filter: on, noise_suppression: apm_noise_suppression } |
| 72 | } |
| 73 | |
| 74 | pub fn uses_device_track(self) -> bool { |
| 75 | self.uses_device_track_on(cfg!(windows)) |
| 76 | } |
| 77 | |
| 78 | pub fn uses_device_track_on(self, windows: bool) -> bool { |
| 79 | !windows && !self.music_mode && self.noise_suppression != NoiseSuppression::Off |
| 80 | } |
| 81 | |
| 82 | pub fn ml_noise_suppression(self) -> bool { |
| 83 | !self.music_mode && self.noise_suppression == NoiseSuppression::Auto |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[cfg(test)] |
| 88 | mod tests { |
| 89 | use super::*; |
| 90 | |
| 91 | #[test] |
| 92 | fn auto_leaves_noise_to_the_ml_stage_and_keeps_the_rest_of_the_apm() { |
| 93 | let settings = MicSettings::default(); |
| 94 | assert!(settings.uses_device_track_on(false)); |
| 95 | assert!(settings.ml_noise_suppression()); |
| 96 | assert_eq!( |
| 97 | settings.apm_config(), |
| 98 | ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: false } |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn low_keeps_the_device_track_with_the_apm_noise_suppression() { |
| 104 | let settings = MicSettings { noise_suppression: NoiseSuppression::Low, music_mode: false }; |
| 105 | assert!(settings.uses_device_track_on(false)); |
| 106 | assert!(!settings.ml_noise_suppression()); |
| 107 | assert_eq!( |
| 108 | settings.apm_config(), |
| 109 | ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: true } |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn gain_runs_in_its_own_module_after_the_front_stages() { |
| 115 | let config = MicSettings::default().apm_config(); |
| 116 | assert!(config.front_module().is_some()); |
| 117 | assert!(config.gain_module().is_some()); |
| 118 | let music = MicSettings { noise_suppression: NoiseSuppression::Auto, music_mode: true }.apm_config(); |
| 119 | assert!(music.front_module().is_some()); |
| 120 | assert!(music.gain_module().is_none()); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn only_auto_outside_music_mode_runs_the_ml_stage() { |
| 125 | assert!(MicSettings::default().ml_noise_suppression()); |
| 126 | assert!(!MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false }.ml_noise_suppression()); |
| 127 | assert!(!MicSettings { noise_suppression: NoiseSuppression::Auto, music_mode: true }.ml_noise_suppression()); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn windows_never_uses_the_device_track() { |
| 132 | for noise_suppression in NoiseSuppression::ALL { |
| 133 | for music_mode in [false, true] { |
| 134 | let settings = MicSettings { noise_suppression, music_mode }; |
| 135 | assert!(!settings.uses_device_track_on(true)); |
| 136 | assert_eq!(settings.uses_device_track_on(false), !music_mode && noise_suppression != NoiseSuppression::Off); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn off_leaves_the_device_track_and_processes_nothing() { |
| 143 | let settings = MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false }; |
| 144 | assert!(!settings.uses_device_track()); |
| 145 | assert!(settings.apm_config().is_passthrough()); |
| 146 | assert!(settings.apm_config().front_module().is_none()); |
| 147 | assert!(settings.apm_config().gain_module().is_none()); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() { |
| 152 | for noise_suppression in NoiseSuppression::ALL { |
| 153 | let settings = MicSettings { noise_suppression, music_mode: true }; |
| 154 | assert!(!settings.uses_device_track()); |
| 155 | assert_eq!( |
| 156 | settings.apm_config(), |
| 157 | ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false } |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn noise_levels_round_trip_through_their_keys() { |
| 164 | for level in NoiseSuppression::ALL { |
| 165 | assert_eq!(NoiseSuppression::from_key(level.key()), Some(level)); |
| 166 | } |
| 167 | assert_eq!(NoiseSuppression::from_key("high"), None); |
| 168 | assert_eq!(NoiseSuppression::Low.key(), "low"); |
| 169 | assert_eq!(NoiseSuppression::Low.label(), "Low"); |
| 170 | } |
| 171 | } |