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
impl PcmBuffer
Sourcepub fn i16(spec: PcmSpec, frames: usize, samples_i16: Vec<i16>) -> Result<Self>
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.
Sourcepub fn f32(spec: PcmSpec, frames: usize, samples_f32: Vec<f32>) -> Result<Self>
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.
Sourcepub fn from_packet(spec: PcmSpec, packet: &PcmPacket) -> Result<Self>
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.
Sourcepub fn to_packet(&self) -> Result<PcmPacket>
pub fn to_packet(&self) -> Result<PcmPacket>
Encodes this buffer as a sim-lib-stream-core PcmPacket for stream
transport.
Sourcepub fn frames(&self) -> usize
pub fn frames(&self) -> usize
Returns the number of audio frames (samples per channel) in this buffer.
Sourcepub fn samples_i16(&self) -> &[i16]
pub fn samples_i16(&self) -> &[i16]
Returns the interleaved i16 samples.
§Panics
Panics if this buffer holds f32 samples rather than i16.
Sourcepub fn samples_f32(&self) -> &[f32]
pub fn samples_f32(&self) -> &[f32]
Returns the interleaved f32 samples.
§Panics
Panics if this buffer holds i16 samples rather than f32.