BlueGit

ict rusty-teams

Files

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

mic_effects.rs

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 /// 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 => Some(false),
58 BEAMFORMING => Some(true),
59 ECHO_CANCELLATION => None,
60 _ => None,
61 }
62}
63
64pub fn effect_name(guid: u128) -> String {
65 known_prefix(guid)
66 .and_then(|prefix| EFFECT_NAMES.iter().find(|(known, _)| *known == prefix))
67 .map_or_else(|| format!("{guid:032x}"), |(_, name)| (*name).to_owned())
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn our_chain_replaces_windows_noise_gain_and_compression_but_keeps_its_echo_canceller() {
76 let guid = |prefix: u32| (u128::from(prefix) << 96) | EFFECT_SUFFIX;
77 assert_eq!(wanted_state(guid(NOISE_SUPPRESSION)), Some(false));
78 assert_eq!(wanted_state(guid(DEEP_NOISE_SUPPRESSION)), Some(false));
79 assert_eq!(wanted_state(guid(GAIN_CONTROL)), Some(false));
80 assert_eq!(wanted_state(guid(RANGE_COMPRESSION)), Some(false));
81 assert_eq!(wanted_state(guid(BEAMFORMING)), Some(true));
82 assert_eq!(wanted_state(guid(ECHO_CANCELLATION)), None);
83 assert_eq!(wanted_state(0x1234), None);
84 let echo = MicEffect { id: guid(ECHO_CANCELLATION), enabled: true, can_set: true };
85 assert!(echo.change().is_none());
86 let deep_noise = MicEffect { id: guid(DEEP_NOISE_SUPPRESSION), enabled: true, can_set: true };
87 assert_eq!(deep_noise.change(), Some(false));
88 assert_eq!(MicEffect { can_set: false, ..deep_noise }.change(), None);
89 assert_eq!(MicEffect { enabled: false, ..deep_noise }.change(), None);
90 }
91
92 #[test]
93 fn known_effect_guids_get_their_names() {
94 assert_eq!(effect_name(0x6f64adbe_8211_11e2_8c70_2c27d7f001fa), "acoustic echo cancellation");
95 assert_eq!(effect_name(0x6f64add0_8211_11e2_8c70_2c27d7f001fa), "deep noise suppression");
96 assert_eq!(effect_name(0x1234), "00000000000000000000000000001234");
97 }
98}