oadsrust 0.2.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::pack::{FetchProgress, Pack, PackFile};
use oa::{Engine, Result};
use std::path::PathBuf;

pub(crate) const MINI_SPEECH_COMMANDS_PACK: Pack = Pack {
	name: "mini_speech_commands",
	version: "1.0.0",
	license: "CC-BY-4.0",
	files: &[PackFile {
		name: "mini_speech_commands.zip",
		size: 182_082_353,
		shake256: "d589233d4d8e49c43f5a4a303d411a54f606cd15a14fe38b245f187e449b1a35",
		urls: &[
			"https://storage.googleapis.com/download.tensorflow.org/data/mini_speech_commands.zip",
			"http://storage.googleapis.com/download.tensorflow.org/data/mini_speech_commands.zip",
		],
	}],
};

/// Command labels in canonical order; label index `i` names `COMMANDS[i]`.
pub const COMMANDS: [&str; 8] = ["down", "go", "left", "no", "right", "stop", "up", "yes"];

/// Clip rate shared by every Speech Commands recording.
pub const CLIP_SAMPLE_RATE: u32 = 16_000;

/// One-second clip length in samples; shorter clips are zero-padded.
pub const CLIP_SAMPLES: usize = 16_000;

/// Load options for [`MiniSpeechCommands`][crate::audio::mini_speech_commands::MiniSpeechCommands].
#[derive(Clone, Debug)]
pub struct MiniSpeechCommandsConfig {
	/// Cache root override. `None` resolves `OA_DATA_DIR`, then the platform
	/// cache directory; see [`data_root`](crate::pack::data_root).
	pub cache_dir: Option<PathBuf>,
	/// Scale samples into `[-1, 1]`. `false` keeps raw `i16` amplitudes as
	/// F32.
	pub normalize: bool,
}

impl Default for MiniSpeechCommandsConfig {
	fn default() -> Self {
		Self {
			cache_dir: None,
			normalize: true,
		}
	}
}

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

impl super::MiniSpeechCommands {
	/// 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: &MiniSpeechCommandsConfig) -> 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: &MiniSpeechCommandsConfig) -> 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: &MiniSpeechCommandsConfig,
		progress: &mut impl FnMut(FetchProgress) -> bool,
	) -> Result<Self> {
		Self::load_with_progress_on(&Engine::current()?, config, progress)
	}
}