diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-06-19 14:46:33 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-06-19 14:46:33 +0200 |
| commit | ee89022431c54eae1f64099439a23cd316a0c7d3 (patch) | |
| tree | ec9d39571334f53635ba95a8ec0ccccd84216738 /mumd/src/audio/sound_effects.rs | |
| parent | d2e1919a5c700997bd31dd087b2bf51a9da00e7b (diff) | |
| download | mum-ee89022431c54eae1f64099439a23cd316a0c7d3.tar.gz | |
try to unpack ogg data
Diffstat (limited to 'mumd/src/audio/sound_effects.rs')
| -rw-r--r-- | mumd/src/audio/sound_effects.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mumd/src/audio/sound_effects.rs b/mumd/src/audio/sound_effects.rs index 21a514b..8a271e0 100644 --- a/mumd/src/audio/sound_effects.rs +++ b/mumd/src/audio/sound_effects.rs @@ -6,6 +6,7 @@ use std::borrow::Cow; use std::collections::HashMap; use std::convert::TryFrom; use std::fs::File; +use std::io::Cursor; use std::io::Read; use std::path::Path; use strum::IntoEnumIterator; @@ -14,6 +15,7 @@ use strum_macros::EnumIter; use crate::audio::SAMPLE_RATE; enum AudioFileKind { + Ogg, Wav, } @@ -22,6 +24,7 @@ impl TryFrom<&str> for AudioFileKind { fn try_from(s: &str) -> Result<Self, Self::Error> { match s { + ".ogg" => Ok(AudioFileKind::Ogg), ".wav" => Ok(AudioFileKind::Wav), _ => Err(()), } @@ -138,10 +141,26 @@ pub fn load_sound_effects(overrides: &[SoundEffect], num_channels: usize) -> Has /// Unpack audio data. The required audio spec is read from the file and returned as well. fn unpack_audio(data: Cow<[u8]>, kind: AudioFileKind) -> (Vec<f32>, AudioSpec) { match kind { + AudioFileKind::Ogg => unpack_ogg(data), AudioFileKind::Wav => unpack_wav(data), } } +/// Unpack ogg data. +fn unpack_ogg(data: Cow<[u8]>) -> (Vec<f32>, AudioSpec) { + let mut reader = lewton::inside_ogg::OggStreamReader::new(Cursor::new(data.as_ref())).unwrap(); + let mut samples = Vec::new(); + while let Ok(Some(mut frame)) = reader.read_dec_packet_itl() { + samples.append(&mut frame); + } + let samples = samples.iter().map(|s| cpal::Sample::to_f32(s)).collect(); + let spec = AudioSpec { + channels: reader.ident_hdr.audio_channels as u32, + sample_rate: reader.ident_hdr.audio_sample_rate, + }; + (samples, spec) +} + /// Unpack wav data. fn unpack_wav(data: Cow<[u8]>) -> (Vec<f32>, AudioSpec) { let reader = hound::WavReader::new(data.as_ref()).unwrap(); |
