use std::{io::Cursor, sync::Arc}; use anyhow::Result; use collections::HashMap; use gpui::{AppContext, AssetSource, Global}; use rodio::{ source::{Buffered, SamplesConverter}, Decoder, Source, }; type Sound = Buffered>>, f32>>; pub struct SoundRegistry { cache: Arc>>, assets: Box, } struct GlobalSoundRegistry(Arc); impl Global for GlobalSoundRegistry {} impl SoundRegistry { pub fn new(source: impl AssetSource) -> Arc { Arc::new(Self { cache: Default::default(), assets: Box::new(source), }) } pub fn global(cx: &AppContext) -> Arc { cx.global::().0.clone() } pub(crate) fn set_global(source: impl AssetSource, cx: &mut AppContext) { cx.set_global(GlobalSoundRegistry(SoundRegistry::new(source))); } pub fn get(&self, name: &str) -> Result> { if let Some(wav) = self.cache.lock().get(name) { return Ok(wav.clone()); } let path = format!("sounds/{}.wav", name); let bytes = self .assets .load(&path)? .map(|asset| Ok(asset)) .unwrap_or_else(|| Err(anyhow::anyhow!("No such asset available")))? .into_owned(); let cursor = Cursor::new(bytes); let source = Decoder::new(cursor)?.convert_samples::().buffered(); self.cache.lock().insert(name.to_string(), source.clone()); Ok(source) } }