Skip to main content

PcmBuffer

Struct PcmBuffer 

Source
pub struct PcmBuffer { /* private fields */ }
Expand description

Owned block of PCM audio: a PcmSpec plus a frame count and the matching interleaved sample data.

A PcmBuffer is the unit of audio carried across a source or sink. Its sample storage always agrees with its spec: an PcmSampleFormat::I16 spec holds i16 samples and an PcmSampleFormat::F32 spec holds f32 samples, with exactly channels * frames interleaved values. The constructors enforce that invariant, so samples_i16 and samples_f32 only need to panic on a caller that asks for the wrong format.

Equality is bit-exact for f32 samples (NaN bit patterns compare equal to themselves), which keeps the type usable as a deterministic test fixture.

§Examples

use sim_lib_stream_audio::{PcmBuffer, PcmSpec};

let spec = PcmSpec::i16(2, 48_000)?;
let buffer = PcmBuffer::i16(spec, 2, vec![1, 2, 3, 4])?;
assert_eq!(buffer.frames(), 2);
assert_eq!(buffer.samples_i16(), &[1, 2, 3, 4]);

Implementations§

Source§

impl PcmBuffer

Source

pub fn i16(spec: PcmSpec, frames: usize, samples_i16: Vec<i16>) -> Result<Self>

Builds an i16 buffer from a spec, frame count, and interleaved samples.

Returns an error when spec is not an PcmSampleFormat::I16 spec or when samples_i16.len() is not spec.channels() * frames.

Source

pub fn f32(spec: PcmSpec, frames: usize, samples_f32: Vec<f32>) -> Result<Self>

Builds an f32 buffer from a spec, frame count, and interleaved samples.

Returns an error when spec is not an PcmSampleFormat::F32 spec, when samples_f32.len() is not spec.channels() * frames, or when any sample is not finite.

Source

pub fn from_packet(spec: PcmSpec, packet: &PcmPacket) -> Result<Self>

Builds a buffer from a sim-lib-stream-core PcmPacket, adopting spec.

Returns an error when the packet’s channel count or sample format does not match spec.

Source

pub fn to_packet(&self) -> Result<PcmPacket>

Encodes this buffer as a sim-lib-stream-core PcmPacket for stream transport.

Source

pub fn spec(&self) -> PcmSpec

Returns the audio format of this buffer.

Source

pub fn frames(&self) -> usize

Returns the number of audio frames (samples per channel) in this buffer.

Source

pub fn samples_i16(&self) -> &[i16]

Returns the interleaved i16 samples.

§Panics

Panics if this buffer holds f32 samples rather than i16.

Source

pub fn samples_f32(&self) -> &[f32]

Returns the interleaved f32 samples.

§Panics

Panics if this buffer holds i16 samples rather than f32.

Trait Implementations§

Source§

impl Clone for PcmBuffer

Source§

fn clone(&self) -> PcmBuffer

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 Debug for PcmBuffer

Source§

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

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

impl Eq for PcmBuffer

Source§

impl PartialEq for PcmBuffer

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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.

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