From 4bf011261e3d1dde375ccfb9d9bea69339cf73f6 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Sun, 20 Jun 2021 13:14:19 +0200 Subject: [PATCH] added delay sync --- src/dsp/helpers.rs | 52 +++++++++++++++++++++++++- src/dsp/mod.rs | 11 ++++-- src/dsp/node_delay.rs | 86 +++++++++++++++++++++++++++---------------- src/dsp/node_tseq.rs | 6 +-- 4 files changed, 115 insertions(+), 40 deletions(-) diff --git a/src/dsp/helpers.rs b/src/dsp/helpers.rs index 844092a..eb5f784 100644 --- a/src/dsp/helpers.rs +++ b/src/dsp/helpers.rs @@ -430,14 +430,14 @@ impl Trigger { } #[derive(Debug, Clone, Copy)] -pub struct TriggerClock { +pub struct TriggerPhaseClock { clock_phase: f64, clock_inc: f64, prev_trigger: bool, clock_samples: u32, } -impl TriggerClock { +impl TriggerPhaseClock { pub fn new() -> Self { Self { clock_phase: 0.0, @@ -481,6 +481,47 @@ impl TriggerClock { } } +#[derive(Debug, Clone, Copy)] +pub struct TriggerSampleClock { + prev_trigger: bool, + clock_samples: u32, + counter: u32, +} + +impl TriggerSampleClock { + pub fn new() -> Self { + Self { + prev_trigger: true, + clock_samples: 0, + counter: 0, + } + } + + #[inline] + pub fn reset(&mut self) { + self.clock_samples = 0; + self.counter = 0; + } + + #[inline] + pub fn next(&mut self, trigger_in: f32) -> u32 { + if self.prev_trigger { + if trigger_in <= 0.25 { + self.prev_trigger = false; + } + + } else if trigger_in > 0.75 { + self.prev_trigger = true; + self.clock_samples = self.counter; + self.counter = 0; + } + + self.counter += 1; + + self.clock_samples + } +} + /// Default size of the delay buffer: 5 seconds at 8 times 48kHz const DEFAULT_DELAY_BUFFER_SAMPLES : usize = 8 * 48000 * 5; @@ -562,6 +603,13 @@ impl DelayBuffer { let idx = ((self.wr + len) - offs) % len; self.data[idx] } + + #[inline] + pub fn at(&self, delay_sample_count: usize) -> f32 { + let len = self.data.len(); + let idx = ((self.wr + len) - delay_sample_count) % len; + self.data[idx] + } } // translated from Odin 2 Synthesizer Plugin diff --git a/src/dsp/mod.rs b/src/dsp/mod.rs index f321b7a..b58d024 100644 --- a/src/dsp/mod.rs +++ b/src/dsp/mod.rs @@ -43,6 +43,7 @@ use crate::fa_sampl_dclick; use crate::fa_sampl_pmode; use crate::fa_sampl_dir; use crate::fa_ad_mult; +use crate::fa_delay_mode; use node_amp::Amp; use node_sin::Sin; @@ -458,14 +459,16 @@ macro_rules! node_list { (3 dcy n_env d_env r_ems f_ms stp_m 0.0, 1.0, 10.0) (4 ashp n_id d_id r_id f_def stp_d 0.0, 1.0, 0.5) (5 dshp n_id d_id r_id f_def stp_d 0.0, 1.0, 0.5) - {6 0 mult setting(0) fa_ad_mult 0 2} + {6 0 mult setting(0) fa_ad_mult 0 2} [0 sig] [1 eoet], delay => Delay UIType::Generic UICategory::Signal (0 inp n_id d_id r_id f_def stp_d -1.0, 1.0, 1.0) - (1 time n_time d_time r_tms f_ms stp_m 0.0, 1.0, 0.5) - (2 fb n_id d_id r_id f_def stp_d 0.0, 1.0, 0.0) - (3 mix n_id d_id r_id f_def stp_d 0.0, 1.0, 0.5) + (1 trig n_id d_id r_id f_def stp_d -1.0, 1.0, 0.0) + (2 time n_time d_time r_tms f_ms stp_m 0.0, 1.0, 0.5) + (3 fb n_id d_id r_id f_def stp_d 0.0, 1.0, 0.0) + (4 mix n_id d_id r_id f_def stp_d 0.0, 1.0, 0.5) + {5 0 mode setting(0) fa_delay_mode 0 1} [0 sig], test => Test UIType::Generic UICategory::IOUtil (0 f n_id d_id r_id f_def stp_d 0.0, 1.0, 0.5) diff --git a/src/dsp/node_delay.rs b/src/dsp/node_delay.rs index 16e1852..4c9bdde 100644 --- a/src/dsp/node_delay.rs +++ b/src/dsp/node_delay.rs @@ -4,23 +4,14 @@ use crate::nodes::{NodeAudioContext, NodeExecContext}; use crate::dsp::{NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals}; -use crate::dsp::helpers::{DelayBuffer, crossfade}; +use crate::dsp::helpers::{DelayBuffer, crossfade, TriggerSampleClock}; #[macro_export] -macro_rules! fa_dly_s { ($formatter: expr, $v: expr, $denorm_v: expr) => { { +macro_rules! fa_delay_mode { ($formatter: expr, $v: expr, $denorm_v: expr) => { { let s = match ($v.round() as usize) { - 0 => "Zero", - 1 => "One", - 2 => "Two", - 3 => "Three", - 4 => "Four", - 5 => "Five", - 6 => "Six", - 7 => "Seven", - 8 => "Eigth", - 9 => "Nine", - 10 => "Ten", + 0 => "Time", + 1 => "Sync", _ => "?", }; write!($formatter, "{}", s) @@ -29,21 +20,26 @@ macro_rules! fa_dly_s { ($formatter: expr, $v: expr, $denorm_v: expr) => { { /// A simple amplifier #[derive(Debug, Clone)] pub struct Delay { - buffer: Box, - fb_sample: f32, + buffer: Box, + fb_sample: f32, + clock: TriggerSampleClock, } impl Delay { pub fn new(_nid: &NodeId) -> Self { Self { - buffer: Box::new(DelayBuffer::new()), - fb_sample: 0.0, + buffer: Box::new(DelayBuffer::new()), + fb_sample: 0.0, + clock: TriggerSampleClock::new(), } } pub const inp : &'static str = "Delay inp\nThe signal input for the delay. You can mix in this \ input to the output with the 'mix' parameter.\nRange: (-1..1)"; + pub const trig : &'static str = + "Delay trig\nIf you set 'mode' to 'Sync', the delay time will be \ + synchronized to the trigger signals received on this input.\nRange: (-1..1)"; pub const time : &'static str = "Delay time\nThe delay time. It can be freely modulated to your \ likings.\nRange: (0..1)"; @@ -52,6 +48,11 @@ impl Delay { \nRange: (0..1)"; pub const mix : &'static str = "Delay mix\nThe dry/wet mix of the delay.\nRange: (0..1)"; + pub const mode : &'static str = + "Delay mode\nAllows different operating modes of the delay. \ + 'Time' is the default, and means that the 'time' input \ + specifies the delay time. 'Sync' will synchronize the delay time \ + with the trigger signals on the 'trig' input."; pub const sig : &'static str = "Delay sig\nThe output of the dry/wet mix.\nRange: (-1..1)"; @@ -86,19 +87,22 @@ impl DspNode for Delay { fn reset(&mut self) { self.buffer.reset(); + self.clock.reset(); } #[inline] fn process( &mut self, ctx: &mut T, _ectx: &mut NodeExecContext, - _atoms: &[SAtom], _params: &[ProcBuf], inputs: &[ProcBuf], - outputs: &mut [ProcBuf], _led: LedPhaseVals) + atoms: &[SAtom], _params: &[ProcBuf], inputs: &[ProcBuf], + outputs: &mut [ProcBuf], ctx_vals: LedPhaseVals) { - use crate::dsp::{out, inp, denorm}; + use crate::dsp::{at, out, inp, denorm}; let buffer = &mut *self.buffer; + let mode = at::Delay::mode(atoms); let inp = inp::Delay::inp(inputs); + let trig = inp::Delay::trig(inputs); let time = inp::Delay::time(inputs); let fb = inp::Delay::fb(inputs); let mix = inp::Delay::mix(inputs); @@ -106,22 +110,42 @@ impl DspNode for Delay { let mut fb_s = self.fb_sample; - for frame in 0..ctx.nframes() { - let dry = inp.read(frame); - buffer.feed( - dry + fb_s * denorm::Delay::fb(fb, frame)); + if mode.i() == 0 { + for frame in 0..ctx.nframes() { + let dry = inp.read(frame); + buffer.feed(dry + fb_s * denorm::Delay::fb(fb, frame)); - let out_sample = - buffer.cubic_interpolate_at( - denorm::Delay::time(time, frame)); + let out_sample = + buffer.cubic_interpolate_at( + denorm::Delay::time(time, frame)); - out.write(frame, - crossfade(dry, out_sample, - denorm::Delay::mix(mix, frame).clamp(0.0, 1.0))); + out.write(frame, + crossfade(dry, out_sample, + denorm::Delay::mix(mix, frame).clamp(0.0, 1.0))); - fb_s = out_sample; + fb_s = out_sample; + } + } else { + for frame in 0..ctx.nframes() { + let dry = inp.read(frame); + buffer.feed(dry + fb_s * denorm::Delay::fb(fb, frame)); + + let clock_samples = + self.clock.next(denorm::Delay::trig(trig, frame)); + + let out_sample = buffer.at(clock_samples as usize); + + out.write(frame, + crossfade(dry, out_sample, + denorm::Delay::mix(mix, frame).clamp(0.0, 1.0))); + + fb_s = out_sample; + } } self.fb_sample = fb_s; + + let last_frame = ctx.nframes() - 1; + ctx_vals[0].set(out.read(last_frame)); } } diff --git a/src/dsp/node_tseq.rs b/src/dsp/node_tseq.rs index 6c871ed..94a3c6a 100644 --- a/src/dsp/node_tseq.rs +++ b/src/dsp/node_tseq.rs @@ -3,7 +3,7 @@ // See README.md and COPYING for details. use crate::nodes::{NodeAudioContext, NodeExecContext}; -use crate::dsp::helpers::TriggerClock; +use crate::dsp::helpers::TriggerPhaseClock; use crate::dsp::{NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals}; use crate::dsp::tracker::TrackerBackend; @@ -26,7 +26,7 @@ macro_rules! fa_tseq_cmode { ($formatter: expr, $v: expr, $denorm_v: expr) => { #[derive(Debug)] pub struct TSeq { backend: Option>, - clock: TriggerClock, + clock: TriggerPhaseClock, srate: f64, } @@ -39,7 +39,7 @@ impl TSeq { Self { backend: None, srate: 48000.0, - clock: TriggerClock::new(), + clock: TriggerPhaseClock::new(), } }