BlueGit

ict rusty-teams

Files

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

mic_processing.rs

mic_processing.rs 5.5 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 pub fn uses_device_track_on(self, windows: bool) -> bool {
76 !windows && !self.music_mode && self.noise_suppression == NoiseSuppression::Low
77 }
78
79 pub fn ml_noise_suppression(self) -> bool {
80 !self.music_mode && self.noise_suppression == NoiseSuppression::Auto
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn auto_leaves_noise_to_the_ml_stage_and_keeps_the_rest_of_the_apm() {
90 let settings = MicSettings::default();
91 assert!(!settings.uses_device_track_on(false));
92 assert!(settings.ml_noise_suppression());
93 assert_eq!(
94 settings.apm_config(),
95 ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: false }
96 );
97 }
98
99 #[test]
100 fn low_keeps_the_device_track_with_the_apm_noise_suppression() {
101 let settings = MicSettings { noise_suppression: NoiseSuppression::Low, music_mode: false };
102 assert!(settings.uses_device_track_on(false));
103 assert!(!settings.ml_noise_suppression());
104 assert_eq!(
105 settings.apm_config(),
106 ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: true }
107 );
108 }
109
110 #[test]
111 fn only_auto_outside_music_mode_runs_the_ml_stage() {
112 assert!(MicSettings::default().ml_noise_suppression());
113 assert!(!MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false }.ml_noise_suppression());
114 assert!(!MicSettings { noise_suppression: NoiseSuppression::Auto, music_mode: true }.ml_noise_suppression());
115 }
116
117 #[test]
118 fn windows_never_uses_the_device_track() {
119 for noise_suppression in NoiseSuppression::ALL {
120 for music_mode in [false, true] {
121 let settings = MicSettings { noise_suppression, music_mode };
122 assert!(!settings.uses_device_track_on(true));
123 assert_eq!(settings.uses_device_track_on(false), !music_mode && noise_suppression == NoiseSuppression::Low);
124 }
125 }
126 }
127
128 #[test]
129 fn off_leaves_the_device_track_and_processes_nothing() {
130 let settings = MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false };
131 assert!(!settings.uses_device_track());
132 assert!(settings.apm_config().is_passthrough());
133 assert!(settings.apm_config().front_module().is_none());
134 }
135
136 #[test]
137 fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() {
138 for noise_suppression in NoiseSuppression::ALL {
139 let settings = MicSettings { noise_suppression, music_mode: true };
140 assert!(!settings.uses_device_track());
141 assert_eq!(
142 settings.apm_config(),
143 ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false }
144 );
145 }
146 }
147
148 #[test]
149 fn noise_levels_round_trip_through_their_keys() {
150 for level in NoiseSuppression::ALL {
151 assert_eq!(NoiseSuppression::from_key(level.key()), Some(level));
152 }
153 assert_eq!(NoiseSuppression::from_key("high"), None);
154 assert_eq!(NoiseSuppression::Low.key(), "low");
155 assert_eq!(NoiseSuppression::Low.label(), "Low");
156 }
157}