Skip to content

Commit

Permalink
replace BusData with BufferData
Browse files Browse the repository at this point in the history
Rename BusData to BufferData. Use a BufferType enum with Const and Mut
values instead of the BusDir enum.
  • Loading branch information
micahrj committed Feb 13, 2024
1 parent 8fc6009 commit 0852e9f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 62 deletions.
59 changes: 31 additions & 28 deletions src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -87,11 +90,11 @@ impl<'a, 'b> Buffers<'a, 'b> {

#[inline]
pub fn get(&mut self, index: usize) -> Option<AnyBuffer> {
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,
))
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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<Self::Item> {
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,
))
Expand Down
2 changes: 1 addition & 1 deletion src/buffers/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 24 additions & 14 deletions src/format/clap/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -48,7 +48,7 @@ pub struct MainThreadState<P: Plugin> {
}

pub struct ProcessState<P: Plugin> {
buses: Vec<BusData>,
buffer_data: Vec<BufferData>,
buffer_ptrs: Vec<*mut f32>,
events: Vec<Event>,
processor: Option<P::Processor>,
Expand Down Expand Up @@ -117,7 +117,7 @@ impl<P: Plugin> Instance<P> {
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,
Expand Down Expand Up @@ -161,15 +161,19 @@ impl<P: Plugin> Instance<P> {

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;
Expand Down Expand Up @@ -239,31 +243,32 @@ impl<P: Plugin> Instance<P> {
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);
}
Expand Down Expand Up @@ -310,7 +315,12 @@ impl<P: Plugin> Instance<P> {

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),
);

Expand Down
Loading

0 comments on commit 0852e9f

Please sign in to comment.