From 6df220ad73aab4d12dd7ae7a874d2173249acb9d Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 6 Nov 2025 11:47:10 -0500 Subject: [PATCH] tsk-50: Added properties --- src/properties/audio.rs | 7 +++---- src/properties/mod.rs | 42 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/properties/audio.rs b/src/properties/audio.rs index 69970e9..9324a5f 100644 --- a/src/properties/audio.rs +++ b/src/properties/audio.rs @@ -1,13 +1,12 @@ use lofty::file::AudioFile; -pub fn get_duration(song_path: &String) -> Result { - match std::fs::File::open(song_path) { +pub fn get_properties(songpath: &str) -> Result { + match std::fs::File::open(songpath) { Ok(mut content) => { match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new()) { Ok(flac_file) => { - let properties = flac_file.properties(); - Ok(properties.duration()) + Ok(*flac_file.properties()) } Err(err) => Err(std::io::Error::other(err.to_string())), } diff --git a/src/properties/mod.rs b/src/properties/mod.rs index c8d1de8..c86f23f 100644 --- a/src/properties/mod.rs +++ b/src/properties/mod.rs @@ -1,4 +1,42 @@ -pub mod properties; +pub mod audio; + + +#[derive(Clone, Debug, Default)] +pub struct SongProperties { + pub duration: std::time::Duration, + pub sample_rate: u32, + pub bitrate: u32, + pub overall_bitrate: u32, + pub bit_depth: u8, + pub channels: u8, +} + + +pub fn get_song_properties(song_path: &str) -> Result { + match audio::get_properties(song_path) { + Ok(flac_properties) => { + Ok(SongProperties { + duration: flac_properties.duration(), + sample_rate: flac_properties.sample_rate(), + bitrate: flac_properties.audio_bitrate(), + overall_bitrate: flac_properties.overall_bitrate(), + bit_depth: flac_properties.bit_depth(), + channels: flac_properties.channels(), + }) + } + Err(err) => Err(err), + } +} + +pub fn get_duration(song_path: &String) -> Result { + match get_song_properties(song_path) { + Ok(song_properties) => { + Ok(song_properties.duration) + } + Err(err) => Err(err), + } +} + #[cfg(test)] mod tests { @@ -12,7 +50,7 @@ mod tests { match test_util::util::file_exists(&dir, &filename) { Ok(_) => { let filepath = test_util::util::get_full_path(&dir, &filename).unwrap(); - match super::properties::get_duration(&filepath) { + match super::get_duration(&filepath) { Ok(duration) => { let song_duration: u64 = 41; let fetched_song_duration = duration.as_secs();