audio_settings.rs

 1use std::sync::atomic::{AtomicBool, Ordering};
 2
 3use gpui::App;
 4use settings::{Settings, SettingsStore};
 5use util::MergeFrom as _;
 6
 7#[derive(Clone, Debug)]
 8pub struct AudioSettings {
 9    /// Opt into the new audio system.
10    pub rodio_audio: bool, // default is false
11    /// Requires 'rodio_audio: true'
12    ///
13    /// Use the new audio systems automatic gain control for your microphone.
14    /// This affects how loud you sound to others.
15    pub control_input_volume: bool,
16    /// Requires 'rodio_audio: true'
17    ///
18    /// Use the new audio systems automatic gain control on everyone in the
19    /// call. This makes call members who are too quite louder and those who are
20    /// too loud quieter. This only affects how things sound for you.
21    pub control_output_volume: bool,
22}
23
24/// Configuration of audio in Zed
25impl Settings for AudioSettings {
26    fn from_defaults(content: &settings::SettingsContent, _cx: &mut App) -> Self {
27        let audio = &content.audio.as_ref().unwrap();
28        AudioSettings {
29            control_input_volume: audio.control_input_volume.unwrap(),
30            control_output_volume: audio.control_output_volume.unwrap(),
31            rodio_audio: audio.rodio_audio.unwrap(),
32        }
33    }
34
35    fn refine(&mut self, content: &settings::SettingsContent, _cx: &mut App) {
36        let Some(audio) = content.audio.as_ref() else {
37            return;
38        };
39        self.control_input_volume
40            .merge_from(&audio.control_input_volume);
41        self.control_output_volume
42            .merge_from(&audio.control_output_volume);
43        self.rodio_audio.merge_from(&audio.rodio_audio);
44    }
45
46    fn import_from_vscode(
47        _vscode: &settings::VsCodeSettings,
48        _current: &mut settings::SettingsContent,
49    ) {
50    }
51}
52
53/// See docs on [LIVE_SETTINGS]
54pub(crate) struct LiveSettings {
55    pub(crate) control_input_volume: AtomicBool,
56    pub(crate) control_output_volume: AtomicBool,
57}
58
59impl LiveSettings {
60    pub(crate) fn initialize(&self, cx: &mut App) {
61        cx.observe_global::<SettingsStore>(move |cx| {
62            LIVE_SETTINGS.control_input_volume.store(
63                AudioSettings::get_global(cx).control_input_volume,
64                Ordering::Relaxed,
65            );
66            LIVE_SETTINGS.control_output_volume.store(
67                AudioSettings::get_global(cx).control_output_volume,
68                Ordering::Relaxed,
69            );
70        })
71        .detach();
72
73        let init_settings = AudioSettings::get_global(cx);
74        LIVE_SETTINGS
75            .control_input_volume
76            .store(init_settings.control_input_volume, Ordering::Relaxed);
77        LIVE_SETTINGS
78            .control_output_volume
79            .store(init_settings.control_output_volume, Ordering::Relaxed);
80    }
81}
82
83/// Allows access to settings from the audio thread. Updated by
84/// observer of SettingsStore. Needed because audio playback and recording are
85/// real time and must each run in a dedicated OS thread, therefore we can not
86/// use the background executor.
87pub(crate) static LIVE_SETTINGS: LiveSettings = LiveSettings {
88    control_input_volume: AtomicBool::new(true),
89    control_output_volume: AtomicBool::new(true),
90};