BlueGit

ict rusty-teams

Files

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

mic_processing.rs

mic_processing.rs 3.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 Off,
8}
9
10impl NoiseSuppression {
11 pub const ALL: [NoiseSuppression; 2] = [NoiseSuppression::Auto, NoiseSuppression::Off];
12
13 pub fn label(self) -> &'static str {
14 match self {
15 NoiseSuppression::Auto => "Auto",
16 NoiseSuppression::Off => "Off",
17 }
18 }
19
20 pub fn key(self) -> &'static str {
21 match self {
22 NoiseSuppression::Auto => "auto",
23 NoiseSuppression::Off => "off",
24 }
25 }
26
27 pub fn from_key(key: &str) -> Option<NoiseSuppression> {
28 NoiseSuppression::ALL.into_iter().find(|level| level.key() == key)
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
33pub struct MicSettings {
34 pub noise_suppression: NoiseSuppression,
35 pub music_mode: bool,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub struct ApmConfig {
40 pub echo_canceller: bool,
41 pub gain_controller: bool,
42 pub high_pass_filter: bool,
43 pub noise_suppression: bool,
44}
45
46impl ApmConfig {
47 pub fn is_passthrough(self) -> bool {
48 self == ApmConfig { echo_canceller: false, gain_controller: false, high_pass_filter: false, noise_suppression: false }
49 }
50
51 pub fn module(self) -> Option<AudioProcessingModule> {
52 (!self.is_passthrough()).then(|| {
53 AudioProcessingModule::new(self.echo_canceller, self.gain_controller, self.high_pass_filter, self.noise_suppression)
54 })
55 }
56}
57
58impl MicSettings {
59 pub fn apm_config(self) -> ApmConfig {
60 if self.music_mode {
61 return ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false };
62 }
63 let on = self.noise_suppression == NoiseSuppression::Auto;
64 ApmConfig { echo_canceller: on, gain_controller: on, high_pass_filter: on, noise_suppression: on }
65 }
66
67 pub fn uses_device_track(self) -> bool {
68 self == MicSettings::default()
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn auto_keeps_the_device_track_with_everything_on() {
78 let settings = MicSettings::default();
79 assert!(settings.uses_device_track());
80 assert_eq!(
81 settings.apm_config(),
82 ApmConfig { echo_canceller: true, gain_controller: true, high_pass_filter: true, noise_suppression: true }
83 );
84 }
85
86 #[test]
87 fn off_leaves_the_device_track_and_processes_nothing() {
88 let settings = MicSettings { noise_suppression: NoiseSuppression::Off, music_mode: false };
89 assert!(!settings.uses_device_track());
90 assert!(settings.apm_config().is_passthrough());
91 assert!(settings.apm_config().module().is_none());
92 }
93
94 #[test]
95 fn music_mode_keeps_only_echo_cancellation_whatever_the_noise_choice() {
96 for noise_suppression in NoiseSuppression::ALL {
97 let settings = MicSettings { noise_suppression, music_mode: true };
98 assert!(!settings.uses_device_track());
99 assert_eq!(
100 settings.apm_config(),
101 ApmConfig { echo_canceller: true, gain_controller: false, high_pass_filter: false, noise_suppression: false }
102 );
103 }
104 }
105
106 #[test]
107 fn noise_levels_round_trip_through_their_keys() {
108 for level in NoiseSuppression::ALL {
109 assert_eq!(NoiseSuppression::from_key(level.key()), Some(level));
110 }
111 assert_eq!(NoiseSuppression::from_key("high"), None);
112 }
113}