BlueGit

ict rusty-teams

Files

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

mic_processing.rs

mic_processing.rs 6.0 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 /// Auto and Low use the native WebRTC platform ADM device track on every desktop OS
93 /// (WASAPI2/CoreAudio on Windows, Pulse/PipeWire on Linux, CoreAudio on macOS), matching
94 /// Web Teams and avoiding software-feeder jitter and unsynchronized AEC.
95 /// Music mode and Off run the software chain.
96 pub fn uses_device_track(self) -> bool {
97 !self.music_mode
98 && matches!(
99 self.noise_suppression,
100 NoiseSuppression::Auto | NoiseSuppression::Low
101 )
102 }
103
104 pub fn ml_noise_suppression(self) -> bool {
105 !self.music_mode && self.noise_suppression == NoiseSuppression::Auto
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn auto_uses_the_device_track_on_all_platforms() {
115 let settings = MicSettings::default();
116 assert!(settings.uses_device_track());
117 assert!(settings.ml_noise_suppression());
118 assert_eq!(
119 settings.apm_config(),
120 ApmConfig {
121 echo_canceller: true,
122 gain_controller: true,
123 high_pass_filter: true,
124 noise_suppression: false
125 }
126 );
127 }
128
129 #[test]
130 fn low_keeps_the_platform_device_track_with_apm_noise_suppression() {
131 let settings = MicSettings {
132 noise_suppression: NoiseSuppression::Low,
133 music_mode: false,
134 };
135 assert!(settings.uses_device_track());
136 assert!(!settings.ml_noise_suppression());
137 assert_eq!(
138 settings.apm_config(),
139 ApmConfig {
140 echo_canceller: true,
141 gain_controller: true,
142 high_pass_filter: true,
143 noise_suppression: true
144 }
145 );
146 }
147
148 #[test]
149 fn only_auto_outside_music_mode_runs_the_ml_stage() {
150 assert!(MicSettings::default().ml_noise_suppression());
151 assert!(
152 !MicSettings {
153 noise_suppression: NoiseSuppression::Off,
154 music_mode: false
155 }
156 .ml_noise_suppression()
157 );
158 assert!(
159 !MicSettings {
160 noise_suppression: NoiseSuppression::Auto,
161 music_mode: true
162 }
163 .ml_noise_suppression()
164 );
165 }
166
167 #[test]
168 fn off_leaves_the_device_track_and_processes_nothing() {
169 let settings = MicSettings {
170 noise_suppression: NoiseSuppression::Off,
171 music_mode: false,
172 };
173 assert!(!settings.uses_device_track());
174 assert!(settings.apm_config().is_passthrough());
175 assert!(settings.apm_config().front_module().is_none());
176 }
177
178 #[test]
179 fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() {
180 for noise_suppression in NoiseSuppression::ALL {
181 let settings = MicSettings {
182 noise_suppression,
183 music_mode: true,
184 };
185 assert!(!settings.uses_device_track());
186 assert_eq!(
187 settings.apm_config(),
188 ApmConfig {
189 echo_canceller: true,
190 gain_controller: false,
191 high_pass_filter: false,
192 noise_suppression: false
193 }
194 );
195 }
196 }
197
198 #[test]
199 fn noise_levels_round_trip_through_their_keys() {
200 for level in NoiseSuppression::ALL {
201 assert_eq!(NoiseSuppression::from_key(level.key()), Some(level));
202 }
203 assert_eq!(NoiseSuppression::from_key("high"), None);
204 assert_eq!(NoiseSuppression::Low.key(), "low");
205 assert_eq!(NoiseSuppression::Low.label(), "Low");
206 }
207}