1use assets::SoundRegistry;
2use derive_more::{Deref, DerefMut};
3use gpui::{App, AssetSource, BorrowAppContext, Global};
4use rodio::{OutputStream, OutputStreamHandle};
5use util::ResultExt;
6
7mod assets;
8
9pub fn init(source: impl AssetSource, cx: &mut App) {
10 SoundRegistry::set_global(source, cx);
11 cx.set_global(GlobalAudio(Audio::new()));
12}
13
14pub enum Sound {
15 Joined,
16 Leave,
17 Mute,
18 Unmute,
19 StartScreenshare,
20 StopScreenshare,
21}
22
23impl Sound {
24 fn file(&self) -> &'static str {
25 match self {
26 Self::Joined => "joined_call",
27 Self::Leave => "leave_call",
28 Self::Mute => "mute",
29 Self::Unmute => "unmute",
30 Self::StartScreenshare => "start_screenshare",
31 Self::StopScreenshare => "stop_screenshare",
32 }
33 }
34}
35
36#[derive(Default)]
37pub struct Audio {
38 _output_stream: Option<OutputStream>,
39 output_handle: Option<OutputStreamHandle>,
40}
41
42#[derive(Deref, DerefMut)]
43struct GlobalAudio(Audio);
44
45impl Global for GlobalAudio {}
46
47impl Audio {
48 pub fn new() -> Self {
49 Self::default()
50 }
51
52 fn ensure_output_exists(&mut self) -> Option<&OutputStreamHandle> {
53 if self.output_handle.is_none() {
54 let (_output_stream, output_handle) = OutputStream::try_default().log_err().unzip();
55 self.output_handle = output_handle;
56 self._output_stream = _output_stream;
57 }
58
59 self.output_handle.as_ref()
60 }
61
62 pub fn play_sound(sound: Sound, cx: &mut App) {
63 if !cx.has_global::<GlobalAudio>() {
64 return;
65 }
66
67 cx.update_global::<GlobalAudio, _>(|this, cx| {
68 let output_handle = this.ensure_output_exists()?;
69 let source = SoundRegistry::global(cx).get(sound.file()).log_err()?;
70 output_handle.play_raw(source).log_err()?;
71 Some(())
72 });
73 }
74
75 pub fn end_call(cx: &mut App) {
76 if !cx.has_global::<GlobalAudio>() {
77 return;
78 }
79
80 cx.update_global::<GlobalAudio, _>(|this, _| {
81 this._output_stream.take();
82 this.output_handle.take();
83 });
84 }
85}