Trait AudioBlockMut

Source
pub trait AudioBlockMut<T: Sample>: AudioBlock<T> {
    // Required methods
    fn resize(&mut self, num_channels: u16, num_frames: usize);
    fn sample_mut(&mut self, channel: u16, frame: usize) -> &mut T;
    fn channel_mut(&mut self, channel: u16) -> impl Iterator<Item = &mut T>;
    fn channels_mut(
        &mut self,
    ) -> impl Iterator<Item = impl Iterator<Item = &mut T> + '_> + '_;
    fn frame_mut(&mut self, frame: usize) -> impl Iterator<Item = &mut T>;
    fn frames_mut(
        &mut self,
    ) -> impl Iterator<Item = impl Iterator<Item = &mut T> + '_> + '_;
    fn view_mut(&mut self) -> impl AudioBlockMut<T>;
    fn raw_data_mut(&mut self, stacked_ch: Option<u16>) -> &mut [T];
}
Expand description

Extends the AudioBlock trait with mutable access operations.

AudioBlockMut provides methods for modifying audio data across different memory layouts. It enables in-place processing, buffer resizing, and direct mutable access to the underlying data.

§Usage

This trait gives you multiple ways to modify audio data:

  • Change individual samples at specific positions
  • Iterate through and modify channels or frames
  • Resize the buffer to accommodate different audio requirements
  • Access raw data for optimized processing

§Example

use audio_blocks::{AudioBlock, AudioBlockMut};

fn process_audio(audio: &mut impl AudioBlockMut<f32>) {
    // Resize to 2 channels, 1024 frames
    audio.resize(2, 1024);

    // Modify individual samples
    *audio.sample_mut(0, 0) = 0.5;

    // Process one channel with mutable access
    for sample in audio.channel_mut(0) {
        *sample *= 0.8; // Apply gain reduction
    }

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

Required Methods§

Source

fn resize(&mut self, num_channels: u16, num_frames: usize)

Resizes the audio block to the specified number of channels and frames.

This operation is real-time safe but only works up to AudioBlock::num_channels_allocated and AudioBlock::num_frames_allocated. Attempting to resize beyond the allocated capacity will have implementation-dependent behavior.

§Panics

This function may panic when attempting to resize beyond the allocated capacity (num_channels_allocated and num_frames_allocated).

Source

fn sample_mut(&mut self, channel: u16, frame: usize) -> &mut T

Returns a mutable reference to the sample at the specified channel and frame position.

§Panics

Panics if channel or frame indices are out of bounds.

Source

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

Returns a mutable iterator over all samples in the specified channel.

§Panics

Panics if channel index is out of bounds.

Source

fn channels_mut( &mut self, ) -> impl Iterator<Item = impl Iterator<Item = &mut T> + '_> + '_

Returns a mutable iterator that yields mutable iterators for each channel.

Source

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

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

§Panics

Panics if frame index is out of bounds.

Source

fn frames_mut( &mut self, ) -> impl Iterator<Item = impl Iterator<Item = &mut T> + '_> + '_

Returns a mutable iterator that yields mutable iterators for each frame.

Source

fn view_mut(&mut self) -> impl AudioBlockMut<T>

Creates a non-owning mutable 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_mut(&mut self, stacked_ch: Option<u16>) -> &mut [T]

Provides direct mutable access to the underlying memory as a slice.

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

A mutable slice containing all allocated data, including any reserved capacity beyond the visible/active range. The data format follows the block’s layout:

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

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§