From 43fbafa80b761d7374c3b68fc05f93934ec17d5f Mon Sep 17 00:00:00 2001 From: Polochon-street Date: Mon, 11 Apr 2022 17:26:05 +0200 Subject: [PATCH] Fix bug leading to empty chroma errors --- .github/workflows/rust.yml | 4 ++-- CHANGELOG.md | 1 + examples/playlist.rs | 2 +- src/chroma.rs | 10 +++++++++- src/playlist.rs | 6 +----- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 035244c..71ce8a9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -24,6 +24,8 @@ jobs: override: false - name: Packages run: sudo apt-get update && sudo apt-get install build-essential yasm libavutil-dev libavcodec-dev libavformat-dev libavfilter-dev libavfilter-dev libavdevice-dev libswresample-dev libfftw3-dev ffmpeg + - name: Check format + run: cargo fmt -- --check - name: Build run: cargo build --verbose - name: Run tests @@ -36,8 +38,6 @@ jobs: run: cargo build --examples --verbose --features=serde - name: Lint run: cargo clippy --examples --features=serde -- -D warnings - - name: Check format - run: cargo fmt -- --check build-test-lint-windows: name: Windows - build, test and lint diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5c14b..143d1a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ #Changelog ## bliss 0.5.0 +* Fix a bug in `estimate_tuning` that led to empty chroma errors. * Remove the unusued Library trait, and extract a few useful functions from there (`analyze_paths`, `closest_to_album_group`. * Rename `distance` module to `playlist`. diff --git a/examples/playlist.rs b/examples/playlist.rs index 214152c..2e8a2e3 100644 --- a/examples/playlist.rs +++ b/examples/playlist.rs @@ -83,7 +83,7 @@ fn main() -> Result<()> { } analyzed_songs.extend_from_slice(&songs); let serialized = serde_json::to_string(&analyzed_songs).unwrap(); - let mut songs_to_chose_from = analyzed_songs + let mut songs_to_chose_from: Vec<_> = analyzed_songs .into_iter() .filter(|x| x == &first_song || paths.contains(&x.path.to_string_lossy().to_string())) .collect(); diff --git a/src/chroma.rs b/src/chroma.rs index 7ca6ce3..d4abb04 100644 --- a/src/chroma.rs +++ b/src/chroma.rs @@ -330,11 +330,14 @@ fn estimate_tuning( .map(|(x, y)| (n64(*x), n64(*y))) .unzip(); + if pitch.is_empty() { + return Ok(0.); + } + let threshold: N64 = Array::from(filtered_mag.to_vec()) .quantile_axis_mut(Axis(0), n64(0.5), &Midpoint) .map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e)))? .into_scalar(); - let mut pitch = filtered_pitch .iter() .zip(&filtered_mag) @@ -486,6 +489,11 @@ mod test { assert!(0.000001 > (-0.09999999999999998 - tuning).abs()); } + #[test] + fn test_chroma_estimate_tuning_empty_fix() { + assert!(0. == estimate_tuning(22050, &Array2::zeros((8192, 1)), 8192, 0.01, 12).unwrap()); + } + #[test] fn test_estimate_tuning_decode() { let signal = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")) diff --git a/src/playlist.rs b/src/playlist.rs index 3b943fc..b3fefcb 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -39,11 +39,7 @@ pub fn cosine_distance(a: &Array1, b: &Array1) -> f32 { /// Sort `songs` in place by putting songs close to `first_song` first /// using the `distance` metric. -pub fn closest_to_first_song( - first_song: &Song, - songs: &mut Vec, - distance: impl DistanceMetric, -) { +pub fn closest_to_first_song(first_song: &Song, songs: &mut [Song], distance: impl DistanceMetric) { songs.sort_by_cached_key(|song| n32(first_song.custom_distance(song, &distance))); }