oadsrust 0.3.0

Content-addressed dataset packs and loaders for OA
// @generated by tool/gen/ds/generate.py — edit tool/gen/ds/schema/packs.json instead.
use crate::core::pack::{FetchProgress, Pack, PackFile};
use oa::{Engine, Result};
use std::path::PathBuf;

pub(crate) const MAESTRO_PACK: Pack = Pack {
	name: "maestro",
	version: "3.0.0",
	license: "CC-BY-NC-SA-4.0",
	files: &[PackFile {
		name: "maestro-v3.0.0-midi.zip",
		size: 58_416_533,
		shake256: "7f85aca2ee4358bcd87417cac9817121edc3ae83b48dcb248571a5b2e064a24d",
		urls: &[
			"https://storage.googleapis.com/magentadata/datasets/maestro/v3.0.0/maestro-v3.0.0-midi.zip",
		],
	}],
};

/// Window length in events; the TensorFlow tutorial uses 25.
pub const DEFAULT_SEQ_LEN: usize = 25;

/// Official MAESTRO v3 performance assignment.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MaestroSplit {
	/// ~962 performances reserved for training.
	#[default]
	Train,
	/// ~137 validation performances.
	Validation,
	/// ~177 test performances.
	Test,
}

/// Load options for [`Maestro`][crate::audio::maestro::Maestro].
#[derive(Clone, Debug)]
pub struct MaestroConfig {
	/// Which official split contributes windows.
	pub split: MaestroSplit,
	/// Events per window; needs `seq_len + 1` events per contributing file.
	pub seq_len: usize,
	/// Cache root override; `None` resolves `OA_DATA_DIR`, then the platform
	/// cache.
	pub cache_dir: Option<PathBuf>,
}

impl Default for MaestroConfig {
	fn default() -> Self {
		Self {
			split: MaestroSplit::Train,
			seq_len: DEFAULT_SEQ_LEN,
			cache_dir: None,
		}
	}
}

impl MaestroConfig {
	/// Construct the record with its default state.
	#[inline]
	#[must_use]
	pub fn new() -> Self {
		Self::default()
	}
}

impl super::Maestro {
	/// Decode the selected split and upload it on this thread's
	/// default engine.
	///
	/// # Errors
	///
	/// Returns an error when the default engine cannot be
	/// initialized, plus the errors of
	/// [`load_on`](Self::load_on).
	#[inline]
	pub fn load(config: &MaestroConfig) -> Result<Self> {
		Self::load_on(&Engine::current()?, config)
	}

	/// Decode the selected split and upload it on `engine`.
	///
	/// This is the [`load`](Self::load) contract evaluated on
	/// `engine` instead of this thread's default engine.
	///
	/// # Errors
	///
	/// Returns the errors of
	/// [`load_with_progress_on`](Self::load_with_progress_on).
	#[inline]
	pub fn load_on(engine: &Engine, config: &MaestroConfig) -> Result<Self> {
		Self::load_with_progress_on(engine, config, &mut |_| true)
	}

	/// [`load`](Self::load) with a fetch progress callback.
	///
	/// # Errors
	///
	/// Returns an error when the default engine cannot be
	/// initialized, plus the errors of
	/// [`load_with_progress_on`](Self::load_with_progress_on).
	#[inline]
	pub fn load_with_progress(
		config: &MaestroConfig,
		progress: &mut impl FnMut(FetchProgress) -> bool,
	) -> Result<Self> {
		Self::load_with_progress_on(&Engine::current()?, config, progress)
	}
}