
Hi all, We just released [Rodio 0.21](https://github.com/RustAudio/rodio/blob/master/CHANGELOG.md) 🥳 with quite some breaking changes. This should take care of those for zed. I tested it by hopping in and out some of the zed channels, sound seems to still work. Given zed uses tracing I also took the liberty of enabling the tracing feature for rodio. edit: We changed the default wav decoder from hound to symphonia. The latter has a slightly more restrictive license however that should be no issue here (as the audio crate uses the GPL) Release Notes: - N/A
54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
use std::{io::Cursor, sync::Arc};
|
|
|
|
use anyhow::{Context as _, Result};
|
|
use collections::HashMap;
|
|
use gpui::{App, AssetSource, Global};
|
|
use rodio::{Decoder, Source, source::Buffered};
|
|
|
|
type Sound = Buffered<Decoder<Cursor<Vec<u8>>>>;
|
|
|
|
pub struct SoundRegistry {
|
|
cache: Arc<parking_lot::Mutex<HashMap<String, Sound>>>,
|
|
assets: Box<dyn AssetSource>,
|
|
}
|
|
|
|
struct GlobalSoundRegistry(Arc<SoundRegistry>);
|
|
|
|
impl Global for GlobalSoundRegistry {}
|
|
|
|
impl SoundRegistry {
|
|
pub fn new(source: impl AssetSource) -> Arc<Self> {
|
|
Arc::new(Self {
|
|
cache: Default::default(),
|
|
assets: Box::new(source),
|
|
})
|
|
}
|
|
|
|
pub fn global(cx: &App) -> Arc<Self> {
|
|
cx.global::<GlobalSoundRegistry>().0.clone()
|
|
}
|
|
|
|
pub(crate) fn set_global(source: impl AssetSource, cx: &mut App) {
|
|
cx.set_global(GlobalSoundRegistry(SoundRegistry::new(source)));
|
|
}
|
|
|
|
pub fn get(&self, name: &str) -> Result<impl Source<Item = f32> + use<>> {
|
|
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(anyhow::Ok)
|
|
.with_context(|| format!("No asset available for path {path}"))??
|
|
.into_owned();
|
|
let cursor = Cursor::new(bytes);
|
|
let source = Decoder::new(cursor)?.buffered();
|
|
|
|
self.cache.lock().insert(name.to_string(), source.clone());
|
|
|
|
Ok(source)
|
|
}
|
|
}
|