BlueGit

ict rusty-teams

Files

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

mic_processing.rs

mic_processing.rs 5.9 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] = [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)]
36pub struct MicSettings {
37 pub noise_suppression: NoiseSuppression,
38 pub music_mode: bool,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub 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
49impl 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.noise_suppression)
56 .then(|| AudioProcessingModule::new(self.echo_canceller, false, false, self.noise_suppression))
57 }
58
59}
60
61impl MicSettings {
62 pub fn apm_config(self) -> ApmConfig {
63 if self.music_mode {
64 return ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false };
65 }
66 let on = self.noise_suppression != NoiseSuppression::Off;
67 let apm_noise_suppression = self.noise_suppression == NoiseSuppression::Low;
68 ApmConfig { echo_canceller: on, gain_controller: on, high_pass_filter: on, noise_suppression: apm_noise_suppression }
69 }
70
71 pub fn uses_device_track(self) -> bool {
72 self.uses_device_track_on(cfg!(windows))
73 }
74
75 /// On Linux, Auto and Low keep the Pulse ADM device track (same path Web Teams uses).
76 /// Music mode and Off still run the software chain; Windows always does.
77 pub fn uses_device_track_on(self, windows: bool) -> bool {
78 !windows
79 && !self.music_mode
80 && matches!(self.noise_suppression, NoiseSuppression::Auto | NoiseSuppression::Low)
81 }
82
83 pub fn ml_noise_suppression(self) -> bool {
84 !self.music_mode && self.noise_suppression == NoiseSuppression::Auto
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn auto_uses_the_device_track_on_linux_and_keeps_ml_for_the_windows_chain() {
94 let settings = MicSettings::default();
95 assert!(settings.uses_device_track_on(false));
96 assert!(!settings.uses_device_track_on(true));
97 assert!(settings.ml_noise_suppression());
98 assert_eq!(
99 settings.apm_config(),
100 ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: false }
101 );
102 }
103
104 #[test]
105 fn low_keeps_the_device_track_with_the_apm_noise_suppression() {
106 let settings = MicSettings { noise_suppression: NoiseSuppression::Low, music_mode: false };
107 assert!(settings.uses_device_track_on(false));
108 assert!(!settings.ml_noise_suppression());
109 assert_eq!(
110 settings.apm_config(),
111 ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: true }
112 );
113 }
114
115 #[test]
116 fn only_auto_outside_music_mode_runs_the_ml_stage() {
117 assert!(MicSettings::default().ml_noise_suppression());
118 assert!(!MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false }.ml_noise_suppression());
119 assert!(!MicSettings { noise_suppression: NoiseSuppression::Auto, music_mode: true }.ml_noise_suppression());
120 }
121
122 #[test]
123 fn windows_never_uses_the_device_track() {
124 for noise_suppression in NoiseSuppression::ALL {
125 for music_mode in [false, true] {
126 let settings = MicSettings { noise_suppression, music_mode };
127 assert!(!settings.uses_device_track_on(true));
128 let linux_device = !music_mode
129 && matches!(noise_suppression, NoiseSuppression::Auto | NoiseSuppression::Low);
130 assert_eq!(settings.uses_device_track_on(false), linux_device);
131 }
132 }
133 }
134
135 #[test]
136 fn off_leaves_the_device_track_and_processes_nothing() {
137 let settings = MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false };
138 assert!(!settings.uses_device_track());
139 assert!(settings.apm_config().is_passthrough());
140 assert!(settings.apm_config().front_module().is_none());
141 }
142
143 #[test]
144 fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() {
145 for noise_suppression in NoiseSuppression::ALL {
146 let settings = MicSettings { noise_suppression, music_mode: true };
147 assert!(!settings.uses_device_track());
148 assert_eq!(
149 settings.apm_config(),
150 ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false }
151 );
152 }
153 }
154
155 #[test]
156 fn noise_levels_round_trip_through_their_keys() {
157 for level in NoiseSuppression::ALL {
158 assert_eq!(NoiseSuppression::from_key(level.key()), Some(level));
159 }
160 assert_eq!(NoiseSuppression::from_key("high"), None);
161 assert_eq!(NoiseSuppression::Low.key(), "low");
162 assert_eq!(NoiseSuppression::Low.label(), "Low");
163 }
164}