2022-03-04 09:34:06 +11:00
|
|
|
//! Adapters and utilities for working with audio buffers.
|
|
|
|
|
2022-02-13 11:47:26 +11:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2022-03-11 04:15:55 +11:00
|
|
|
mod blocks;
|
|
|
|
mod samples;
|
|
|
|
|
|
|
|
pub use blocks::{Block, BlockChannelsIter, BlocksIter};
|
|
|
|
pub use samples::{ChannelSamples, ChannelSamplesIter, SamplesIter};
|
2022-03-02 07:39:53 +11:00
|
|
|
|
2022-02-03 01:01:41 +11:00
|
|
|
/// The audio buffers used during processing. This contains the output audio output buffers with the
|
|
|
|
/// inputs already copied to the outputs. You can either use the iterator adapters to conveniently
|
|
|
|
/// and efficiently iterate over the samples, or you can do your own thing using the raw audio
|
|
|
|
/// buffers.
|
2022-03-06 12:10:33 +11:00
|
|
|
///
|
|
|
|
/// TODO: This lifetime makes zero sense because you're going to need unsafe lifetime casts to use
|
|
|
|
/// this either way. Maybe just get rid of it in favor for raw pointers.
|
2022-02-03 06:37:06 +11:00
|
|
|
#[derive(Default)]
|
2022-02-03 01:01:41 +11:00
|
|
|
pub struct Buffer<'a> {
|
|
|
|
/// Contains slices for the plugin's outputs. You can't directly create a nested slice form
|
|
|
|
/// apointer to pointers, so this needs to be preallocated in the setup call and kept around
|
|
|
|
/// between process calls. And because storing a reference to this means a) that you need a lot
|
|
|
|
/// of lifetime annotations everywhere and b) that at some point you need unsound lifetime casts
|
|
|
|
/// because this `Buffers` either cannot have the same lifetime as the separately stored output
|
|
|
|
/// buffers, and it also cannot be stored in a field next to it because that would mean
|
|
|
|
/// containing mutable references to data stored in a mutex.
|
|
|
|
output_slices: Vec<&'a mut [f32]>,
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
impl<'a> Buffer<'a> {
|
2022-04-27 03:39:03 +10:00
|
|
|
/// Returns the numer of samples in this buffer.
|
2022-03-07 08:26:37 +11:00
|
|
|
#[inline]
|
2022-03-06 11:12:28 +11:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
if self.output_slices.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
self.output_slices[0].len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 03:39:03 +10:00
|
|
|
/// Returns the numer of channels in this buffer.
|
2022-03-10 01:02:38 +11:00
|
|
|
#[inline]
|
2022-03-06 11:21:00 +11:00
|
|
|
pub fn channels(&self) -> usize {
|
|
|
|
self.output_slices.len()
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
/// Returns true if this buffer does not contain any samples.
|
2022-03-10 01:02:38 +11:00
|
|
|
#[inline]
|
2022-03-01 14:20:59 +11:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.output_slices.is_empty() || self.output_slices[0].is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtain the raw audio buffers.
|
2022-03-10 01:02:38 +11:00
|
|
|
#[inline]
|
2022-03-01 14:20:59 +11:00
|
|
|
pub fn as_slice(&mut self) -> &mut [&'a mut [f32]] {
|
|
|
|
&mut self.output_slices
|
|
|
|
}
|
|
|
|
|
2022-03-06 12:07:23 +11:00
|
|
|
/// The same as [`as_slice()`][Self::as_slice()], but for a non-mutable reference. This is
|
|
|
|
/// usually not needed.
|
2022-03-10 01:02:38 +11:00
|
|
|
#[inline]
|
2022-03-06 12:07:23 +11:00
|
|
|
pub fn as_slice_immutable(&self) -> &[&'a mut [f32]] {
|
|
|
|
&self.output_slices
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
/// Iterate over the samples, returning a channel iterator for each sample.
|
2022-03-10 05:45:42 +11:00
|
|
|
#[inline]
|
2022-03-10 06:11:37 +11:00
|
|
|
pub fn iter_samples<'slice>(&'slice mut self) -> SamplesIter<'slice, 'a> {
|
2022-03-01 14:20:59 +11:00
|
|
|
SamplesIter {
|
|
|
|
buffers: self.output_slices.as_mut_slice(),
|
|
|
|
current_sample: 0,
|
2022-03-10 05:27:08 +11:00
|
|
|
samples_end: self.len(),
|
2022-03-01 14:20:59 +11:00
|
|
|
_marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 02:03:13 +11:00
|
|
|
/// Iterate over the buffer in blocks with the specified maximum size. The ideal maximum block
|
|
|
|
/// size depends on the plugin in question, but 64 or 128 samples works for most plugins. Since
|
|
|
|
/// the buffer's total size may not be cleanly divisble by the maximum size, the returned
|
|
|
|
/// buffers may have any size in `[1, max_block_size]`. This is useful when using algorithms
|
|
|
|
/// that work on entire blocks of audio, like those that would otherwise need to perform
|
|
|
|
/// expensive per-sample branching or that can use per-sample SIMD as opposed to per-channel
|
|
|
|
/// SIMD.
|
|
|
|
///
|
|
|
|
/// The parameter smoothers can also produce smoothed values for an entire block using
|
2022-07-06 22:26:43 +10:00
|
|
|
/// [`Smoother::next_block()`][crate::prelude::Smoother::next_block()].
|
2022-03-02 02:16:13 +11:00
|
|
|
///
|
2022-03-04 09:05:01 +11:00
|
|
|
/// You can use this to obtain block-slices from a buffer so you can pass them to a library:
|
2022-03-02 02:16:13 +11:00
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// for block in buffer.iter_blocks(128) {
|
|
|
|
/// let mut block_channels = block.into_iter();
|
|
|
|
/// let stereo_slice = &[
|
|
|
|
/// block_channels.next().unwrap(),
|
|
|
|
/// block_channels.next().unwrap(),
|
|
|
|
/// ];
|
|
|
|
///
|
|
|
|
/// // Do something cool with `stereo_slice`
|
|
|
|
/// }
|
|
|
|
/// ````
|
2022-03-10 05:45:42 +11:00
|
|
|
#[inline]
|
2022-03-02 02:03:13 +11:00
|
|
|
pub fn iter_blocks<'slice>(&'slice mut self, max_block_size: usize) -> BlocksIter<'slice, 'a> {
|
|
|
|
BlocksIter {
|
|
|
|
buffers: self.output_slices.as_mut_slice(),
|
|
|
|
max_block_size,
|
|
|
|
current_block_start: 0,
|
|
|
|
_marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the raw output slice vector. This needs to be resized to match the number of output
|
2022-03-01 14:20:59 +11:00
|
|
|
/// channels during the plugin's initialization. Then during audio processing, these slices
|
|
|
|
/// should be updated to point to the plugin's audio buffers.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The stored slices must point to live data when this object is passed to the plugins' process
|
|
|
|
/// function. The rest of this object also assumes all channel lengths are equal. Panics will
|
|
|
|
/// likely occur if this is not the case.
|
2022-03-02 01:00:32 +11:00
|
|
|
pub unsafe fn with_raw_vec(&mut self, update: impl FnOnce(&mut Vec<&'a mut [f32]>)) {
|
|
|
|
update(&mut self.output_slices);
|
2022-03-01 14:20:59 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 03:43:04 +11:00
|
|
|
#[cfg(any(miri, test))]
|
2022-02-13 12:20:31 +11:00
|
|
|
mod miri {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn repeated_access() {
|
|
|
|
let mut real_buffers = vec![vec![0.0; 512]; 2];
|
|
|
|
let mut buffer = Buffer::default();
|
2022-03-02 01:00:32 +11:00
|
|
|
unsafe {
|
|
|
|
buffer.with_raw_vec(|output_slices| {
|
|
|
|
let (first_channel, other_channels) = real_buffers.split_at_mut(1);
|
|
|
|
*output_slices = vec![&mut first_channel[0], &mut other_channels[0]];
|
|
|
|
})
|
|
|
|
};
|
2022-02-13 12:20:31 +11:00
|
|
|
|
2022-03-10 06:11:37 +11:00
|
|
|
for samples in buffer.iter_samples() {
|
2022-02-13 12:20:31 +11:00
|
|
|
for sample in samples {
|
|
|
|
*sample += 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 06:11:37 +11:00
|
|
|
for mut samples in buffer.iter_samples() {
|
2022-02-14 01:54:19 +11:00
|
|
|
for _ in 0..2 {
|
2022-02-13 12:20:31 +11:00
|
|
|
for sample in samples.iter_mut() {
|
|
|
|
*sample += 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 01:54:19 +11:00
|
|
|
assert_eq!(real_buffers[0][0], 0.003);
|
2022-02-13 12:20:31 +11:00
|
|
|
}
|
2022-03-02 02:03:13 +11:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn repeated_slices() {
|
|
|
|
let mut real_buffers = vec![vec![0.0; 512]; 2];
|
|
|
|
let mut buffer = Buffer::default();
|
|
|
|
unsafe {
|
|
|
|
buffer.with_raw_vec(|output_slices| {
|
|
|
|
let (first_channel, other_channels) = real_buffers.split_at_mut(1);
|
|
|
|
*output_slices = vec![&mut first_channel[0], &mut other_channels[0]];
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// These iterators should not alias
|
|
|
|
let mut blocks = buffer.iter_blocks(16);
|
2022-03-02 03:46:34 +11:00
|
|
|
let (_block1_offset, block1) = blocks.next().unwrap();
|
|
|
|
let (_block2_offset, block2) = blocks.next().unwrap();
|
2022-03-02 02:03:13 +11:00
|
|
|
for channel in block1 {
|
|
|
|
for sample in channel.iter_mut() {
|
|
|
|
*sample += 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for channel in block2 {
|
|
|
|
for sample in channel.iter_mut() {
|
|
|
|
*sample += 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..32 {
|
|
|
|
assert_eq!(real_buffers[0][i], 0.001);
|
|
|
|
}
|
|
|
|
for i in 32..48 {
|
|
|
|
assert_eq!(real_buffers[0][i], 0.0);
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 12:20:31 +11:00
|
|
|
}
|