From 1da5b2b1a220cf75eff034d138e11ace2835a6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 24 Jun 2021 20:28:34 +0200 Subject: WIP add SoundEffects (doesn't compile) --- mumd/src/audio/sound_effects.rs | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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>, + loaded_paths: HashMap, +} + +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>(&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, -- cgit v1.2.1