use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SampleFormat {
Int { bits: u8, slot_bytes: u8 },
Float32,
}
impl SampleFormat {
pub const I16: Self = Self::Int {
bits: 16,
slot_bytes: 2,
};
pub const I24: Self = Self::Int {
bits: 24,
slot_bytes: 3,
};
pub const I24_IN_32: Self = Self::Int {
bits: 24,
slot_bytes: 4,
};
pub const I32: Self = Self::Int {
bits: 32,
slot_bytes: 4,
};
pub fn int(bits: u8, slot_bytes: u8) -> Result<Self, ConvertError> {
if !(8..=32).contains(&bits)
|| !(1..=4).contains(&slot_bytes)
|| u32::from(bits) > u32::from(slot_bytes) * 8
{
return Err(ConvertError::InvalidArgument(format!(
"{bits} significant bits do not fit a slot of {slot_bytes} bytes (8 to 32 bits, slots of 1 to 4 bytes)"
)));
}
Ok(Self::Int { bits, slot_bytes })
}
pub fn bytes_per_sample(self) -> usize {
match self {
Self::Int { slot_bytes, .. } => usize::from(slot_bytes),
Self::Float32 => 4,
}
}
pub fn valid_bits(self) -> u8 {
match self {
Self::Int { bits, .. } => bits,
Self::Float32 => 32,
}
}
pub fn is_float(self) -> bool {
matches!(self, Self::Float32)
}
pub fn describe(self) -> String {
match self {
Self::Float32 => "32-bit float".to_string(),
Self::Int { bits, slot_bytes } => {
if u32::from(bits) == u32::from(slot_bytes) * 8 && bits != 24 {
format!("{bits}-bit")
} else if bits == 24 && slot_bytes == 3 {
"24-bit packed".to_string()
} else {
format!("{bits}-bit in a {slot_bytes}-byte slot")
}
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PcmFormat {
pub sample_rate: u32,
pub channels: u16,
pub sample: SampleFormat,
}
impl PcmFormat {
pub fn new(sample_rate: u32, channels: u16, sample: SampleFormat) -> Self {
Self {
sample_rate,
channels,
sample,
}
}
pub fn bytes_per_frame(&self) -> usize {
self.sample.bytes_per_sample() * usize::from(self.channels)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ConversionKind {
None,
BitExact,
Lossless,
Lossy,
}
impl ConversionKind {
pub fn code(self) -> &'static str {
match self {
Self::None => "none",
Self::BitExact => "bit_exact",
Self::Lossless => "lossless",
Self::Lossy => "lossy",
}
}
pub fn preserves_bit_perfect(self) -> bool {
self <= Self::BitExact
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub enum Narrowing {
Truncate,
#[default]
TpdfDither,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub enum Quality {
#[default]
High,
Fast,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ConvertOptions {
pub narrowing: Narrowing,
pub quality: Quality,
pub headroom_db: f64,
pub dither_seed: u64,
pub source_sample: Option<SampleFormat>,
}
impl Default for ConvertOptions {
fn default() -> Self {
Self {
narrowing: Narrowing::default(),
quality: Quality::default(),
headroom_db: 0.0,
dither_seed: 0x853c_49e6_748f_ea9b,
source_sample: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConvertError {
Unsupported(String),
InvalidArgument(String),
InvalidState(String),
}
impl ConvertError {
pub fn code(&self) -> &'static str {
match self {
Self::Unsupported(_) => "unsupported",
Self::InvalidArgument(_) => "invalid_argument",
Self::InvalidState(_) => "invalid_state",
}
}
pub fn message(&self) -> &str {
match self {
Self::Unsupported(message)
| Self::InvalidArgument(message)
| Self::InvalidState(message) => message,
}
}
}
impl fmt::Display for ConvertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code(), self.message())
}
}
impl std::error::Error for ConvertError {}