BlueGit

ict rusty-teams

Files

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

mic_effects.rs

8fa80d73 History
mic_effects.rs 3.9 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 pub fn is_echo_cancellation(&self) -> bool {
14 known_prefix(self.id) == Some(ECHO_CANCELLATION)
15 }
16
17 /// The state to switch to, when it differs from the current one and Windows lets us change it.
18 pub fn change(&self) -> Option<bool> {
19 wanted_state(self.id).filter(|wanted| self.can_set && *wanted != self.enabled)
20 }
21}
22
23const EFFECT_SUFFIX: u128 = 0x8211_11e2_8c70_2c27_d7f0_01fa;
24const EFFECT_NAMES: [(u32, &str); 19] = [
25 (0x6f64adbe, "acoustic echo cancellation"),
26 (0x6f64adbf, "noise suppression"),
27 (0x6f64adc0, "automatic gain control"),
28 (0x6f64adc1, "beamforming"),
29 (0x6f64adc2, "constant tone removal"),
30 (0x6f64adc3, "equalizer"),
31 (0x6f64adc4, "loudness equalizer"),
32 (0x6f64adc5, "bass boost"),
33 (0x6f64adc6, "virtual surround"),
34 (0x6f64adc7, "virtual headphones"),
35 (0x6f64adc8, "speaker fill"),
36 (0x6f64adc9, "room correction"),
37 (0x6f64adca, "bass management"),
38 (0x6f64adcb, "environmental effects"),
39 (0x6f64adcc, "speaker protection"),
40 (0x6f64adcd, "speaker compensation"),
41 (0x6f64adce, "dynamic range compression"),
42 (0x6f64adcf, "far field beamforming"),
43 (0x6f64add0, "deep noise suppression"),
44];
45
46const ECHO_CANCELLATION: u32 = 0x6f64adbe;
47const NOISE_SUPPRESSION: u32 = 0x6f64adbf;
48const GAIN_CONTROL: u32 = 0x6f64adc0;
49const BEAMFORMING: u32 = 0x6f64adc1;
50const RANGE_COMPRESSION: u32 = 0x6f64adce;
51const DEEP_NOISE_SUPPRESSION: u32 = 0x6f64add0;
52
53fn known_prefix(guid: u128) -> Option<u32> {
54 (guid & ((1u128 << 96) - 1) == EFFECT_SUFFIX).then_some((guid >> 96) as u32)
55}
56
57/// DeepFilterNet and AGC2 replace the Windows noise, gain and compression stages, stacking them muffles speech.
58/// The Windows echo canceller stays: it has the exact speaker signal, ours only a copy of the decoded stream.
59pub fn wanted_state(guid: u128) -> Option<bool> {
60 match known_prefix(guid)? {
61 NOISE_SUPPRESSION | DEEP_NOISE_SUPPRESSION | GAIN_CONTROL | RANGE_COMPRESSION => Some(false),
62 BEAMFORMING => Some(true),
63 _ => None,
64 }
65}
66
67pub fn effect_name(guid: u128) -> String {
68 known_prefix(guid)
69 .and_then(|prefix| EFFECT_NAMES.iter().find(|(known, _)| *known == prefix))
70 .map_or_else(|| format!("{guid:032x}"), |(_, name)| (*name).to_owned())
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn our_chain_replaces_windows_noise_gain_and_compression_but_keeps_its_echo_canceller() {
79 let guid = |prefix: u32| (u128::from(prefix) << 96) | EFFECT_SUFFIX;
80 assert_eq!(wanted_state(guid(NOISE_SUPPRESSION)), Some(false));
81 assert_eq!(wanted_state(guid(DEEP_NOISE_SUPPRESSION)), Some(false));
82 assert_eq!(wanted_state(guid(GAIN_CONTROL)), Some(false));
83 assert_eq!(wanted_state(guid(RANGE_COMPRESSION)), Some(false));
84 assert_eq!(wanted_state(guid(BEAMFORMING)), Some(true));
85 assert_eq!(wanted_state(guid(ECHO_CANCELLATION)), None);
86 assert_eq!(wanted_state(0x1234), None);
87 let echo = MicEffect { id: guid(ECHO_CANCELLATION), enabled: true, can_set: true };
88 assert!(echo.is_echo_cancellation() && echo.change().is_none());
89 let deep_noise = MicEffect { id: guid(DEEP_NOISE_SUPPRESSION), enabled: true, can_set: true };
90 assert_eq!(deep_noise.change(), Some(false));
91 assert_eq!(MicEffect { can_set: false, ..deep_noise }.change(), None);
92 assert_eq!(MicEffect { enabled: false, ..deep_noise }.change(), None);
93 }
94
95 #[test]
96 fn known_effect_guids_get_their_names() {
97 assert_eq!(effect_name(0x6f64adbe_8211_11e2_8c70_2c27d7f001fa), "acoustic echo cancellation");
98 assert_eq!(effect_name(0x6f64add0_8211_11e2_8c70_2c27d7f001fa), "deep noise suppression");
99 assert_eq!(effect_name(0x1234), "00000000000000000000000000001234");
100 }
101}