BlueGit

ict rusty-teams

Files

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

mic_effects.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
mic_effects.rs 4.2 KiB Download Raw
1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct MicEffect {
3 pub id: u128,
4 pub enabled: bool,
5 pub can_set: bool,
6}
7
8impl MicEffect {
9 pub fn name(&self) -> String {
10 effect_name(self.id)
11 }
12
13 /// The state to switch to, when it differs from the current one and Windows lets us change it.
14 pub fn change(&self) -> Option<bool> {
15 wanted_state(self.id).filter(|wanted| self.can_set && *wanted != self.enabled)
16 }
17}
18
19const EFFECT_SUFFIX: u128 = 0x8211_11e2_8c70_2c27_d7f0_01fa;
20const EFFECT_NAMES: [(u32, &str); 19] = [
21 (0x6f64adbe, "acoustic echo cancellation"),
22 (0x6f64adbf, "noise suppression"),
23 (0x6f64adc0, "automatic gain control"),
24 (0x6f64adc1, "beamforming"),
25 (0x6f64adc2, "constant tone removal"),
26 (0x6f64adc3, "equalizer"),
27 (0x6f64adc4, "loudness equalizer"),
28 (0x6f64adc5, "bass boost"),
29 (0x6f64adc6, "virtual surround"),
30 (0x6f64adc7, "virtual headphones"),
31 (0x6f64adc8, "speaker fill"),
32 (0x6f64adc9, "room correction"),
33 (0x6f64adca, "bass management"),
34 (0x6f64adcb, "environmental effects"),
35 (0x6f64adcc, "speaker protection"),
36 (0x6f64adcd, "speaker compensation"),
37 (0x6f64adce, "dynamic range compression"),
38 (0x6f64adcf, "far field beamforming"),
39 (0x6f64add0, "deep noise suppression"),
40];
41
42const ECHO_CANCELLATION: u32 = 0x6f64adbe;
43const NOISE_SUPPRESSION: u32 = 0x6f64adbf;
44const GAIN_CONTROL: u32 = 0x6f64adc0;
45const BEAMFORMING: u32 = 0x6f64adc1;
46const RANGE_COMPRESSION: u32 = 0x6f64adce;
47const DEEP_NOISE_SUPPRESSION: u32 = 0x6f64add0;
48
49fn known_prefix(guid: u128) -> Option<u32> {
50 (guid & ((1u128 << 96) - 1) == EFFECT_SUFFIX).then_some((guid >> 96) as u32)
51}
52
53/// DeepFilterNet and AGC2 replace the Windows noise, gain and compression stages, stacking them muffles speech.
54/// The Windows echo canceller stays: it has the exact speaker signal; AEC3 after it removes the rest before AGC2 lifts it.
55pub fn wanted_state(guid: u128) -> Option<bool> {
56 match known_prefix(guid)? {
57 NOISE_SUPPRESSION | DEEP_NOISE_SUPPRESSION | GAIN_CONTROL | RANGE_COMPRESSION => {
58 Some(false)
59 }
60 BEAMFORMING => Some(true),
61 ECHO_CANCELLATION => None,
62 _ => None,
63 }
64}
65
66pub fn effect_name(guid: u128) -> String {
67 known_prefix(guid)
68 .and_then(|prefix| EFFECT_NAMES.iter().find(|(known, _)| *known == prefix))
69 .map_or_else(|| format!("{guid:032x}"), |(_, name)| (*name).to_owned())
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn our_chain_replaces_windows_noise_gain_and_compression_but_keeps_its_echo_canceller() {
78 let guid = |prefix: u32| (u128::from(prefix) << 96) | EFFECT_SUFFIX;
79 assert_eq!(wanted_state(guid(NOISE_SUPPRESSION)), Some(false));
80 assert_eq!(wanted_state(guid(DEEP_NOISE_SUPPRESSION)), Some(false));
81 assert_eq!(wanted_state(guid(GAIN_CONTROL)), Some(false));
82 assert_eq!(wanted_state(guid(RANGE_COMPRESSION)), Some(false));
83 assert_eq!(wanted_state(guid(BEAMFORMING)), Some(true));
84 assert_eq!(wanted_state(guid(ECHO_CANCELLATION)), None);
85 assert_eq!(wanted_state(0x1234), None);
86 let echo = MicEffect {
87 id: guid(ECHO_CANCELLATION),
88 enabled: true,
89 can_set: true,
90 };
91 assert!(echo.change().is_none());
92 let deep_noise = MicEffect {
93 id: guid(DEEP_NOISE_SUPPRESSION),
94 enabled: true,
95 can_set: true,
96 };
97 assert_eq!(deep_noise.change(), Some(false));
98 assert_eq!(
99 MicEffect {
100 can_set: false,
101 ..deep_noise
102 }
103 .change(),
104 None
105 );
106 assert_eq!(
107 MicEffect {
108 enabled: false,
109 ..deep_noise
110 }
111 .change(),
112 None
113 );
114 }
115
116 #[test]
117 fn known_effect_guids_get_their_names() {
118 assert_eq!(
119 effect_name(0x6f64adbe_8211_11e2_8c70_2c27d7f001fa),
120 "acoustic echo cancellation"
121 );
122 assert_eq!(
123 effect_name(0x6f64add0_8211_11e2_8c70_2c27d7f001fa),
124 "deep noise suppression"
125 );
126 assert_eq!(effect_name(0x1234), "00000000000000000000000000001234");
127 }
128}