1use assets::SoundRegistry;
2use gpui::{AppContext, AssetSource};
3use rodio::{OutputStream, OutputStreamHandle};
4use util::ResultExt;
5
6mod assets;
7
8pub fn init(source: impl AssetSource, cx: &mut AppContext) {
9 cx.set_global(SoundRegistry::new(source));
10 cx.set_global(Audio::new());
11}
12
13pub enum Sound {
14 Joined,
15 Leave,
16 Mute,
17 Unmute,
18}
19
20impl Sound {
21 fn file(&self) -> &'static str {
22 match self {
23 Self::Joined => "joined",
24 Self::Leave => "leave",
25 Self::Mute => "mute",
26 Self::Unmute => "unmute",
27 }
28 }
29}
30
31pub struct Audio {
32 _output_stream: Option<OutputStream>,
33 output_handle: Option<OutputStreamHandle>,
34}
35
36impl Audio {
37 pub fn new() -> Self {
38 let (_output_stream, output_handle) = OutputStream::try_default().log_err().unzip();
39
40 Self {
41 _output_stream,
42 output_handle,
43 }
44 }
45
46 pub fn play_sound(sound: Sound, cx: &AppContext) {
47 if !cx.has_global::<Self>() {
48 return;
49 }
50
51 let this = cx.global::<Self>();
52
53 let Some(output_handle) = this.output_handle.as_ref() else {
54 return;
55 };
56
57 let Some(source) = SoundRegistry::global(cx).get(sound.file()).log_err() else {
58 return;
59 };
60
61 output_handle.play_raw(source).log_err();
62 }
63}