Skip to main content

SampleFormat

Enum SampleFormat 

Source
#[non_exhaustive]
pub enum SampleFormat { U8, S16, S32, F32, F64, S16p, S32p, F32p, F64p, S24, S24p, }
Expand description

Core OxiMedia types: errors, codecs, pixel/sample formats, timestamps. Audio sample format.

Defines how audio samples are stored in memory, including bit depth, signedness, and whether samples are interleaved or planar.

Formats ending with ‘p’ are planar (one plane per channel).

§Examples

use oximedia_core::types::SampleFormat;

let format = SampleFormat::F32;
assert!(!format.is_planar());
assert_eq!(format.bytes_per_sample(), 4);

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

U8

Unsigned 8-bit integer, interleaved.

§

S16

Signed 16-bit integer, interleaved.

§

S32

Signed 32-bit integer, interleaved.

§

F32

32-bit floating point, interleaved.

§

F64

64-bit floating point, interleaved.

§

S16p

Signed 16-bit integer, planar.

§

S32p

Signed 32-bit integer, planar.

§

F32p

32-bit floating point, planar.

§

F64p

64-bit floating point, planar.

§

S24

Signed 24-bit integer, interleaved (stored in 3 bytes per sample). Common in professional audio interfaces and WAV files.

§

S24p

Signed 24-bit integer, planar (stored in 3 bytes per sample). Planar variant for professional audio pipelines.

Implementations§

Source§

impl SampleFormat

Source

pub const fn bytes_per_sample(&self) -> usize

Returns the number of bytes per sample.

§Examples
use oximedia_core::types::SampleFormat;

assert_eq!(SampleFormat::U8.bytes_per_sample(), 1);
assert_eq!(SampleFormat::S16.bytes_per_sample(), 2);
assert_eq!(SampleFormat::F32.bytes_per_sample(), 4);
assert_eq!(SampleFormat::F64.bytes_per_sample(), 8);
Source

pub const fn is_planar(&self) -> bool

Returns whether this format uses planar storage.

Planar formats store each channel in a separate contiguous memory region, as opposed to interleaved formats where samples alternate between channels.

§Examples
use oximedia_core::types::SampleFormat;

assert!(!SampleFormat::F32.is_planar());
assert!(SampleFormat::F32p.is_planar());
Source

pub const fn bits_per_sample(&self) -> u32

Returns the number of bits per sample.

§Examples
use oximedia_core::types::SampleFormat;

assert_eq!(SampleFormat::U8.bits_per_sample(), 8);
assert_eq!(SampleFormat::S16.bits_per_sample(), 16);
assert_eq!(SampleFormat::F32.bits_per_sample(), 32);
Source

pub const fn is_float(&self) -> bool

Returns whether this format uses floating point samples.

§Examples
use oximedia_core::types::SampleFormat;

assert!(SampleFormat::F32.is_float());
assert!(SampleFormat::F64p.is_float());
assert!(!SampleFormat::S16.is_float());
Source

pub const fn is_signed(&self) -> bool

Returns whether this format uses signed integer samples.

§Examples
use oximedia_core::types::SampleFormat;

assert!(SampleFormat::S16.is_signed());
assert!(SampleFormat::S32p.is_signed());
assert!(!SampleFormat::U8.is_signed());
Source

pub const fn to_packed(&self) -> SampleFormat

Returns the packed (interleaved) equivalent of this format.

If the format is already packed, returns self.

§Examples
use oximedia_core::types::SampleFormat;

assert_eq!(SampleFormat::F32p.to_packed(), SampleFormat::F32);
assert_eq!(SampleFormat::S16.to_packed(), SampleFormat::S16);
Source

pub const fn to_planar(&self) -> SampleFormat

Returns the planar equivalent of this format.

If the format is already planar, returns self. Note: U8 has no planar equivalent and returns U8.

§Examples
use oximedia_core::types::SampleFormat;

assert_eq!(SampleFormat::F32.to_planar(), SampleFormat::F32p);
assert_eq!(SampleFormat::S16p.to_planar(), SampleFormat::S16p);
Source§

impl SampleFormat

Source

pub const fn bit_depth(&self) -> u8

Returns the bit depth of this sample format.

§Examples
use oximedia_core::types::SampleFormat;

assert_eq!(SampleFormat::U8.bit_depth(), 8);
assert_eq!(SampleFormat::S16.bit_depth(), 16);
assert_eq!(SampleFormat::S24.bit_depth(), 24);
assert_eq!(SampleFormat::F32.bit_depth(), 32);
assert_eq!(SampleFormat::F64.bit_depth(), 64);
Source

pub fn dynamic_range_db(&self) -> f64

Returns the theoretical dynamic range in decibels for integer formats.

For floating-point formats, returns an approximate practical range. The formula for integer formats is 6.02 * bits + 1.76 dB.

§Examples
use oximedia_core::types::SampleFormat;

let dr = SampleFormat::S16.dynamic_range_db();
assert!((dr - 98.09).abs() < 0.1); // ~96.3 + 1.76
Source

pub const fn buffer_size(&self, sample_count: usize, channels: usize) -> usize

Returns the buffer size in bytes needed for sample_count samples across channels channels.

For interleaved formats, all channels are stored in a single buffer. For planar formats, this returns the total size of all per-channel planes.

§Examples
use oximedia_core::types::SampleFormat;

// 1024 stereo F32 samples: 1024 * 2 * 4 = 8192 bytes
assert_eq!(SampleFormat::F32.buffer_size(1024, 2), 8192);

// Planar is the same total: 1024 * 4 * 2 planes = 8192
assert_eq!(SampleFormat::F32p.buffer_size(1024, 2), 8192);

Trait Implementations§

Source§

impl Clone for SampleFormat

Source§

fn clone(&self) -> SampleFormat

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SampleFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for SampleFormat

Source§

fn default() -> SampleFormat

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for SampleFormat

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<SampleFormat, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for SampleFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FormatCost for SampleFormat

Source§

fn conversion_cost(&self, target: &SampleFormat) -> Option<u32>

Returns the conversion cost from self to target, or None if no conversion path is available.
Source§

impl FromStr for SampleFormat

Source§

fn from_str(s: &str) -> Result<SampleFormat, <SampleFormat as FromStr>::Err>

Parses a sample format name string.

§Examples
use oximedia_core::types::SampleFormat;

let fmt: SampleFormat = "f32".parse().expect("should parse");
assert_eq!(fmt, SampleFormat::F32);
Source§

type Err = OxiError

The associated error which can be returned from parsing.
Source§

impl Hash for SampleFormat

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for SampleFormat

Source§

fn eq(&self, other: &SampleFormat) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for SampleFormat

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for SampleFormat

Source§

impl Eq for SampleFormat

Source§

impl StructuralPartialEq for SampleFormat

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,