Skip to content

Commit

Permalink
implement tuple binding for buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Jan 16, 2024
1 parent eae8ca8 commit e973f3a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
7 changes: 3 additions & 4 deletions examples/gain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::io::{self, Read, Write};

use serde::{Deserialize, Serialize};

use coupler::buffers::bind::*;
use coupler::format::clap::*;
use coupler::format::vst3::*;
use coupler::{block::*, buffers::*, bus::*, events::*, params::*, parent::*, *};
use coupler::{block::*, bus::*, events::*, params::*, parent::*, *};

#[derive(Params, Serialize, Deserialize, Clone)]
struct GainParams {
Expand Down Expand Up @@ -125,9 +126,7 @@ impl Processor for GainProcessor {
}
}

let Some(BufferDir::InOut(mut main)) = block.buffers.get(0) else {
unreachable!();
};
let InOut(mut main) = block.buffers.bind().unwrap();

for i in 0..main.channel_count() {
for sample in &mut main[i] {
Expand Down
28 changes: 28 additions & 0 deletions src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::slice;

use crate::bus::BusDir;

pub mod bind;

use bind::BindBuffers;

pub enum BufferDir<'a> {
In(Buffer<'a>),
Out(BufferMut<'a>),
Expand Down Expand Up @@ -67,6 +71,17 @@ impl<'a> Buffers<'a> {
self.buses.len()
}

#[inline]
pub fn reborrow(&mut self) -> Buffers {
Buffers {
buses: self.buses,
ptrs: self.ptrs,
offset: self.offset,
len: self.len,
_marker: self._marker,
}
}

#[inline]
pub fn get(&mut self, index: usize) -> Option<BufferDir> {
if let Some(bus) = self.buses.get(index) {
Expand All @@ -83,6 +98,19 @@ impl<'a> Buffers<'a> {
}
}

#[inline]
pub fn bind<'b, B: BindBuffers<'b>>(&'b mut self) -> Option<B> {
let mut iter = self.reborrow().into_iter();

let result = B::bind(&mut iter)?;

if iter.next().is_none() {
Some(result)
} else {
None
}
}

#[inline]
pub fn slice(&mut self, range: Range<usize>) -> Option<Buffers> {
if range.start > range.end || range.end > self.len {
Expand Down
84 changes: 84 additions & 0 deletions src/buffers/bind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use super::{Buffer, BufferDir, BufferMut};

pub trait BindBuffers<'a>: Sized {
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>;
}

pub struct In<'a>(pub Buffer<'a>);

impl<'a> BindBuffers<'a> for In<'a> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
{
match buffers.next() {
Some(BufferDir::In(buffer)) => Some(In(buffer)),
_ => None,
}
}
}

pub struct Out<'a>(pub BufferMut<'a>);

impl<'a> BindBuffers<'a> for Out<'a> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
{
match buffers.next() {
Some(BufferDir::Out(buffer)) => Some(Out(buffer)),
_ => None,
}
}
}

pub struct InOut<'a>(pub BufferMut<'a>);

impl<'a> BindBuffers<'a> for InOut<'a> {
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
{
match buffers.next() {
Some(BufferDir::InOut(buffer)) => Some(InOut(buffer)),
_ => None,
}
}
}

macro_rules! bind_buffers {
($($binding:ident),*) => {
impl<'a, $($binding),*> BindBuffers<'a> for ($($binding,)*)
where
$($binding: BindBuffers<'a>),*
{
#[inline]
fn bind<I>(buffers: &mut I) -> Option<Self>
where
I: Iterator<Item = BufferDir<'a>>,
{
Some((
$(
$binding::bind(buffers)?,
)*
))
}
}
}
}

bind_buffers!(B0);
bind_buffers!(B0, B1);
bind_buffers!(B0, B1, B2);
bind_buffers!(B0, B1, B2, B3);
bind_buffers!(B0, B1, B2, B3, B4);
bind_buffers!(B0, B1, B2, B3, B4, B5);
bind_buffers!(B0, B1, B2, B3, B4, B5, B6);
bind_buffers!(B0, B1, B2, B3, B4, B5, B6, B7);
bind_buffers!(B0, B1, B2, B3, B4, B5, B6, B7, B8);
bind_buffers!(B0, B1, B2, B3, B4, B5, B6, B7, B8, B9);

0 comments on commit e973f3a

Please sign in to comment.