Skip to main content

AudioSampleIterators

Trait AudioSampleIterators 

Source
pub trait AudioSampleIterators<'a, T>
where T: StandardSample,
{ // Required methods fn frames<'iter>(&'iter self) -> FrameIterator<'iter, 'a, T> where 'a: 'iter; fn channels<'iter>(&'iter self) -> ChannelIterator<'iter, 'a, T> ; fn windows<'iter>( &'iter self, window_size: usize, hop_size: usize, ) -> WindowIterator<'iter, 'a, T> where 'a: 'iter; fn windows_ref<'iter>( &'iter self, window_size: NonZeroUsize, hop_size: NonZeroUsize, ) -> WindowRefIterator<'iter, 'a, T> where 'a: 'iter; }
Expand description

Extension trait providing iterator methods for AudioSamples.

Required Methods§

Source

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

Returns an iterator over frames, where each frame is a snapshot of one sample from each channel at the same point in time.

For mono audio, each frame contains exactly one sample. For multi-channel audio, each frame contains one sample per channel, preserving channel alignment across time.

§Returns

A FrameIterator that yields one AudioSamples view per time index. The total number of frames equals self.samples_per_channel().

§Panics

Does not panic.

§Examples
use audio_samples::{AudioSamples, sample_rate, iterators::AudioSampleIterators};
use ndarray::array;

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

// Each frame has one sample per channel; two time steps → two frames.
let mut count = 0;
for frame in audio.frames() {
    assert_eq!(frame.num_channels().get(), 2);
    count += 1;
}
assert_eq!(count, 2);
Source

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

Returns an iterator over complete channels.

Each iteration yields the full temporal sequence of samples belonging to one channel. Channels are yielded in increasing channel-index order.

§Returns

A ChannelIterator that yields one owned AudioSamples per channel. The total number of items equals self.num_channels().

§Panics

Does not panic.

§Examples
use audio_samples::{AudioSamples, sample_rate, iterators::AudioSampleIterators};
use ndarray::array;

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

let channels: Vec<_> = audio.channels().collect();
assert_eq!(channels.len(), 2);
assert_eq!(channels[0].samples_per_channel().get(), 3);
Source

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

Returns an iterator over fixed-size, optionally overlapping windows.

Each window covers window_size samples per channel. Successive windows start hop_size samples apart, so windows overlap when hop_size < window_size.

The default boundary strategy is PaddingMode::Zero. Call WindowIterator::with_padding_mode on the returned iterator to change it.

§Arguments

window_size — number of samples per channel in each window. If zero, no windows are yielded. – hop_size — number of samples to advance between window starts. If zero, no windows are yielded.

§Returns

A WindowIterator that yields one owned AudioSamples per window.

§Panics

Does not panic.

§Examples

See AudioSamples::windows for a runnable usage example.

// Conceptual usage via the trait interface (usize arguments):
let windows: Vec<_> = audio.windows(3_usize, 3_usize).collect();
Source

fn windows_ref<'iter>( &'iter self, window_size: NonZeroUsize, hop_size: NonZeroUsize, ) -> WindowRefIterator<'iter, 'a, T>
where 'a: 'iter,

Returns a zero-copy, borrowing iterator over fully-contained windows.

Unlike windows, which yields an owned AudioSamples per window (re-copying overlapping data), this iterator yields a WindowView that borrows directly into the underlying buffer. No allocation or copying occurs per window, making it well suited to the STFT use case where windows overlap heavily.

Only windows that lie fully within the signal are yielded — equivalent to PaddingMode::Skip. Padded or partial trailing windows require the owning windows.

§Arguments

window_size — number of samples per channel in each window. – hop_size — number of samples to advance between window starts.

§Returns

A WindowRefIterator yielding one borrowing WindowView per window. If window_size > samples_per_channel, the iterator yields zero windows.

§Panics

Does not panic.

§Examples

See AudioSamples::windows_ref for a runnable example.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§