#[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
FFmpegdecoders for processing
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
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
impl SampleFormat
Sourcepub const fn name(&self) -> &'static str
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");Sourcepub const fn bytes_per_sample(&self) -> usize
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);Sourcepub const fn is_planar(&self) -> bool
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());Sourcepub const fn is_packed(&self) -> bool
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());Sourcepub const fn is_float(&self) -> bool
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());Sourcepub const fn is_integer(&self) -> bool
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());Sourcepub const fn is_signed(&self) -> bool
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());Sourcepub const fn packed_equivalent(&self) -> SampleFormat
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);Sourcepub const fn planar_equivalent(&self) -> SampleFormat
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);Sourcepub const fn bit_depth(&self) -> usize
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
impl Clone for SampleFormat
Source§fn clone(&self) -> SampleFormat
fn clone(&self) -> SampleFormat
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SampleFormat
impl Debug for SampleFormat
Source§impl Default for SampleFormat
impl Default for SampleFormat
Source§fn default() -> SampleFormat
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.