#![cfg_attr(feature = "bench", feature(test))]
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
mod chroma;
pub mod cue;
#[cfg(feature = "library")]
pub mod library;
mod misc;
pub mod playlist;
mod song;
mod temporal;
mod timbral;
mod utils;
extern crate crossbeam;
extern crate num_cpus;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
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;
pub use song::{Analysis, AnalysisIndex, Song, NUMBER_FEATURES};
const CHANNELS: u16 = 1;
const SAMPLE_RATE: u32 = 22050;
pub const FEATURES_VERSION: u16 = 1;
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum BlissError {
#[error("error happened while decoding file – {0}")]
DecodingError(String),
#[error("error happened while analyzing file – {0}")]
AnalysisError(String),
#[error("error happened with the music library provider - {0}")]
ProviderError(String),
}
pub type BlissResult<T> = Result<T, BlissError>;
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)
}
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)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
#[test]
fn test_send_song() {
fn assert_send<T: Send>() {}
assert_send::<Song>();
}
#[test]
fn test_sync_song() {
fn assert_sync<T: Send>() {}
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);
}
}