From 2468a83d4b633800781bbd1f920804ebad4f594d Mon Sep 17 00:00:00 2001 From: CDrummond Date: Mon, 17 Mar 2025 17:55:24 +0000 Subject: [PATCH] Reduce code duplication --- Cargo.lock | 5 ++- src/analyse.rs | 100 ++++--------------------------------------------- 2 files changed, 10 insertions(+), 95 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76de8c4..83be087 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -205,8 +205,9 @@ dependencies = [ [[package]] name = "bliss-audio" -version = "0.9.4" -source = "git+https://github.com/Polochon-street/bliss-rs.git?rev=006927ac16752ff2e00bfe0d6b7756f67fa822c0#006927ac16752ff2e00bfe0d6b7756f67fa822c0" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "000abff3621dffbae7e9b7574791d587001768224cedcb169158706b0f0ce0e2" dependencies = [ "adler32", "bliss-audio-aubio-rs", diff --git a/src/analyse.rs b/src/analyse.rs index 3cfeffc..da616e3 100644 --- a/src/analyse.rs +++ b/src/analyse.rs @@ -32,12 +32,13 @@ use std::thread; #[cfg(feature = "ffmpeg")] use std::time::Duration; use num_cpus; +use bliss_audio::decoder::Decoder; #[cfg(feature = "libav")] -use bliss_audio::{decoder::Decoder, decoder::ffmpeg::FFmpegDecoder}; -#[cfg(feature = "ffmpeg")] -use bliss_audio::{decoder::Decoder, BlissResult, Song}; +use bliss_audio::decoder::ffmpeg::FFmpegDecoder as SongDecoder; #[cfg(feature = "symphonia")] -use bliss_audio::{decoder::Decoder, decoder::symphonia::SymphoniaDecoder}; +use bliss_audio::decoder::symphonia::SymphoniaDecoder as SongDecoder; +#[cfg(feature = "ffmpeg")] +use bliss_audio::{BlissResult, Song}; const DONT_ANALYSE: &str = ".notmusic"; const MAX_ERRORS_TO_SHOW: usize = 100; @@ -159,7 +160,7 @@ fn show_errors(failed: &mut Vec, tag_error: &mut Vec) { } } -#[cfg(feature = "libav")] +#[cfg(not(feature = "ffmpeg"))] fn analyse_new_files(db: &db::Db, mpath: &PathBuf, track_paths: Vec, max_threads: usize, use_tags: bool) -> Result<()> { let total = track_paths.len(); let progress = ProgressBar::new(total.try_into().unwrap()).with_style( @@ -178,7 +179,7 @@ fn analyse_new_files(db: &db::Db, mpath: &PathBuf, track_paths: Vec, max let mut reported_cue:HashSet = HashSet::new(); log::info!("Analysing new files"); - for (path, result) in ::analyze_paths_with_cores(track_paths, cpu_threads) { + for (path, result) in SongDecoder::analyze_paths_with_cores(track_paths, cpu_threads) { let stripped = path.strip_prefix(mpath).unwrap(); let spbuff = stripped.to_path_buf(); let sname = String::from(spbuff.to_string_lossy()); @@ -257,93 +258,6 @@ fn analyse_new_files(db: &db::Db, mpath: &PathBuf, track_paths: Vec, max Ok(()) } -#[cfg(feature = "symphonia")] -fn analyse_new_files(db: &db::Db, mpath: &PathBuf, track_paths: Vec, max_threads: usize, use_tags: bool) -> Result<()> { - let total = track_paths.len(); - let progress = ProgressBar::new(total.try_into().unwrap()).with_style( - ProgressStyle::default_bar() - .template("[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}") - .progress_chars("=> "), - ); - let cpu_threads: NonZeroUsize = match max_threads { - 0 => NonZeroUsize::new(num_cpus::get()).unwrap(), - _ => NonZeroUsize::new(max_threads).unwrap(), - }; - - let mut analysed = 0; - let mut failed: Vec = Vec::new(); - let mut tag_error: Vec = Vec::new(); - let mut reported_cue:HashSet = HashSet::new(); - - log::info!("Analysing new files"); - for (path, result) in ::analyze_paths_with_cores(track_paths, cpu_threads) { - let stripped = path.strip_prefix(mpath).unwrap(); - let spbuff = stripped.to_path_buf(); - let sname = String::from(spbuff.to_string_lossy()); - progress.set_message(format!("{}", sname)); - let mut inc_progress = true; // Only want to increment progress once for cue tracks - match result { - Ok(track) => { - let cpath = String::from(path.to_string_lossy()); - match track.cue_info { - Some(cue) => { - match track.track_number { - Some(track_num) => { - if reported_cue.contains(&cpath) { - inc_progress = false; - } else { - analysed += 1; - reported_cue.insert(cpath); - } - let meta = db::Metadata { - title: track.title.unwrap_or_default().to_string(), - artist: track.artist.unwrap_or_default().to_string(), - album: track.album.unwrap_or_default().to_string(), - album_artist: track.album_artist.unwrap_or_default().to_string(), - genre: track.genre.unwrap_or_default().to_string(), - duration: track.duration.as_secs() as u32, - analysis: None - }; - - // Remove prefix from audio_file_path - let pbuff = PathBuf::from(&cue.audio_file_path); - let stripped = pbuff.strip_prefix(mpath).unwrap(); - let spbuff = stripped.to_path_buf(); - let sname = String::from(spbuff.to_string_lossy()); - - let db_path = format!("{}{}{}", sname, db::CUE_MARKER, track_num); - db.add_track(&db_path, &meta, &track.analysis); - } - None => { failed.push(format!("{} - No track number?", sname)); } - } - } - None => { - let meta = tags::read(&cpath, false); - if meta.is_empty() { - tag_error.push(sname.clone()); - } - if use_tags { - tags::write_analysis(&cpath, &track.analysis); - } - db.add_track(&sname, &meta, &track.analysis); - } - } - analysed += 1; - } - Err(e) => { failed.push(format!("{} - {}", sname, e)); } - }; - - if inc_progress { - progress.inc(1); - } - } - - progress.finish_with_message("Finished!"); - log::info!("{} Analysed. {} Failure(s).", analysed, failed.len()); - show_errors(&mut failed, &mut tag_error); - Ok(()) -} - #[cfg(feature = "ffmpeg")] fn analyse_new_files(db: &db::Db, mpath: &PathBuf, track_paths: Vec, max_threads: usize, use_tags: bool) -> Result<()> { let total = track_paths.len();