From b7cbe68690ec9c051eef1c5c13051a220b43a5c7 Mon Sep 17 00:00:00 2001 From: UserIsntAvailable Date: Fri, 26 Jan 2024 20:17:41 -0500 Subject: [PATCH 1/2] refactor!: `SoundCollection` returns a `Vec`. feat: make `SAMPLE_RATE` and `CHANNEL_COUNT` pub. refactor!: rename `SoundAssetCollection` to `SoundCollection`. --- engine/src/asset/sound/mod.rs | 71 +++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/engine/src/asset/sound/mod.rs b/engine/src/asset/sound/mod.rs index 0753068..628a3e9 100644 --- a/engine/src/asset/sound/mod.rs +++ b/engine/src/asset/sound/mod.rs @@ -9,19 +9,29 @@ use crate::{ utils::{compression::decompress, nom::*}, }; -pub struct SoundAssetCollection { - songs: Vec, - effects: Vec, +pub enum Sound { + Song(TSong), + Effect(TEffect), } -impl SoundAssetCollection { - const SAMPLE_RATE: usize = 16000; - const CHANNEL_COUNT: usize = 1; +impl Sound { + pub fn mix(&self) -> Vec { + match self { + Sound::Song(sound) => sound.mix(), + Sound::Effect(effect) => effect.mix(), + } + } } -impl AssetParser for SoundAssetCollection { - // TODO(nenikitov): Make it output vecs somehow to follow collection convention - type Output = Self; +pub struct SoundCollection; + +impl SoundCollection { + pub const SAMPLE_RATE: usize = 16000; + pub const CHANNEL_COUNT: usize = 1; +} + +impl AssetParser for SoundCollection { + type Output = Vec; type Context<'ctx> = (); @@ -35,7 +45,7 @@ impl AssetParser for SoundAssetCollection { .into_iter() .map(|s| decompress(&input[s])) .map(|s| TSong::parser(())(s.as_slice()).map(|(_, d)| d)) - .collect::, _>>()?; + .map(|s| s.map(|s| Sound::Song(s))); let (_, effects) = SoundChunkHeader::parser(())(&input[header.effects])?; let effects = effects @@ -43,9 +53,13 @@ impl AssetParser for SoundAssetCollection { .into_iter() .map(|s| decompress(&input[s])) .map(|s| TEffect::parser(())(s.as_slice()).map(|(_, d)| d)) + .map(|s| s.map(|s| Sound::Effect(s))); + + let sounds = songs + .chain(effects) .collect::, _>>()?; - Ok((&[], SoundAssetCollection { songs, effects })) + Ok((&[], sounds)) } } } @@ -61,35 +75,36 @@ mod tests { #[test] #[ignore = "uses Ashen ROM files"] fn parse_rom_asset() -> eyre::Result<()> { - let (_, asset) = >::parser(())(&SOUND_DATA)?; + let (_, sounds) = >::parser(())(&SOUND_DATA)?; let output_dir = PathBuf::from(parsed_file_path!("sounds/songs/")); - asset.songs.iter().enumerate().try_for_each(|(i, song)| { - let file = output_dir.join(format!("{i:0>2X}.wav")); - output_file( - file, - song.mix().to_wave( - SoundAssetCollection::SAMPLE_RATE, - SoundAssetCollection::CHANNEL_COUNT, - ), - ) - })?; + sounds + .iter() + .filter(|s| matches!(s, Sound::Song(_))) + .enumerate() + .try_for_each(|(i, song)| { + let file = output_dir.join(format!("{i:0>2X}.wav")); + output_file( + file, + song.mix() + .to_wave(SoundCollection::SAMPLE_RATE, SoundCollection::CHANNEL_COUNT), + ) + })?; let output_dir = PathBuf::from(parsed_file_path!("sounds/effects/")); - asset - .effects + sounds .iter() + .filter(|s| matches!(s, Sound::Effect(_))) .enumerate() .try_for_each(|(i, effect)| { let file = output_dir.join(format!("{i:0>2X}.wav")); output_file( file, - effect.mix().to_wave( - SoundAssetCollection::SAMPLE_RATE, - SoundAssetCollection::CHANNEL_COUNT, - ), + effect + .mix() + .to_wave(SoundCollection::SAMPLE_RATE, SoundCollection::CHANNEL_COUNT), ) })?; From 34d268707f5da13c31a7429a6f22ece3c3cfbb9f Mon Sep 17 00:00:00 2001 From: UserIsntAvailable Date: Fri, 26 Jan 2024 20:32:00 -0500 Subject: [PATCH 2/2] chore: make clippy happy. --- engine/src/asset/sound/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/src/asset/sound/mod.rs b/engine/src/asset/sound/mod.rs index 628a3e9..1b22227 100644 --- a/engine/src/asset/sound/mod.rs +++ b/engine/src/asset/sound/mod.rs @@ -45,7 +45,7 @@ impl AssetParser for SoundCollection { .into_iter() .map(|s| decompress(&input[s])) .map(|s| TSong::parser(())(s.as_slice()).map(|(_, d)| d)) - .map(|s| s.map(|s| Sound::Song(s))); + .map(|s| s.map(Sound::Song)); let (_, effects) = SoundChunkHeader::parser(())(&input[header.effects])?; let effects = effects @@ -53,7 +53,7 @@ impl AssetParser for SoundCollection { .into_iter() .map(|s| decompress(&input[s])) .map(|s| TEffect::parser(())(s.as_slice()).map(|(_, d)| d)) - .map(|s| s.map(|s| Sound::Effect(s))); + .map(|s| s.map(Sound::Effect)); let sounds = songs .chain(effects)