Skip to main content

SampleFormat

Enum SampleFormat 

Source
pub enum SampleFormat {
    Int {
        bits: u8,
        slot_bytes: u8,
    },
    Float32,
}
Expand description

How the samples of a PCM stream are stored.

Variants§

§

Int

Signed two’s-complement integers, little-endian: bits significant bits, left-justified in a slot of slot_bytes bytes (the unused low bits of the slot are zero). This is the wire layout of docs/API.md section 4: 16-bit in 2 bytes, packed 24-bit in 3, 24-bit in 4 (the sample shifted left by 8) and 32-bit in 4.

Fields

§bits: u8
§slot_bytes: u8
§

Float32

32-bit IEEE-754 floats, little-endian, nominal range -1.0 to 1.0.

Implementations§

Source§

impl SampleFormat

Source

pub const I16: Self

16-bit in 2 bytes.

Source

pub const I24: Self

Packed 24-bit in 3 bytes.

Source

pub const I24_IN_32: Self

24-bit left-justified in a 4-byte slot (the low byte is zero).

Source

pub const I32: Self

32-bit in 4 bytes.

Source

pub fn int(bits: u8, slot_bytes: u8) -> Result<Self, ConvertError>

An integer layout, checked: 8 to 32 significant bits in a slot of 1 to 4 bytes that holds them.

Source

pub fn bytes_per_sample(self) -> usize

Bytes one sample takes.

Examples found in repository?
examples/convert_bench.rs (line 28)
16fn run(label: &str, source: PcmFormat, target: PcmFormat, quality: Quality) {
17    let options = ConvertOptions {
18        quality,
19        ..ConvertOptions::default()
20    };
21    let plan = ConversionPlan::pcm(source, target, &options).unwrap();
22    let build = Instant::now();
23    let mut converter = Converter::new(&plan).unwrap();
24    let build_ms = build.elapsed().as_secs_f64() * 1000.0;
25
26    let chunk_frames = source.sample_rate as usize / 100;
27    let chunk: Vec<u8> =
28        (0..chunk_frames * usize::from(source.channels) * source.sample.bytes_per_sample())
29            .map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
30            .collect();
31    let mut output = vec![0u8; converter.min_output_bytes().max(1 << 20)];
32    let seconds = 10;
33    let start = Instant::now();
34    let mut produced = 0usize;
35    for _ in 0..seconds * 100 {
36        let done = converter.process(&chunk, &mut output).unwrap();
37        produced += done.produced;
38    }
39    let elapsed = start.elapsed().as_secs_f64();
40    std::hint::black_box(produced);
41    println!(
42        "{label:<46} build {build_ms:6.1} ms   {:6.1}x realtime   {:5.2}% of a core",
43        seconds as f64 / elapsed,
44        elapsed / seconds as f64 * 100.0
45    );
46}
Source

pub fn valid_bits(self) -> u8

Significant bits of an integer layout; 32 for floats.

Source

pub fn is_float(self) -> bool

Source

pub fn describe(self) -> String

A phrase for descriptions: “16-bit”, “24-bit (3-byte slot)”, “32-bit float”.

Trait Implementations§

Source§

impl Clone for SampleFormat

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for SampleFormat

Source§

impl Debug for SampleFormat

Source§

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

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

impl Eq for SampleFormat

Source§

impl Hash for SampleFormat

Source§

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

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: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.