BlueGit

ict rusty-teams

Files

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

mic_processing.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
mic_processing.rs 6.8 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 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, Auto and Low keep the Pulse ADM device track (same path Web Teams uses).
97 /// Music mode and Off still run the software chain; Windows always does.
98 pub fn uses_device_track_on(self, windows: bool) -> bool {
99 !windows
100 && !self.music_mode
101 && matches!(
102 self.noise_suppression,
103 NoiseSuppression::Auto | NoiseSuppression::Low
104 )
105 }
106
107 pub fn ml_noise_suppression(self) -> bool {
108 !self.music_mode && self.noise_suppression == NoiseSuppression::Auto
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn auto_uses_the_device_track_on_linux_and_keeps_ml_for_the_windows_chain() {
118 let settings = MicSettings::default();
119 assert!(settings.uses_device_track_on(false));
120 assert!(!settings.uses_device_track_on(true));
121 assert!(settings.ml_noise_suppression());
122 assert_eq!(
123 settings.apm_config(),
124 ApmConfig {
125 echo_canceller: true,
126 gain_controller: true,
127 high_pass_filter: true,
128 noise_suppression: false
129 }
130 );
131 }
132
133 #[test]
134 fn low_keeps_the_device_track_with_the_apm_noise_suppression() {
135 let settings = MicSettings {
136 noise_suppression: NoiseSuppression::Low,
137 music_mode: false,
138 };
139 assert!(settings.uses_device_track_on(false));
140 assert!(!settings.ml_noise_suppression());
141 assert_eq!(
142 settings.apm_config(),
143 ApmConfig {
144 echo_canceller: true,
145 gain_controller: true,
146 high_pass_filter: true,
147 noise_suppression: true
148 }
149 );
150 }
151
152 #[test]
153 fn only_auto_outside_music_mode_runs_the_ml_stage() {
154 assert!(MicSettings::default().ml_noise_suppression());
155 assert!(
156 !MicSettings {
157 noise_suppression: NoiseSuppression::Off,
158 music_mode: false
159 }
160 .ml_noise_suppression()
161 );
162 assert!(
163 !MicSettings {
164 noise_suppression: NoiseSuppression::Auto,
165 music_mode: true
166 }
167 .ml_noise_suppression()
168 );
169 }
170
171 #[test]
172 fn windows_never_uses_the_device_track() {
173 for noise_suppression in NoiseSuppression::ALL {
174 for music_mode in [false, true] {
175 let settings = MicSettings {
176 noise_suppression,
177 music_mode,
178 };
179 assert!(!settings.uses_device_track_on(true));
180 let linux_device = !music_mode
181 && matches!(
182 noise_suppression,
183 NoiseSuppression::Auto | NoiseSuppression::Low
184 );
185 assert_eq!(settings.uses_device_track_on(false), linux_device);
186 }
187 }
188 }
189
190 #[test]
191 fn off_leaves_the_device_track_and_processes_nothing() {
192 let settings = MicSettings {
193 noise_suppression: NoiseSuppression::Off,
194 music_mode: false,
195 };
196 assert!(!settings.uses_device_track());
197 assert!(settings.apm_config().is_passthrough());
198 assert!(settings.apm_config().front_module().is_none());
199 }
200
201 #[test]
202 fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() {
203 for noise_suppression in NoiseSuppression::ALL {
204 let settings = MicSettings {
205 noise_suppression,
206 music_mode: true,
207 };
208 assert!(!settings.uses_device_track());
209 assert_eq!(
210 settings.apm_config(),
211 ApmConfig {
212 echo_canceller: true,
213 gain_controller: false,
214 high_pass_filter: false,
215 noise_suppression: false
216 }
217 );
218 }
219 }
220
221 #[test]
222 fn noise_levels_round_trip_through_their_keys() {
223 for level in NoiseSuppression::ALL {
224 assert_eq!(NoiseSuppression::from_key(level.key()), Some(level));
225 }
226 assert_eq!(NoiseSuppression::from_key("high"), None);
227 assert_eq!(NoiseSuppression::Low.key(), "low");
228 assert_eq!(NoiseSuppression::Low.label(), "Low");
229 }
230}