AudioBlockMut

Trait AudioBlockMut 

Source
pub trait AudioBlockMut<S: Sample>: AudioBlock<S> {
    type PlanarViewMut: AsRef<[S]> + AsMut<[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_iter_mut(&mut self, channel: u16) -> impl Iterator<Item = &mut S>;
    fn channels_iter_mut(
        &mut self,
    ) -> impl Iterator<Item = impl Iterator<Item = &mut S> + '_> + '_;
    fn frame_iter_mut(&mut self, frame: usize) -> impl Iterator<Item = &mut S>;
    fn frames_iter_mut(
        &mut self,
    ) -> impl Iterator<Item = impl Iterator<Item = &mut S> + '_> + '_;
    fn as_view_mut(&mut self) -> impl AudioBlockMut<S>;

    // Provided methods
    fn set_active_size(&mut self, num_channels: u16, num_frames: usize) { ... }
    fn as_interleaved_view_mut(
        &mut self,
    ) -> Option<AudioBlockInterleavedViewMut<'_, S>> { ... }
    fn as_planar_view_mut(
        &mut self,
    ) -> Option<AudioBlockPlanarViewMut<'_, S, Self::PlanarViewMut>> { ... }
    fn as_sequential_view_mut(
        &mut self,
    ) -> Option<AudioBlockSequentialViewMut<'_, 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_iter_mut(0) {
        *sample *= 0.8; // Apply gain reduction
    }

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

Required Associated Types§

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_iter_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_iter_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_iter_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_iter_mut( &mut self, ) -> impl Iterator<Item = impl Iterator<Item = &mut S> + '_> + '_

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

Source

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

Creates a non-owning mutable 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 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 as_interleaved_view_mut( &mut self, ) -> Option<AudioBlockInterleavedViewMut<'_, 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_mut( &mut self, ) -> Option<AudioBlockPlanarViewMut<'_, S, Self::PlanarViewMut>>

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_mut( &mut self, ) -> Option<AudioBlockSequentialViewMut<'_, 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§