pub trait AudioBuffer {
type SampleType;
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);
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
type SampleType
type SampleType
The type of samples within this buffer.
Required Methods
fn num_channels(&self) -> usize
fn num_channels(&self) -> usize
The number of channels in this buffer
fn num_samples(&self) -> usize
fn num_samples(&self) -> usize
The number of samples in this buffer
fn slice(&self) -> &[Self::SampleType]
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
fn slice_mut(&mut self) -> &mut [Self::SampleType]
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
fn get(&self, channel: usize, sample: usize) -> &Self::SampleType
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
.
fn get_mut(&mut self, channel: usize, sample: usize) -> &mut Self::SampleType
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
.
fn set(&mut self, channel: usize, sample: usize, value: Self::SampleType)
fn set(&mut self, channel: usize, sample: usize, value: Self::SampleType)
Set an OUTPUT sample in this buffer
Provided Methods
fn frames(&self) -> Chunks<'_, Self::SampleType>
fn frames(&self) -> Chunks<'_, Self::SampleType>
Shortcut for .slice().chunks(num_channels)
fn frames_mut(&mut self) -> ChunksMut<'_, Self::SampleType>
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.
unsafe fn get_unchecked(
&self,
channel: usize,
sample: usize
) -> &Self::SampleType
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.
unsafe fn get_unchecked_mut(
&mut self,
channel: usize,
sample: usize
) -> &mut Self::SampleType
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.
unsafe fn set_unchecked(
&mut self,
channel: usize,
sample: usize,
value: Self::SampleType
)
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.