mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Use empty sound data when a sound file cannot be loaded
This commit is contained in:
parent
85ad70e862
commit
eda2f14940
@ -51,7 +51,7 @@ impl AudioFrontend {
|
||||
device: device.clone(),
|
||||
device_list: list_devices(),
|
||||
audio_device,
|
||||
sound_cache: SoundCache::new(),
|
||||
sound_cache: SoundCache::default(),
|
||||
music_channels: Vec::new(),
|
||||
sfx_channels,
|
||||
sfx_volume: 1.0,
|
||||
@ -69,7 +69,7 @@ impl AudioFrontend {
|
||||
device: "none".to_string(),
|
||||
device_list: Vec::new(),
|
||||
audio_device: None,
|
||||
sound_cache: SoundCache::new(),
|
||||
sound_cache: SoundCache::default(),
|
||||
music_channels: Vec::new(),
|
||||
sfx_channels: Vec::new(),
|
||||
sfx_volume: 1.0,
|
||||
|
@ -1,3 +1,5 @@
|
||||
//! Handles caching and retrieval of decoded `.wav` sfx sound data, eliminating
|
||||
//! the need to decode files on each playback
|
||||
use common::assets;
|
||||
use hashbrown::HashMap;
|
||||
use std::{convert::AsRef, io, io::Read, sync::Arc};
|
||||
@ -10,6 +12,7 @@ impl AsRef<[u8]> for Sound {
|
||||
fn as_ref(&self) -> &[u8] { &self.0 }
|
||||
}
|
||||
|
||||
/// Wrapper for decoded audio data
|
||||
impl Sound {
|
||||
pub fn load(filename: &str) -> Result<Sound, assets::Error> {
|
||||
let mut file = assets::load_file(filename, &["wav"])?;
|
||||
@ -23,24 +26,37 @@ impl Sound {
|
||||
pub fn decoder(&self) -> rodio::Decoder<io::Cursor<Sound>> {
|
||||
rodio::Decoder::new(self.cursor()).unwrap()
|
||||
}
|
||||
|
||||
/// Returns a `Sound` containing empty .wav data. This intentionally doesn't
|
||||
/// load from the filesystem so we have a reliable fallback when there
|
||||
/// is a failure to read a file.
|
||||
///
|
||||
/// The data below is the result of passing a very short, silent .wav file
|
||||
/// to `Sound::load()`.
|
||||
pub fn empty() -> Sound {
|
||||
Sound(Arc::new(vec![
|
||||
82, 73, 70, 70, 40, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1,
|
||||
0, 68, 172, 0, 0, 136, 88, 1, 0, 2, 0, 16, 0, 100, 97, 116, 97, 4, 0, 0, 0, 0, 0, 0, 0,
|
||||
]))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SoundCache {
|
||||
sounds: HashMap<String, Sound>,
|
||||
}
|
||||
|
||||
impl SoundCache {
|
||||
#[allow(clippy::new_without_default)] // TODO: Pending review in #587
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
sounds: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_sound(&mut self, name: &str) -> rodio::Decoder<io::Cursor<Sound>> {
|
||||
self.sounds
|
||||
.entry(name.to_string())
|
||||
.or_insert(Sound::load(name).unwrap())
|
||||
.or_insert_with(|| {
|
||||
Sound::load(name).unwrap_or_else(|_| {
|
||||
log::warn!("SoundCache: Failed to load sound: {}", name);
|
||||
|
||||
Sound::empty()
|
||||
})
|
||||
})
|
||||
.decoder()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user