Skip to main content

SampleFormat

Enum SampleFormat 

Source
#[non_exhaustive]
pub enum SampleFormat { U8, I16, I32, F32, F64, U8p, I16p, I32p, F32p, F64p, Other(u32), }
Expand description

Audio sample format for audio frames.

This enum represents various sample formats used in audio processing. It is designed to cover the most common formats used in audio editing while remaining extensible via the Other variant.

§Format Categories

  • Packed (Interleaved): Samples from all channels are interleaved (U8, I16, I32, F32, F64)
  • Planar: Each channel stored in a separate buffer (U8p, I16p, I32p, F32p, F64p)

§Common Usage

  • I16: CD quality audio (16-bit signed)
  • F32: Most common for audio editing (32-bit float)
  • F32p: Common in FFmpeg decoders for processing

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

8-bit unsigned integer (0-255)

§

I16

16-bit signed integer - CD quality audio

§

I32

32-bit signed integer

§

F32

32-bit floating point - most common for editing

§

F64

64-bit floating point - highest precision

§

U8p

8-bit unsigned integer, planar

§

I16p

16-bit signed integer, planar

§

I32p

32-bit signed integer, planar

§

F32p

32-bit floating point, planar - common in decoders

§

F64p

64-bit floating point, planar

§

Other(u32)

Unknown or unsupported format with FFmpeg’s AVSampleFormat value

Implementations§

Source§

impl SampleFormat

Source

pub const fn name(&self) -> &'static str

Returns the format name as a human-readable string.

§Examples
use ff_format::SampleFormat;

assert_eq!(SampleFormat::I16.name(), "s16");
assert_eq!(SampleFormat::F32.name(), "flt");
assert_eq!(SampleFormat::F32p.name(), "fltp");
Source

pub const fn bytes_per_sample(&self) -> usize

Returns the number of bytes per sample.

This is the size of a single sample value, regardless of whether the format is planar or packed.

§Examples
use ff_format::SampleFormat;

assert_eq!(SampleFormat::U8.bytes_per_sample(), 1);
assert_eq!(SampleFormat::I16.bytes_per_sample(), 2);
assert_eq!(SampleFormat::I32.bytes_per_sample(), 4);
assert_eq!(SampleFormat::F32.bytes_per_sample(), 4);
assert_eq!(SampleFormat::F64.bytes_per_sample(), 8);
// Planar formats have the same bytes per sample
assert_eq!(SampleFormat::F32p.bytes_per_sample(), 4);
Source

pub const fn is_planar(&self) -> bool

Returns true if this is a planar format.

In planar formats, samples for each channel are stored in separate contiguous buffers. This is more efficient for processing but requires conversion for output.

§Examples
use ff_format::SampleFormat;

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

pub const fn is_packed(&self) -> bool

Returns true if this is a packed (interleaved) format.

In packed formats, samples from all channels are interleaved (e.g., L R L R L R for stereo). This is the format typically used for audio output.

§Examples
use ff_format::SampleFormat;

assert!(SampleFormat::F32.is_packed());
assert!(!SampleFormat::F32p.is_packed());
assert!(SampleFormat::I16.is_packed());
assert!(!SampleFormat::I16p.is_packed());
Source

pub const fn is_float(&self) -> bool

Returns true if this is a floating-point format.

Floating-point formats (F32, F64, F32p, F64p) offer higher dynamic range and are preferred for audio processing to avoid clipping.

§Examples
use ff_format::SampleFormat;

assert!(SampleFormat::F32.is_float());
assert!(SampleFormat::F64.is_float());
assert!(SampleFormat::F32p.is_float());
assert!(!SampleFormat::I16.is_float());
assert!(!SampleFormat::I32.is_float());
Source

pub const fn is_integer(&self) -> bool

Returns true if this is an integer format.

Integer formats include both signed (I16, I32) and unsigned (U8) types.

§Examples
use ff_format::SampleFormat;

assert!(SampleFormat::I16.is_integer());
assert!(SampleFormat::U8.is_integer());
assert!(!SampleFormat::F32.is_integer());
Source

pub const fn is_signed(&self) -> bool

Returns true if this is a signed format.

All formats except U8 and U8p are signed.

§Examples
use ff_format::SampleFormat;

assert!(SampleFormat::I16.is_signed());
assert!(SampleFormat::F32.is_signed());
assert!(!SampleFormat::U8.is_signed());
Source

pub const fn packed_equivalent(&self) -> SampleFormat

Returns the packed (interleaved) equivalent of this format.

If the format is already packed, returns itself.

§Examples
use ff_format::SampleFormat;

assert_eq!(SampleFormat::F32p.packed_equivalent(), SampleFormat::F32);
assert_eq!(SampleFormat::I16p.packed_equivalent(), SampleFormat::I16);
assert_eq!(SampleFormat::F32.packed_equivalent(), SampleFormat::F32);
Source

pub const fn planar_equivalent(&self) -> SampleFormat

Returns the planar equivalent of this format.

If the format is already planar, returns itself.

§Examples
use ff_format::SampleFormat;

assert_eq!(SampleFormat::F32.planar_equivalent(), SampleFormat::F32p);
assert_eq!(SampleFormat::I16.planar_equivalent(), SampleFormat::I16p);
assert_eq!(SampleFormat::F32p.planar_equivalent(), SampleFormat::F32p);
Source

pub const fn bit_depth(&self) -> usize

Returns the bit depth of this format.

§Examples
use ff_format::SampleFormat;

assert_eq!(SampleFormat::U8.bit_depth(), 8);
assert_eq!(SampleFormat::I16.bit_depth(), 16);
assert_eq!(SampleFormat::F32.bit_depth(), 32);
assert_eq!(SampleFormat::F64.bit_depth(), 64);

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 sample format.

The default is SampleFormat::F32 as it’s the most common format used in audio editing and processing.

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 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 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<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> 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.