1use std::{io::Cursor, sync::Arc};
2
3use anyhow::{Context as _, Result};
4use collections::HashMap;
5use gpui::{App, AssetSource, Global};
6use rodio::{
7 Decoder, Source,
8 source::{Buffered, SamplesConverter},
9};
10
11type Sound = Buffered<SamplesConverter<Decoder<Cursor<Vec<u8>>>, f32>>;
12
13pub struct SoundRegistry {
14 cache: Arc<parking_lot::Mutex<HashMap<String, Sound>>>,
15 assets: Box<dyn AssetSource>,
16}
17
18struct GlobalSoundRegistry(Arc<SoundRegistry>);
19
20impl Global for GlobalSoundRegistry {}
21
22impl SoundRegistry {
23 pub fn new(source: impl AssetSource) -> Arc<Self> {
24 Arc::new(Self {
25 cache: Default::default(),
26 assets: Box::new(source),
27 })
28 }
29
30 pub fn global(cx: &App) -> Arc<Self> {
31 cx.global::<GlobalSoundRegistry>().0.clone()
32 }
33
34 pub(crate) fn set_global(source: impl AssetSource, cx: &mut App) {
35 cx.set_global(GlobalSoundRegistry(SoundRegistry::new(source)));
36 }
37
38 pub fn get(&self, name: &str) -> Result<impl Source<Item = f32> + use<>> {
39 if let Some(wav) = self.cache.lock().get(name) {
40 return Ok(wav.clone());
41 }
42
43 let path = format!("sounds/{}.wav", name);
44 let bytes = self
45 .assets
46 .load(&path)?
47 .map(anyhow::Ok)
48 .with_context(|| format!("No asset available for path {path}"))??
49 .into_owned();
50 let cursor = Cursor::new(bytes);
51 let source = Decoder::new(cursor)?.convert_samples::<f32>().buffered();
52
53 self.cache.lock().insert(name.to_string(), source.clone());
54
55 Ok(source)
56 }
57}