AudioSampleIterators

Trait AudioSampleIterators 

Source
pub trait AudioSampleIterators<'a, T: AudioSample> {
    // Required methods
    fn frames(&'a self) -> FrameIterator<'a, T> ;
    fn channels<'iter>(&'iter self) -> ChannelIterator<'iter, 'a, T> ;
    fn windows(
        &'a self,
        window_size: usize,
        hop_size: usize,
    ) -> WindowIterator<'a, T> ;
    fn frames_mut(&'a mut self) -> FrameIteratorMut<'a, T> ;
    fn channels_mut(&'a mut self) -> ChannelIteratorMut<'a, T> ;
    fn windows_mut(
        &'a mut self,
        window_size: usize,
        hop_size: usize,
    ) -> WindowIteratorMut<'a, T> ;
}
Expand description

Extension trait providing iterator methods for AudioSamples.

Required Methods§

Source

fn frames(&'a self) -> FrameIterator<'a, T>

Returns an iterator over frames (one sample from each channel).

For mono audio, each frame contains one sample. For multi-channel audio, each frame contains one sample from each channel.

§Inputs
  • &self
§Returns

An iterator yielding an AudioSamples view for each frame. Each yielded item contains exactly one sample per channel.

§Errors

This iterator does not report errors. Internally it relies on valid in-bounds slicing.

§Panics

Does not panic.

§Examples
let audio = AudioSamples::new_multi_channel(
    array![[1.0f32, 2.0], [3.0, 4.0]],
    44100
);

let frames: Vec<Vec<f32>> = audio.frames().map(|f| f.to_interleaved_vec()).collect();
assert_eq!(frames, vec![vec![1.0, 3.0], vec![2.0, 4.0]]);
Source

fn channels<'iter>(&'iter self) -> ChannelIterator<'iter, 'a, T>

Returns an iterator over complete channels.

Each iteration yields all samples from one channel before moving to the next.

§Inputs
  • &self
§Returns

An iterator yielding owned AudioSamples values, one per channel.

§Errors

This iterator does not report errors. Internally it relies on valid in-bounds slicing.

§Panics

Does not panic.

§Examples
let audio = AudioSamples::new_multi_channel(
    array![[1.0f32, 2.0, 3.0], [4.0, 5.0, 6.0]],
    44100
);

let channels: Vec<Vec<f32>> = audio.channels().map(|c| c.to_interleaved_vec()).collect();
assert_eq!(channels[0], vec![1.0, 2.0, 3.0]);
assert_eq!(channels[1], vec![4.0, 5.0, 6.0]);
Source

fn windows( &'a self, window_size: usize, hop_size: usize, ) -> WindowIterator<'a, T>

Returns an iterator over overlapping windows of audio data.

§Inputs
  • window_size: size of each window in samples (per channel)
  • hop_size: number of samples to advance between windows
§Returns

An iterator yielding owned AudioSamples windows.

§Padding

The default padding mode is PaddingMode::Zero. Use WindowIterator::with_padding_mode to change it.

§Panics

Does not panic. If window_size == 0 or hop_size == 0, the iterator yields no items.

§Examples
let audio = AudioSamples::new_mono(array![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], 44100);

let windows: Vec<Vec<f32>> = audio
    .windows(4, 2)
    .map(|w| w.to_interleaved_vec())
    .collect();
assert_eq!(windows, vec![
    vec![1.0, 2.0, 3.0, 4.0],
    vec![3.0, 4.0, 5.0, 6.0],
    vec![5.0, 6.0, 0.0, 0.0], // zero-padded tail
]);
Source

fn frames_mut(&'a mut self) -> FrameIteratorMut<'a, T>

Returns a mutable iterator over frames (one sample from each channel).

This allows in-place modification of frames. Each iteration yields mutable references to samples that can be modified directly.

§Inputs
  • &mut self
§Returns

A mutable iterator yielding one frame at a time.

For multi-channel audio, each yielded FrameMut stores per-channel pointers and may allocate a small Vec of pointers per frame.

§Panics

Panics if the underlying multi-channel storage is not contiguous in memory.

§Examples
let mut audio = AudioSamples::new_multi_channel(
    array![[1.0f32, 2.0], [3.0, 4.0]],
    44100
);

// Apply gain to each frame
for frame in audio.frames_mut() {
    frame.apply(|s| s * 0.5);
}
assert_eq!(audio.to_interleaved_vec(), vec![0.5, 1.5, 1.0, 2.0]);
Source

fn channels_mut(&'a mut self) -> ChannelIteratorMut<'a, T>

Returns a mutable iterator over complete channels.

Each iteration yields mutable references to all samples in one channel, allowing for efficient channel-wise processing.

§Inputs
  • &mut self
§Returns

A mutable iterator yielding one mutable slice per channel.

§Panics

Panics if the underlying multi-channel storage is not contiguous in memory.

§Examples
let mut audio = AudioSamples::new_multi_channel(
    array![[1.0f32, 2.0, 3.0], [4.0, 5.0, 6.0]],
    44100
);

// Apply different processing to each channel
for (ch, channel) in audio.channels_mut().enumerate() {
    let gain = if ch == 0 { 0.8 } else { 0.6 }; // Left/Right balance
    for sample in channel {
        *sample *= gain;
    }
}
Source

fn windows_mut( &'a mut self, window_size: usize, hop_size: usize, ) -> WindowIteratorMut<'a, T>

Returns a mutable iterator over non-overlapping windows of audio data.

This allows in-place modification of windows. For soundness, this iterator cannot yield overlapping mutable windows.

§Inputs
  • window_size: size of each window in samples (per channel)
  • hop_size: number of samples to advance between windows
§Returns

A mutable iterator yielding one window at a time.

§Panics
  • Panics if hop_size < window_size and more than one window would be yielded (overlap would create aliasing mutable windows).
  • Panics if the underlying multi-channel storage is not contiguous in memory.

If window_size == 0 or hop_size == 0, the iterator yields no items.

§Examples
let mut audio = AudioSamples::new_mono(array![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], 44100);

// In-place non-overlapping window processing
for mut window in audio.windows_mut(4, 4) {
    window.apply_window_function(|i, n| {
        let pi = std::f32::consts::PI;
        // Hann window
        0.5 * (1.0 - (2.0 * pi * i as f32 / (n - 1) as f32).cos())
    });
}

Implementors§