diff --git a/src/buffers.rs b/src/buffers.rs index 7c03fa9..783bb93 100644 --- a/src/buffers.rs +++ b/src/buffers.rs @@ -2,7 +2,6 @@ use std::marker::PhantomData; use std::ops::{Index, IndexMut, Range}; use std::slice; -use crate::bus::BusDir; use crate::events::Events; pub mod bind; @@ -11,6 +10,12 @@ pub mod iter; use bind::BindBuffers; use iter::SplitAtEvents; +#[derive(Copy, Clone, Eq, PartialEq)] +pub enum BufferType { + Const, + Mut, +} + pub enum AnyBuffer<'a, 'b> { Const(Buffer<'a, 'b>), Mut(BufferMut<'a, 'b>), @@ -19,28 +24,26 @@ pub enum AnyBuffer<'a, 'b> { impl<'a, 'b> AnyBuffer<'a, 'b> { #[inline] pub unsafe fn from_raw_parts( - dir: BusDir, + buffer_type: BufferType, ptrs: &'a [*mut f32], offset: usize, len: usize, ) -> AnyBuffer<'a, 'b> { - match dir { - BusDir::In => AnyBuffer::Const(Buffer::from_raw_parts(ptrs, offset, len)), - BusDir::Out | BusDir::InOut => { - AnyBuffer::Mut(BufferMut::from_raw_parts(ptrs, offset, len)) - } + match buffer_type { + BufferType::Const => AnyBuffer::Const(Buffer::from_raw_parts(ptrs, offset, len)), + BufferType::Mut => AnyBuffer::Mut(BufferMut::from_raw_parts(ptrs, offset, len)), } } } -pub struct BusData { - pub dir: BusDir, +pub struct BufferData { + pub buffer_type: BufferType, pub start: usize, pub end: usize, } pub struct Buffers<'a, 'b> { - buses: &'a [BusData], + buffers: &'a [BufferData], ptrs: &'a [*mut f32], offset: usize, len: usize, @@ -50,13 +53,13 @@ pub struct Buffers<'a, 'b> { impl<'a, 'b> Buffers<'a, 'b> { #[inline] pub unsafe fn from_raw_parts( - buses: &'a [BusData], + buffers: &'a [BufferData], ptrs: &'a [*mut f32], offset: usize, len: usize, ) -> Buffers<'a, 'b> { Buffers { - buses, + buffers, ptrs, offset, len, @@ -70,14 +73,14 @@ impl<'a, 'b> Buffers<'a, 'b> { } #[inline] - pub fn bus_count(&self) -> usize { - self.buses.len() + pub fn buffer_count(&self) -> usize { + self.buffers.len() } #[inline] pub fn reborrow<'c>(&'c mut self) -> Buffers<'a, 'c> { Buffers { - buses: self.buses, + buffers: self.buffers, ptrs: self.ptrs, offset: self.offset, len: self.len, @@ -87,11 +90,11 @@ impl<'a, 'b> Buffers<'a, 'b> { #[inline] pub fn get(&mut self, index: usize) -> Option { - if let Some(bus) = self.buses.get(index) { + if let Some(buffer) = self.buffers.get(index) { unsafe { Some(AnyBuffer::from_raw_parts( - bus.dir, - &self.ptrs[bus.start..bus.end], + buffer.buffer_type, + &self.ptrs[buffer.start..buffer.end], self.offset, self.len, )) @@ -120,7 +123,7 @@ impl<'a, 'b> Buffers<'a, 'b> { None } else { Some(Buffers { - buses: self.buses, + buffers: self.buffers, ptrs: self.ptrs, offset: self.offset + range.start, len: range.end - range.start, @@ -137,12 +140,12 @@ impl<'a, 'b> Buffers<'a, 'b> { impl<'a, 'b> IntoIterator for Buffers<'a, 'b> { type Item = AnyBuffer<'a, 'b>; - type IntoIter = Buses<'a, 'b>; + type IntoIter = BufferIter<'a, 'b>; #[inline] fn into_iter(self) -> Self::IntoIter { - Buses { - iter: self.buses.into_iter(), + BufferIter { + iter: self.buffers.into_iter(), ptrs: self.ptrs, offset: self.offset, len: self.len, @@ -151,24 +154,24 @@ impl<'a, 'b> IntoIterator for Buffers<'a, 'b> { } } -pub struct Buses<'a, 'b> { - iter: slice::Iter<'a, BusData>, +pub struct BufferIter<'a, 'b> { + iter: slice::Iter<'a, BufferData>, ptrs: &'a [*mut f32], offset: usize, len: usize, _marker: PhantomData<&'b mut f32>, } -impl<'a, 'b> Iterator for Buses<'a, 'b> { +impl<'a, 'b> Iterator for BufferIter<'a, 'b> { type Item = AnyBuffer<'a, 'b>; #[inline] fn next(&mut self) -> Option { - if let Some(bus) = self.iter.next() { + if let Some(buffer) = self.iter.next() { unsafe { Some(AnyBuffer::from_raw_parts( - bus.dir, - &self.ptrs[bus.start..bus.end], + buffer.buffer_type, + &self.ptrs[buffer.start..buffer.end], self.offset, self.len, )) diff --git a/src/buffers/iter.rs b/src/buffers/iter.rs index cab1b0f..a50449f 100644 --- a/src/buffers/iter.rs +++ b/src/buffers/iter.rs @@ -41,7 +41,7 @@ impl<'a, 'b, 'e> Iterator for SplitAtEvents<'a, 'b, 'e> { } let buffers = Buffers { - buses: self.buffers.buses, + buffers: self.buffers.buffers, ptrs: self.buffers.ptrs, offset: self.buffers.offset, len: split, diff --git a/src/format/clap/instance.rs b/src/format/clap/instance.rs index df8d0b3..0638b1f 100644 --- a/src/format/clap/instance.rs +++ b/src/format/clap/instance.rs @@ -9,7 +9,7 @@ use std::{io, ptr, slice}; use clap_sys::ext::{audio_ports::*, audio_ports_config::*, gui::*, params::*, state::*}; use clap_sys::{events::*, id::*, plugin::*, process::*, stream::*}; -use crate::buffers::{Buffers, BusData}; +use crate::buffers::{BufferData, BufferType, Buffers}; use crate::bus::{BusDir, Format}; use crate::events::{Data, Event, Events}; use crate::params::{ParamId, ParamInfo, ParamValue}; @@ -48,7 +48,7 @@ pub struct MainThreadState { } pub struct ProcessState { - buses: Vec, + buffer_data: Vec, buffer_ptrs: Vec<*mut f32>, events: Vec, processor: Option, @@ -117,7 +117,7 @@ impl Instance

