added delay sync

This commit is contained in:
Weird Constructor 2021-06-20 13:14:19 +02:00
parent 63644b00da
commit 4bf011261e
4 changed files with 115 additions and 40 deletions

View file

@ -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

View file

@ -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)

View file

@ -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<DelayBuffer>,
fb_sample: f32,
buffer: Box<DelayBuffer>,
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<T: NodeAudioContext>(
&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));
}
}

View file

@ -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<Box<TrackerBackend>>,
clock: TriggerClock,
clock: TriggerPhaseClock,
srate: f64,
}
@ -39,7 +39,7 @@ impl TSeq {
Self {
backend: None,
srate: 48000.0,
clock: TriggerClock::new(),
clock: TriggerPhaseClock::new(),
}
}