BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / mic_processing.rs

mic_processing.rs

mic_processing.rs 6.1 KiB Download Raw
1use libwebrtc::native::apm::AudioProcessingModule;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
4pub enum NoiseSuppression {
5 #[default]
6 Auto,
7 Low,
8 Off,
9}
10
11impl 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)]
42pub struct MicSettings {
43 pub noise_suppression: NoiseSuppression,
44 pub music_mode: bool,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub 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
55impl 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
72impl 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 // Off only turns noise suppression off: echo cancellation and gain control stay on, as in Teams.
83 ApmConfig {
84 echo_canceller: true,
85 gain_controller: true,
86 high_pass_filter: true,
87 noise_suppression: self.noise_suppression == NoiseSuppression::Low,
88 }
89 }
90
91 /// Every noise level uses the native WebRTC platform ADM device track on every desktop OS
92 /// (WASAPI2/CoreAudio on Windows, Pulse/PipeWire on Linux, CoreAudio on macOS), matching
93 /// Web Teams and avoiding software-feeder jitter and unsynchronized AEC.
94 /// Only music mode runs the software chain.
95 pub fn uses_device_track(self) -> bool {
96 !self.music_mode
97 }
98
99 pub fn ml_noise_suppression(self) -> bool {
100 !self.music_mode && self.noise_suppression == NoiseSuppression::Auto
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn auto_uses_the_device_track_on_all_platforms() {
110 let settings = MicSettings::default();
111 assert!(settings.uses_device_track());
112 assert!(settings.ml_noise_suppression());
113 assert_eq!(
114 settings.apm_config(),
115 ApmConfig {
116 echo_canceller: true,
117 gain_controller: true,
118 high_pass_filter: true,
119 noise_suppression: false
120 }
121 );
122 }
123
124 #[test]
125 fn low_keeps_the_platform_device_track_with_apm_noise_suppression() {
126 let settings = MicSettings {
127 noise_suppression: NoiseSuppression::Low,
128 music_mode: false,
129 };
130 assert!(settings.uses_device_track());
131 assert!(!settings.ml_noise_suppression());
132 assert_eq!(
133 settings.apm_config(),
134 ApmConfig {
135 echo_canceller: true,
136 gain_controller: true,
137 high_pass_filter: true,
138 noise_suppression: true
139 }
140 );
141 }
142
143 #[test]
144 fn only_auto_outside_music_mode_runs_the_ml_stage() {
145 assert!(MicSettings::default().ml_noise_suppression());
146 assert!(
147 !MicSettings {
148 noise_suppression: NoiseSuppression::Off,
149 music_mode: false
150 }
151 .ml_noise_suppression()
152 );
153 assert!(
154 !MicSettings {
155 noise_suppression: NoiseSuppression::Auto,
156 music_mode: true
157 }
158 .ml_noise_suppression()
159 );
160 }
161
162 #[test]
163 fn off_keeps_echo_cancellation_and_gain_control_on_the_device_track() {
164 let settings = MicSettings {
165 noise_suppression: NoiseSuppression::Off,
166 music_mode: false,
167 };
168 assert!(settings.uses_device_track());
169 assert!(!settings.ml_noise_suppression());
170 assert_eq!(
171 settings.apm_config(),
172 ApmConfig {
173 echo_canceller: true,
174 gain_controller: true,
175 high_pass_filter: true,
176 noise_suppression: false
177 }
178 );
179 }
180
181 #[test]
182 fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() {
183 for noise_suppression in NoiseSuppression::ALL {
184 let settings = MicSettings {
185 noise_suppression,
186 music_mode: true,
187 };
188 assert!(!settings.uses_device_track());
189 assert_eq!(
190 settings.apm_config(),
191 ApmConfig {
192 echo_canceller: true,
193 gain_controller: false,
194 high_pass_filter: false,
195 noise_suppression: false
196 }
197 );
198 }
199 }
200
201 #[test]
202 fn noise_levels_round_trip_through_their_keys() {
203 for level in NoiseSuppression::ALL {
204 assert_eq!(NoiseSuppression::from_key(level.key()), Some(level));
205 }
206 assert_eq!(NoiseSuppression::from_key("high"), None);
207 assert_eq!(NoiseSuppression::Low.key(), "low");
208 assert_eq!(NoiseSuppression::Low.label(), "Low");
209 }
210}