Remove unused stuff
This commit is contained in:
parent
75d6db0ee8
commit
719f6baaa4
838
Cargo.lock
generated
838
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
45
Cargo.toml
45
Cargo.toml
|
@ -19,36 +19,14 @@ crate-type = ["rlib", "cdylib"]
|
||||||
features = ["bliss-audio-aubio-rs/rustdoc", "library"]
|
features = ["bliss-audio-aubio-rs/rustdoc", "library"]
|
||||||
no-default-features = true
|
no-default-features = true
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["bliss-audio-aubio-rs/static"]
|
|
||||||
# Build ffmpeg instead of using the host's.
|
|
||||||
build-ffmpeg = ["ffmpeg-next/build"]
|
|
||||||
ffmpeg-static = ["ffmpeg-next/static"]
|
|
||||||
# Build for raspberry pis
|
|
||||||
rpi = ["ffmpeg-next/rpi"]
|
|
||||||
# Use if you get "No prebuilt bindings. Try use `bindgen` feature"
|
|
||||||
update-aubio-bindings = ["bliss-audio-aubio-rs/bindgen"]
|
|
||||||
# Use if you want to build python bindings with maturin.
|
|
||||||
python-bindings = ["bliss-audio-aubio-rs/fftw3"]
|
|
||||||
# Enable the benchmarks with `cargo +nightly bench --features=bench`
|
|
||||||
bench = []
|
|
||||||
library = [
|
|
||||||
"serde", "dep:rusqlite", "dep:dirs", "dep:tempdir",
|
|
||||||
"dep:anyhow", "dep:serde_ini", "dep:serde_json",
|
|
||||||
"dep:indicatif",
|
|
||||||
]
|
|
||||||
serde = ["dep:serde"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# Until https://github.com/aubio/aubio/issues/336 is somehow solved
|
# 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.
|
# Hopefully we'll be able to use the official aubio-rs at some point.
|
||||||
bliss-audio-aubio-rs = { version = "0.2.1", features = ["bindgen"] }
|
bliss-audio-aubio-rs = { version = "0.2.1", features = ["static"] }
|
||||||
crossbeam = "0.8.2"
|
crossbeam = "0.8.2"
|
||||||
ffmpeg-next = "6.0.0"
|
ffmpeg-next = { version = "6.0.0", features = ["static"] }
|
||||||
ffmpeg-sys-next = { version = "6.0.1", default-features = false }
|
|
||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
ndarray = { version = "0.15.6", features = ["rayon"] }
|
ndarray = { version = "0.15.6", features = ["rayon"] }
|
||||||
num_cpus = "1.15.0"
|
|
||||||
ndarray-stats = "0.5.1"
|
ndarray-stats = "0.5.1"
|
||||||
noisy_float = "0.2.0"
|
noisy_float = "0.2.0"
|
||||||
ripemd = "0.1.3"
|
ripemd = "0.1.3"
|
||||||
|
@ -56,17 +34,11 @@ rustfft = "6.1.0"
|
||||||
thiserror = "1.0.40"
|
thiserror = "1.0.40"
|
||||||
strum = "0.24.1"
|
strum = "0.24.1"
|
||||||
strum_macros = "0.24.3"
|
strum_macros = "0.24.3"
|
||||||
rcue = "0.1.3"
|
|
||||||
|
|
||||||
# Deps for the library feature
|
# Deps for the library feature
|
||||||
serde = { version = "1.0", optional = true, features = ["derive"] }
|
serde = { version = "1.0", optional = true, features = ["derive"] }
|
||||||
serde_json = { version = "1.0.59", optional = true }
|
serde_json = { version = "1.0.59", optional = true }
|
||||||
serde_ini = { version = "0.2.0", optional = true }
|
serde_ini = { version = "0.2.0", optional = true }
|
||||||
tempdir = { version = "0.3.7", optional = true }
|
|
||||||
rusqlite = { version = "0.28.0", optional = true }
|
|
||||||
dirs = { version = "5.0.0", optional = true }
|
|
||||||
anyhow = { version = "1.0.58", optional = true }
|
|
||||||
indicatif = { version = "0.17.0", optional = true }
|
|
||||||
|
|
||||||
[dependencies.neon]
|
[dependencies.neon]
|
||||||
version = "1.0.0-alpha.4"
|
version = "1.0.0-alpha.4"
|
||||||
|
@ -81,16 +53,3 @@ anyhow = "1.0.45"
|
||||||
clap = "2.33.3"
|
clap = "2.33.3"
|
||||||
pretty_assertions = "1.3.0"
|
pretty_assertions = "1.3.0"
|
||||||
serde_json = "1.0.59"
|
serde_json = "1.0.59"
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "library"
|
|
||||||
required-features = ["library"]
|
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "library_extra_info"
|
|
||||||
required-features = ["library"]
|
|
||||||
|
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "playlist"
|
|
||||||
required-features = ["serde"]
|
|
||||||
|
|
|
@ -16,11 +16,18 @@ fn main() -> Result<(), String> {
|
||||||
let song1 = Song::from_path(first_path).map_err(|x| x.to_string())?;
|
let song1 = Song::from_path(first_path).map_err(|x| x.to_string())?;
|
||||||
let song2 = Song::from_path(second_path).map_err(|x| x.to_string())?;
|
let song2 = Song::from_path(second_path).map_err(|x| x.to_string())?;
|
||||||
|
|
||||||
|
let mut distance_squared: f64 = 0.0;
|
||||||
|
let analysis1 = song1.analysis.as_bytes();
|
||||||
|
let analysis2 = song2.analysis.as_bytes();
|
||||||
|
for (i, feature1) in analysis1.iter().enumerate() {
|
||||||
|
distance_squared += (feature1 - analysis2[i]).pow(2) as f64;
|
||||||
|
}
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"d({:?}, {:?}) = {}",
|
"d({:?}, {:?}) = {}",
|
||||||
song1.path,
|
song1.path,
|
||||||
song2.path,
|
song2.path,
|
||||||
song1.distance(&song2)
|
distance_squared.sqrt(),
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,204 +0,0 @@
|
||||||
/// Basic example of how one would combine bliss with an "audio player",
|
|
||||||
/// through [Library].
|
|
||||||
///
|
|
||||||
/// For simplicity's sake, this example recursively gets songs from a folder
|
|
||||||
/// to emulate an audio player library, without handling CUE files.
|
|
||||||
use anyhow::Result;
|
|
||||||
use bliss_audio::library::{AppConfigTrait, BaseConfig, Library};
|
|
||||||
use clap::{App, Arg, SubCommand};
|
|
||||||
use glob::glob;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::fs;
|
|
||||||
use std::num::NonZeroUsize;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
// A config structure, that will be serialized as a
|
|
||||||
// JSON file upon Library creation.
|
|
||||||
pub struct Config {
|
|
||||||
#[serde(flatten)]
|
|
||||||
// The base configuration, containing both the config file
|
|
||||||
// path, as well as the database path.
|
|
||||||
pub base_config: BaseConfig,
|
|
||||||
// An extra field, to store the music library path. Any number
|
|
||||||
// of arbitrary fields (even Serializable structures) can
|
|
||||||
// of course be added.
|
|
||||||
pub music_library_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn new(
|
|
||||||
music_library_path: PathBuf,
|
|
||||||
config_path: Option<PathBuf>,
|
|
||||||
database_path: Option<PathBuf>,
|
|
||||||
number_cores: Option<NonZeroUsize>,
|
|
||||||
) -> Result<Self> {
|
|
||||||
let base_config = BaseConfig::new(config_path, database_path, number_cores)?;
|
|
||||||
Ok(Self {
|
|
||||||
base_config,
|
|
||||||
music_library_path,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The AppConfigTrait must know how to access the base config.
|
|
||||||
impl AppConfigTrait for Config {
|
|
||||||
fn base_config(&self) -> &BaseConfig {
|
|
||||||
&self.base_config
|
|
||||||
}
|
|
||||||
|
|
||||||
fn base_config_mut(&mut self) -> &mut BaseConfig {
|
|
||||||
&mut self.base_config
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A trait allowing to implement methods for the Library,
|
|
||||||
// useful if you don't need to store extra information in fields.
|
|
||||||
// Otherwise, doing
|
|
||||||
// ```
|
|
||||||
// struct CustomLibrary {
|
|
||||||
// library: Library<Config>,
|
|
||||||
// extra_field: ...,
|
|
||||||
// }
|
|
||||||
// ```
|
|
||||||
// and implementing functions for that struct would be the way to go.
|
|
||||||
// That's what the [reference](https://github.com/Polochon-street/blissify-rs)
|
|
||||||
// implementation does.
|
|
||||||
trait CustomLibrary {
|
|
||||||
fn song_paths(&self) -> Result<Vec<String>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CustomLibrary for Library<Config> {
|
|
||||||
/// Get all songs in the player library
|
|
||||||
fn song_paths(&self) -> Result<Vec<String>> {
|
|
||||||
let music_path = &self.config.music_library_path;
|
|
||||||
let pattern = Path::new(&music_path).join("**").join("*");
|
|
||||||
|
|
||||||
Ok(glob(&pattern.to_string_lossy())?
|
|
||||||
.map(|e| fs::canonicalize(e.unwrap()).unwrap())
|
|
||||||
.filter(|e| match mime_guess::from_path(e).first() {
|
|
||||||
Some(m) => m.type_() == "audio",
|
|
||||||
None => false,
|
|
||||||
})
|
|
||||||
.map(|x| x.to_string_lossy().to_string())
|
|
||||||
.collect::<Vec<String>>())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A simple example of what a CLI-app would look.
|
|
||||||
//
|
|
||||||
// Note that `Library::new` is used only on init, and subsequent
|
|
||||||
// commands use `Library::from_path`.
|
|
||||||
fn main() -> Result<()> {
|
|
||||||
let matches = App::new("library-example")
|
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
|
||||||
.author("Polochon_street")
|
|
||||||
.about("Example binary implementing bliss for an audio player.")
|
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("init")
|
|
||||||
.about(
|
|
||||||
"Initialize a Library, both storing the config and analyzing folders
|
|
||||||
containing songs.",
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("FOLDER")
|
|
||||||
.help("A folder containing the music library to analyze.")
|
|
||||||
.required(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("database-path")
|
|
||||||
.short("d")
|
|
||||||
.long("database-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to store the database file containing
|
|
||||||
the songs' analysis. Defaults to XDG_DATA_HOME/bliss-rs/bliss.db.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("config-path")
|
|
||||||
.short("c")
|
|
||||||
.long("config-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to store the config file containing
|
|
||||||
the library setup. Defaults to XDG_DATA_HOME/bliss-rs/config.json.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("update")
|
|
||||||
.about(
|
|
||||||
"Update a Library's songs, trying to analyze failed songs,
|
|
||||||
as well as songs not in the library.",
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("config-path")
|
|
||||||
.short("c")
|
|
||||||
.long("config-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to load the config file containing
|
|
||||||
the library setup. Defaults to XDG_DATA_HOME/bliss-rs/config.json.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("playlist")
|
|
||||||
.about(
|
|
||||||
"Make a playlist, starting with the song at SONG_PATH, returning
|
|
||||||
the songs' paths.",
|
|
||||||
)
|
|
||||||
.arg(Arg::with_name("SONG_PATH").takes_value(true))
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("config-path")
|
|
||||||
.short("c")
|
|
||||||
.long("config-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to load the config file containing
|
|
||||||
the library setup. Defaults to XDG_DATA_HOME/bliss-rs/config.json.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("playlist-length")
|
|
||||||
.short("l")
|
|
||||||
.long("playlist-length")
|
|
||||||
.help("Optional playlist length. Defaults to 20.")
|
|
||||||
.takes_value(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.get_matches();
|
|
||||||
if let Some(sub_m) = matches.subcommand_matches("init") {
|
|
||||||
let folder = PathBuf::from(sub_m.value_of("FOLDER").unwrap());
|
|
||||||
let config_path = sub_m.value_of("config-path").map(PathBuf::from);
|
|
||||||
let database_path = sub_m.value_of("database-path").map(PathBuf::from);
|
|
||||||
|
|
||||||
let config = Config::new(folder, config_path, database_path, None)?;
|
|
||||||
let mut library = Library::new(config)?;
|
|
||||||
|
|
||||||
library.analyze_paths(library.song_paths()?, true)?;
|
|
||||||
} else if let Some(sub_m) = matches.subcommand_matches("update") {
|
|
||||||
let config_path = sub_m.value_of("config-path").map(PathBuf::from);
|
|
||||||
let mut library: Library<Config> = Library::from_config_path(config_path)?;
|
|
||||||
library.update_library(library.song_paths()?, true, true)?;
|
|
||||||
} else if let Some(sub_m) = matches.subcommand_matches("playlist") {
|
|
||||||
let song_path = sub_m.value_of("SONG_PATH").unwrap();
|
|
||||||
let config_path = sub_m.value_of("config-path").map(PathBuf::from);
|
|
||||||
let playlist_length = sub_m
|
|
||||||
.value_of("playlist-length")
|
|
||||||
.unwrap_or("20")
|
|
||||||
.parse::<usize>()?;
|
|
||||||
let library: Library<Config> = Library::from_config_path(config_path)?;
|
|
||||||
let songs = library.playlist_from::<()>(song_path, playlist_length)?;
|
|
||||||
let song_paths = songs
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| s.bliss_song.path.to_string_lossy().to_string())
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
for song in song_paths {
|
|
||||||
println!("{song:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,227 +0,0 @@
|
||||||
/// Basic example of how one would combine bliss with an "audio player",
|
|
||||||
/// through [Library], showing how to put extra info in the database for
|
|
||||||
/// each song.
|
|
||||||
///
|
|
||||||
/// For simplicity's sake, this example recursively gets songs from a folder
|
|
||||||
/// to emulate an audio player library, without handling CUE files.
|
|
||||||
use anyhow::Result;
|
|
||||||
use bliss_audio::library::{AppConfigTrait, BaseConfig, Library};
|
|
||||||
use clap::{App, Arg, SubCommand};
|
|
||||||
use glob::glob;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::fs;
|
|
||||||
use std::num::NonZeroUsize;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
/// A config structure, that will be serialized as a
|
|
||||||
/// JSON file upon Library creation.
|
|
||||||
pub struct Config {
|
|
||||||
#[serde(flatten)]
|
|
||||||
/// The base configuration, containing both the config file
|
|
||||||
/// path, as well as the database path.
|
|
||||||
pub base_config: BaseConfig,
|
|
||||||
/// An extra field, to store the music library path. Any number
|
|
||||||
/// of arbitrary fields (even Serializable structures) can
|
|
||||||
/// of course be added.
|
|
||||||
pub music_library_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn new(
|
|
||||||
music_library_path: PathBuf,
|
|
||||||
config_path: Option<PathBuf>,
|
|
||||||
database_path: Option<PathBuf>,
|
|
||||||
number_cores: Option<NonZeroUsize>,
|
|
||||||
) -> Result<Self> {
|
|
||||||
let base_config = BaseConfig::new(config_path, database_path, number_cores)?;
|
|
||||||
Ok(Self {
|
|
||||||
base_config,
|
|
||||||
music_library_path,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The AppConfigTrait must know how to access the base config.
|
|
||||||
impl AppConfigTrait for Config {
|
|
||||||
fn base_config(&self) -> &BaseConfig {
|
|
||||||
&self.base_config
|
|
||||||
}
|
|
||||||
|
|
||||||
fn base_config_mut(&mut self) -> &mut BaseConfig {
|
|
||||||
&mut self.base_config
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A trait allowing to implement methods for the Library,
|
|
||||||
// useful if you don't need to store extra information in fields.
|
|
||||||
// Otherwise, doing
|
|
||||||
// ```
|
|
||||||
// struct CustomLibrary {
|
|
||||||
// library: Library<Config>,
|
|
||||||
// extra_field: ...,
|
|
||||||
// }
|
|
||||||
// ```
|
|
||||||
// and implementing functions for that struct would be the way to go.
|
|
||||||
// That's what the [reference](https://github.com/Polochon-street/blissify-rs)
|
|
||||||
// implementation does.
|
|
||||||
trait CustomLibrary {
|
|
||||||
fn song_paths_info(&self) -> Result<Vec<(String, ExtraInfo)>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CustomLibrary for Library<Config> {
|
|
||||||
/// Get all songs in the player library, along with the extra info
|
|
||||||
/// one would want to store along with each song.
|
|
||||||
fn song_paths_info(&self) -> Result<Vec<(String, ExtraInfo)>> {
|
|
||||||
let music_path = &self.config.music_library_path;
|
|
||||||
let pattern = Path::new(&music_path).join("**").join("*");
|
|
||||||
|
|
||||||
Ok(glob(&pattern.to_string_lossy())?
|
|
||||||
.map(|e| fs::canonicalize(e.unwrap()).unwrap())
|
|
||||||
.filter_map(|e| {
|
|
||||||
mime_guess::from_path(&e).first().map(|m| {
|
|
||||||
(
|
|
||||||
e.to_string_lossy().to_string(),
|
|
||||||
ExtraInfo {
|
|
||||||
extension: e.extension().map(|e| e.to_string_lossy().to_string()),
|
|
||||||
file_name: e.file_name().map(|e| e.to_string_lossy().to_string()),
|
|
||||||
mime_type: format!("{}/{}", m.type_(), m.subtype()),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<(String, ExtraInfo)>>())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
|
|
||||||
// An (somewhat simple) example of what extra metadata one would put, along
|
|
||||||
// with song analysis data.
|
|
||||||
struct ExtraInfo {
|
|
||||||
extension: Option<String>,
|
|
||||||
file_name: Option<String>,
|
|
||||||
mime_type: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// A simple example of what a CLI-app would look.
|
|
||||||
//
|
|
||||||
// Note that `Library::new` is used only on init, and subsequent
|
|
||||||
// commands use `Library::from_path`.
|
|
||||||
fn main() -> Result<()> {
|
|
||||||
let matches = App::new("library-example")
|
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
|
||||||
.author("Polochon_street")
|
|
||||||
.about("Example binary implementing bliss for an audio player.")
|
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("init")
|
|
||||||
.about(
|
|
||||||
"Initialize a Library, both storing the config and analyzing folders
|
|
||||||
containing songs.",
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("FOLDER")
|
|
||||||
.help("A folder containing the music library to analyze.")
|
|
||||||
.required(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("database-path")
|
|
||||||
.short("d")
|
|
||||||
.long("database-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to store the database file containing
|
|
||||||
the songs' analysis. Defaults to XDG_DATA_HOME/bliss-rs/bliss.db.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("config-path")
|
|
||||||
.short("c")
|
|
||||||
.long("config-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to store the config file containing
|
|
||||||
the library setup. Defaults to XDG_DATA_HOME/bliss-rs/config.json.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("update")
|
|
||||||
.about(
|
|
||||||
"Update a Library's songs, trying to analyze failed songs,
|
|
||||||
as well as songs not in the library.",
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("config-path")
|
|
||||||
.short("c")
|
|
||||||
.long("config-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to load the config file containing
|
|
||||||
the library setup. Defaults to XDG_DATA_HOME/bliss-rs/config.json.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.subcommand(
|
|
||||||
SubCommand::with_name("playlist")
|
|
||||||
.about(
|
|
||||||
"Make a playlist, starting with the song at SONG_PATH, returning
|
|
||||||
the songs' paths.",
|
|
||||||
)
|
|
||||||
.arg(Arg::with_name("SONG_PATH").takes_value(true))
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("config-path")
|
|
||||||
.short("c")
|
|
||||||
.long("config-path")
|
|
||||||
.help(
|
|
||||||
"Optional path where to load the config file containing
|
|
||||||
the library setup. Defaults to XDG_DATA_HOME/bliss-rs/config.json.",
|
|
||||||
)
|
|
||||||
.takes_value(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("playlist-length")
|
|
||||||
.short("l")
|
|
||||||
.long("playlist-length")
|
|
||||||
.help("Optional playlist length. Defaults to 20.")
|
|
||||||
.takes_value(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.get_matches();
|
|
||||||
if let Some(sub_m) = matches.subcommand_matches("init") {
|
|
||||||
let folder = PathBuf::from(sub_m.value_of("FOLDER").unwrap());
|
|
||||||
let config_path = sub_m.value_of("config-path").map(PathBuf::from);
|
|
||||||
let database_path = sub_m.value_of("database-path").map(PathBuf::from);
|
|
||||||
|
|
||||||
let config = Config::new(folder, config_path, database_path, None)?;
|
|
||||||
let mut library = Library::new(config)?;
|
|
||||||
|
|
||||||
library.analyze_paths_extra_info(library.song_paths_info()?, true)?;
|
|
||||||
} else if let Some(sub_m) = matches.subcommand_matches("update") {
|
|
||||||
let config_path = sub_m.value_of("config-path").map(PathBuf::from);
|
|
||||||
let mut library: Library<Config> = Library::from_config_path(config_path)?;
|
|
||||||
library.update_library_extra_info(library.song_paths_info()?, true, true)?;
|
|
||||||
} else if let Some(sub_m) = matches.subcommand_matches("playlist") {
|
|
||||||
let song_path = sub_m.value_of("SONG_PATH").unwrap();
|
|
||||||
let config_path = sub_m.value_of("config-path").map(PathBuf::from);
|
|
||||||
let playlist_length = sub_m
|
|
||||||
.value_of("playlist-length")
|
|
||||||
.unwrap_or("20")
|
|
||||||
.parse::<usize>()?;
|
|
||||||
let library: Library<Config> = Library::from_config_path(config_path)?;
|
|
||||||
let songs = library.playlist_from::<ExtraInfo>(song_path, playlist_length)?;
|
|
||||||
let playlist = songs
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| {
|
|
||||||
(
|
|
||||||
s.bliss_song.path.to_string_lossy().to_string(),
|
|
||||||
s.extra_info.mime_type,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<(String, String)>>();
|
|
||||||
for (path, mime_type) in playlist {
|
|
||||||
println!("{path} <{mime_type}>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,95 +0,0 @@
|
||||||
use anyhow::Result;
|
|
||||||
use bliss_rs::playlist::{closest_to_first_song, dedup_playlist, euclidean_distance};
|
|
||||||
use bliss_rs::bliss_lib::{analyze_paths, Song};
|
|
||||||
use clap::{App, Arg};
|
|
||||||
use glob::glob;
|
|
||||||
use std::env;
|
|
||||||
use std::fs;
|
|
||||||
use std::io::BufReader;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
/* Analyzes a folder recursively, and make a playlist out of the file
|
|
||||||
* provided by the user. */
|
|
||||||
// How to use: ./playlist [-o file.m3u] [-a analysis.json] <folder> <file to start the playlist from>
|
|
||||||
fn main() -> Result<()> {
|
|
||||||
let matches = App::new("playlist")
|
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
|
||||||
.author("Polochon_street")
|
|
||||||
.about("Analyze a folder and make a playlist from a target song")
|
|
||||||
.arg(Arg::with_name("output-playlist").short("o").long("output-playlist")
|
|
||||||
.value_name("PLAYLIST.M3U")
|
|
||||||
.help("Outputs the playlist to a file.")
|
|
||||||
.takes_value(true))
|
|
||||||
.arg(Arg::with_name("analysis-file").short("a").long("analysis-file")
|
|
||||||
.value_name("ANALYSIS.JSON")
|
|
||||||
.help("Use the songs that have been analyzed in <analysis-file>, and appends newly analyzed songs to it. Defaults to /tmp/analysis.json.")
|
|
||||||
.takes_value(true))
|
|
||||||
.arg(Arg::with_name("FOLDER").help("Folders containing some songs.").required(true))
|
|
||||||
.arg(Arg::with_name("FIRST-SONG").help("Song to start from (can be outside of FOLDER).").required(true))
|
|
||||||
.get_matches();
|
|
||||||
|
|
||||||
let folder = matches.value_of("FOLDER").unwrap();
|
|
||||||
let file = fs::canonicalize(matches.value_of("FIRST-SONG").unwrap())?;
|
|
||||||
let pattern = Path::new(folder).join("**").join("*");
|
|
||||||
|
|
||||||
let mut songs: Vec<Song> = Vec::new();
|
|
||||||
let analysis_path = matches
|
|
||||||
.value_of("analysis-file")
|
|
||||||
.unwrap_or("/tmp/analysis.json");
|
|
||||||
let analysis_file = fs::File::open(analysis_path);
|
|
||||||
if let Ok(f) = analysis_file {
|
|
||||||
let reader = BufReader::new(f);
|
|
||||||
songs = serde_json::from_reader(reader)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let analyzed_paths = songs
|
|
||||||
.iter()
|
|
||||||
.map(|s| s.path.to_owned())
|
|
||||||
.collect::<Vec<PathBuf>>();
|
|
||||||
|
|
||||||
let paths = glob(&pattern.to_string_lossy())?
|
|
||||||
.map(|e| fs::canonicalize(e.unwrap()).unwrap())
|
|
||||||
.filter(|e| match mime_guess::from_path(e).first() {
|
|
||||||
Some(m) => m.type_() == "audio",
|
|
||||||
None => false,
|
|
||||||
})
|
|
||||||
.map(|x| x.to_string_lossy().to_string())
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
|
|
||||||
let song_iterator = analyze_paths(
|
|
||||||
paths
|
|
||||||
.iter()
|
|
||||||
.filter(|p| !analyzed_paths.contains(&PathBuf::from(p)))
|
|
||||||
.map(|p| p.to_owned())
|
|
||||||
.collect::<Vec<String>>(),
|
|
||||||
);
|
|
||||||
let first_song = Song::from_path(file)?;
|
|
||||||
let mut analyzed_songs = vec![first_song.to_owned()];
|
|
||||||
for (path, result) in song_iterator {
|
|
||||||
match result {
|
|
||||||
Ok(song) => analyzed_songs.push(song),
|
|
||||||
Err(e) => println!("error analyzing {}: {}", path.display(), e),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
analyzed_songs.extend_from_slice(&songs);
|
|
||||||
let serialized = serde_json::to_string(&analyzed_songs).unwrap();
|
|
||||||
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();
|
|
||||||
closest_to_first_song(&first_song, &mut songs_to_chose_from, euclidean_distance);
|
|
||||||
dedup_playlist(&mut songs_to_chose_from, None);
|
|
||||||
|
|
||||||
fs::write(analysis_path, serialized)?;
|
|
||||||
let playlist = songs_to_chose_from
|
|
||||||
.iter()
|
|
||||||
.map(|s| s.path.to_string_lossy().to_string())
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join("\n");
|
|
||||||
if let Some(m) = matches.value_of("output-playlist") {
|
|
||||||
fs::write(m, playlist)?;
|
|
||||||
} else {
|
|
||||||
println!("{playlist}");
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
240
src/bliss_lib.rs
240
src/bliss_lib.rs
|
@ -15,14 +15,6 @@
|
||||||
//! is as easy as computing distances between that song and the rest, and ordering
|
//! is as easy as computing distances between that song and the rest, and ordering
|
||||||
//! the songs by distance, ascending.
|
//! the songs by distance, ascending.
|
||||||
//!
|
//!
|
||||||
//! If you want to implement a bliss plugin for an already existing audio
|
|
||||||
//! player, the [Library] struct is a collection of goodies that should prove
|
|
||||||
//! useful (it contains utilities to store analyzed songs in a self-contained
|
|
||||||
//! database file, to make playlists directly from the database, etc).
|
|
||||||
//! [blissify](https://github.com/Polochon-street/blissify-rs/) for both
|
|
||||||
//! an example of how the [Library] struct works, and a real-life demo of bliss
|
|
||||||
//! implemented for [MPD](https://www.musicpd.org/).
|
|
||||||
//!
|
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
//! ### Analyze & compute the distance between two songs
|
//! ### Analyze & compute the distance between two songs
|
||||||
|
@ -37,40 +29,9 @@
|
||||||
//! Ok(())
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
|
||||||
//! ### Make a playlist from a song, discarding failed songs
|
|
||||||
//! ```no_run
|
|
||||||
//! use bliss_audio::{
|
|
||||||
//! analyze_paths,
|
|
||||||
//! playlist::{closest_to_first_song, euclidean_distance},
|
|
||||||
//! BlissResult, Song,
|
|
||||||
//! };
|
|
||||||
//!
|
|
||||||
//! fn main() -> BlissResult<()> {
|
|
||||||
//! let paths = vec!["/path/to/song1", "/path/to/song2", "/path/to/song3"];
|
|
||||||
//! let mut songs: Vec<Song> = analyze_paths(&paths).filter_map(|(_, s)| s.ok()).collect();
|
|
||||||
//!
|
|
||||||
//! // Assuming there is a first song
|
|
||||||
//! let first_song = songs.first().unwrap().to_owned();
|
|
||||||
//!
|
|
||||||
//! closest_to_first_song(&first_song, &mut songs, euclidean_distance);
|
|
||||||
//!
|
|
||||||
//! println!("Playlist is:");
|
|
||||||
//! for song in songs {
|
|
||||||
//! println!("{}", song.path.display());
|
|
||||||
//! }
|
|
||||||
//! Ok(())
|
|
||||||
//! }
|
|
||||||
//! ```
|
|
||||||
#![cfg_attr(feature = "bench", feature(test))]
|
#![cfg_attr(feature = "bench", feature(test))]
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
use crate::cue::BlissCue;
|
|
||||||
use log::info;
|
|
||||||
use std::num::NonZeroUsize;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::sync::mpsc;
|
|
||||||
use std::thread;
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub use crate::song::{Analysis, AnalysisIndex, Song, NUMBER_FEATURES};
|
pub use crate::song::{Analysis, AnalysisIndex, Song, NUMBER_FEATURES};
|
||||||
|
@ -103,147 +64,9 @@ pub enum BlissError {
|
||||||
/// bliss error type
|
/// bliss error type
|
||||||
pub type BlissResult<T> = Result<T, BlissError>;
|
pub type BlissResult<T> = Result<T, BlissError>;
|
||||||
|
|
||||||
/// Analyze songs in `paths`, and return the analyzed [Song] objects through an
|
|
||||||
/// [mpsc::IntoIter].
|
|
||||||
///
|
|
||||||
/// Returns an iterator, whose items are a tuple made of
|
|
||||||
/// the song path (to display to the user in case the analysis failed),
|
|
||||||
/// and a Result<Song>.
|
|
||||||
///
|
|
||||||
/// # Note
|
|
||||||
///
|
|
||||||
/// This function also works with CUE files - it finds the audio files
|
|
||||||
/// mentionned in the CUE sheet, and then runs the analysis on each song
|
|
||||||
/// defined by it, returning a proper [Song] object for each one of them.
|
|
||||||
///
|
|
||||||
/// Make sure that you don't submit both the audio file along with the CUE
|
|
||||||
/// sheet if your library uses them, otherwise the audio file will be
|
|
||||||
/// analyzed as one, single, long song. For instance, with a CUE sheet named
|
|
||||||
/// `cue-file.cue` with the corresponding audio files `album-1.wav` and
|
|
||||||
/// `album-2.wav` defined in the CUE sheet, you would just pass `cue-file.cue`
|
|
||||||
/// to `analyze_paths`, and it will return [Song]s from both files, with
|
|
||||||
/// more information about which file it is extracted from in the
|
|
||||||
/// [cue info field](Song::cue_info).
|
|
||||||
///
|
|
||||||
/// # Example:
|
|
||||||
/// ```no_run
|
|
||||||
/// use bliss_audio::{analyze_paths, BlissResult};
|
|
||||||
///
|
|
||||||
/// fn main() -> BlissResult<()> {
|
|
||||||
/// let paths = vec![String::from("/path/to/song1"), String::from("/path/to/song2")];
|
|
||||||
/// for (path, result) in analyze_paths(&paths) {
|
|
||||||
/// match result {
|
|
||||||
/// Ok(song) => println!("Do something with analyzed song {} with title {:?}", song.path.display(), song.title),
|
|
||||||
/// Err(e) => println!("Song at {} could not be analyzed. Failed with: {}", path.display(), e),
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// Ok(())
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub fn analyze_paths<P: Into<PathBuf>, F: IntoIterator<Item = P>>(
|
|
||||||
paths: F,
|
|
||||||
) -> mpsc::IntoIter<(PathBuf, BlissResult<Song>)> {
|
|
||||||
let cores = NonZeroUsize::new(num_cpus::get()).unwrap();
|
|
||||||
analyze_paths_with_cores(paths, cores)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Analyze songs in `paths`, and return the analyzed [Song] objects through an
|
|
||||||
/// [mpsc::IntoIter]. `number_cores` sets the number of cores the analysis
|
|
||||||
/// will use, capped by your system's capacity. Most of the time, you want to
|
|
||||||
/// use the simpler `analyze_paths` functions, which autodetects the number
|
|
||||||
/// of cores in your system.
|
|
||||||
///
|
|
||||||
/// Return an iterator, whose items are a tuple made of
|
|
||||||
/// the song path (to display to the user in case the analysis failed),
|
|
||||||
/// and a Result<Song>.
|
|
||||||
///
|
|
||||||
/// # Note
|
|
||||||
///
|
|
||||||
/// This function also works with CUE files - it finds the audio files
|
|
||||||
/// mentionned in the CUE sheet, and then runs the analysis on each song
|
|
||||||
/// defined by it, returning a proper [Song] object for each one of them.
|
|
||||||
///
|
|
||||||
/// Make sure that you don't submit both the audio file along with the CUE
|
|
||||||
/// sheet if your library uses them, otherwise the audio file will be
|
|
||||||
/// analyzed as one, single, long song. For instance, with a CUE sheet named
|
|
||||||
/// `cue-file.cue` with the corresponding audio files `album-1.wav` and
|
|
||||||
/// `album-2.wav` defined in the CUE sheet, you would just pass `cue-file.cue`
|
|
||||||
/// to `analyze_paths`, and it will return [Song]s from both files, with
|
|
||||||
/// more information about which file it is extracted from in the
|
|
||||||
/// [cue info field](Song::cue_info).
|
|
||||||
///
|
|
||||||
/// # Example:
|
|
||||||
/// ```no_run
|
|
||||||
/// use bliss_audio::{analyze_paths, BlissResult};
|
|
||||||
///
|
|
||||||
/// fn main() -> BlissResult<()> {
|
|
||||||
/// let paths = vec![String::from("/path/to/song1"), String::from("/path/to/song2")];
|
|
||||||
/// for (path, result) in analyze_paths(&paths) {
|
|
||||||
/// match result {
|
|
||||||
/// Ok(song) => println!("Do something with analyzed song {} with title {:?}", song.path.display(), song.title),
|
|
||||||
/// Err(e) => println!("Song at {} could not be analyzed. Failed with: {}", path.display(), e),
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// Ok(())
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub fn analyze_paths_with_cores<P: Into<PathBuf>, F: IntoIterator<Item = P>>(
|
|
||||||
paths: F,
|
|
||||||
number_cores: NonZeroUsize,
|
|
||||||
) -> mpsc::IntoIter<(PathBuf, BlissResult<Song>)> {
|
|
||||||
let mut cores = NonZeroUsize::new(num_cpus::get()).unwrap();
|
|
||||||
if cores > number_cores {
|
|
||||||
cores = number_cores;
|
|
||||||
}
|
|
||||||
let paths: Vec<PathBuf> = paths.into_iter().map(|p| p.into()).collect();
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
let (tx, rx): (
|
|
||||||
mpsc::Sender<(PathBuf, BlissResult<Song>)>,
|
|
||||||
mpsc::Receiver<(PathBuf, BlissResult<Song>)>,
|
|
||||||
) = mpsc::channel();
|
|
||||||
if paths.is_empty() {
|
|
||||||
return rx.into_iter();
|
|
||||||
}
|
|
||||||
let mut handles = Vec::new();
|
|
||||||
let mut chunk_length = paths.len() / cores;
|
|
||||||
if chunk_length == 0 {
|
|
||||||
chunk_length = paths.len();
|
|
||||||
}
|
|
||||||
for chunk in paths.chunks(chunk_length) {
|
|
||||||
let tx_thread = tx.clone();
|
|
||||||
let owned_chunk = chunk.to_owned();
|
|
||||||
let child = thread::spawn(move || {
|
|
||||||
for path in owned_chunk {
|
|
||||||
info!("Analyzing file '{:?}'", path);
|
|
||||||
if let Some(extension) = Path::new(&path).extension() {
|
|
||||||
let extension = extension.to_string_lossy().to_lowercase();
|
|
||||||
if extension == "cue" {
|
|
||||||
match BlissCue::songs_from_path(&path) {
|
|
||||||
Ok(songs) => {
|
|
||||||
for song in songs {
|
|
||||||
tx_thread.send((path.to_owned(), song)).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => tx_thread.send((path.to_owned(), Err(e))).unwrap(),
|
|
||||||
};
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let song = Song::from_path(&path);
|
|
||||||
tx_thread.send((path.to_owned(), song)).unwrap();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
handles.push(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
rx.into_iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[cfg(test)]
|
|
||||||
use pretty_assertions::assert_eq;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_send_song() {
|
fn test_send_song() {
|
||||||
|
@ -256,67 +79,4 @@ mod tests {
|
||||||
fn assert_sync<T: Send>() {}
|
fn assert_sync<T: Send>() {}
|
||||||
assert_sync::<Song>();
|
assert_sync::<Song>();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_analyze_paths() {
|
|
||||||
let paths = vec![
|
|
||||||
"./data/s16_mono_22_5kHz.flac",
|
|
||||||
"./data/testcue.cue",
|
|
||||||
"./data/white_noise.flac",
|
|
||||||
"definitely-not-existing.foo",
|
|
||||||
"not-existing.foo",
|
|
||||||
];
|
|
||||||
let mut results = analyze_paths(&paths)
|
|
||||||
.map(|x| match &x.1 {
|
|
||||||
Ok(s) => (true, s.path.to_owned(), None),
|
|
||||||
Err(e) => (false, x.0.to_owned(), Some(e.to_string())),
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
results.sort();
|
|
||||||
let expected_results = vec![
|
|
||||||
(
|
|
||||||
false,
|
|
||||||
PathBuf::from("./data/testcue.cue"),
|
|
||||||
Some(String::from(
|
|
||||||
"error happened while decoding file – while \
|
|
||||||
opening format for file './data/not-existing.wav': \
|
|
||||||
ffmpeg::Error(2: No such file or directory).",
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
false,
|
|
||||||
PathBuf::from("definitely-not-existing.foo"),
|
|
||||||
Some(String::from(
|
|
||||||
"error happened while decoding file – while \
|
|
||||||
opening format for file 'definitely-not-existing\
|
|
||||||
.foo': ffmpeg::Error(2: No such file or directory).",
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
false,
|
|
||||||
PathBuf::from("not-existing.foo"),
|
|
||||||
Some(String::from(
|
|
||||||
"error happened while decoding file – \
|
|
||||||
while opening format for file 'not-existing.foo': \
|
|
||||||
ffmpeg::Error(2: No such file or directory).",
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
(true, PathBuf::from("./data/s16_mono_22_5kHz.flac"), None),
|
|
||||||
(true, PathBuf::from("./data/testcue.cue/CUE_TRACK001"), None),
|
|
||||||
(true, PathBuf::from("./data/testcue.cue/CUE_TRACK002"), None),
|
|
||||||
(true, PathBuf::from("./data/testcue.cue/CUE_TRACK003"), None),
|
|
||||||
(true, PathBuf::from("./data/white_noise.flac"), None),
|
|
||||||
];
|
|
||||||
|
|
||||||
assert_eq!(results, expected_results);
|
|
||||||
|
|
||||||
let mut results = analyze_paths_with_cores(&paths, NonZeroUsize::new(1).unwrap())
|
|
||||||
.map(|x| match &x.1 {
|
|
||||||
Ok(s) => (true, s.path.to_owned(), None),
|
|
||||||
Err(e) => (false, x.0.to_owned(), Some(e.to_string())),
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
results.sort();
|
|
||||||
assert_eq!(results, expected_results);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
339
src/cue.rs
339
src/cue.rs
|
@ -1,339 +0,0 @@
|
||||||
//! CUE-handling module.
|
|
||||||
//!
|
|
||||||
//! Using [BlissCue::songs_from_path] is most likely what you want.
|
|
||||||
|
|
||||||
use crate::bliss_lib::{Analysis, BlissError, BlissResult, Song, FEATURES_VERSION, SAMPLE_RATE};
|
|
||||||
use rcue::cue::{Cue, Track};
|
|
||||||
use rcue::parser::parse_from_file;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
||||||
#[derive(Default, Debug, PartialEq, Eq, Clone)]
|
|
||||||
/// A struct populated when the corresponding [Song] has been extracted from an
|
|
||||||
/// audio file split with the help of a CUE sheet.
|
|
||||||
pub struct CueInfo {
|
|
||||||
/// The path of the original CUE sheet, e.g. `/path/to/album_name.cue`.
|
|
||||||
pub cue_path: PathBuf,
|
|
||||||
/// The path of the audio file the song was extracted from, e.g.
|
|
||||||
/// `/path/to/album_name.wav`. Used because one CUE sheet can refer to
|
|
||||||
/// several audio files.
|
|
||||||
pub audio_file_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A struct to handle CUEs with bliss.
|
|
||||||
/// Use either [analyze_paths](crate::analyze_paths) with CUE files or
|
|
||||||
/// [songs_from_path](BlissCue::songs_from_path) to return a list of [Song]s
|
|
||||||
/// from CUE files.
|
|
||||||
pub struct BlissCue {
|
|
||||||
cue: Cue,
|
|
||||||
cue_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[derive(Default, Debug, PartialEq, Clone)]
|
|
||||||
struct BlissCueFile {
|
|
||||||
sample_array: Vec<f32>,
|
|
||||||
album: Option<String>,
|
|
||||||
artist: Option<String>,
|
|
||||||
genre: Option<String>,
|
|
||||||
tracks: Vec<Track>,
|
|
||||||
cue_path: PathBuf,
|
|
||||||
audio_file_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BlissCue {
|
|
||||||
/// Analyze songs from a CUE file, extracting individual [Song] objects
|
|
||||||
/// for each individual song.
|
|
||||||
///
|
|
||||||
/// Each returned [Song] has a populated [cue_info](Song::cue_info) object, that can be
|
|
||||||
/// be used to retrieve which CUE sheet was used to extract it, as well
|
|
||||||
/// as the corresponding audio file.
|
|
||||||
pub fn songs_from_path<P: AsRef<Path>>(path: P) -> BlissResult<Vec<BlissResult<Song>>> {
|
|
||||||
let cue = BlissCue::from_path(&path)?;
|
|
||||||
let cue_files = cue.files();
|
|
||||||
let mut songs = Vec::new();
|
|
||||||
for cue_file in cue_files.into_iter() {
|
|
||||||
match cue_file {
|
|
||||||
Ok(f) => {
|
|
||||||
if !f.sample_array.is_empty() {
|
|
||||||
songs.extend_from_slice(&f.get_songs());
|
|
||||||
} else {
|
|
||||||
songs.push(Err(BlissError::DecodingError(
|
|
||||||
"empty audio file associated to CUE sheet".into(),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => songs.push(Err(e)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(songs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract a BlissCue from a given path.
|
|
||||||
fn from_path<P: AsRef<Path>>(path: P) -> BlissResult<Self> {
|
|
||||||
let cue = parse_from_file(&path.as_ref().to_string_lossy(), false).map_err(|e| {
|
|
||||||
BlissError::DecodingError(format!(
|
|
||||||
"when opening CUE file '{:?}': {:?}",
|
|
||||||
path.as_ref(),
|
|
||||||
e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
Ok(BlissCue {
|
|
||||||
cue,
|
|
||||||
cue_path: path.as_ref().to_owned(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// List all BlissCueFile from a BlissCue.
|
|
||||||
fn files(&self) -> Vec<BlissResult<BlissCueFile>> {
|
|
||||||
let mut cue_files = Vec::new();
|
|
||||||
for cue_file in self.cue.files.iter() {
|
|
||||||
let audio_file_path = match &self.cue_path.parent() {
|
|
||||||
Some(parent) => parent.join(Path::new(&cue_file.file)),
|
|
||||||
None => PathBuf::from(cue_file.file.to_owned()),
|
|
||||||
};
|
|
||||||
let genre = self
|
|
||||||
.cue
|
|
||||||
.comments
|
|
||||||
.iter()
|
|
||||||
.find(|(c, _)| c == "GENRE")
|
|
||||||
.map(|(_, v)| v.to_owned());
|
|
||||||
let raw_song = Song::decode(Path::new(&audio_file_path));
|
|
||||||
if let Ok(song) = raw_song {
|
|
||||||
let bliss_cue_file = BlissCueFile {
|
|
||||||
sample_array: song.sample_array,
|
|
||||||
genre,
|
|
||||||
artist: self.cue.performer.to_owned(),
|
|
||||||
album: self.cue.title.to_owned(),
|
|
||||||
tracks: cue_file.tracks.to_owned(),
|
|
||||||
audio_file_path,
|
|
||||||
cue_path: self.cue_path.to_owned(),
|
|
||||||
};
|
|
||||||
cue_files.push(Ok(bliss_cue_file))
|
|
||||||
} else {
|
|
||||||
cue_files.push(Err(raw_song.unwrap_err()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cue_files
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BlissCueFile {
|
|
||||||
fn create_song(
|
|
||||||
&self,
|
|
||||||
analysis: BlissResult<Analysis>,
|
|
||||||
current_track: &Track,
|
|
||||||
duration: Duration,
|
|
||||||
index: usize,
|
|
||||||
) -> BlissResult<Song> {
|
|
||||||
if let Ok(a) = analysis {
|
|
||||||
let song = Song {
|
|
||||||
path: PathBuf::from(format!(
|
|
||||||
"{}/CUE_TRACK{:03}",
|
|
||||||
self.cue_path.to_string_lossy(),
|
|
||||||
index,
|
|
||||||
)),
|
|
||||||
album: self.album.to_owned(),
|
|
||||||
artist: current_track.performer.to_owned(),
|
|
||||||
album_artist: self.artist.to_owned(),
|
|
||||||
analysis: a,
|
|
||||||
duration,
|
|
||||||
genre: self.genre.to_owned(),
|
|
||||||
title: current_track.title.to_owned(),
|
|
||||||
track_number: Some(current_track.no.to_owned()),
|
|
||||||
features_version: FEATURES_VERSION,
|
|
||||||
cue_info: Some(CueInfo {
|
|
||||||
cue_path: self.cue_path.to_owned(),
|
|
||||||
audio_file_path: self.audio_file_path.to_owned(),
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
Ok(song)
|
|
||||||
} else {
|
|
||||||
Err(analysis.unwrap_err())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all songs from a BlissCueFile, using Song::analyze, each song being
|
|
||||||
// located using the sample_array and the timestamp delimiter.
|
|
||||||
fn get_songs(&self) -> Vec<BlissResult<Song>> {
|
|
||||||
let mut songs = Vec::new();
|
|
||||||
for (index, tuple) in (self.tracks[..]).windows(2).enumerate() {
|
|
||||||
let (current_track, next_track) = (tuple[0].to_owned(), tuple[1].to_owned());
|
|
||||||
if let Some((_, start_current)) = current_track.indices.get(0) {
|
|
||||||
if let Some((_, end_current)) = next_track.indices.get(0) {
|
|
||||||
let start_current = (start_current.as_secs_f32() * SAMPLE_RATE as f32) as usize;
|
|
||||||
let end_current = (end_current.as_secs_f32() * SAMPLE_RATE as f32) as usize;
|
|
||||||
let duration = Duration::from_secs_f32(
|
|
||||||
(end_current - start_current) as f32 / SAMPLE_RATE as f32,
|
|
||||||
);
|
|
||||||
let analysis = Song::analyze(&self.sample_array[start_current..end_current]);
|
|
||||||
|
|
||||||
let song = self.create_song(analysis, ¤t_track, duration, index + 1);
|
|
||||||
songs.push(song);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Take care of the last track, since the windows iterator doesn't.
|
|
||||||
if let Some(last_track) = self.tracks.last() {
|
|
||||||
if let Some((_, start_current)) = last_track.indices.get(0) {
|
|
||||||
let start_current = (start_current.as_secs_f32() * SAMPLE_RATE as f32) as usize;
|
|
||||||
let duration = Duration::from_secs_f32(
|
|
||||||
(self.sample_array.len() - start_current) as f32 / SAMPLE_RATE as f32,
|
|
||||||
);
|
|
||||||
let analysis = Song::analyze(&self.sample_array[start_current..]);
|
|
||||||
let song = self.create_song(analysis, last_track, duration, self.tracks.len());
|
|
||||||
songs.push(song);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
songs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use pretty_assertions::assert_eq;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_empty_cue() {
|
|
||||||
let songs = BlissCue::songs_from_path("data/empty.cue").unwrap();
|
|
||||||
let error = songs[0].to_owned().unwrap_err();
|
|
||||||
assert_eq!(
|
|
||||||
error,
|
|
||||||
BlissError::DecodingError("empty audio file associated to CUE sheet".to_string())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_cue_analysis() {
|
|
||||||
let songs = BlissCue::songs_from_path("data/testcue.cue").unwrap();
|
|
||||||
let expected = vec![
|
|
||||||
Ok(Song {
|
|
||||||
path: Path::new("data/testcue.cue/CUE_TRACK001").to_path_buf(),
|
|
||||||
analysis: Analysis {
|
|
||||||
internal_analysis: [
|
|
||||||
0.38463724,
|
|
||||||
-0.85219246,
|
|
||||||
-0.761946,
|
|
||||||
-0.8904667,
|
|
||||||
-0.63892543,
|
|
||||||
-0.73945934,
|
|
||||||
-0.8004017,
|
|
||||||
-0.8237293,
|
|
||||||
0.33865356,
|
|
||||||
0.32481194,
|
|
||||||
-0.35692245,
|
|
||||||
-0.6355889,
|
|
||||||
-0.29584837,
|
|
||||||
0.06431806,
|
|
||||||
0.21875131,
|
|
||||||
-0.58104205,
|
|
||||||
-0.9466792,
|
|
||||||
-0.94811195,
|
|
||||||
-0.9820919,
|
|
||||||
-0.9596871,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
album: Some(String::from("Album for CUE test")),
|
|
||||||
artist: Some(String::from("David TMX")),
|
|
||||||
title: Some(String::from("Renaissance")),
|
|
||||||
genre: Some(String::from("Random")),
|
|
||||||
track_number: Some(String::from("01")),
|
|
||||||
features_version: FEATURES_VERSION,
|
|
||||||
album_artist: Some(String::from("Polochon_street")),
|
|
||||||
duration: Duration::from_secs_f32(11.066666603),
|
|
||||||
cue_info: Some(CueInfo {
|
|
||||||
cue_path: PathBuf::from("data/testcue.cue"),
|
|
||||||
audio_file_path: PathBuf::from("data/testcue.flac"),
|
|
||||||
}),
|
|
||||||
..Default::default()
|
|
||||||
}),
|
|
||||||
Ok(Song {
|
|
||||||
path: Path::new("data/testcue.cue/CUE_TRACK002").to_path_buf(),
|
|
||||||
analysis: Analysis {
|
|
||||||
internal_analysis: [
|
|
||||||
0.18622077,
|
|
||||||
-0.5989029,
|
|
||||||
-0.5554645,
|
|
||||||
-0.6343865,
|
|
||||||
-0.24163479,
|
|
||||||
-0.25766593,
|
|
||||||
-0.40616858,
|
|
||||||
-0.23334873,
|
|
||||||
0.76875293,
|
|
||||||
0.7785741,
|
|
||||||
-0.5075115,
|
|
||||||
-0.5272629,
|
|
||||||
-0.56706166,
|
|
||||||
-0.568486,
|
|
||||||
-0.5639081,
|
|
||||||
-0.5706943,
|
|
||||||
-0.96501005,
|
|
||||||
-0.96501285,
|
|
||||||
-0.9649896,
|
|
||||||
-0.96498996,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
features_version: FEATURES_VERSION,
|
|
||||||
album: Some(String::from("Album for CUE test")),
|
|
||||||
artist: Some(String::from("Polochon_street")),
|
|
||||||
title: Some(String::from("Piano")),
|
|
||||||
genre: Some(String::from("Random")),
|
|
||||||
track_number: Some(String::from("02")),
|
|
||||||
album_artist: Some(String::from("Polochon_street")),
|
|
||||||
duration: Duration::from_secs_f64(5.853333473),
|
|
||||||
cue_info: Some(CueInfo {
|
|
||||||
cue_path: PathBuf::from("data/testcue.cue"),
|
|
||||||
audio_file_path: PathBuf::from("data/testcue.flac"),
|
|
||||||
}),
|
|
||||||
..Default::default()
|
|
||||||
}),
|
|
||||||
Ok(Song {
|
|
||||||
path: Path::new("data/testcue.cue/CUE_TRACK003").to_path_buf(),
|
|
||||||
analysis: Analysis {
|
|
||||||
internal_analysis: [
|
|
||||||
0.0024261475,
|
|
||||||
0.9874661,
|
|
||||||
0.97330654,
|
|
||||||
-0.9724426,
|
|
||||||
0.99678576,
|
|
||||||
-0.9961549,
|
|
||||||
-0.9840142,
|
|
||||||
-0.9269961,
|
|
||||||
0.7498772,
|
|
||||||
0.22429907,
|
|
||||||
-0.8355152,
|
|
||||||
-0.9977258,
|
|
||||||
-0.9977849,
|
|
||||||
-0.997785,
|
|
||||||
-0.99778515,
|
|
||||||
-0.997785,
|
|
||||||
-0.99999976,
|
|
||||||
-0.99999976,
|
|
||||||
-0.99999976,
|
|
||||||
-0.99999976,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
album: Some(String::from("Album for CUE test")),
|
|
||||||
artist: Some(String::from("Polochon_street")),
|
|
||||||
title: Some(String::from("Tone")),
|
|
||||||
genre: Some(String::from("Random")),
|
|
||||||
track_number: Some(String::from("03")),
|
|
||||||
features_version: FEATURES_VERSION,
|
|
||||||
album_artist: Some(String::from("Polochon_street")),
|
|
||||||
duration: Duration::from_secs_f32(5.586666584),
|
|
||||||
cue_info: Some(CueInfo {
|
|
||||||
cue_path: PathBuf::from("data/testcue.cue"),
|
|
||||||
audio_file_path: PathBuf::from("data/testcue.flac"),
|
|
||||||
}),
|
|
||||||
..Default::default()
|
|
||||||
}),
|
|
||||||
Err(BlissError::DecodingError(String::from(
|
|
||||||
"while opening format for file 'data/not-existing.wav': \
|
|
||||||
ffmpeg::Error(2: No such file or directory).",
|
|
||||||
))),
|
|
||||||
];
|
|
||||||
assert_eq!(expected, songs);
|
|
||||||
}
|
|
||||||
}
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -1,24 +1,14 @@
|
||||||
extern crate crossbeam;
|
|
||||||
extern crate num_cpus;
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
#[macro_use]
|
|
||||||
extern crate serde;
|
|
||||||
|
|
||||||
pub mod bliss_lib;
|
pub mod bliss_lib;
|
||||||
mod chroma;
|
mod chroma;
|
||||||
pub mod cue;
|
|
||||||
#[cfg(feature = "library")]
|
|
||||||
pub mod library;
|
|
||||||
mod misc;
|
|
||||||
pub mod playlist;
|
|
||||||
mod song;
|
mod song;
|
||||||
|
mod misc;
|
||||||
mod temporal;
|
mod temporal;
|
||||||
mod timbral;
|
mod timbral;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use neon::{prelude::*, types::buffer::TypedArray};
|
use neon::{prelude::*, types::buffer::TypedArray};
|
||||||
use song::Song;
|
// use song::Song;
|
||||||
use bliss_lib::BlissResult;
|
// use bliss_lib::BlissResult;
|
||||||
|
|
||||||
#[neon::main]
|
#[neon::main]
|
||||||
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
||||||
|
@ -69,9 +59,13 @@ fn analyze(mut cx: FunctionContext) -> JsResult<JsUint8Array> {
|
||||||
Ok(buffer_handle)
|
Ok(buffer_handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn analyze_raw(path: &str) -> BlissResult<([u8; 2], [u8; 80])> {
|
// fn analyze_raw(path: &str) -> BlissResult<([u8; 2], [u8; 80])> {
|
||||||
let song = Song::from_path(path)?;
|
// let song = Song::from_path(path)?;
|
||||||
let version_bytes = song.features_version.to_le_bytes();
|
// let version_bytes = song.features_version.to_le_bytes();
|
||||||
let analysis_bytes = song.analysis.as_bytes();
|
// let analysis_bytes = song.analysis.as_bytes();
|
||||||
Ok((version_bytes, analysis_bytes))
|
// Ok((version_bytes, analysis_bytes))
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn analyze_raw(path: &str) -> Result<([u8; 2], [u8; 80]), u8> {
|
||||||
|
return Ok(([0; 2], [0; 80]));
|
||||||
}
|
}
|
||||||
|
|
3183
src/library.rs
3183
src/library.rs
File diff suppressed because it is too large
Load Diff
984
src/playlist.rs
984
src/playlist.rs
|
@ -1,984 +0,0 @@
|
||||||
//! Module containing various functions to build playlists, as well as various
|
|
||||||
//! distance metrics.
|
|
||||||
//!
|
|
||||||
//! All of the distance functions are intended to be used with the
|
|
||||||
//! [custom_distance](Song::custom_distance) method, or with
|
|
||||||
//!
|
|
||||||
//! They will yield different styles of playlists, so don't hesitate to
|
|
||||||
//! experiment with them if the default (euclidean distance for now) doesn't
|
|
||||||
//! suit you.
|
|
||||||
// TODO on the `by_key` functions: maybe Fn(&T) -> &Song is enough? Compared
|
|
||||||
// to -> Song
|
|
||||||
use crate::bliss_lib::{BlissError, BlissResult, Song, NUMBER_FEATURES};
|
|
||||||
use ndarray::{Array, Array1, Array2, Axis};
|
|
||||||
use ndarray_stats::QuantileExt;
|
|
||||||
use noisy_float::prelude::*;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
/// Convenience trait for user-defined distance metrics.
|
|
||||||
pub trait DistanceMetric: Fn(&Array1<f32>, &Array1<f32>) -> f32 {}
|
|
||||||
impl<F> DistanceMetric for F where F: Fn(&Array1<f32>, &Array1<f32>) -> f32 {}
|
|
||||||
|
|
||||||
/// Return the [euclidean
|
|
||||||
/// distance](https://en.wikipedia.org/wiki/Euclidean_distance#Higher_dimensions)
|
|
||||||
/// between two vectors.
|
|
||||||
pub fn euclidean_distance(a: &Array1<f32>, b: &Array1<f32>) -> f32 {
|
|
||||||
// Could be any square symmetric positive semi-definite matrix;
|
|
||||||
// just no metric learning has been done yet.
|
|
||||||
// See https://lelele.io/thesis.pdf chapter 4.
|
|
||||||
let m = Array::eye(NUMBER_FEATURES);
|
|
||||||
|
|
||||||
(a - b).dot(&m).dot(&(a - b)).sqrt()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the [cosine
|
|
||||||
/// distance](https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity)
|
|
||||||
/// between two vectors.
|
|
||||||
pub fn cosine_distance(a: &Array1<f32>, b: &Array1<f32>) -> f32 {
|
|
||||||
let similarity = a.dot(b) / (a.dot(a).sqrt() * b.dot(b).sqrt());
|
|
||||||
1. - similarity
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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,
|
|
||||||
#[allow(clippy::ptr_arg)] songs: &mut Vec<Song>,
|
|
||||||
distance: impl DistanceMetric,
|
|
||||||
) {
|
|
||||||
songs.sort_by_cached_key(|song| n32(first_song.custom_distance(song, &distance)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sort `songs` in place by putting songs close to `first_song` first
|
|
||||||
/// using the `distance` metric.
|
|
||||||
///
|
|
||||||
/// Sort songs with a key extraction function, useful for when you have a
|
|
||||||
/// structure like `CustomSong { bliss_song: Song, something_else: bool }`
|
|
||||||
pub fn closest_to_first_song_by_key<F, T>(
|
|
||||||
first_song: &T,
|
|
||||||
#[allow(clippy::ptr_arg)] songs: &mut Vec<T>,
|
|
||||||
distance: impl DistanceMetric,
|
|
||||||
key_fn: F,
|
|
||||||
) where
|
|
||||||
F: Fn(&T) -> Song,
|
|
||||||
{
|
|
||||||
let first_song = key_fn(first_song);
|
|
||||||
songs.sort_by_cached_key(|song| n32(first_song.custom_distance(&key_fn(song), &distance)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sort `songs` in place using the `distance` metric and ordering by
|
|
||||||
/// the smallest distance between each song.
|
|
||||||
///
|
|
||||||
/// If the generated playlist is `[song1, song2, song3, song4]`, it means
|
|
||||||
/// song2 is closest to song1, song3 is closest to song2, and song4 is closest
|
|
||||||
/// to song3.
|
|
||||||
///
|
|
||||||
/// Note that this has a tendency to go from one style to the other very fast,
|
|
||||||
/// and it can be slow on big libraries.
|
|
||||||
pub fn song_to_song(first_song: &Song, songs: &mut Vec<Song>, distance: impl DistanceMetric) {
|
|
||||||
let mut new_songs = Vec::with_capacity(songs.len());
|
|
||||||
let mut song = first_song.to_owned();
|
|
||||||
|
|
||||||
while !songs.is_empty() {
|
|
||||||
let distances: Array1<f32> =
|
|
||||||
Array::from_shape_fn(songs.len(), |i| song.custom_distance(&songs[i], &distance));
|
|
||||||
let idx = distances.argmin().unwrap();
|
|
||||||
song = songs[idx].to_owned();
|
|
||||||
new_songs.push(song.to_owned());
|
|
||||||
songs.retain(|s| s != &song);
|
|
||||||
}
|
|
||||||
*songs = new_songs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sort `songs` in place using the `distance` metric and ordering by
|
|
||||||
/// the smallest distance between each song.
|
|
||||||
///
|
|
||||||
/// If the generated playlist is `[song1, song2, song3, song4]`, it means
|
|
||||||
/// song2 is closest to song1, song3 is closest to song2, and song4 is closest
|
|
||||||
/// to song3.
|
|
||||||
///
|
|
||||||
/// Note that this has a tendency to go from one style to the other very fast,
|
|
||||||
/// and it can be slow on big libraries.
|
|
||||||
///
|
|
||||||
/// Sort songs with a key extraction function, useful for when you have a
|
|
||||||
/// structure like `CustomSong { bliss_song: Song, something_else: bool }`
|
|
||||||
// TODO: maybe Clone is not needed?
|
|
||||||
pub fn song_to_song_by_key<F, T: std::cmp::PartialEq + Clone>(
|
|
||||||
first_song: &T,
|
|
||||||
songs: &mut Vec<T>,
|
|
||||||
distance: impl DistanceMetric,
|
|
||||||
key_fn: F,
|
|
||||||
) where
|
|
||||||
F: Fn(&T) -> Song,
|
|
||||||
{
|
|
||||||
let mut new_songs: Vec<T> = Vec::with_capacity(songs.len());
|
|
||||||
let mut bliss_song = key_fn(&first_song.to_owned());
|
|
||||||
|
|
||||||
while !songs.is_empty() {
|
|
||||||
let distances: Array1<f32> = Array::from_shape_fn(songs.len(), |i| {
|
|
||||||
bliss_song.custom_distance(&key_fn(&songs[i]), &distance)
|
|
||||||
});
|
|
||||||
let idx = distances.argmin().unwrap();
|
|
||||||
let song = songs[idx].to_owned();
|
|
||||||
bliss_song = key_fn(&songs[idx]).to_owned();
|
|
||||||
new_songs.push(song.to_owned());
|
|
||||||
songs.retain(|s| s != &song);
|
|
||||||
}
|
|
||||||
*songs = new_songs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove duplicate songs from a playlist, in place.
|
|
||||||
///
|
|
||||||
/// Two songs are considered duplicates if they either have the same,
|
|
||||||
/// non-empty title and artist name, or if they are close enough in terms
|
|
||||||
/// of distance.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `songs`: The playlist to remove duplicates from.
|
|
||||||
/// * `distance_threshold`: The distance threshold under which two songs are
|
|
||||||
/// considered identical. If `None`, a default value of 0.05 will be used.
|
|
||||||
pub fn dedup_playlist(songs: &mut Vec<Song>, distance_threshold: Option<f32>) {
|
|
||||||
dedup_playlist_custom_distance(songs, distance_threshold, euclidean_distance);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove duplicate songs from a playlist, in place.
|
|
||||||
///
|
|
||||||
/// Two songs are considered duplicates if they either have the same,
|
|
||||||
/// non-empty title and artist name, or if they are close enough in terms
|
|
||||||
/// of distance.
|
|
||||||
///
|
|
||||||
/// Dedup songs with a key extraction function, useful for when you have a
|
|
||||||
/// structure like `CustomSong { bliss_song: Song, something_else: bool }` you
|
|
||||||
/// want to deduplicate.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `songs`: The playlist to remove duplicates from.
|
|
||||||
/// * `distance_threshold`: The distance threshold under which two songs are
|
|
||||||
/// considered identical. If `None`, a default value of 0.05 will be used.
|
|
||||||
/// * `key_fn`: A function used to retrieve the bliss [Song] from `T`.
|
|
||||||
pub fn dedup_playlist_by_key<T, F>(songs: &mut Vec<T>, distance_threshold: Option<f32>, key_fn: F)
|
|
||||||
where
|
|
||||||
F: Fn(&T) -> Song,
|
|
||||||
{
|
|
||||||
dedup_playlist_custom_distance_by_key(songs, distance_threshold, euclidean_distance, key_fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove duplicate songs from a playlist, in place, using a custom distance
|
|
||||||
/// metric.
|
|
||||||
///
|
|
||||||
/// Two songs are considered duplicates if they either have the same,
|
|
||||||
/// non-empty title and artist name, or if they are close enough in terms
|
|
||||||
/// of distance.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `songs`: The playlist to remove duplicates from.
|
|
||||||
/// * `distance_threshold`: The distance threshold under which two songs are
|
|
||||||
/// considered identical. If `None`, a default value of 0.05 will be used.
|
|
||||||
/// * `distance`: A custom distance metric.
|
|
||||||
pub fn dedup_playlist_custom_distance(
|
|
||||||
songs: &mut Vec<Song>,
|
|
||||||
distance_threshold: Option<f32>,
|
|
||||||
distance: impl DistanceMetric,
|
|
||||||
) {
|
|
||||||
songs.dedup_by(|s1, s2| {
|
|
||||||
n32(s1.custom_distance(s2, &distance)) < distance_threshold.unwrap_or(0.05)
|
|
||||||
|| (s1.title.is_some()
|
|
||||||
&& s2.title.is_some()
|
|
||||||
&& s1.artist.is_some()
|
|
||||||
&& s2.artist.is_some()
|
|
||||||
&& s1.title == s2.title
|
|
||||||
&& s1.artist == s2.artist)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove duplicate songs from a playlist, in place, using a custom distance
|
|
||||||
/// metric.
|
|
||||||
///
|
|
||||||
/// Two songs are considered duplicates if they either have the same,
|
|
||||||
/// non-empty title and artist name, or if they are close enough in terms
|
|
||||||
/// of distance.
|
|
||||||
///
|
|
||||||
/// Dedup songs with a key extraction function, useful for when you have a
|
|
||||||
/// structure like `CustomSong { bliss_song: Song, something_else: bool }`
|
|
||||||
/// you want to deduplicate.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `songs`: The playlist to remove duplicates from.
|
|
||||||
/// * `distance_threshold`: The distance threshold under which two songs are
|
|
||||||
/// considered identical. If `None`, a default value of 0.05 will be used.
|
|
||||||
/// * `distance`: A custom distance metric.
|
|
||||||
/// * `key_fn`: A function used to retrieve the bliss [Song] from `T`.
|
|
||||||
pub fn dedup_playlist_custom_distance_by_key<F, T>(
|
|
||||||
songs: &mut Vec<T>,
|
|
||||||
distance_threshold: Option<f32>,
|
|
||||||
distance: impl DistanceMetric,
|
|
||||||
key_fn: F,
|
|
||||||
) where
|
|
||||||
F: Fn(&T) -> Song,
|
|
||||||
{
|
|
||||||
songs.dedup_by(|s1, s2| {
|
|
||||||
let s1 = key_fn(s1);
|
|
||||||
let s2 = key_fn(s2);
|
|
||||||
n32(s1.custom_distance(&s2, &distance)) < distance_threshold.unwrap_or(0.05)
|
|
||||||
|| (s1.title.is_some()
|
|
||||||
&& s2.title.is_some()
|
|
||||||
&& s1.artist.is_some()
|
|
||||||
&& s2.artist.is_some()
|
|
||||||
&& s1.title == s2.title
|
|
||||||
&& s1.artist == s2.artist)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return a list of albums in a `pool` of songs that are similar to
|
|
||||||
/// songs in `group`, discarding songs that don't belong to an album.
|
|
||||||
/// It basically makes an "album" playlist from the `pool` of songs.
|
|
||||||
///
|
|
||||||
/// `group` should be ordered by track number.
|
|
||||||
///
|
|
||||||
/// Songs from `group` would usually just be songs from an album, but not
|
|
||||||
/// necessarily - they are discarded from `pool` no matter what.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `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.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// A vector of songs, including `group` at the beginning, that you
|
|
||||||
/// most likely want to plug in your audio player by using something like
|
|
||||||
/// `ret.map(|song| song.path.to_owned()).collect::<Vec<String>>()`.
|
|
||||||
pub fn closest_album_to_group(group: Vec<Song>, pool: Vec<Song>) -> BlissResult<Vec<Song>> {
|
|
||||||
let mut albums_analysis: HashMap<&str, Array2<f32>> = HashMap::new();
|
|
||||||
let mut albums = Vec::new();
|
|
||||||
|
|
||||||
// Remove songs from the group from the pool.
|
|
||||||
let pool = pool
|
|
||||||
.into_iter()
|
|
||||||
.filter(|s| !group.contains(s))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
for song in &pool {
|
|
||||||
if let Some(album) = &song.album {
|
|
||||||
if let Some(analysis) = albums_analysis.get_mut(album as &str) {
|
|
||||||
analysis
|
|
||||||
.push_row(song.analysis.as_arr1().view())
|
|
||||||
.map_err(|e| {
|
|
||||||
BlissError::ProviderError(format!("while computing distances: {e}"))
|
|
||||||
})?;
|
|
||||||
} else {
|
|
||||||
let mut array = Array::zeros((1, song.analysis.as_arr1().len()));
|
|
||||||
array.assign(&song.analysis.as_arr1());
|
|
||||||
albums_analysis.insert(album, array);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut group_analysis = Array::zeros((group.len(), NUMBER_FEATURES));
|
|
||||||
for (song, mut column) in group.iter().zip(group_analysis.axis_iter_mut(Axis(0))) {
|
|
||||||
column.assign(&song.analysis.as_arr1());
|
|
||||||
}
|
|
||||||
let first_analysis = group_analysis
|
|
||||||
.mean_axis(Axis(0))
|
|
||||||
.ok_or_else(|| BlissError::ProviderError(String::from("Mean of empty slice")))?;
|
|
||||||
for (album, analysis) in albums_analysis.iter() {
|
|
||||||
let mean_analysis = analysis
|
|
||||||
.mean_axis(Axis(0))
|
|
||||||
.ok_or_else(|| BlissError::ProviderError(String::from("Mean of empty slice")))?;
|
|
||||||
let album = album.to_owned();
|
|
||||||
albums.push((album, mean_analysis.to_owned()));
|
|
||||||
}
|
|
||||||
|
|
||||||
albums.sort_by_key(|(_, analysis)| n32(euclidean_distance(&first_analysis, analysis)));
|
|
||||||
let mut playlist = group;
|
|
||||||
for (album, _) in albums {
|
|
||||||
let mut al = pool
|
|
||||||
.iter()
|
|
||||||
.filter(|s| s.album.is_some() && s.album.as_ref().unwrap() == &album.to_string())
|
|
||||||
.map(|s| s.to_owned())
|
|
||||||
.collect::<Vec<Song>>();
|
|
||||||
al.sort_by(|s1, s2| {
|
|
||||||
let track_number1 = s1
|
|
||||||
.track_number
|
|
||||||
.to_owned()
|
|
||||||
.unwrap_or_else(|| String::from(""));
|
|
||||||
let track_number2 = s2
|
|
||||||
.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>() {
|
|
||||||
return x.cmp(&y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s1.track_number.cmp(&s2.track_number)
|
|
||||||
});
|
|
||||||
playlist.extend_from_slice(&al);
|
|
||||||
}
|
|
||||||
Ok(playlist)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return a list of albums in a `pool` of songs that are similar to
|
|
||||||
/// songs in `group`, discarding songs that don't belong to an album.
|
|
||||||
/// It basically makes an "album" playlist from the `pool` of songs.
|
|
||||||
///
|
|
||||||
/// `group` should be ordered by track number.
|
|
||||||
///
|
|
||||||
/// Songs from `group` would usually just be songs from an album, but not
|
|
||||||
/// necessarily - they are discarded from `pool` no matter what.
|
|
||||||
///
|
|
||||||
/// Order songs with a key extraction function, useful for when you have a
|
|
||||||
/// structure like `CustomSong { bliss_song: Song, something_else: bool }`
|
|
||||||
/// you want to order.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `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.
|
|
||||||
/// * `key_fn`: A function used to retrieve the bliss [Song] from `T`.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// A vector of T, including `group` at the beginning, that you
|
|
||||||
/// most likely want to plug in your audio player by using something like
|
|
||||||
/// `ret.map(|song| song.path.to_owned()).collect::<Vec<String>>()`.
|
|
||||||
// TODO: maybe Clone is not needed?
|
|
||||||
pub fn closest_album_to_group_by_key<T: PartialEq + Clone, F>(
|
|
||||||
group: Vec<T>,
|
|
||||||
pool: Vec<T>,
|
|
||||||
key_fn: F,
|
|
||||||
) -> BlissResult<Vec<T>>
|
|
||||||
where
|
|
||||||
F: Fn(&T) -> Song,
|
|
||||||
{
|
|
||||||
let mut albums_analysis: HashMap<String, Array2<f32>> = HashMap::new();
|
|
||||||
let mut albums = Vec::new();
|
|
||||||
|
|
||||||
// Remove songs from the group from the pool.
|
|
||||||
let pool = pool
|
|
||||||
.into_iter()
|
|
||||||
.filter(|s| !group.contains(s))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
for song in &pool {
|
|
||||||
let song = key_fn(song);
|
|
||||||
if let Some(album) = song.album {
|
|
||||||
if let Some(analysis) = albums_analysis.get_mut(&album as &str) {
|
|
||||||
analysis
|
|
||||||
.push_row(song.analysis.as_arr1().view())
|
|
||||||
.map_err(|e| {
|
|
||||||
BlissError::ProviderError(format!("while computing distances: {e}"))
|
|
||||||
})?;
|
|
||||||
} else {
|
|
||||||
let mut array = Array::zeros((1, song.analysis.as_arr1().len()));
|
|
||||||
array.assign(&song.analysis.as_arr1());
|
|
||||||
albums_analysis.insert(album.to_owned(), array);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut group_analysis = Array::zeros((group.len(), NUMBER_FEATURES));
|
|
||||||
for (song, mut column) in group.iter().zip(group_analysis.axis_iter_mut(Axis(0))) {
|
|
||||||
let song = key_fn(song);
|
|
||||||
column.assign(&song.analysis.as_arr1());
|
|
||||||
}
|
|
||||||
let first_analysis = group_analysis
|
|
||||||
.mean_axis(Axis(0))
|
|
||||||
.ok_or_else(|| BlissError::ProviderError(String::from("Mean of empty slice")))?;
|
|
||||||
for (album, analysis) in albums_analysis.iter() {
|
|
||||||
let mean_analysis = analysis
|
|
||||||
.mean_axis(Axis(0))
|
|
||||||
.ok_or_else(|| BlissError::ProviderError(String::from("Mean of empty slice")))?;
|
|
||||||
let album = album.to_owned();
|
|
||||||
albums.push((album, mean_analysis.to_owned()));
|
|
||||||
}
|
|
||||||
|
|
||||||
albums.sort_by_key(|(_, analysis)| n32(euclidean_distance(&first_analysis, analysis)));
|
|
||||||
let mut playlist = group;
|
|
||||||
for (album, _) in albums {
|
|
||||||
let mut al = pool
|
|
||||||
.iter()
|
|
||||||
.filter(|s| {
|
|
||||||
let s = key_fn(s);
|
|
||||||
s.album.is_some() && s.album.as_ref().unwrap() == &album.to_string()
|
|
||||||
})
|
|
||||||
.map(|s| s.to_owned())
|
|
||||||
.collect::<Vec<T>>();
|
|
||||||
al.sort_by(|s1, s2| {
|
|
||||||
let s1 = key_fn(s1);
|
|
||||||
let s2 = key_fn(s2);
|
|
||||||
let track_number1 = s1
|
|
||||||
.track_number
|
|
||||||
.to_owned()
|
|
||||||
.unwrap_or_else(|| String::from(""));
|
|
||||||
let track_number2 = s2
|
|
||||||
.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>() {
|
|
||||||
return x.cmp(&y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s1.track_number.cmp(&s2.track_number)
|
|
||||||
});
|
|
||||||
playlist.extend_from_slice(&al);
|
|
||||||
}
|
|
||||||
Ok(playlist)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
use crate::bliss_lib::Analysis;
|
|
||||||
use ndarray::arr1;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
struct CustomSong {
|
|
||||||
something: bool,
|
|
||||||
bliss_song: Song,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_dedup_playlist_custom_distance() {
|
|
||||||
let first_song = Song {
|
|
||||||
path: Path::new("path-to-first").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let first_song_dupe = Song {
|
|
||||||
path: Path::new("path-to-dupe").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let second_song = Song {
|
|
||||||
path: Path::new("path-to-second").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1.9, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
title: Some(String::from("dupe-title")),
|
|
||||||
artist: Some(String::from("dupe-artist")),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let third_song = Song {
|
|
||||||
path: Path::new("path-to-third").to_path_buf(),
|
|
||||||
title: Some(String::from("dupe-title")),
|
|
||||||
artist: Some(String::from("dupe-artist")),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.5, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fourth_song = Song {
|
|
||||||
path: Path::new("path-to-fourth").to_path_buf(),
|
|
||||||
artist: Some(String::from("no-dupe-artist")),
|
|
||||||
title: Some(String::from("dupe-title")),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fifth_song = Song {
|
|
||||||
path: Path::new("path-to-fourth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0.001, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist_custom_distance(&mut playlist, None, euclidean_distance);
|
|
||||||
assert_eq!(
|
|
||||||
playlist,
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist_custom_distance(&mut playlist, Some(20.), cosine_distance);
|
|
||||||
assert_eq!(playlist, vec![first_song.to_owned()]);
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist(&mut playlist, Some(20.));
|
|
||||||
assert_eq!(playlist, vec![first_song.to_owned()]);
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist(&mut playlist, None);
|
|
||||||
assert_eq!(
|
|
||||||
playlist,
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
let first_song = CustomSong {
|
|
||||||
bliss_song: first_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let second_song = CustomSong {
|
|
||||||
bliss_song: second_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let first_song_dupe = CustomSong {
|
|
||||||
bliss_song: first_song_dupe,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let third_song = CustomSong {
|
|
||||||
bliss_song: third_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let fourth_song = CustomSong {
|
|
||||||
bliss_song: fourth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let fifth_song = CustomSong {
|
|
||||||
bliss_song: fifth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist_custom_distance_by_key(&mut playlist, None, euclidean_distance, |s| {
|
|
||||||
s.bliss_song.to_owned()
|
|
||||||
});
|
|
||||||
assert_eq!(
|
|
||||||
playlist,
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist_custom_distance_by_key(&mut playlist, Some(20.), cosine_distance, |s| {
|
|
||||||
s.bliss_song.to_owned()
|
|
||||||
});
|
|
||||||
assert_eq!(playlist, vec![first_song.to_owned()]);
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist_by_key(&mut playlist, Some(20.), |s| s.bliss_song.to_owned());
|
|
||||||
assert_eq!(playlist, vec![first_song.to_owned()]);
|
|
||||||
let mut playlist = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
dedup_playlist_by_key(&mut playlist, None, |s| s.bliss_song.to_owned());
|
|
||||||
assert_eq!(
|
|
||||||
playlist,
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_song_to_song() {
|
|
||||||
let first_song = Song {
|
|
||||||
path: Path::new("path-to-first").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let first_song_dupe = Song {
|
|
||||||
path: Path::new("path-to-dupe").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let second_song = Song {
|
|
||||||
path: Path::new("path-to-second").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1.9, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let third_song = Song {
|
|
||||||
path: Path::new("path-to-third").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.5, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fourth_song = Song {
|
|
||||||
path: Path::new("path-to-fourth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let mut songs = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
];
|
|
||||||
song_to_song(&first_song, &mut songs, euclidean_distance);
|
|
||||||
assert_eq!(
|
|
||||||
songs,
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
let first_song = CustomSong {
|
|
||||||
bliss_song: first_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let second_song = CustomSong {
|
|
||||||
bliss_song: second_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let first_song_dupe = CustomSong {
|
|
||||||
bliss_song: first_song_dupe,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let third_song = CustomSong {
|
|
||||||
bliss_song: third_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let fourth_song = CustomSong {
|
|
||||||
bliss_song: fourth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut songs: Vec<CustomSong> = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
];
|
|
||||||
|
|
||||||
song_to_song_by_key(&first_song, &mut songs, euclidean_distance, |s| {
|
|
||||||
s.bliss_song.to_owned()
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
songs,
|
|
||||||
vec![
|
|
||||||
first_song,
|
|
||||||
first_song_dupe,
|
|
||||||
second_song,
|
|
||||||
third_song,
|
|
||||||
fourth_song,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_sort_closest_to_first_song() {
|
|
||||||
let first_song = Song {
|
|
||||||
path: Path::new("path-to-first").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let first_song_dupe = Song {
|
|
||||||
path: Path::new("path-to-dupe").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let second_song = Song {
|
|
||||||
path: Path::new("path-to-second").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1.9, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let third_song = Song {
|
|
||||||
path: Path::new("path-to-third").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.5, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fourth_song = Song {
|
|
||||||
path: Path::new("path-to-fourth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fifth_song = Song {
|
|
||||||
path: Path::new("path-to-fifth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut songs = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
closest_to_first_song(&first_song, &mut songs, euclidean_distance);
|
|
||||||
|
|
||||||
let first_song = CustomSong {
|
|
||||||
bliss_song: first_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let second_song = CustomSong {
|
|
||||||
bliss_song: second_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let first_song_dupe = CustomSong {
|
|
||||||
bliss_song: first_song_dupe,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let third_song = CustomSong {
|
|
||||||
bliss_song: third_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let fourth_song = CustomSong {
|
|
||||||
bliss_song: fourth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let fifth_song = CustomSong {
|
|
||||||
bliss_song: fifth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut songs: Vec<CustomSong> = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
|
|
||||||
closest_to_first_song_by_key(&first_song, &mut songs, euclidean_distance, |s| {
|
|
||||||
s.bliss_song.to_owned()
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
songs,
|
|
||||||
vec![
|
|
||||||
first_song,
|
|
||||||
first_song_dupe,
|
|
||||||
second_song,
|
|
||||||
fourth_song,
|
|
||||||
fifth_song,
|
|
||||||
third_song
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_euclidean_distance() {
|
|
||||||
let a = arr1(&[
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,
|
|
||||||
]);
|
|
||||||
let b = arr1(&[
|
|
||||||
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
|
|
||||||
]);
|
|
||||||
assert_eq!(euclidean_distance(&a, &b), 4.242640687119285);
|
|
||||||
|
|
||||||
let a = arr1(&[0.5; 20]);
|
|
||||||
let b = arr1(&[0.5; 20]);
|
|
||||||
assert_eq!(euclidean_distance(&a, &b), 0.);
|
|
||||||
assert_eq!(euclidean_distance(&a, &b), 0.);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_cosine_distance() {
|
|
||||||
let a = arr1(&[
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,
|
|
||||||
]);
|
|
||||||
let b = arr1(&[
|
|
||||||
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
|
|
||||||
]);
|
|
||||||
assert_eq!(cosine_distance(&a, &b), 0.7705842661294382);
|
|
||||||
|
|
||||||
let a = arr1(&[0.5; 20]);
|
|
||||||
let b = arr1(&[0.5; 20]);
|
|
||||||
assert_eq!(cosine_distance(&a, &b), 0.);
|
|
||||||
assert_eq!(cosine_distance(&a, &b), 0.);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_closest_to_group() {
|
|
||||||
let first_song = Song {
|
|
||||||
path: Path::new("path-to-first").to_path_buf(),
|
|
||||||
analysis: Analysis::new([0.; 20]),
|
|
||||||
album: Some(String::from("Album")),
|
|
||||||
artist: Some(String::from("Artist")),
|
|
||||||
track_number: Some(String::from("01")),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let second_song = Song {
|
|
||||||
path: Path::new("path-to-second").to_path_buf(),
|
|
||||||
analysis: Analysis::new([0.1; 20]),
|
|
||||||
album: Some(String::from("Another Album")),
|
|
||||||
artist: Some(String::from("Artist")),
|
|
||||||
track_number: Some(String::from("10")),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let third_song = Song {
|
|
||||||
path: Path::new("path-to-third").to_path_buf(),
|
|
||||||
analysis: Analysis::new([10.; 20]),
|
|
||||||
album: Some(String::from("Album")),
|
|
||||||
artist: Some(String::from("Another Artist")),
|
|
||||||
track_number: Some(String::from("02")),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let fourth_song = Song {
|
|
||||||
path: Path::new("path-to-fourth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([20.; 20]),
|
|
||||||
album: Some(String::from("Another Album")),
|
|
||||||
artist: Some(String::from("Another Artist")),
|
|
||||||
track_number: Some(String::from("01")),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fifth_song = Song {
|
|
||||||
path: Path::new("path-to-fifth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([40.; 20]),
|
|
||||||
artist: Some(String::from("Third Artist")),
|
|
||||||
album: None,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let pool = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
let group = vec![first_song.to_owned(), third_song.to_owned()];
|
|
||||||
assert_eq!(
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
second_song.to_owned()
|
|
||||||
],
|
|
||||||
closest_album_to_group(group, pool.to_owned()).unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let first_song = CustomSong {
|
|
||||||
bliss_song: first_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let second_song = CustomSong {
|
|
||||||
bliss_song: second_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let third_song = CustomSong {
|
|
||||||
bliss_song: third_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
let fourth_song = CustomSong {
|
|
||||||
bliss_song: fourth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let fifth_song = CustomSong {
|
|
||||||
bliss_song: fifth_song,
|
|
||||||
something: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
let pool = vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
let group = vec![first_song.to_owned(), third_song.to_owned()];
|
|
||||||
assert_eq!(
|
|
||||||
vec![
|
|
||||||
first_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
second_song.to_owned()
|
|
||||||
],
|
|
||||||
closest_album_to_group_by_key(group, pool.to_owned(), |s| s.bliss_song.to_owned())
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
334
src/song.rs
334
src/song.rs
|
@ -12,11 +12,7 @@ extern crate ffmpeg_next as ffmpeg;
|
||||||
extern crate ndarray;
|
extern crate ndarray;
|
||||||
|
|
||||||
use crate::chroma::ChromaDesc;
|
use crate::chroma::ChromaDesc;
|
||||||
use crate::cue::CueInfo;
|
|
||||||
use crate::misc::LoudnessDesc;
|
use crate::misc::LoudnessDesc;
|
||||||
#[cfg(doc)]
|
|
||||||
use crate::playlist;
|
|
||||||
use crate::playlist::{closest_to_first_song, dedup_playlist, euclidean_distance, DistanceMetric};
|
|
||||||
use crate::temporal::BPMDesc;
|
use crate::temporal::BPMDesc;
|
||||||
use crate::timbral::{SpectralDesc, ZeroCrossingRateDesc};
|
use crate::timbral::{SpectralDesc, ZeroCrossingRateDesc};
|
||||||
use crate::bliss_lib::{BlissError, BlissResult, SAMPLE_RATE};
|
use crate::bliss_lib::{BlissError, BlissResult, SAMPLE_RATE};
|
||||||
|
@ -52,19 +48,6 @@ use strum_macros::{EnumCount, EnumIter};
|
||||||
pub struct Song {
|
pub struct Song {
|
||||||
/// Song's provided file path
|
/// Song's provided file path
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
/// Song's artist, read from the metadata
|
|
||||||
pub artist: Option<String>,
|
|
||||||
/// Song's title, read from the metadata
|
|
||||||
pub title: Option<String>,
|
|
||||||
/// Song's album name, read from the metadata
|
|
||||||
pub album: Option<String>,
|
|
||||||
/// 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>,
|
|
||||||
/// Song's genre, read from the metadata (`""` if empty)
|
|
||||||
pub genre: Option<String>,
|
|
||||||
/// bliss analysis results
|
/// bliss analysis results
|
||||||
pub analysis: Analysis,
|
pub analysis: Analysis,
|
||||||
/// The song's duration
|
/// The song's duration
|
||||||
|
@ -73,12 +56,6 @@ pub struct Song {
|
||||||
/// A simple integer that is bumped every time a breaking change
|
/// A simple integer that is bumped every time a breaking change
|
||||||
/// is introduced in the features.
|
/// is introduced in the features.
|
||||||
pub features_version: u16,
|
pub features_version: u16,
|
||||||
/// Populated only if the song was extracted from a larger audio file,
|
|
||||||
/// through the use of a CUE sheet.
|
|
||||||
/// By default, such a song's path would be
|
|
||||||
/// `path/to/cue_file.wav/CUE_TRACK00<track_number>`. Using this field,
|
|
||||||
/// you can change `song.path` to fit your needs.
|
|
||||||
pub cue_info: Option<CueInfo>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, EnumIter, EnumCount)]
|
#[derive(Debug, EnumIter, EnumCount)]
|
||||||
|
@ -218,96 +195,9 @@ impl Analysis {
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute distance between two analysis using a user-provided distance
|
|
||||||
/// metric. You most likely want to use `song.custom_distance` directly
|
|
||||||
/// rather than this function.
|
|
||||||
///
|
|
||||||
/// For this function to be integrated properly with the rest
|
|
||||||
/// of bliss' parts, it should be a valid distance metric, i.e.:
|
|
||||||
/// 1. For X, Y real vectors, d(X, Y) = 0 ⇔ X = Y
|
|
||||||
/// 2. For X, Y real vectors, d(X, Y) >= 0
|
|
||||||
/// 3. For X, Y real vectors, d(X, Y) = d(Y, X)
|
|
||||||
/// 4. For X, Y, Z real vectors d(X, Y) ≤ d(X + Z) + d(Z, Y)
|
|
||||||
///
|
|
||||||
/// Note that almost all distance metrics you will find obey these
|
|
||||||
/// properties, so don't sweat it too much.
|
|
||||||
pub fn custom_distance(&self, other: &Self, distance: impl DistanceMetric) -> f32 {
|
|
||||||
distance(&self.as_arr1(), &other.as_arr1())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Song {
|
impl Song {
|
||||||
#[allow(dead_code)]
|
|
||||||
/// Compute the distance between the current song and any given
|
|
||||||
/// Song.
|
|
||||||
///
|
|
||||||
/// The smaller the number, the closer the songs; usually more useful
|
|
||||||
/// if compared between several songs
|
|
||||||
/// (e.g. if song1.distance(song2) < song1.distance(song3), then song1 is
|
|
||||||
/// closer to song2 than it is to song3.
|
|
||||||
///
|
|
||||||
/// Currently uses the euclidean distance, but this can change in an
|
|
||||||
/// upcoming release if another metric performs better.
|
|
||||||
pub fn distance(&self, other: &Self) -> f32 {
|
|
||||||
self.analysis
|
|
||||||
.custom_distance(&other.analysis, euclidean_distance)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compute distance between two songs using a user-provided distance
|
|
||||||
/// metric.
|
|
||||||
///
|
|
||||||
/// For this function to be integrated properly with the rest
|
|
||||||
/// of bliss' parts, it should be a valid distance metric, i.e.:
|
|
||||||
/// 1. For X, Y real vectors, d(X, Y) = 0 ⇔ X = Y
|
|
||||||
/// 2. For X, Y real vectors, d(X, Y) >= 0
|
|
||||||
/// 3. For X, Y real vectors, d(X, Y) = d(Y, X)
|
|
||||||
/// 4. For X, Y, Z real vectors d(X, Y) ≤ d(X + Z) + d(Z, Y)
|
|
||||||
///
|
|
||||||
/// Note that almost all distance metrics you will find obey these
|
|
||||||
/// properties, so don't sweat it too much.
|
|
||||||
pub fn custom_distance(&self, other: &Self, distance: impl DistanceMetric) -> f32 {
|
|
||||||
self.analysis.custom_distance(&other.analysis, distance)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Orders songs in `pool` by proximity to `self`, using the distance
|
|
||||||
/// metric `distance` to compute the order.
|
|
||||||
/// Basically return a playlist from songs in `pool`, starting
|
|
||||||
/// from `self`, using `distance` (some distance metrics can
|
|
||||||
/// be found in the [playlist] module).
|
|
||||||
///
|
|
||||||
/// Note that contrary to [Song::closest_from_pool], `self` is NOT added
|
|
||||||
/// to the beginning of the returned vector.
|
|
||||||
///
|
|
||||||
/// No deduplication is ran either; if you're looking for something easy
|
|
||||||
/// that works "out of the box", use [Song::closest_from_pool].
|
|
||||||
pub fn closest_from_pool_custom(
|
|
||||||
&self,
|
|
||||||
pool: Vec<Self>,
|
|
||||||
distance: impl DistanceMetric,
|
|
||||||
) -> Vec<Self> {
|
|
||||||
let mut pool = pool;
|
|
||||||
closest_to_first_song(self, &mut pool, distance);
|
|
||||||
pool
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Order songs in `pool` by proximity to `self`.
|
|
||||||
/// Convenience method to return a playlist from songs in `pool`,
|
|
||||||
/// starting from `self`.
|
|
||||||
///
|
|
||||||
/// The distance is already chosen, deduplication is ran, and the first song
|
|
||||||
/// is added to the top of the playlist, to make everything easier.
|
|
||||||
///
|
|
||||||
/// If you want more control over which distance metric is chosen,
|
|
||||||
/// run deduplication manually, etc, use [Song::closest_from_pool_custom].
|
|
||||||
pub fn closest_from_pool(&self, pool: Vec<Self>) -> Vec<Self> {
|
|
||||||
let mut playlist = vec![self.to_owned()];
|
|
||||||
playlist.extend_from_slice(&pool);
|
|
||||||
closest_to_first_song(self, &mut playlist, euclidean_distance);
|
|
||||||
dedup_playlist(&mut playlist, None);
|
|
||||||
playlist
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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.
|
||||||
///
|
///
|
||||||
|
@ -329,16 +219,9 @@ impl Song {
|
||||||
|
|
||||||
Ok(Song {
|
Ok(Song {
|
||||||
path: raw_song.path,
|
path: raw_song.path,
|
||||||
artist: raw_song.artist,
|
|
||||||
album_artist: raw_song.album_artist,
|
|
||||||
title: raw_song.title,
|
|
||||||
album: raw_song.album,
|
|
||||||
track_number: raw_song.track_number,
|
|
||||||
genre: raw_song.genre,
|
|
||||||
duration: raw_song.duration,
|
duration: raw_song.duration,
|
||||||
analysis: Song::analyze(&raw_song.sample_array)?,
|
analysis: Song::analyze(&raw_song.sample_array)?,
|
||||||
features_version: FEATURES_VERSION,
|
features_version: FEATURES_VERSION,
|
||||||
cue_info: None,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -503,8 +386,7 @@ impl Song {
|
||||||
context.set_threading(Config {
|
context.set_threading(Config {
|
||||||
kind: ThreadingType::Frame,
|
kind: ThreadingType::Frame,
|
||||||
count: 0,
|
count: 0,
|
||||||
#[cfg(not(feature = "ffmpeg_6_0"))]
|
// safe: true,
|
||||||
safe: true,
|
|
||||||
});
|
});
|
||||||
let decoder = context.decoder().audio().map_err(|e| {
|
let decoder = context.decoder().audio().map_err(|e| {
|
||||||
BlissError::DecodingError(format!(
|
BlissError::DecodingError(format!(
|
||||||
|
@ -528,42 +410,7 @@ impl Song {
|
||||||
(decoder, input.index(), expected_sample_number)
|
(decoder, input.index(), expected_sample_number)
|
||||||
};
|
};
|
||||||
let sample_array: Vec<f32> = Vec::with_capacity(expected_sample_number as usize);
|
let sample_array: Vec<f32> = Vec::with_capacity(expected_sample_number as usize);
|
||||||
if let Some(title) = ictx.metadata().get("title") {
|
|
||||||
song.title = match title {
|
|
||||||
"" => None,
|
|
||||||
t => Some(t.to_string()),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if let Some(artist) = ictx.metadata().get("artist") {
|
|
||||||
song.artist = match artist {
|
|
||||||
"" => None,
|
|
||||||
a => Some(a.to_string()),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if let Some(album) = ictx.metadata().get("album") {
|
|
||||||
song.album = match album {
|
|
||||||
"" => None,
|
|
||||||
a => Some(a.to_string()),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if let Some(genre) = ictx.metadata().get("genre") {
|
|
||||||
song.genre = match genre {
|
|
||||||
"" => None,
|
|
||||||
g => Some(g.to_string()),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if let Some(track_number) = ictx.metadata().get("track") {
|
|
||||||
song.track_number = match track_number {
|
|
||||||
"" => None,
|
|
||||||
t => Some(t.to_string()),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if let Some(album_artist) = ictx.metadata().get("album_artist") {
|
|
||||||
song.album_artist = match album_artist {
|
|
||||||
"" => None,
|
|
||||||
t => Some(t.to_string()),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
let (empty_in_channel_layout, in_channel_layout) = {
|
let (empty_in_channel_layout, in_channel_layout) = {
|
||||||
if decoder.channel_layout() == ChannelLayout::empty() {
|
if decoder.channel_layout() == ChannelLayout::empty() {
|
||||||
(true, ChannelLayout::default(decoder.channels().into()))
|
(true, ChannelLayout::default(decoder.channels().into()))
|
||||||
|
@ -676,12 +523,6 @@ impl Song {
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub(crate) struct InternalSong {
|
pub(crate) struct InternalSong {
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub artist: Option<String>,
|
|
||||||
pub album_artist: Option<String>,
|
|
||||||
pub title: Option<String>,
|
|
||||||
pub album: Option<String>,
|
|
||||||
pub track_number: Option<String>,
|
|
||||||
pub genre: Option<String>,
|
|
||||||
pub duration: Duration,
|
pub duration: Duration,
|
||||||
pub sample_array: Vec<f32>,
|
pub sample_array: Vec<f32>,
|
||||||
}
|
}
|
||||||
|
@ -840,33 +681,6 @@ mod tests {
|
||||||
assert_eq!(expected_hash, hasher.finalize().as_slice());
|
assert_eq!(expected_hash, hasher.finalize().as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_tags() {
|
|
||||||
let song = Song::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
|
|
||||||
assert_eq!(song.artist, Some(String::from("David TMX")));
|
|
||||||
assert_eq!(
|
|
||||||
song.album_artist,
|
|
||||||
Some(String::from("David TMX - Album Artist"))
|
|
||||||
);
|
|
||||||
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.genre, Some(String::from("Pop")));
|
|
||||||
// Test that there is less than 10ms of difference between what
|
|
||||||
// the song advertises and what we compute.
|
|
||||||
assert!((song.duration.as_millis() as f32 - 11070.).abs() < 10.);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_empty_tags() {
|
|
||||||
let song = Song::decode(Path::new("data/no_tags.flac")).unwrap();
|
|
||||||
assert_eq!(song.artist, None);
|
|
||||||
assert_eq!(song.title, None);
|
|
||||||
assert_eq!(song.album, None);
|
|
||||||
assert_eq!(song.track_number, None);
|
|
||||||
assert_eq!(song.genre, None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resample_multi() {
|
fn test_resample_multi() {
|
||||||
let path = Path::new("data/s32_stereo_44_1_kHz.flac");
|
let path = Path::new("data/s32_stereo_44_1_kHz.flac");
|
||||||
|
@ -944,34 +758,6 @@ mod tests {
|
||||||
assert!(sample_array.len() as f32 / (sample_array.capacity() as f32) < 1.);
|
assert!(sample_array.len() as f32 / (sample_array.capacity() as f32) < 1.);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_analysis_distance() {
|
|
||||||
let mut a = Song::default();
|
|
||||||
a.analysis = Analysis::new([
|
|
||||||
0.16391512, 0.11326739, 0.96868552, 0.8353934, 0.49867523, 0.76532606, 0.63448005,
|
|
||||||
0.82506196, 0.71457147, 0.62395476, 0.69680329, 0.9855766, 0.41369333, 0.13900452,
|
|
||||||
0.68001012, 0.11029723, 0.97192943, 0.57727861, 0.07994821, 0.88993185,
|
|
||||||
]);
|
|
||||||
|
|
||||||
let mut b = Song::default();
|
|
||||||
b.analysis = Analysis::new([
|
|
||||||
0.5075758, 0.36440256, 0.28888011, 0.43032829, 0.62387977, 0.61894916, 0.99676086,
|
|
||||||
0.11913155, 0.00640396, 0.15943407, 0.33829514, 0.34947174, 0.82927523, 0.18987604,
|
|
||||||
0.54437275, 0.22076826, 0.91232151, 0.29233168, 0.32846024, 0.04522147,
|
|
||||||
]);
|
|
||||||
assert_eq!(a.distance(&b), 1.9469079)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_analysis_distance_indiscernible() {
|
|
||||||
let mut a = Song::default();
|
|
||||||
a.analysis = Analysis::new([
|
|
||||||
1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
|
|
||||||
20.,
|
|
||||||
]);
|
|
||||||
assert_eq!(a.distance(&a), 0.)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_decode_errors() {
|
fn test_decode_errors() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1023,120 +809,4 @@ mod tests {
|
||||||
format!("{:?}", song.analysis),
|
format!("{:?}", song.analysis),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dummy_distance(_: &Array1<f32>, _: &Array1<f32>) -> f32 {
|
|
||||||
0.
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_custom_distance() {
|
|
||||||
let mut a = Song::default();
|
|
||||||
a.analysis = Analysis::new([
|
|
||||||
0.16391512, 0.11326739, 0.96868552, 0.8353934, 0.49867523, 0.76532606, 0.63448005,
|
|
||||||
0.82506196, 0.71457147, 0.62395476, 0.69680329, 0.9855766, 0.41369333, 0.13900452,
|
|
||||||
0.68001012, 0.11029723, 0.97192943, 0.57727861, 0.07994821, 0.88993185,
|
|
||||||
]);
|
|
||||||
|
|
||||||
let mut b = Song::default();
|
|
||||||
b.analysis = Analysis::new([
|
|
||||||
0.5075758, 0.36440256, 0.28888011, 0.43032829, 0.62387977, 0.61894916, 0.99676086,
|
|
||||||
0.11913155, 0.00640396, 0.15943407, 0.33829514, 0.34947174, 0.82927523, 0.18987604,
|
|
||||||
0.54437275, 0.22076826, 0.91232151, 0.29233168, 0.32846024, 0.04522147,
|
|
||||||
]);
|
|
||||||
assert_eq!(a.custom_distance(&b, dummy_distance), 0.);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_closest_from_pool() {
|
|
||||||
let song = Song {
|
|
||||||
path: Path::new("path-to-first").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let first_song_dupe = Song {
|
|
||||||
path: Path::new("path-to-dupe").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let second_song = Song {
|
|
||||||
path: Path::new("path-to-second").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1.9, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let third_song = Song {
|
|
||||||
path: Path::new("path-to-third").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.5, 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fourth_song = Song {
|
|
||||||
path: Path::new("path-to-fourth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let fifth_song = Song {
|
|
||||||
path: Path::new("path-to-fifth").to_path_buf(),
|
|
||||||
analysis: Analysis::new([
|
|
||||||
2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 1., 1., 1.,
|
|
||||||
]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let songs = vec![
|
|
||||||
song.to_owned(),
|
|
||||||
first_song_dupe.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
fifth_song.to_owned(),
|
|
||||||
];
|
|
||||||
let playlist = song.closest_from_pool(songs.to_owned());
|
|
||||||
assert_eq!(
|
|
||||||
playlist,
|
|
||||||
vec![
|
|
||||||
song.to_owned(),
|
|
||||||
second_song.to_owned(),
|
|
||||||
fourth_song.to_owned(),
|
|
||||||
third_song.to_owned(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
let playlist = song.closest_from_pool_custom(songs, euclidean_distance);
|
|
||||||
assert_eq!(
|
|
||||||
playlist,
|
|
||||||
vec![
|
|
||||||
song,
|
|
||||||
first_song_dupe,
|
|
||||||
second_song,
|
|
||||||
fourth_song,
|
|
||||||
fifth_song,
|
|
||||||
third_song
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(all(feature = "bench", test))]
|
|
||||||
mod bench {
|
|
||||||
extern crate test;
|
|
||||||
use crate::Song;
|
|
||||||
use std::path::Path;
|
|
||||||
use test::Bencher;
|
|
||||||
|
|
||||||
#[bench]
|
|
||||||
fn bench_resample_multi(b: &mut Bencher) {
|
|
||||||
let path = Path::new("./data/s32_stereo_44_1_kHz.flac");
|
|
||||||
b.iter(|| {
|
|
||||||
Song::decode(&path).unwrap();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user