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
62pub(crate) struct LiveSettings {
63 pub(crate) control_input_volume: AtomicBool,
64 pub(crate) control_output_volume: AtomicBool,
65}
66
67impl LiveSettings {
68 pub(crate) fn initialize(&self, cx: &mut App) {
69 cx.observe_global::<SettingsStore>(move |cx| {
70 LIVE_SETTINGS.control_input_volume.store(
71 AudioSettings::get_global(cx).control_input_volume,
72 Ordering::Relaxed,
73 );
74 LIVE_SETTINGS.control_output_volume.store(
75 AudioSettings::get_global(cx).control_output_volume,
76 Ordering::Relaxed,
77 );
78 })
79 .detach();
80
81 let init_settings = AudioSettings::get_global(cx);
82 LIVE_SETTINGS
83 .control_input_volume
84 .store(init_settings.control_input_volume, Ordering::Relaxed);
85 LIVE_SETTINGS
86 .control_output_volume
87 .store(init_settings.control_output_volume, Ordering::Relaxed);
88 }
89}
90
91/// Allows access to settings from the audio thread. Updated by
92/// observer of SettingsStore.
93pub(crate) static LIVE_SETTINGS: LiveSettings = LiveSettings {
94 control_input_volume: AtomicBool::new(true),
95 control_output_volume: AtomicBool::new(true),
96};