vector_core/calls/
settings.rs1use serde::{Deserialize, Serialize};
5use std::sync::atomic::{AtomicU32, AtomicU8, Ordering};
6
7#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
8pub struct AudioSettings {
9 pub auto_gain: bool,
10 pub echo_cancel: bool,
11 pub noise_suppress: bool,
12 #[serde(default = "unity")]
14 pub mic_volume: f32,
15 #[serde(default = "unity")]
17 pub speaker_volume: f32,
18}
19
20fn unity() -> f32 {
21 1.0
22}
23
24impl Default for AudioSettings {
25 fn default() -> Self {
26 Self { auto_gain: true, echo_cancel: true, noise_suppress: true, mic_volume: 1.0, speaker_volume: 1.0 }
27 }
28}
29
30const AGC: u8 = 1;
31const AEC: u8 = 2;
32const NS: u8 = 4;
33const KEY: &str = "calls_audio";
34
35static FLAGS: AtomicU8 = AtomicU8::new(AGC | AEC | NS);
36static MIC_GAIN: AtomicU32 = AtomicU32::new(0x3F80_0000); static SPEAKER_GAIN: AtomicU32 = AtomicU32::new(0x3F80_0000); impl AudioSettings {
40 fn to_flags(self) -> u8 {
41 (self.auto_gain as u8 * AGC) | (self.echo_cancel as u8 * AEC) | (self.noise_suppress as u8 * NS)
42 }
43 fn store(self) {
44 FLAGS.store(self.to_flags(), Ordering::Relaxed);
45 MIC_GAIN.store(level(self.mic_volume).to_bits(), Ordering::Relaxed);
46 SPEAKER_GAIN.store(level(self.speaker_volume).to_bits(), Ordering::Relaxed);
47 }
48}
49
50fn level(v: f32) -> f32 {
52 if v.is_finite() { v.clamp(0.0, 1.0) } else { 1.0 }
53}
54
55pub fn current() -> AudioSettings {
57 let f = FLAGS.load(Ordering::Relaxed);
58 AudioSettings {
59 auto_gain: f & AGC != 0,
60 echo_cancel: f & AEC != 0,
61 noise_suppress: f & NS != 0,
62 mic_volume: mic_gain(),
63 speaker_volume: speaker_gain(),
64 }
65}
66
67pub fn mic_gain() -> f32 {
68 f32::from_bits(MIC_GAIN.load(Ordering::Relaxed))
69}
70
71pub fn speaker_gain() -> f32 {
72 f32::from_bits(SPEAKER_GAIN.load(Ordering::Relaxed))
73}
74
75pub fn set(s: AudioSettings) -> Result<(), String> {
77 s.store();
78 let json = serde_json::to_string(¤t()).map_err(|e| e.to_string())?;
79 crate::db::set_sql_setting(KEY.to_string(), json)
80}
81
82#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
85pub struct MicState {
86 #[serde(default)]
87 pub muted: bool,
88 #[serde(default)]
89 pub deafened: bool,
90}
91
92const MIC_KEY: &str = "calls_mic";
93
94pub fn mic_state() -> MicState {
95 crate::db::get_sql_setting(MIC_KEY.to_string())
96 .ok()
97 .flatten()
98 .and_then(|j| serde_json::from_str(&j).ok())
99 .unwrap_or_default()
100}
101
102pub fn remember_mic(s: MicState) {
104 if mic_state() == s {
105 return;
106 }
107 if let Ok(json) = serde_json::to_string(&s) {
108 let _ = crate::db::set_sql_setting(MIC_KEY.to_string(), json);
109 }
110 crate::traits::emit_event("call_mic_state", &s);
111}
112
113pub fn heard(volume: f32) -> f32 {
115 if mic_state().deafened { 0.0 } else { volume }
116}
117
118pub fn load() -> AudioSettings {
120 let s = crate::db::get_sql_setting(KEY.to_string())
121 .ok()
122 .flatten()
123 .and_then(|j| serde_json::from_str::<AudioSettings>(&j).ok())
124 .unwrap_or_default();
125 s.store();
126 current()
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn a_setting_saved_before_the_volumes_existed_reads_them_as_unity() {
135 let s: AudioSettings = serde_json::from_str(r#"{"auto_gain":false,"echo_cancel":true,"noise_suppress":true}"#).unwrap();
136 assert!(!s.auto_gain);
137 assert_eq!((s.mic_volume, s.speaker_volume), (1.0, 1.0));
138 }
139
140 #[test]
141 fn the_switches_carry_from_call_to_call_and_deafened_hears_nothing() {
142 let _guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
143 crate::db::close_database();
144 let tmp = tempfile::tempdir().unwrap();
145 let account = nostr_sdk::prelude::ToBech32::to_bech32(&nostr_sdk::prelude::Keys::generate().public_key()).unwrap();
146 std::fs::create_dir_all(tmp.path().join(&account)).unwrap();
147 crate::db::set_app_data_dir(crate::db::shared_test_data_dir().to_path_buf());
148 crate::db::set_current_account(account.clone()).unwrap();
149 crate::db::init_database(&account).unwrap();
150
151 assert_eq!(mic_state(), MicState::default(), "a first call starts open");
152 remember_mic(MicState { muted: true, deafened: false });
153 assert_eq!(mic_state(), MicState { muted: true, deafened: false });
154 assert_eq!(heard(0.8), 0.8);
155 remember_mic(MicState { muted: true, deafened: true });
156 assert_eq!(heard(0.8), 0.0, "deafened, a call plays nothing");
157 }
158
159 #[test]
160 fn a_volume_outside_its_range_is_held_to_it() {
161 assert_eq!(level(1.7), 1.0);
162 assert_eq!(level(-0.2), 0.0);
163 assert_eq!(level(f32::NAN), 1.0);
164 assert_eq!(level(0.35), 0.35);
165 }
166}