diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-06-24 20:28:34 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-06-26 07:55:38 +0200 |
| commit | 1da5b2b1a220cf75eff034d138e11ace2835a6a2 (patch) | |
| tree | 56c04ddae40545a6dde95f64741479be5cc2154b | |
| parent | 87200110aca88237cfd3b4c926996df06e045304 (diff) | |
| download | mum-1da5b2b1a220cf75eff034d138e11ace2835a6a2.tar.gz | |
WIP add SoundEffects (doesn't compile)
| -rw-r--r-- | mumd/src/audio/sound_effects.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/mumd/src/audio/sound_effects.rs b/mumd/src/audio/sound_effects.rs index 0faacdf..8f68118 100644 --- a/mumd/src/audio/sound_effects.rs +++ b/mumd/src/audio/sound_effects.rs @@ -4,7 +4,9 @@ use log::warn; use mumlib::config::SoundEffect; use std::borrow::Cow; use std::collections::HashMap; +use std::collections::hash_map::Entry; use std::convert::TryFrom; +use std::fmt; use std::fs::File; #[cfg(feature = "ogg")] use std::io::Cursor; @@ -15,6 +17,47 @@ use strum_macros::EnumIter; use crate::audio::SAMPLE_RATE; +pub struct SoundEffects { + data: Vec<Vec<f32>>, + loaded_paths: HashMap<PathBuf, usize>, +} + +impl fmt::Debug for SoundEffects { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SoundEffects") + .field("loaded_paths", &self.loaded_paths) + .finish_non_exhaustive() + } +} + +impl SoundEffects { + pub fn new() -> Self { + SoundEffects { + data: Vec::new(), + loaded_paths: HashMap::new(), + } + } + + pub fn get<P: AsRef<Path>>(&mut self, path: &P) -> &[f32] { + let idx = match self.loaded_paths.entry(path.as_ref().to_owned()) { + Entry::Occupied(o) => *o.get(), + Entry::Vacant(v) => { + if let Ok(samples) = open_and_unpack_audio(v.key(), 2) { + let idx = self.data.len(); + self.data.push(samples.0); + v.insert(idx); + idx + } else { + // Default sound effect + 0 + } + + } + }; + &self.data[idx] + } +} + /// The different kinds of files we can open. enum AudioFileKind { Ogg, |
