Skip to content

Commit

Permalink
Change track number from String to i32
Browse files Browse the repository at this point in the history
  • Loading branch information
Polochon-street committed Jul 29, 2024
1 parent 6cd495a commit 7dc1dda
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 207 deletions.
338 changes: 177 additions & 161 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ serde = ["dep:serde", "extended-isolation-forest/serde"]
# Until https://github.com/aubio/aubio/issues/336 is somehow solved
# Hopefully we'll be able to use the official aubio-rs at some point.
bliss-audio-aubio-rs = "0.2.2"
ffmpeg-next = { version = "7.0.2", optional = true }
ffmpeg-sys-next = { version = "7.0.0", optional = true, default-features = false }
ffmpeg-next = { version = "7.0.3", optional = true }
ffmpeg-sys-next = { version = "7.0.2", optional = true, default-features = false }
log = "0.4.17"
# `rayon` is used only by `par_mapv_inplace` in chroma.rs.
# TODO: is the speed gain that substantial?
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ask questions if you want to tackle an item.

## Actual TODO

- The album playlist should take into account multi-CDs albums
- Add a list of dependencies / installation guide
- Maybe add playlist functions for single songs as convenience methods?
- Regularly update the python bindings with the new code
Expand Down
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ fn main() {
r#"cargo:rustc-cfg=feature="{}""#,
name["DEP_FFMPEG_".len()..name.len()].to_lowercase()
);
println!(
r#"cargo:rustc-check-cfg=cfg(feature, values("{}"))"#,
name["DEP_FFMPEG_".len()..name.len()].to_lowercase()
);
}
}
}
2 changes: 1 addition & 1 deletion ci_check.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cargo fmt -- --check && cargo clippy --examples --features=serde -- -D warnings && cargo build --verbose && cargo test --verbose && cargo test --verbose --examples && cargo +nightly-2024-01-16 bench --verbose --features=bench --no-run && cargo build --examples --verbose --features=serde && cargo build --no-default-features
cargo fmt -- --check && cargo clippy --examples --features=serde -- -D warnings && cargo build --verbose && cargo test --verbose && cargo test --verbose --examples && cargo +nightly-2024-01-16 bench --verbose --features=bench --no-run && cargo build --examples --verbose --features=serde && cargo build --no-default-features && cargo build --features=default,library
8 changes: 4 additions & 4 deletions src/cue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl BlissCueFile {
duration,
genre: self.genre.to_owned(),
title: current_track.title.to_owned(),
track_number: Some(current_track.no.to_owned()),
track_number: current_track.no.parse::<i32>().ok(),
features_version: FEATURES_VERSION,
cue_info: Some(CueInfo {
cue_path: self.cue_path.to_owned(),
Expand Down Expand Up @@ -256,7 +256,7 @@ mod tests {
artist: Some(String::from("David TMX")),
title: Some(String::from("Renaissance")),
genre: Some(String::from("Random")),
track_number: Some(String::from("01")),
track_number: Some(1),
features_version: FEATURES_VERSION,
album_artist: Some(String::from("Polochon_street")),
duration: Duration::from_secs_f32(11.066666603),
Expand Down Expand Up @@ -297,7 +297,7 @@ mod tests {
artist: Some(String::from("Polochon_street")),
title: Some(String::from("Piano")),
genre: Some(String::from("Random")),
track_number: Some(String::from("02")),
track_number: Some(2),
album_artist: Some(String::from("Polochon_street")),
duration: Duration::from_secs_f64(5.853333473),
cue_info: Some(CueInfo {
Expand Down Expand Up @@ -336,7 +336,7 @@ mod tests {
artist: Some(String::from("Polochon_street")),
title: Some(String::from("Tone")),
genre: Some(String::from("Random")),
track_number: Some(String::from("03")),
track_number: Some(3),
features_version: FEATURES_VERSION,
album_artist: Some(String::from("Polochon_street")),
duration: Duration::from_secs_f32(5.586666584),
Expand Down
30 changes: 15 additions & 15 deletions src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl<Config: AppConfigTrait, D: ?Sized + DecoderTrait> Library<Config, D> {
artist text,
title text,
album text,
track_number text,
track_number integer,
genre text,
cue_path text,
audio_file_path text,
Expand Down Expand Up @@ -1026,15 +1026,15 @@ impl<Config: AppConfigTrait, D: ?Sized + DecoderTrait> Library<Config, D> {
audio_file_path, id
from song where album = ? and analyzed = true and version = ?
order
by cast(track_number as integer);
by track_number;
";

// Get the song's analysis, and attach it to the existing song.
let features_statement = "
select
feature, song.id from feature join song on song.id = feature.song_id
where album=? and analyzed = true and version = ?
order by cast(track_number as integer);
order by track_number;
";
let songs = self._songs_from_statement(songs_statement, features_statement, params)?;
if songs.is_empty() {
Expand Down Expand Up @@ -1137,9 +1137,9 @@ impl<Config: AppConfigTrait, D: ?Sized + DecoderTrait> Library<Config, D> {
track_number: row
.get_ref(5)
.unwrap()
.as_bytes_or_null()
.as_i64_or_null()
.unwrap()
.map(|v| String::from_utf8_lossy(v).to_string()),
.map(|v| v as i32),
genre: row
.get_ref(6)
.unwrap()
Expand Down Expand Up @@ -1442,7 +1442,7 @@ mod test {
title: Some("Title1001".into()),
album: Some("An Album1001".into()),
album_artist: Some("An Album Artist1001".into()),
track_number: Some("03".into()),
track_number: Some(3),
genre: Some("Electronica1001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1470,7 +1470,7 @@ mod test {
title: Some("Title2001".into()),
album: Some("An Album2001".into()),
album_artist: Some("An Album Artist2001".into()),
track_number: Some("02".into()),
track_number: Some(2),
genre: Some("Electronica2001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1498,7 +1498,7 @@ mod test {
title: Some("Title2001".into()),
album: Some("Remixes of Album2001".into()),
album_artist: Some("An Album Artist2001".into()),
track_number: Some("02".into()),
track_number: Some(2),
genre: Some("Electronica2001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1526,7 +1526,7 @@ mod test {
title: Some("Title5001".into()),
album: Some("An Album1001".into()),
album_artist: Some("An Album Artist5001".into()),
track_number: Some("01".into()),
track_number: Some(1),
genre: Some("Electronica5001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1554,7 +1554,7 @@ mod test {
title: Some("Title6001".into()),
album: Some("An Album2001".into()),
album_artist: Some("An Album Artist6001".into()),
track_number: Some("01".into()),
track_number: Some(1),
genre: Some("Electronica6001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1582,7 +1582,7 @@ mod test {
title: Some("Title7001".into()),
album: Some("An Album7001".into()),
album_artist: Some("An Album Artist7001".into()),
track_number: Some("01".into()),
track_number: Some(1),
genre: Some("Electronica7001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1611,7 +1611,7 @@ mod test {
title: Some("CUE Title 01".into()),
album: Some("CUE Album".into()),
album_artist: Some("CUE Album Artist".into()),
track_number: Some("01".into()),
track_number: Some(1),
genre: None,
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1643,7 +1643,7 @@ mod test {
title: Some("CUE Title 02".into()),
album: Some("CUE Album".into()),
album_artist: Some("CUE Album Artist".into()),
track_number: Some("02".into()),
track_number: Some(2),
genre: None,
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -1952,7 +1952,7 @@ mod test {
title: Some("Title".into()),
album: Some("An Album".into()),
album_artist: Some("An Album Artist".into()),
track_number: Some("03".into()),
track_number: Some(3),
genre: Some("Electronica".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down Expand Up @@ -3020,7 +3020,7 @@ mod test {
title: Some("Title2001".into()),
album: Some("An Album2001".into()),
album_artist: Some("An Album Artist2001".into()),
track_number: Some("02".into()),
track_number: Some(2),
genre: Some("Electronica2001".into()),
analysis: Analysis {
internal_analysis: analysis_vector,
Expand Down
28 changes: 10 additions & 18 deletions src/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub fn dedup_playlist_custom_distance<'a, T: AsRef<Song>>(
///
/// * `group` - A small group of songs, e.g. an album.
/// * `pool` - A pool of songs to find similar songs in, e.g. a user's song
/// library.
/// library.
///
/// # Returns
///
Expand Down Expand Up @@ -322,22 +322,14 @@ pub fn closest_album_to_group<T: AsRef<Song> + Clone>(
.cloned()
.collect::<Vec<T>>();
al.sort_by(|s1, s2| {
let track_number1 = s1
.as_ref()
.track_number
.to_owned()
.unwrap_or_else(|| String::from(""));
let track_number2 = s2
.as_ref()
.track_number
.to_owned()
.unwrap_or_else(|| String::from(""));
if let Ok(x) = track_number1.parse::<i32>() {
if let Ok(y) = track_number2.parse::<i32>() {
let track_number1 = s1.as_ref().track_number.to_owned();
let track_number2 = s2.as_ref().track_number.to_owned();
if let Some(x) = track_number1 {
if let Some(y) = track_number2 {
return x.cmp(&y);
}
}
s1.as_ref().track_number.cmp(&s2.as_ref().track_number)
std::cmp::Ordering::Equal
});
playlist.extend(al);
}
Expand Down Expand Up @@ -820,7 +812,7 @@ mod test {
analysis: Analysis::new([0.; 20]),
album: Some(String::from("Album")),
artist: Some(String::from("Artist")),
track_number: Some(String::from("01")),
track_number: Some(1),
..Default::default()
};

Expand All @@ -829,7 +821,7 @@ mod test {
analysis: Analysis::new([0.1; 20]),
album: Some(String::from("Another Album")),
artist: Some(String::from("Artist")),
track_number: Some(String::from("10")),
track_number: Some(10),
..Default::default()
};

Expand All @@ -838,7 +830,7 @@ mod test {
analysis: Analysis::new([10.; 20]),
album: Some(String::from("Album")),
artist: Some(String::from("Another Artist")),
track_number: Some(String::from("02")),
track_number: Some(2),
..Default::default()
};

Expand All @@ -847,7 +839,7 @@ mod test {
analysis: Analysis::new([20.; 20]),
album: Some(String::from("Another Album")),
artist: Some(String::from("Another Artist")),
track_number: Some(String::from("01")),
track_number: Some(1),
..Default::default()
};
let fifth_song = Song {
Expand Down
7 changes: 3 additions & 4 deletions src/song/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub struct PreAnalyzedSong {
/// Song's album name, read from the metadata
pub album: Option<String>,
/// Song's tracked number, read from the metadata
/// TODO normalize this into an integer
pub track_number: Option<String>,
pub track_number: Option<i32>,
/// Song's genre, read from the metadata
pub genre: Option<String>,
/// The song's duration
Expand Down Expand Up @@ -510,7 +509,7 @@ pub mod ffmpeg {
if let Some(track_number) = ictx.metadata().get("track") {
song.track_number = match track_number {
"" => None,
t => Some(t.to_string()),
t => t.parse::<i32>().ok(),
};
};
if let Some(album_artist) = ictx.metadata().get("album_artist") {
Expand Down Expand Up @@ -666,7 +665,7 @@ pub mod ffmpeg {
);
assert_eq!(song.title, Some(String::from("Renaissance")));
assert_eq!(song.album, Some(String::from("Renaissance")));
assert_eq!(song.track_number, Some(String::from("02")));
assert_eq!(song.track_number, Some(2));
assert_eq!(song.genre, Some(String::from("Pop")));
// Test that there is less than 10ms of difference between what
// the song advertises and what we compute.
Expand Down
3 changes: 1 addition & 2 deletions src/song/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ pub struct Song {
/// Song's album's artist name, read from the metadata
pub album_artist: Option<String>,
/// Song's tracked number, read from the metadata
/// TODO normalize this into an integer
pub track_number: Option<String>,
pub track_number: Option<i32>,
/// Song's genre, read from the metadata
pub genre: Option<String>,
/// bliss analysis results
Expand Down

0 comments on commit 7dc1dda

Please sign in to comment.