AudioBuffer<S> is a container for multi-channel planar audio sample data characterized by duration (capacity), sample format, and signal specification.
Overview
AudioBuffer stores audio samples in a planar layout where each channel’s samples are stored contiguously in memory. This layout is optimal for many DSP operations and codec decoding.
Type Parameters
The sample format type. Common formats include:
u8, u16, u24, u32 - Unsigned integer samples
i8, i16, i24, i32 - Signed integer samples
f32, f64 - Floating-point samples
Creating AudioBuffers
new
pub fn new(duration: Duration, spec: SignalSpec) -> Self
Instantiates a new AudioBuffer with the specified signal specification and duration.
Maximum number of frames (samples per channel) the buffer can store
Signal specification containing sample rate and channel configuration
Example:
use symphonia::core::audio::{AudioBuffer, SignalSpec, Layout};
let spec = SignalSpec::new_with_layout(48000, Layout::Stereo);
let mut buffer = AudioBuffer::<f32>::new(4096, spec);
unused
Creates an unused AudioBuffer that allocates no memory. Useful as a placeholder.
Accessing Buffer Properties
spec
pub fn spec(&self) -> &SignalSpec
Returns the signal specification for the buffer.
capacity
pub fn capacity(&self) -> usize
Returns the maximum number of frames the buffer can store.
frames
pub fn frames(&self) -> usize
Returns the number of frames currently written to the buffer.
is_unused
pub fn is_unused(&self) -> bool
Returns true if the buffer is unused (zero capacity).
Accessing Audio Data
chan
pub fn chan(&self, channel: usize) -> &[S]
Gets an immutable slice to all written samples in the specified channel.
Example:
use symphonia::core::audio::Signal;
// Read samples from the left channel
for &sample in buffer.chan(0) {
println!("Sample: {}", sample);
}
chan_mut
pub fn chan_mut(&mut self, channel: usize) -> &mut [S]
Gets a mutable slice to all written samples in the specified channel.
chan_pair_mut
pub fn chan_pair_mut(&mut self, first: usize, second: usize) -> (&mut [S], &mut [S])
Gets mutable references to two different channels simultaneously. Useful for stereo processing.
Example:
// Swap left and right channels
let (left, right) = buffer.chan_pair_mut(0, 1);
for (l, r) in left.iter_mut().zip(right.iter_mut()) {
std::mem::swap(l, r);
}
planes
pub fn planes(&self) -> AudioPlanes<'_, S>
Gets immutable references to all audio planes (channels) within the buffer.
This is not a cheap operation for buffers with > 8 channels. Prefer using chan() to selectively access individual planes when possible.
Example:
let planes = buffer.planes();
for plane in planes.planes() {
// Process each channel
for &sample in plane {
// ...
}
}
planes_mut
pub fn planes_mut(&mut self) -> AudioPlanesMut<'_, S>
Gets mutable references to all audio planes within the buffer.
convert
pub fn convert<T: Sample>(&self, dest: &mut AudioBuffer<T>)
where
S: IntoSample<T>
Converts the buffer contents into a destination AudioBuffer of a different sample format.
dest
&mut AudioBuffer<T>
required
Destination buffer. Must have capacity >= source capacity and matching signal spec.
Example:
let mut i16_buffer = AudioBuffer::<i16>::new(4096, spec);
let mut f32_buffer = AudioBuffer::<f32>::new(4096, spec);
// Decode into i16_buffer...
// Convert to f32 for processing
i16_buffer.convert(&mut f32_buffer);
make_equivalent
pub fn make_equivalent<E: Sample>(&self) -> AudioBuffer<E>
Creates a new AudioBuffer of a different sample type with the same capacity and specification.
AudioBufferRef
AudioBufferRef is a copy-on-write reference to an AudioBuffer of any sample format. Decoders return AudioBufferRef since the sample format isn’t known at compile time.
pub enum AudioBufferRef<'a> {
U8(Cow<'a, AudioBuffer<u8>>),
U16(Cow<'a, AudioBuffer<u16>>),
U24(Cow<'a, AudioBuffer<u24>>),
U32(Cow<'a, AudioBuffer<u32>>),
S8(Cow<'a, AudioBuffer<i8>>),
S16(Cow<'a, AudioBuffer<i16>>),
S24(Cow<'a, AudioBuffer<i24>>),
S32(Cow<'a, AudioBuffer<i32>>),
F32(Cow<'a, AudioBuffer<f32>>),
F64(Cow<'a, AudioBuffer<f64>>),
}
Working with AudioBufferRef
use symphonia::core::audio::{AudioBufferRef, Signal};
let decoded: AudioBufferRef = decoder.decode(&packet)?;
match decoded {
AudioBufferRef::F32(buf) => {
for &sample in buf.chan(0) {
// Process f32 samples
}
}
AudioBufferRef::S16(buf) => {
for &sample in buf.chan(0) {
// Process i16 samples
}
}
_ => {
// Handle other formats
}
}
AudioBufferRef Methods
Returns the signal specification
Returns the buffer capacity in frames
Returns the number of written frames
Converts to a typed AudioBuffer
Signal Trait
AudioBuffer implements the Signal trait for rendering and transforming audio data.
render
pub fn render<'a, F>(&'a mut self, n_frames: Option<usize>, render: F) -> Result<()>
where
F: FnMut(&mut AudioPlanesMut<'a, S>, usize) -> Result<()>
Renders audio frames using the provided render function.
Number of frames to render. If None, renders remaining capacity.
Function called for each frame. Receives mutable plane references and frame index.
render_silence
pub fn render_silence(&mut self, n_frames: Option<usize>)
Renders silent frames (mid-point value for the sample format).
render_reserved
pub fn render_reserved(&mut self, n_frames: Option<usize>)
Reserves frames without modifying underlying data. Advances the frame counter.
clear
Clears all written frames. This is a cheap operation that only resets the frame counter.
pub fn transform<F>(&mut self, f: F)
where
F: Fn(S) -> S
Applies a transformation function to every written sample.
Example:
// Apply 0.5x gain to all samples
buffer.transform(|s| s * 0.5);
truncate
pub fn truncate(&mut self, n_frames: usize)
Truncates the buffer to the specified number of frames.
shift
pub fn shift(&mut self, shift: usize)
Shifts buffer contents backward, dropping the leading frames.
trim
pub fn trim(&mut self, start: usize, end: usize)
Trims frames from the start and end of the buffer.
Number of frames to trim from the start
Number of frames to trim from the end
See Also