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 SPEECH_COMMANDS_PACK: Pack = Pack {
	name: "speech_commands",
	version: "0.2.0",
	license: "CC-BY-4.0",
	files: &[PackFile {
		name: "speech_commands_v0.02.tar.gz",
		size: 2_428_923_189,
		shake256: "f95034ea22e755d99cf746f26ed06f4ddfd6901762e1381400fcdd7392ef3f15",
		urls: &["http://download.tensorflow.org/data/speech_commands_v0.02.tar.gz"],
	}],
};

/// Command labels in canonical order; label index `i` names `COMMANDS[i]`.
pub const COMMANDS: [&str; 35] = [
	"backward", "bed", "bird", "cat", "dog", "down", "eight", "five", "follow", "forward", "four",
	"go", "happy", "house", "learn", "left", "marvin", "nine", "no", "off", "on", "one", "right",
	"seven", "sheila", "six", "stop", "three", "tree", "two", "up", "visual", "wow", "yes", "zero",
];

/// 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;

/// Official Speech Commands v0.02 split selection.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SpeechCommandsSplit {
	/// Clips named by neither list.
	#[default]
	Train,
	/// `validation_list.txt` members.
	Validation,
	/// `testing_list.txt` members.
	Test,
}

/// Load options for [`SpeechCommands`][crate::audio::speech_commands::SpeechCommands].
#[derive(Clone, Debug)]
pub struct SpeechCommandsConfig {
	/// Which official split to materialize.
	pub split: SpeechCommandsSplit,
	/// 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 SpeechCommandsConfig {
	fn default() -> Self {
		Self {
			split: SpeechCommandsSplit::Train,
			cache_dir: None,
			normalize: true,
		}
	}
}

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

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