2022-02-13 11:47:26 +11:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
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-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
|
|
|
/// An iterator over all samples in the buffer, yielding iterators over each channel for every
|
|
|
|
/// sample. This iteration order offers good cache locality for per-sample access.
|
|
|
|
pub struct SamplesIter<'slice, 'sample: 'slice> {
|
|
|
|
/// The raw output buffers.
|
|
|
|
pub(self) buffers: *mut [&'sample mut [f32]],
|
|
|
|
pub(self) current_sample: usize,
|
|
|
|
pub(self) _marker: PhantomData<&'slice mut [&'sample mut [f32]]>,
|
|
|
|
}
|
2022-02-03 01:01:41 +11:00
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
/// Can construct iterators over actual iterator over the channel data for a sample, yielded by
|
|
|
|
/// [Samples].
|
|
|
|
pub struct Channels<'slice, 'sample: 'slice> {
|
|
|
|
/// The raw output buffers.
|
|
|
|
pub(self) buffers: *mut [&'sample mut [f32]],
|
|
|
|
pub(self) current_sample: usize,
|
|
|
|
pub(self) _marker: PhantomData<&'slice mut [&'sample mut [f32]]>,
|
2022-02-03 01:01:41 +11:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
/// The actual iterator over the channel data for a sample, yielded by [Channels].
|
|
|
|
pub struct ChannelsIter<'slice, 'sample: 'slice> {
|
2022-02-03 01:01:41 +11:00
|
|
|
/// The raw output buffers.
|
2022-02-13 11:48:56 +11:00
|
|
|
pub(self) buffers: *mut [&'sample mut [f32]],
|
2022-02-03 01:01:41 +11:00
|
|
|
pub(self) current_sample: usize,
|
2022-03-01 14:20:59 +11:00
|
|
|
pub(self) current_channel: usize,
|
2022-02-13 11:58:22 +11:00
|
|
|
pub(self) _marker: PhantomData<&'slice mut [&'sample mut [f32]]>,
|
2022-02-03 01:01:41 +11:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
impl<'slice, 'sample> Iterator for SamplesIter<'slice, 'sample> {
|
2022-02-13 11:58:22 +11:00
|
|
|
type Item = Channels<'slice, 'sample>;
|
2022-02-03 01:01:41 +11:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2022-02-13 11:16:52 +11:00
|
|
|
if self.current_sample < unsafe { (*self.buffers)[0].len() } {
|
2022-02-03 01:01:41 +11:00
|
|
|
let channels = Channels {
|
2022-02-13 10:59:25 +11:00
|
|
|
buffers: self.buffers,
|
2022-02-03 01:01:41 +11:00
|
|
|
current_sample: self.current_sample,
|
2022-02-13 11:47:26 +11:00
|
|
|
_marker: self._marker,
|
2022-02-03 01:01:41 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
self.current_sample += 1;
|
|
|
|
|
|
|
|
Some(channels)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 22:45:13 +11:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2022-02-13 10:59:25 +11:00
|
|
|
let remaining = unsafe { (*self.buffers)[0].len() } - self.current_sample;
|
2022-02-06 22:45:13 +11:00
|
|
|
(remaining, Some(remaining))
|
|
|
|
}
|
2022-02-03 01:01:41 +11:00
|
|
|
}
|
|
|
|
|
2022-02-13 11:58:22 +11:00
|
|
|
impl<'slice, 'sample> Iterator for ChannelsIter<'slice, 'sample> {
|
2022-02-13 11:48:56 +11:00
|
|
|
type Item = &'sample mut f32;
|
2022-02-03 01:01:41 +11:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2022-02-13 11:16:52 +11:00
|
|
|
if self.current_channel < unsafe { (*self.buffers).len() } {
|
2022-02-03 01:01:41 +11:00
|
|
|
// SAFETY: These bounds have already been checked
|
2022-02-13 11:58:22 +11:00
|
|
|
// SAFETY: It is also not possible to have multiple mutable references to the same
|
|
|
|
// sample at the same time
|
2022-02-03 01:01:41 +11:00
|
|
|
let sample = unsafe {
|
2022-02-13 10:59:25 +11:00
|
|
|
(*self.buffers)
|
2022-02-03 01:01:41 +11:00
|
|
|
.get_unchecked_mut(self.current_channel)
|
|
|
|
.get_unchecked_mut(self.current_sample)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.current_channel += 1;
|
|
|
|
|
|
|
|
Some(sample)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 22:45:13 +11:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2022-02-13 10:59:25 +11:00
|
|
|
let remaining = unsafe { (*self.buffers).len() } - self.current_channel;
|
2022-02-06 22:45:13 +11:00
|
|
|
(remaining, Some(remaining))
|
|
|
|
}
|
2022-02-03 01:01:41 +11:00
|
|
|
}
|
2022-02-06 22:45:13 +11:00
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
impl<'slice, 'sample> IntoIterator for Channels<'slice, 'sample> {
|
|
|
|
type Item = &'sample mut f32;
|
|
|
|
type IntoIter = ChannelsIter<'slice, 'sample>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
ChannelsIter {
|
|
|
|
buffers: self.buffers,
|
|
|
|
current_sample: self.current_sample,
|
|
|
|
current_channel: 0,
|
|
|
|
_marker: self._marker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExactSizeIterator for SamplesIter<'_, '_> {}
|
|
|
|
|
2022-02-13 11:47:26 +11:00
|
|
|
impl ExactSizeIterator for ChannelsIter<'_, '_> {}
|
2022-02-13 10:59:25 +11:00
|
|
|
|
2022-03-01 14:20:59 +11:00
|
|
|
impl<'a> Buffer<'a> {
|
|
|
|
/// Returns true if this buffer does not contain any samples.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.output_slices.is_empty() || self.output_slices[0].is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtain the raw audio buffers.
|
|
|
|
pub fn as_slice(&mut self) -> &mut [&'a mut [f32]] {
|
|
|
|
&mut self.output_slices
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterate over the samples, returning a channel iterator for each sample.
|
|
|
|
pub fn iter_mut<'slice>(&'slice mut self) -> SamplesIter<'slice, 'a> {
|
|
|
|
SamplesIter {
|
|
|
|
buffers: self.output_slices.as_mut_slice(),
|
|
|
|
current_sample: 0,
|
|
|
|
_marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the raw output slice vector. This neds to be resized to match the number of output
|
|
|
|
/// 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-02-13 11:58:22 +11:00
|
|
|
impl<'slice, 'sample> Channels<'slice, 'sample> {
|
2022-02-13 10:59:25 +11:00
|
|
|
/// Get the number of channels.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
unsafe { (*self.buffers).len() }
|
|
|
|
}
|
2022-02-13 05:24:41 +11:00
|
|
|
|
2022-02-13 09:28:15 +11:00
|
|
|
/// A resetting iterator. This lets you iterate over the same channels multiple times. Otherwise
|
|
|
|
/// you don't need to use this function as [Channels] already implements [Iterator].
|
2022-02-13 11:58:22 +11:00
|
|
|
pub fn iter_mut(&mut self) -> ChannelsIter<'slice, 'sample> {
|
2022-02-13 10:59:25 +11:00
|
|
|
ChannelsIter {
|
2022-02-13 11:58:22 +11:00
|
|
|
buffers: self.buffers,
|
2022-02-13 10:59:25 +11:00
|
|
|
current_sample: self.current_sample,
|
|
|
|
current_channel: 0,
|
2022-02-13 11:47:26 +11:00
|
|
|
_marker: PhantomData,
|
2022-02-13 10:59:25 +11:00
|
|
|
}
|
2022-02-13 09:16:07 +11:00
|
|
|
}
|
|
|
|
|
2022-02-13 05:24:41 +11:00
|
|
|
/// Access a sample by index. Useful when you would otehrwise iterate over this 'Channels'
|
|
|
|
/// iterator multiple times.
|
|
|
|
#[inline]
|
2022-02-13 06:28:58 +11:00
|
|
|
pub fn get_mut(&mut self, channel_index: usize) -> Option<&mut f32> {
|
2022-02-13 05:24:41 +11:00
|
|
|
// SAFETY: The channel bound has already been checked
|
|
|
|
unsafe {
|
2022-02-13 06:28:58 +11:00
|
|
|
Some(
|
2022-02-13 10:59:25 +11:00
|
|
|
(*self.buffers)
|
2022-02-13 06:28:58 +11:00
|
|
|
.get_mut(channel_index)?
|
|
|
|
.get_unchecked_mut(self.current_sample),
|
|
|
|
)
|
2022-02-13 05:24:41 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as [Self::get_mut], but without any bounds checking.
|
2022-02-13 06:28:58 +11:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// `channel_index` must be in the range `0..Self::len()`.
|
2022-02-13 05:24:41 +11:00
|
|
|
#[inline]
|
2022-02-13 06:28:58 +11:00
|
|
|
pub unsafe fn get_unchecked_mut(&mut self, channel_index: usize) -> &mut f32 {
|
2022-02-13 10:59:25 +11:00
|
|
|
(*self.buffers)
|
2022-02-13 06:28:58 +11:00
|
|
|
.get_unchecked_mut(channel_index)
|
|
|
|
.get_unchecked_mut(self.current_sample)
|
2022-02-13 05:24:41 +11:00
|
|
|
}
|
|
|
|
}
|
2022-02-13 12:20:31 +11:00
|
|
|
|
|
|
|
#[cfg(miri)]
|
|
|
|
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
|
|
|
|
|
|
|
for samples in buffer.iter_mut() {
|
|
|
|
for sample in samples {
|
|
|
|
*sample += 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for mut samples in buffer.iter_mut() {
|
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
|
|
|
}
|
|
|
|
}
|