From a58c796fc0fd27c4fe9bd1f099adaff8acef0859 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 1 Mar 2022 15:00:32 +0100 Subject: [PATCH] Add an output slices field to Buffer --- src/buffer.rs | 15 ++++++++------- src/wrapper/vst3/wrapper.rs | 11 +++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index eff7554c..4085ab05 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -143,8 +143,8 @@ impl<'a> Buffer<'a> { /// 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. - pub unsafe fn as_raw_vec(&mut self) -> &mut Vec<&'a mut [f32]> { - &mut self.output_slices + pub unsafe fn with_raw_vec(&mut self, update: impl FnOnce(&mut Vec<&'a mut [f32]>)) { + update(&mut self.output_slices); } } @@ -200,11 +200,12 @@ mod miri { fn repeated_access() { let mut real_buffers = vec![vec![0.0; 512]; 2]; let mut buffer = Buffer::default(); - { - let slices = unsafe { buffer.as_raw_vec() }; - let (first_channel, other_channels) = real_buffers.split_at_mut(1); - *slices = vec![&mut first_channel[0], &mut other_channels[0]]; - } + 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]]; + }) + }; for samples in buffer.iter_mut() { for sample in samples { diff --git a/src/wrapper/vst3/wrapper.rs b/src/wrapper/vst3/wrapper.rs index a4e25320..49b52cb7 100644 --- a/src/wrapper/vst3/wrapper.rs +++ b/src/wrapper/vst3/wrapper.rs @@ -19,7 +19,6 @@ use super::inner::WrapperInner; use super::util::{VstPtr, BYPASS_PARAM_HASH, BYPASS_PARAM_ID}; use super::view::WrapperView; use crate::param::internals::ParamPtr; -use crate::param::range::Range; use crate::param::Param; use crate::plugin::{BufferConfig, BusConfig, NoteEvent, ProcessStatus, Vst3Plugin}; use crate::wrapper::state::{ParamValue, State}; @@ -699,8 +698,9 @@ impl IAudioProcessor for Wrapper

{ self.inner .output_buffer .write() - .as_raw_vec() - .resize_with(bus_config.num_output_channels as usize, || &mut []); + .with_raw_vec(|output_slices| { + output_slices.resize_with(bus_config.num_output_channels as usize, || &mut []) + }); // Also store this for later, so we can reinitialize the plugin after restoring state self.inner.current_buffer_config.store(Some(buffer_config)); @@ -829,8 +829,7 @@ impl IAudioProcessor for Wrapper

{ // This vector has been reallocated to contain enough slices as there are output // channels let mut output_buffer = self.inner.output_buffer.write(); - { - let output_slices = output_buffer.as_raw_vec(); + output_buffer.with_raw_vec(|output_slices| { nih_debug_assert_eq!(num_output_channels, output_slices.len()); for (output_channel_idx, output_channel_slice) in output_slices.iter_mut().enumerate() @@ -843,7 +842,7 @@ impl IAudioProcessor for Wrapper

{ data.num_samples as usize, ); } - } + }); // Most hosts process data in place, in which case we don't need to do any copying // ourselves. If the pointers do not alias, then we'll do the copy here and then the