audio_settings.rs

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