Replace Song::new by Song::from_path

This commit is contained in:
Polochon-street 2022-04-02 19:47:06 +02:00
parent ef263c1eb1
commit c9342d7226
8 changed files with 24 additions and 23 deletions

View file

@ -3,6 +3,7 @@
## bliss 0.5.0 ## bliss 0.5.0
* Remove all traces of the "analyse" word vs "analyze" to make the codebase * Remove all traces of the "analyse" word vs "analyze" to make the codebase
more coherent. more coherent.
* Rename `Song::new` to `Song::from_path`.
## bliss 0.4.6 ## bliss 0.4.6
* Bump ffmpeg crate version to allow for cross-compilation. * Bump ffmpeg crate version to allow for cross-compilation.

View file

@ -48,8 +48,8 @@ Ready to use code examples:
use bliss_audio::{BlissError, Song}; use bliss_audio::{BlissError, Song};
fn main() -> Result<(), BlissError> { fn main() -> Result<(), BlissError> {
let song1 = Song::new("/path/to/song1")?; let song1 = Song::from_path("/path/to/song1")?;
let song2 = Song::new("/path/to/song2")?; let song2 = Song::from_path("/path/to/song2")?;
println!("Distance between song1 and song2 is {}", song1.distance(&song2)); println!("Distance between song1 and song2 is {}", song1.distance(&song2));
Ok(()) Ok(())
@ -65,7 +65,7 @@ fn main() -> Result<(), BlissError> {
let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"]; let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"];
let mut songs: Vec<Song> = paths let mut songs: Vec<Song> = paths
.iter() .iter()
.map(|path| Song::new(path)) .map(|path| Song::from_path(path))
.collect::<Result<Vec<Song>, BlissError>>()?; .collect::<Result<Vec<Song>, BlissError>>()?;
// Assuming there is a first song // Assuming there is a first song

View file

@ -9,7 +9,7 @@ use std::env;
fn main() { fn main() {
let args: Vec<String> = env::args().skip(1).collect(); let args: Vec<String> = env::args().skip(1).collect();
for path in &args { for path in &args {
match Song::new(&path) { match Song::from_path(&path) {
Ok(song) => println!("{}: {:?}", path, song.analysis), Ok(song) => println!("{}: {:?}", path, song.analysis),
Err(e) => println!("{}: {}", path, e), Err(e) => println!("{}: {}", path, e),
} }

View file

@ -13,8 +13,8 @@ fn main() -> Result<(), String> {
let first_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?; let first_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
let second_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?; let second_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
let song1 = Song::new(&first_path).map_err(|x| x.to_string())?; let song1 = Song::from_path(&first_path).map_err(|x| x.to_string())?;
let song2 = Song::new(&second_path).map_err(|x| x.to_string())?; let song2 = Song::from_path(&second_path).map_err(|x| x.to_string())?;
println!( println!(
"d({:?}, {:?}) = {}", "d({:?}, {:?}) = {}",

View file

@ -73,7 +73,7 @@ fn main() -> Result<()> {
.map(|p| p.to_owned()) .map(|p| p.to_owned())
.collect(), .collect(),
)?; )?;
let first_song = Song::new(file)?; let first_song = Song::from_path(file)?;
let mut analyzed_songs = vec![first_song.to_owned()]; let mut analyzed_songs = vec![first_song.to_owned()];
for (path, result) in rx.iter() { for (path, result) in rx.iter() {
match result { match result {

View file

@ -5,7 +5,7 @@
//! The core of the library is the `Song` object, which relates to a //! The core of the library is the `Song` object, which relates to a
//! specific analyzed song and contains its path, title, analysis, and //! specific analyzed song and contains its path, title, analysis, and
//! other metadata fields (album, genre...). //! other metadata fields (album, genre...).
//! Analyzing a song is as simple as running `Song::new("/path/to/song")`. //! Analyzing a song is as simple as running `Song::from_path("/path/to/song")`.
//! //!
//! The [analysis](Song::analysis) field of each song is an array of f32, which makes the //! The [analysis](Song::analysis) field of each song is an array of f32, which makes the
//! comparison between songs easy, by just using euclidean distance (see //! comparison between songs easy, by just using euclidean distance (see
@ -27,8 +27,8 @@
//! use bliss_audio::{BlissResult, Song}; //! use bliss_audio::{BlissResult, Song};
//! //!
//! fn main() -> BlissResult<()> { //! fn main() -> BlissResult<()> {
//! let song1 = Song::new("/path/to/song1")?; //! let song1 = Song::from_path("/path/to/song1")?;
//! let song2 = Song::new("/path/to/song2")?; //! let song2 = Song::from_path("/path/to/song2")?;
//! //!
//! println!("Distance between song1 and song2 is {}", song1.distance(&song2)); //! println!("Distance between song1 and song2 is {}", song1.distance(&song2));
//! Ok(()) //! Ok(())
@ -44,7 +44,7 @@
//! let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"]; //! let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"];
//! let mut songs: Vec<Song> = paths //! let mut songs: Vec<Song> = paths
//! .iter() //! .iter()
//! .map(|path| Song::new(path)) //! .map(|path| Song::from_path(path))
//! .collect::<BlissResult<Vec<Song>>>()?; //! .collect::<BlissResult<Vec<Song>>>()?;
//! //!
//! // Assuming there is a first song //! // Assuming there is a first song
@ -128,7 +128,7 @@ pub fn bulk_analyze(paths: Vec<String>) -> Vec<BlissResult<Song>> {
handles.push(s.spawn(move |_| { handles.push(s.spawn(move |_| {
let mut result = Vec::with_capacity(chunk.len()); let mut result = Vec::with_capacity(chunk.len());
for path in chunk { for path in chunk {
let song = Song::new(&path); let song = Song::from_path(&path);
result.push(song); result.push(song);
} }
result result

View file

@ -221,8 +221,8 @@ pub trait Library {
/// * `distance` - a user-supplied valid distance metric, either taken /// * `distance` - a user-supplied valid distance metric, either taken
/// from the [distance](distance) module, or made from scratch. /// from the [distance](distance) module, or made from scratch.
/// * `sort` - a user-supplied sorting function that uses the `distance` /// * `sort` - a user-supplied sorting function that uses the `distance`
/// metric, either taken from the [distance](module), or made from /// metric, either taken from the [distance module](distance), or made
/// scratch. /// from scratch.
/// ///
/// # Returns /// # Returns
/// ///
@ -276,7 +276,7 @@ pub trait Library {
let child = thread::spawn(move || { let child = thread::spawn(move || {
for path in owned_chunk { for path in owned_chunk {
info!("Analyzing file '{}'", path); info!("Analyzing file '{}'", path);
let song = Song::new(&path); let song = Song::from_path(&path);
tx_thread.send((path.to_string(), song)).unwrap(); tx_thread.send((path.to_string(), song)).unwrap();
} }
drop(tx_thread); drop(tx_thread);
@ -395,7 +395,7 @@ pub fn analyze_paths_streaming(
let child = thread::spawn(move || { let child = thread::spawn(move || {
for path in owned_chunk { for path in owned_chunk {
info!("Analyzing file '{}'", path); info!("Analyzing file '{}'", path);
let song = Song::new(&path); let song = Song::from_path(&path);
tx_thread.send((path.to_string(), song)).unwrap(); tx_thread.send((path.to_string(), song)).unwrap();
} }
}); });

View file

@ -75,7 +75,7 @@ pub struct Song {
/// use bliss_audio::{AnalysisIndex, BlissResult, Song}; /// use bliss_audio::{AnalysisIndex, BlissResult, Song};
/// ///
/// fn main() -> BlissResult<()> { /// fn main() -> BlissResult<()> {
/// let song = Song::new("path/to/song")?; /// let song = Song::from_path("path/to/song")?;
/// println!("{}", song.analysis[AnalysisIndex::Tempo]); /// println!("{}", song.analysis[AnalysisIndex::Tempo]);
/// Ok(()) /// Ok(())
/// } /// }
@ -228,12 +228,12 @@ impl Song {
self.analysis.custom_distance(&other.analysis, distance) self.analysis.custom_distance(&other.analysis, distance)
} }
/// Returns a decoded Song given a file path, or an error if the song /// Returns a decoded [Song] given a file path, or an error if the song
/// could not be analyzed for some reason. /// could not be analyzed for some reason.
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `path` - A string holding a valid file path to a valid audio file. /// * `path` - A [Path] holding a valid file path to a valid audio file.
/// ///
/// # Errors /// # Errors
/// ///
@ -244,7 +244,7 @@ impl Song {
/// The error type returned should give a hint as to whether it was a /// The error type returned should give a hint as to whether it was a
/// decoding ([DecodingError](BlissError::DecodingError)) or an analysis /// decoding ([DecodingError](BlissError::DecodingError)) or an analysis
/// ([AnalysisError](BlissError::AnalysisError)) error. /// ([AnalysisError](BlissError::AnalysisError)) error.
pub fn new<P: AsRef<Path>>(path: P) -> BlissResult<Self> { pub fn from_path<P: AsRef<Path>>(path: P) -> BlissResult<Self> {
let raw_song = Song::decode(path.as_ref())?; let raw_song = Song::decode(path.as_ref())?;
Ok(Song { Ok(Song {
@ -640,7 +640,7 @@ mod tests {
#[test] #[test]
fn test_analyze() { fn test_analyze() {
let song = Song::new(Path::new("data/s16_mono_22_5kHz.flac")).unwrap(); let song = Song::from_path(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
let expected_analysis = vec![ let expected_analysis = vec![
0.3846389, 0.3846389,
-0.849141, -0.849141,
@ -820,14 +820,14 @@ mod tests {
#[test] #[test]
fn test_index_analysis() { fn test_index_analysis() {
let song = Song::new("data/s16_mono_22_5kHz.flac").unwrap(); let song = Song::from_path("data/s16_mono_22_5kHz.flac").unwrap();
assert_eq!(song.analysis[AnalysisIndex::Tempo], 0.3846389); assert_eq!(song.analysis[AnalysisIndex::Tempo], 0.3846389);
assert_eq!(song.analysis[AnalysisIndex::Chroma10], -0.95968974); assert_eq!(song.analysis[AnalysisIndex::Chroma10], -0.95968974);
} }
#[test] #[test]
fn test_debug_analysis() { fn test_debug_analysis() {
let song = Song::new("data/s16_mono_22_5kHz.flac").unwrap(); let song = Song::from_path("data/s16_mono_22_5kHz.flac").unwrap();
assert_eq!( assert_eq!(
"Analysis { Tempo: 0.3846389, Zcr: -0.849141, MeanSpectralCentroid: \ "Analysis { Tempo: 0.3846389, Zcr: -0.849141, MeanSpectralCentroid: \
-0.75481045, StdDeviationSpectralCentroid: -0.8790748, MeanSpectralR\ -0.75481045, StdDeviationSpectralCentroid: -0.8790748, MeanSpectralR\