AudioBlock

Trait AudioBlock 

Source
pub trait AudioBlock<S>
where S: Sample,
{ type PlanarView: AsRef<[S]>;
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_iter(&self, channel: u16) -> impl Iterator<Item = &S>; fn channels_iter(&self) -> impl Iterator<Item = impl Iterator<Item = &S>>; fn frame_iter(&self, frame: usize) -> impl Iterator<Item = &S>; fn frame_iters(&self) -> impl Iterator<Item = impl Iterator<Item = &S>>; fn as_view(&self) -> impl AudioBlock<S>; // Provided methods fn as_interleaved_view(&self) -> Option<AudioBlockInterleavedView<'_, S>> { ... } fn as_planar_view( &self, ) -> Option<AudioBlockPlanarView<'_, S, Self::PlanarView>> { ... } fn as_sequential_view(&self) -> Option<AudioBlockSequentialView<'_, 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::Planar). 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_iter(0) {
        // work with each sample
    }

    // Process all channels
    for channel in audio.channels_iter() {
        for sample in channel {
            // work with each sample
        }
    }
}

Required Associated Types§

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 planar).

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_iter(&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_iter(&self) -> impl Iterator<Item = impl Iterator<Item = &S>>

Returns an iterator that yields an iterator for each channel.

Source

fn frame_iter(&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 frame_iters(&self) -> impl Iterator<Item = impl Iterator<Item = &S>>

Returns an iterator that yields an iterator for each frame.

Source

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

Creates a non-owning view of this audio block.

This operation is real-time safe, as it returns a lightweight wrapper around the original data.

Provided Methods§

Source

fn as_interleaved_view(&self) -> Option<AudioBlockInterleavedView<'_, S>>

Attempts to downcast this generic audio block to a concrete interleaved view. This enables access to frame slices and the underlying raw data.

Returns Some if the underlying data is stored in interleaved format, otherwise returns None.

Source

fn as_planar_view( &self, ) -> Option<AudioBlockPlanarView<'_, S, Self::PlanarView>>

Attempts to downcast this generic audio block to a concrete planar view. This enables access to frame slices and the underlying raw data.

Returns Some if the underlying data is stored in planar format, otherwise returns None.

Source

fn as_sequential_view(&self) -> Option<AudioBlockSequentialView<'_, S>>

Attempts to downcast this generic audio block to a concrete sequential view. This enables access to frame slices and the underlying raw data.

Returns Some if the underlying data is stored in sequential format, otherwise returns None.

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§