Add number cores option to Library

This commit is contained in:
Polochon-street 2022-09-28 23:33:14 +02:00
parent df976c3725
commit 8d3e328cee
6 changed files with 74 additions and 48 deletions

View file

@ -1,5 +1,8 @@
#Changelog
## bliss 0.6.3
* Add customizable CPU number in the `library` module.
## bliss 0.6.2
* Add a `library` module, that greatly helps when making player plug-ins.

62
Cargo.lock generated
View file

@ -96,7 +96,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bliss-audio"
version = "0.6.2"
version = "0.6.3"
dependencies = [
"anyhow",
"bliss-audio-aubio-rs",
@ -256,13 +256,13 @@ dependencies = [
[[package]]
name = "console"
version = "0.15.1"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89eab4d20ce20cea182308bca13088fecea9c05f6776cf287205d41a0ed3c847"
checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c"
dependencies = [
"encode_unicode",
"lazy_static 1.4.0",
"libc",
"once_cell",
"terminal_size",
"unicode-width",
"winapi 0.3.9",
@ -323,15 +323,14 @@ dependencies = [
[[package]]
name = "crossbeam-epoch"
version = "0.9.10"
version = "0.9.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "045ebe27666471bb549370b4b0b3e51b07f56325befa4284db65fc89c02511b1"
checksum = "f916dfc5d356b0ed9dae65f1db9fc9770aa2851d2662b988ccf4fe3516e86348"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"once_cell",
"scopeguard",
]
@ -347,12 +346,11 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.11"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51887d4adc7b564537b15adcfb307936f8075dfcd5f00dde9a9f1d29383682bc"
checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
@ -652,9 +650,9 @@ checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]]
name = "jobserver"
version = "0.1.24"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa"
checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b"
dependencies = [
"libc",
]
@ -689,9 +687,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.133"
version = "0.2.134"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966"
checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb"
[[package]]
name = "libloading"
@ -963,9 +961,9 @@ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "pest"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb779fcf4bb850fbbb0edc96ff6cf34fd90c4b1a112ce042653280d9a7364048"
checksum = "dbc7bc69c062e492337d74d59b120c274fd3d261b6bf6d3207d499b4b379c41a"
dependencies = [
"thiserror",
"ucd-trie",
@ -973,9 +971,9 @@ dependencies = [
[[package]]
name = "pest_derive"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "502b62a6d0245378b04ffe0a7fb4f4419a4815fce813bd8a0ec89a56e07d67b1"
checksum = "60b75706b9642ebcb34dab3bc7750f811609a0eb1dd8b88c2d15bf628c1c65b2"
dependencies = [
"pest",
"pest_generator",
@ -983,9 +981,9 @@ dependencies = [
[[package]]
name = "pest_generator"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "451e629bf49b750254da26132f1a5a9d11fd8a95a3df51d15c4abd1ba154cb6c"
checksum = "f4f9272122f5979a6511a749af9db9bfc810393f63119970d7085fed1c4ea0db"
dependencies = [
"pest",
"pest_meta",
@ -996,9 +994,9 @@ dependencies = [
[[package]]
name = "pest_meta"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcec162c71c45e269dfc3fc2916eaeb97feab22993a21bcce4721d08cd7801a6"
checksum = "4c8717927f9b79515e565a64fe46c38b8cd0427e64c40680b14a7365ab09ac8d"
dependencies = [
"once_cell",
"pest",
@ -1040,9 +1038,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.43"
version = "1.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b"
dependencies = [
"unicode-ident",
]
@ -1362,9 +1360,9 @@ checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]]
name = "smallvec"
version = "1.9.0"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "strength_reduce"
@ -1398,9 +1396,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.100"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e"
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
dependencies = [
"proc-macro2",
"quote",
@ -1447,18 +1445,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.35"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c53f98874615aea268107765aa1ed8f6116782501d18e53d08b471733bea6c85"
checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.35"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8b463991b4eab2d801e724172285ec4195c650e8ec79b149e6c2a8e6dd3f783"
checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb"
dependencies = [
"proc-macro2",
"quote",

View file

@ -1,6 +1,6 @@
[package]
name = "bliss-audio"
version = "0.6.2"
version = "0.6.3"
authors = ["Polochon-street <polochonstreet@gmx.fr>"]
edition = "2018"
license = "GPL-3.0-only"

View file

@ -30,8 +30,9 @@ impl Config {
music_library_path: PathBuf,
config_path: Option<PathBuf>,
database_path: Option<PathBuf>,
number_cores: Option<usize>,
) -> Result<Self> {
let base_config = BaseConfig::new(config_path, database_path)?;
let base_config = BaseConfig::new(config_path, database_path, number_cores)?;
Ok(Self {
base_config,
music_library_path,
@ -172,7 +173,7 @@ fn main() -> Result<()> {
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)?;
let config = Config::new(folder, config_path, database_path, None)?;
let mut library = Library::new(config)?;
library.analyze_paths(library.song_paths()?, true)?;

View file

@ -31,8 +31,9 @@ impl Config {
music_library_path: PathBuf,
config_path: Option<PathBuf>,
database_path: Option<PathBuf>,
number_cores: Option<usize>,
) -> Result<Self> {
let base_config = BaseConfig::new(config_path, database_path)?;
let base_config = BaseConfig::new(config_path, database_path, number_cores)?;
Ok(Self {
base_config,
music_library_path,
@ -190,7 +191,7 @@ fn main() -> Result<()> {
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)?;
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)?;

View file

@ -52,10 +52,11 @@
//! music_library_path: PathBuf,
//! config_path: Option<PathBuf>,
//! database_path: Option<PathBuf>,
//! number_cores: Option<usize>,
//! ) -> Result<Self> {
//! // Note that by passing `(None, None)` here, the paths will
//! // be inferred automatically using user data dirs.
//! let base_config = BaseConfig::new(config_path, database_path)?;
//! let base_config = BaseConfig::new(config_path, database_path, number_cores)?;
//! Ok(Self {
//! base_config,
//! music_library_path,
@ -73,7 +74,7 @@
//!
//! let config_path = Some(PathBuf::from("path/to/config/config.json"));
//! let database_path = Some(PathBuf::from("path/to/config/bliss.db"));
//! let config = BaseConfig::new(config_path, database_path)?;
//! let config = BaseConfig::new(config_path, database_path, None)?;
//! let library: Library<BaseConfig> = Library::new(config)?;
//! # Ok::<(), Error>(())
//! ```
@ -203,9 +204,18 @@ pub trait AppConfigTrait: Serialize + Sized + DeserializeOwned {
/// The minimum configuration an application needs to work with
/// a [Library].
pub struct BaseConfig {
/// The path to where the configuration file should be stored,
/// e.g. `/home/foo/.local/share/bliss-rs/config.json`
config_path: PathBuf,
/// The path to where the database file should be stored,
/// e.g. `/home/foo/.local/share/bliss-rs/bliss.db`
database_path: PathBuf,
/// The latest features version a song has been analyzed
/// with.
features_version: u16,
/// The number of CPU cores an analysis will be performed with.
/// Defaults to the number of CPUs in the user's computer.
number_cores: usize,
}
impl BaseConfig {
@ -225,8 +235,14 @@ impl BaseConfig {
/// written to `config_path`.
//
/// Any path omitted will instead default to a "clever" path using
/// data directory inference.
pub fn new(config_path: Option<PathBuf>, database_path: Option<PathBuf>) -> Result<Self> {
/// data directory inference. The number of cores is the number of cores
/// that should be used for any analysis. If not provided, it will default
/// to the computer's number of cores.
pub fn new(
config_path: Option<PathBuf>,
database_path: Option<PathBuf>,
number_cores: Option<usize>,
) -> Result<Self> {
let config_path = {
// User provided a path; let the future file creation determine
// whether it points to something valid or not
@ -245,10 +261,13 @@ impl BaseConfig {
}
};
let number_cores = number_cores.unwrap_or_else(num_cpus::get);
Ok(Self {
config_path,
database_path,
features_version: FEATURES_VERSION,
number_cores,
})
}
}
@ -387,7 +406,7 @@ impl<Config: AppConfigTrait> Library<Config> {
/// set using default data folder path.
pub fn from_config_path(config_path: Option<PathBuf>) -> Result<Self> {
let config_path: Result<PathBuf> =
config_path.map_or_else(|| Ok(BaseConfig::new(None, None)?.config_path), Ok);
config_path.map_or_else(|| Ok(BaseConfig::new(None, None, None)?.config_path), Ok);
let config_path = config_path?;
let data = fs::read_to_string(config_path)?;
let config = Config::deserialize_config(&data)?;
@ -430,11 +449,12 @@ impl<Config: AppConfigTrait> Library<Config> {
pub fn new_from_base(
config_path: Option<PathBuf>,
database_path: Option<PathBuf>,
number_cores: Option<usize>,
) -> Result<Self>
where
BaseConfig: Into<Config>,
{
let base = BaseConfig::new(config_path, database_path)?;
let base = BaseConfig::new(config_path, database_path, number_cores)?;
let config = base.into();
Self::new(config)
}
@ -1224,7 +1244,8 @@ mod test {
let config_file = config_dir.path().join("config.json");
let database_file = config_dir.path().join("bliss.db");
let library =
Library::<BaseConfig>::new_from_base(Some(config_file), Some(database_file)).unwrap();
Library::<BaseConfig>::new_from_base(Some(config_file), Some(database_file), None)
.unwrap();
let analysis_vector: [f32; NUMBER_FEATURES] = (0..NUMBER_FEATURES)
.map(|x| x as f32 / 10.)
@ -2684,10 +2705,11 @@ mod test {
assert_eq!(
config_content,
format!(
"{{\"config_path\":\"{}\",\"database_path\":\"{}\",\"features_version\":{}}}",
"{{\"config_path\":\"{}\",\"database_path\":\"{}\",\"features_version\":{},\"number_cores\":{}}}",
library.config.base_config().config_path.display(),
library.config.base_config().database_path.display(),
FEATURES_VERSION,
num_cpus::get(),
)
);
}
@ -2766,7 +2788,7 @@ mod test {
// In reality, someone would just do that with `(None, None)` to get the default
// paths.
let base_config =
BaseConfig::new(Some(config_file.to_owned()), Some(database_file)).unwrap();
BaseConfig::new(Some(config_file.to_owned()), Some(database_file), Some(1)).unwrap();
let config = CustomConfig {
base_config,
@ -2800,7 +2822,7 @@ mod test {
// In reality, someone would just do that with `(None, None)` to get the default
// paths.
let base_config =
BaseConfig::new(Some(config_file.to_owned()), Some(database_file)).unwrap();
BaseConfig::new(Some(config_file.to_owned()), Some(database_file), Some(1)).unwrap();
let config = CustomConfig {
base_config,
@ -2844,7 +2866,8 @@ mod test {
assert!(!config_dir.is_dir());
let config_file = config_dir.join("config.json");
let database_file = config_dir.join("bliss.db");
Library::<BaseConfig>::new_from_base(Some(config_file), Some(database_file)).unwrap();
Library::<BaseConfig>::new_from_base(Some(config_file), Some(database_file), Some(1))
.unwrap();
assert!(config_dir.is_dir());
}
}