Trait AudioBlock

Source
pub trait AudioBlock<S: Sample> {
Show 14 methods // Required methods fn num_channels(&self) -> u16; fn num_frames(&self) -> usize; fn num_channels_allocated(&self) -> u16; fn num_frames_allocated(&self) -> usize; fn layout(&self) -> BlockLayout; fn sample(&self, channel: u16, frame: usize) -> S; fn channel(&self, channel: u16) -> impl Iterator<Item = &S>; fn channels( &self, ) -> impl Iterator<Item = impl Iterator<Item = &S> + '_> + '_; fn frame(&self, frame: usize) -> impl Iterator<Item = &S>; fn frames(&self) -> impl Iterator<Item = impl Iterator<Item = &S> + '_> + '_; fn view(&self) -> impl AudioBlock<S>; fn raw_data(&self, stacked_ch: Option<u16>) -> &[S]; // Provided methods fn channel_slice(&self, channel: u16) -> Option<&[S]> { ... } fn frame_slice(&self, frame: usize) -> Option<&[S]> { ... }
}
Expand description

Core trait for audio data access operations across various memory layouts.

AudioBlock provides a unified interface for interacting with audio data regardless of its underlying memory representation (BlockLayout::Interleaved, BlockLayout::Sequential, or BlockLayout::Stacked). It supports operations on both owned audio blocks and temporary views.

§Usage

This trait gives you multiple ways to access audio data:

  • Direct sample access via indices
  • Channel and frame iterators for processing data streams
  • Raw data access for optimized operations
  • Layout information for specialized handling

§Example

use audio_blocks::AudioBlock;

fn example(audio: &impl AudioBlock<f32>) {
    // Get number of channels and frames
    let channels = audio.num_channels();
    let frames = audio.num_frames();

    // Access individual samples
    let first_sample = audio.sample(0, 0);

    // Process one channel
    for sample in audio.channel(0) {
        // work with each sample
    }

    // Process all channels
    for channel in audio.channels() {
        for sample in channel {
            // Apply processing to each sample
        }
    }
}

Required Methods§

Source

fn num_channels(&self) -> u16

Returns the number of active audio channels.

Source

fn num_frames(&self) -> usize

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

Source

fn num_channels_allocated(&self) -> u16

Returns the total number of channels allocated in memory.

This may be greater than num_channels() if the buffer has reserved capacity.

Source

fn num_frames_allocated(&self) -> usize

Returns the total number of frames allocated in memory.

This may be greater than num_frames() if the buffer has reserved capacity.

Source

fn layout(&self) -> BlockLayout

Returns the memory layout of this audio block (interleaved, sequential, or stacked).

Source

fn sample(&self, channel: u16, frame: usize) -> S

Returns the sample value at the specified channel and frame position.

§Panics

Panics if channel or frame indices are out of bounds.

Source

fn channel(&self, channel: u16) -> impl Iterator<Item = &S>

Returns an iterator over all samples in the specified channel.

§Panics

Panics if channel index is out of bounds.

Source

fn channels(&self) -> impl Iterator<Item = impl Iterator<Item = &S> + '_> + '_

Returns an iterator that yields iterators for each channel.

Source

fn frame(&self, frame: usize) -> impl Iterator<Item = &S>

Returns an iterator over all samples in the specified frame (across all channels).

§Panics

Panics if frame index is out of bounds.

Source

fn frames(&self) -> impl Iterator<Item = impl Iterator<Item = &S> + '_> + '_

Returns an iterator that yields iterators for each frame.

Source

fn view(&self) -> impl AudioBlock<S>

Creates a non-owning view of this audio block.

This operation is zero-cost (no allocation or copying) and real-time safe, as it returns a lightweight wrapper around the original data.

Source

fn raw_data(&self, stacked_ch: Option<u16>) -> &[S]

Provides direct access to the underlying memory as a slice.

§Safety

This function gives access to all allocated data, including any reserved capacity beyond the active range, but it is safe in terms of memory safety.

§Parameters
  • stacked_ch - For Layout::Stacked, specifies which channel to access (required). For other layouts, this parameter is ignored.
§Returns

A slice containing all allocated data. The data format follows the block’s layout:

  • For Interleaved: returns interleaved samples across all allocated channels
  • For Sequential: returns planar data with all allocated channels
  • For Stacked: returns data for the specified channel only

Provided Methods§

Source

fn channel_slice(&self, channel: u16) -> Option<&[S]>

Returns a slice of the data in case of sequential or stacked layout.

Source

fn frame_slice(&self, frame: usize) -> Option<&[S]>

Returns a slice of the data in case of interleaved memory layout.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<S: Sample> AudioBlock<S> for Interleaved<S>

Source§

impl<S: Sample> AudioBlock<S> for InterleavedView<'_, S>

Source§

impl<S: Sample> AudioBlock<S> for InterleavedViewMut<'_, S>

Source§

impl<S: Sample> AudioBlock<S> for Sequential<S>

Source§

impl<S: Sample> AudioBlock<S> for SequentialView<'_, S>

Source§

impl<S: Sample> AudioBlock<S> for SequentialViewMut<'_, S>

Source§

impl<S: Sample> AudioBlock<S> for Stacked<S>

Source§

impl<S: Sample, V: AsMut<[S]> + AsRef<[S]>> AudioBlock<S> for StackedViewMut<'_, S, V>

Source§

impl<S: Sample, V: AsRef<[S]>> AudioBlock<S> for StackedView<'_, S, V>