pub trait AudioBuffer {
    type SampleType;

    // Required methods
    fn num_channels(&self) -> usize;
    fn num_samples(&self) -> usize;
    fn slice(&self) -> &[Self::SampleType];
    fn slice_mut(&mut self) -> &mut [Self::SampleType];
    fn get(&self, channel: usize, sample: usize) -> &Self::SampleType;
    fn get_mut(
        &mut self,
        channel: usize,
        sample: usize
    ) -> &mut Self::SampleType;
    fn set(&mut self, channel: usize, sample: usize, value: Self::SampleType);

    // Provided methods
    fn frames(&self) -> Chunks<'_, Self::SampleType> { ... }
    fn frames_mut(&mut self) -> ChunksMut<'_, Self::SampleType> { ... }
    unsafe fn get_unchecked(
        &self,
        channel: usize,
        sample: usize
    ) -> &Self::SampleType { ... }
    unsafe fn get_unchecked_mut(
        &mut self,
        channel: usize,
        sample: usize
    ) -> &mut Self::SampleType { ... }
    unsafe fn set_unchecked(
        &mut self,
        channel: usize,
        sample: usize,
        value: Self::SampleType
    ) { ... }
}
Expand description

Represents an audio buffer. This decouples audio processing code from a certain representation of multi-channel sample buffers.

This crate provides implementations of this trait for CPal style buffers, which use interleaved internal representation.

When processing samples, it’ll be more efficient to use .slice and .slice_mut than .get / .set methods. For the VST buffer, these methods will not work.

It’s recommended to convert the buffer into interleaved layout before processing as that will be around as expensive as the overhead of get/set methods on a single loop through samples.

(due to bounds checking and other compiler optimisations that fail with them)

Required Associated Types§

source

type SampleType

The type of samples within this buffer.

Required Methods§

source

fn num_channels(&self) -> usize

The number of channels in this buffer

source

fn num_samples(&self) -> usize

The number of samples in this buffer

source

fn slice(&self) -> &[Self::SampleType]

Get a slice to the internal data. Will not work with VST adapter

This is the faster way to process

source

fn slice_mut(&mut self) -> &mut [Self::SampleType]

Get a mutable slice to the internal data. Will not work with VST adapter

This is the faster way to process

source

fn get(&self, channel: usize, sample: usize) -> &Self::SampleType

Get a ref to an INPUT sample in this buffer.

Calling this on a loop will be ~20x slower than reading from slice.

source

fn get_mut(&mut self, channel: usize, sample: usize) -> &mut Self::SampleType

Get a mutable ref to an OUTPUT sample in this buffer

On some implementations this may yield a different value than .get.

Calling this on a loop will be ~20x slower than reading from slice.

source

fn set(&mut self, channel: usize, sample: usize, value: Self::SampleType)

Set an OUTPUT sample in this buffer

Provided Methods§

source

fn frames(&self) -> Chunks<'_, Self::SampleType>

Shortcut for .slice().chunks(num_channels)

source

fn frames_mut(&mut self) -> ChunksMut<'_, Self::SampleType>

Shortcut for .slice_mut().chunks_mut(num_channels)

This is a frame representing a sample in time, for all channels.

source

unsafe fn get_unchecked( &self, channel: usize, sample: usize ) -> &Self::SampleType

Unsafe, no bounds check - Get a ref to an INPUT sample in this buffer

Calling this on a loop will be ~10x slower than reading from slice.

Safety

This performs no bounds checks. Make sure indexes are in range.

source

unsafe fn get_unchecked_mut( &mut self, channel: usize, sample: usize ) -> &mut Self::SampleType

Unsafe, no bounds check - Get a mutable ref to an OUTPUT sample in this buffer

On some implementations this may yield a different value than .get.

Calling this on a loop will be ~10x slower than reading from slice.

Safety

This performs no bounds checks. Make sure indexes are in range.

source

unsafe fn set_unchecked( &mut self, channel: usize, sample: usize, value: Self::SampleType )

Unsafe, no bounds check - Set an OUTPUT sample in this buffer

Calling this on a loop will be ~10x slower than reading from slice.

Safety

This performs no bounds checks. Make sure indexes are in range.

Implementors§

source§

impl<'a, SampleType> AudioBuffer for InterleavedAudioBuffer<'a, SampleType>

§

type SampleType = SampleType

source§

impl<SampleType> AudioBuffer for VecAudioBuffer<SampleType>

§

type SampleType = SampleType