Trait AudioBlockMut

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

    // Provided methods
    fn set_active_size(&mut self, num_channels: u16, num_frames: usize) { ... }
    fn channel_slice_mut(&mut self, channel: u16) -> Option<&mut [S]> { ... }
    fn frame_slice_mut(&mut self, frame: usize) -> Option<&mut [S]> { ... }
}
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.set_active_size(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 set_active_num_channels(&mut self, num_channels: u16)

Sets the active size of the audio block to the specified number of channels.

This operation is real-time safe but only works up to AudioBlock::num_channels_allocated.

§Panics

When num_channels exceeds AudioBlock::num_channels_allocated.

Source

fn set_active_num_frames(&mut self, num_frames: usize)

Sets the active size of the audio block to the specified number of frames.

§Panics

When num_frames exceeds AudioBlock::num_frames_allocated.

Source

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

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 S>

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 S> + '_> + '_

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

Source

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

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 S> + '_> + '_

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

Source

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

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 [S]

Provides direct mutable 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 BlockLayout::Stacked, specifies which channel to access (required). For other layouts, this parameter is ignored.
§Returns

A mutable 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 set_active_size(&mut self, num_channels: u16, num_frames: usize)

Sets the active size of the audio block to the specified number of channels and frames.

§Panics

When num_channels exceeds AudioBlock::num_channels_allocated or num_frames exceeds AudioBlock::num_frames_allocated.

Source

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

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

Source

fn frame_slice_mut(&mut self, frame: usize) -> Option<&mut [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§