BlueGit

ict rusty-teams

Files

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

mic_processing.rs

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