{ editor: None, }), process_state: UnsafeCell::new(ProcessState { - buses: Vec::new(), + buffer_data: Vec::new(), buffer_ptrs: Vec::new(), events: Vec::with_capacity(4096), processor: None, @@ -161,15 +161,19 @@ impl Instance

{ let layout = &instance.info.layouts[main_thread_state.layout_index]; - process_state.buses.clear(); + process_state.buffer_data.clear(); let mut total_channels = 0; for (info, format) in zip(&instance.info.buses, &layout.formats) { + let buffer_type = match info.dir { + BusDir::In => BufferType::Const, + BusDir::Out | BusDir::InOut => BufferType::Mut, + }; let channel_count = format.channel_count(); - process_state.buses.push(BusData { + process_state.buffer_data.push(BufferData { + buffer_type, start: total_channels, end: total_channels + channel_count, - dir: info.dir, }); total_channels += channel_count; @@ -239,31 +243,32 @@ impl Instance

{ let outputs = slice_from_raw_parts_checked(process.audio_outputs, output_count); for (&bus_index, output) in zip(&instance.output_bus_map, outputs) { - let bus_data = &process_state.buses[bus_index]; + let data = &process_state.buffer_data[bus_index]; let channel_count = output.channel_count as usize; - if channel_count != bus_data.end - bus_data.start { + if channel_count != data.end - data.start { return CLAP_PROCESS_ERROR; } let channels = slice_from_raw_parts_checked(output.data32 as *const *mut f32, channel_count); - process_state.buffer_ptrs[bus_data.start..bus_data.end].copy_from_slice(channels); + process_state.buffer_ptrs[data.start..data.end].copy_from_slice(channels); } for (&bus_index, input) in zip(&instance.input_bus_map, inputs) { - let bus_data = &process_state.buses[bus_index]; + let data = &process_state.buffer_data[bus_index]; + let bus_info = &instance.info.buses[bus_index]; let channel_count = input.channel_count as usize; - if channel_count != bus_data.end - bus_data.start { + if channel_count != data.end - data.start { return CLAP_PROCESS_ERROR; } let channels = slice_from_raw_parts_checked(input.data32 as *const *mut f32, channel_count); - let ptrs = &mut process_state.buffer_ptrs[bus_data.start..bus_data.end]; + let ptrs = &mut process_state.buffer_ptrs[data.start..data.end]; - match bus_data.dir { + match bus_info.dir { BusDir::In => { ptrs.copy_from_slice(channels); } @@ -310,7 +315,12 @@ impl Instance

{ instance.sync_processor(processor); processor.process( - Buffers::from_raw_parts(&process_state.buses, &process_state.buffer_ptrs, 0, len), + Buffers::from_raw_parts( + &process_state.buffer_data, + &process_state.buffer_ptrs, + 0, + len, + ), Events::new(&process_state.events), ); diff --git a/src/format/vst3/buffers.rs b/src/format/vst3/buffers.rs index 3cc4f14..13a27f8 100644 --- a/src/format/vst3/buffers.rs +++ b/src/format/vst3/buffers.rs @@ -4,7 +4,7 @@ use std::slice; use vst3::Steinberg::Vst::ProcessData; -use crate::buffers::{Buffers, BusData}; +use crate::buffers::{BufferData, BufferType, Buffers}; use crate::bus::{BusDir, BusInfo}; use crate::process::Config; use crate::util::slice_from_raw_parts_checked; @@ -12,7 +12,7 @@ use crate::util::slice_from_raw_parts_checked; pub struct ScratchBuffers { inputs_active: Vec, outputs_active: Vec, - buses: Vec, + data: Vec, ptrs: Vec<*mut f32>, buffers: Vec, silence: Vec, @@ -25,7 +25,7 @@ impl ScratchBuffers { ScratchBuffers { inputs_active: vec![true; input_count], outputs_active: vec![true; output_count], - buses: Vec::new(), + data: Vec::new(), ptrs: Vec::new(), buffers: Vec::new(), silence: Vec::new(), @@ -43,17 +43,21 @@ impl ScratchBuffers { } pub fn resize(&mut self, buses: &[BusInfo], config: &Config) { - self.buses.clear(); + self.data.clear(); let mut total_channels = 0; let mut output_channels = 0; let mut in_out_channels = 0; for (info, format) in zip(buses, &config.layout.formats) { + let buffer_type = match info.dir { + BusDir::In => BufferType::Const, + BusDir::Out | BusDir::InOut => BufferType::Mut, + }; let channel_count = format.channel_count(); - self.buses.push(BusData { + self.data.push(BufferData { + buffer_type, start: total_channels, end: total_channels + channel_count, - dir: info.dir, }); total_channels += channel_count; @@ -85,6 +89,7 @@ impl ScratchBuffers { pub unsafe fn get_buffers( &mut self, + buses: &[BusInfo], input_bus_map: &[usize], output_bus_map: &[usize], config: &Config, @@ -99,7 +104,7 @@ impl ScratchBuffers { if len == 0 { self.ptrs.fill(NonNull::dangling().as_ptr()); - return Ok(Buffers::from_raw_parts(&self.buses, &self.ptrs, 0, len)); + return Ok(Buffers::from_raw_parts(&self.data, &self.ptrs, 0, len)); } let input_count = data.numInputs as usize; @@ -126,7 +131,7 @@ impl ScratchBuffers { // Set up output pointers. self.output_ptrs.clear(); for (output_index, &bus_index) in output_bus_map.iter().enumerate() { - let bus_data = &self.buses[bus_index]; + let data = &self.data[bus_index]; if self.outputs_active[output_index] { let output = &outputs[output_index]; let channels = slice_from_raw_parts_checked( @@ -134,11 +139,11 @@ impl ScratchBuffers { output.numChannels as usize, ); - self.ptrs[bus_data.start..bus_data.end].copy_from_slice(channels); + self.ptrs[data.start..data.end].copy_from_slice(channels); self.output_ptrs.extend_from_slice(&channels); } else { // For inactive output buses, allocate a scratch buffer for each channel. - for ptr in &mut self.ptrs[bus_data.start..bus_data.end] { + for ptr in &mut self.ptrs[data.start..data.end] { let (first, rest) = scratch.split_at_mut(len); scratch = rest; @@ -153,8 +158,9 @@ impl ScratchBuffers { // Set up input pointers. for (input_index, &bus_index) in input_bus_map.iter().enumerate() { - let bus_data = &self.buses[bus_index]; - if bus_data.dir == BusDir::In { + let data = &self.data[bus_index]; + let bus_info = &buses[bus_index]; + if bus_info.dir == BusDir::In { if self.inputs_active[input_index] { let input = &inputs[input_index]; let channels = slice_from_raw_parts_checked( @@ -162,7 +168,7 @@ impl ScratchBuffers { input.numChannels as usize, ); - let ptrs = &mut self.ptrs[bus_data.start..bus_data.end]; + let ptrs = &mut self.ptrs[data.start..data.end]; for (&channel, ptr) in zip(channels, ptrs) { // If an input buffer is aliased by some output buffer, copy its contents to // a scratch buffer. @@ -180,7 +186,7 @@ impl ScratchBuffers { } else { // For inactive input buses, provide pointers to the silence buffer. let silence = self.silence.as_ptr() as *mut f32; - self.ptrs[bus_data.start..bus_data.end].fill(silence); + self.ptrs[data.start..data.end].fill(silence); } } } @@ -189,8 +195,9 @@ impl ScratchBuffers { // inputs to outputs. self.moves.clear(); for (input_index, &bus_index) in input_bus_map.iter().enumerate() { - let bus_data = &self.buses[bus_index]; - if bus_data.dir == BusDir::InOut { + let data = &self.data[bus_index]; + let bus_info = &buses[bus_index]; + if bus_info.dir == BusDir::InOut { if self.inputs_active[input_index] { let input = &inputs[input_index]; let channels = slice_from_raw_parts_checked( @@ -198,7 +205,7 @@ impl ScratchBuffers { input.numChannels as usize, ); - let ptrs = &self.ptrs[bus_data.start..bus_data.end]; + let ptrs = &self.ptrs[data.start..data.end]; for (&src, &dst) in zip(channels, ptrs) { // Only perform a copy if input and output pointers are not equal. if src != dst { @@ -219,7 +226,7 @@ impl ScratchBuffers { } } else { // For inactive input buses, copy from the silence buffer. - for &dst in &self.ptrs[bus_data.start..bus_data.end] { + for &dst in &self.ptrs[data.start..data.end] { self.moves.push((self.silence.as_ptr(), dst)); } } @@ -236,6 +243,6 @@ impl ScratchBuffers { self.output_ptrs.clear(); - Ok(Buffers::from_raw_parts(&self.buses, &self.ptrs, 0, len)) + Ok(Buffers::from_raw_parts(&self.data, &self.ptrs, 0, len)) } } diff --git a/src/format/vst3/component.rs b/src/format/vst3/component.rs index b7e8903..154312a 100644 --- a/src/format/vst3/component.rs +++ b/src/format/vst3/component.rs @@ -476,6 +476,7 @@ impl IAudioProcessorTrait for Component

{ let data = &*data; let Ok(buffers) = process_state.scratch_buffers.get_buffers( + &self.info.buses, &self.input_bus_map, &self.output_bus_map, &process_state.config,