BlueGit

ict rusty-teams

Files

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

mic_processing.rs

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