Skip to content

Commit

Permalink
Get rid of tick1
Browse files Browse the repository at this point in the history
  • Loading branch information
ahicks92 committed Dec 19, 2024
1 parent 50de271 commit e042986
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 150 deletions.
15 changes: 0 additions & 15 deletions crates/synthizer/src/chain_mathops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@ macro_rules! impl_mathop {
type Parameters = (SignalParameters<S1>, SignalParameters<S2>);
type State = (SignalState<S1>, SignalState<S2>);

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
// The complexity here is that we cannot project the context twice. We need the left value first.
let mut left = None;
S1::tick1(&mut ctx.wrap(|s| &mut s.0, |p| &p.0), input, |y| {
left = Some(y)
});
S2::tick1(&mut ctx.wrap(|s| &mut s.1, |p| &p.1), input, |y| {
destination.send(left.unwrap().$method(y));
})
}

fn on_block_start(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
) {
Expand Down
28 changes: 6 additions & 22 deletions crates/synthizer/src/core_traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::any::Any;
use std::sync::Arc;

use crate::config;
use crate::context::*;
use crate::error::Result;
use crate::unique_id::UniqueId;
Expand All @@ -13,25 +12,15 @@ pub(crate) mod sealed {
///
/// # Safety
///
/// This trait is unsafe because the library relies on it to uphold the contracts documented with the method. In
/// particular, calling `tick1` must always send exactly one value to the destination, as the destination may be
/// writing into uninitialized memory. This lets us get performance out, especially in debug builds where things
/// like immediate unwrapping of options will not be optimized away.
/// This trait is unsafe because the library relies on it to uphold the contracts documented with the method. This
/// lets us get performance out, especially in debug builds where things like immediate unwrapping of options will
/// not be optimized away.
pub unsafe trait Signal: Sized + Send + Sync {
type Input: Sized;
type Output: Sized;
type State: Sized + Send + Sync;
type Parameters: Sized + Send + Sync;

/// Tick this signal once.
///
/// Must use the destination to send exactly one value.
fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
);

/// Tick this signal [config::BLOCK_SIZE] times. Implemented via `tick1` by default.
///
/// The default implementation is only suitable for signals which are "endpoints" e.g. signals that produce
Expand All @@ -50,15 +39,10 @@ pub(crate) mod sealed {
D: ReusableSignalDestination<Self::Output>,
>(
ctx: &'_ mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
mut input: I,
mut destination: D,
input: I,
destination: D,
) where
Self::Input: 'a,
{
for i in 0..config::BLOCK_SIZE {
Self::tick1(ctx, input(i), |x| destination.send_reusable(x));
}
}
Self::Input: 'a;

/// Called when a signal is starting a new block.
///
Expand Down
16 changes: 0 additions & 16 deletions crates/synthizer/src/signals/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ where
type State = (S1::State, S2::State);
type Parameters = (S1::Parameters, S2::Parameters);

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
// LLVM should be able to see through this. Not that that's guaranteed, but it avoids wild unsafety.
let mut left: Option<S1::Output> = None;

S1::tick1(&mut ctx.wrap(|s| &mut s.0, |p| &p.0), input, |x| {
left = Some(x)
});
let left = left.unwrap();

S2::tick1(&mut ctx.wrap(|s| &mut s.1, |p| &p.1), &left, destination);
}

fn on_block_start(ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {
S1::on_block_start(&mut ctx.wrap(|s| &mut s.0, |p| &p.0));
S2::on_block_start(&mut ctx.wrap(|s| &mut s.1, |p| &p.1));
Expand Down
16 changes: 0 additions & 16 deletions crates/synthizer/src/signals/audio_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@ where
type State = (S::State, usize);
type Parameters = S::Parameters;

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
let mut val: Option<S::Output> = None;
S::tick1(&mut ctx.wrap(|s| &mut s.0, |p| p), input, |x| val = Some(x));

// We output the unit type instead.
destination.send(());

// Later this will go to a bus. But we are not at buses yet.
ctx.fixed.audio_destinationh[ctx.state.1] = val.unwrap();
ctx.state.1 += 1;
}

fn on_block_start(ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {
S::on_block_start(&mut ctx.wrap(|s| &mut s.0, |p| p));

Expand Down
9 changes: 0 additions & 9 deletions crates/synthizer/src/signals/consume_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ where
type State = S::State;
type Parameters = S::Parameters;

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
_input: &'_ Self::Input,
destination: D,
) {
let new_in: S::Input = Default::default();
S::tick1(ctx, &new_in, destination);
}

fn on_block_start(ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {
S::on_block_start(ctx);
}
Expand Down
11 changes: 0 additions & 11 deletions crates/synthizer/src/signals/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ where
type Parameters = Sig::Parameters;
type State = Sig::State;

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
Sig::tick1(ctx, input, |x: Sig::Output| {
let y: DType = x.into();
destination.send(y)
})
}

fn on_block_start(ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {
Sig::on_block_start(ctx);
}
Expand Down
13 changes: 0 additions & 13 deletions crates/synthizer/src/signals/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ where
ParSig::on_block_start(&mut ctx.wrap(|s| &mut s.parent_state, |p| p));
}

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut crate::context::SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
let mut par_in = MaybeUninit::uninit();
ParSig::tick1(&mut ctx.wrap(|s| &mut s.parent_state, |p| p), input, |x| {
par_in.write(x);
});

destination.send((ctx.state.closure)(unsafe { par_in.assume_init_ref() }));
}

fn tick_block<
'a,
I: FnMut(usize) -> &'a Self::Input,
Expand Down
21 changes: 15 additions & 6 deletions crates/synthizer/src/signals/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ unsafe impl Signal for NullSignal {
type State = ();
type Parameters = ();

fn tick1<D: SignalDestination<Self::Output>>(
_ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
_input: &'_ Self::Input,
destination: D,
) {
destination.send(());
fn tick_block<
'a,
I: FnMut(usize) -> &'a Self::Input,
D: ReusableSignalDestination<Self::Output>,
>(
_ctx: &'_ mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
mut input: I,
mut destination: D,
) where
Self::Input: 'a,
{
for i in 0..crate::config::BLOCK_SIZE {
input(i);
destination.send_reusable(());
}
}

fn on_block_start(_ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {}
Expand Down
17 changes: 0 additions & 17 deletions crates/synthizer/src/signals/periodic_f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,6 @@ where
type State = PeriodicF64State<SIncr>;
type Parameters = PeriodicF64Parameters<SIncr>;

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
let mut parent: f64 = 0.0;
SIncr::tick1(
&mut ctx.wrap(|s| &mut s.freq_state, |p| &p.freq_params),
input,
|incr| {
parent = incr;
},
);

destination.send(inc1(ctx.state, ctx.parameters, parent));
}

fn on_block_start(ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {
SIncr::on_block_start(&mut ctx.wrap(|s| &mut s.freq_state, |p| &p.freq_params));
}
Expand Down
20 changes: 14 additions & 6 deletions crates/synthizer/src/signals/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ macro_rules! impl_scalar {
type State = ();
type Parameters = $t;

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
_input: &'_ Self::Input,
destination: D,
) {
destination.send(*ctx.parameters);
fn tick_block<
'a,
I: FnMut(usize) -> &'a Self::Input,
D: ReusableSignalDestination<Self::Output>,
>(
ctx: &'_ mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
mut input: I,
mut destination: D,
) where
Self::Input: 'a {
for i in 0..crate::config::BLOCK_SIZE {
input(i);
destination.send_reusable(*ctx.parameters);
}
}

fn on_block_start(_ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {}
Expand Down
11 changes: 0 additions & 11 deletions crates/synthizer/src/signals/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,6 @@ where
ctx.state.changed_this_block = true;
}

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut crate::context::SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
_input: &'_ Self::Input,
destination: D,
) {
destination.send(SlotSignalOutput {
value: (*ctx.state.value).clone(),
changed_this_block: ctx.state.changed_this_block,
});
}

fn tick_block<
'a,
I: FnMut(usize) -> &'a Self::Input,
Expand Down
8 changes: 0 additions & 8 deletions crates/synthizer/src/signals/trig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ where
type State = S::State;
type Parameters = S::Parameters;

fn tick1<D: SignalDestination<Self::Output>>(
ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>,
input: &'_ Self::Input,
destination: D,
) {
S::tick1(ctx, input, |x: f64| destination.send(x.sin()));
}

fn on_block_start(ctx: &mut SignalExecutionContext<'_, '_, Self::State, Self::Parameters>) {
S::on_block_start(ctx);
}
Expand Down

0 comments on commit e042986

Please sign in to comment.