HexoDSP/src/dsp/node_delay.rs

141 lines
4.6 KiB
Rust
Raw Normal View History

2021-06-19 08:50:39 +00:00
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
2021-08-04 01:58:43 +00:00
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
2021-06-19 08:50:39 +00:00
// See README.md and COPYING for details.
use synfx_dsp::{crossfade, DelayBuffer, TriggerSampleClock};
2022-07-17 09:58:28 +00:00
use crate::dsp::{DspNode, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom};
2021-06-19 08:50:39 +00:00
use crate::nodes::{NodeAudioContext, NodeExecContext};
#[macro_export]
2022-07-17 09:58:28 +00:00
macro_rules! fa_delay_mode {
($formatter: expr, $v: expr, $denorm_v: expr) => {{
let s = match ($v.round() as usize) {
0 => "Time",
1 => "Sync",
_ => "?",
};
2021-06-19 08:50:39 +00:00
write!($formatter, "{}", s)
2022-07-17 09:58:28 +00:00
}};
}
2021-06-19 08:50:39 +00:00
/// A simple amplifier
#[derive(Debug, Clone)]
pub struct Delay {
2022-07-17 09:58:28 +00:00
buffer: Box<DelayBuffer<f32>>,
clock: TriggerSampleClock,
2021-06-19 08:50:39 +00:00
}
impl Delay {
pub fn new(_nid: &NodeId) -> Self {
2022-07-17 09:58:28 +00:00
Self { buffer: Box::new(DelayBuffer::new()), clock: TriggerSampleClock::new() }
2021-06-19 08:50:39 +00:00
}
2021-06-19 09:17:00 +00:00
2022-07-17 09:58:28 +00:00
pub const inp: &'static str = "Delay inp\nThe signal input for the delay. You can mix in this \
2021-06-19 09:17:00 +00:00
input to the output with the 'mix' parameter.\nRange: (-1..1)";
2022-07-17 09:58:28 +00:00
pub const trig: &'static str =
2021-06-20 11:14:19 +00:00
"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)";
2022-07-17 09:58:28 +00:00
pub const time: &'static str =
2021-06-19 09:17:00 +00:00
"Delay time\nThe delay time. It can be freely modulated to your \
likings.\nRange: (0..1)";
2022-07-17 09:58:28 +00:00
pub const fb: &'static str =
2021-06-19 09:17:00 +00:00
"Delay fb\nThe feedback amount of the delay output to it's input. \
2021-06-23 03:12:37 +00:00
\nRange: (-1..1)";
2022-07-17 09:58:28 +00:00
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. \
2021-06-20 11:14:19 +00:00
'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.";
2022-07-17 09:58:28 +00:00
pub const sig: &'static str = "Delay sig\nThe output of the dry/wet mix.\nRange: (-1..1)";
2021-06-19 09:17:00 +00:00
2022-07-17 09:58:28 +00:00
pub const DESC: &'static str = r#"Simple Delay Line
2021-06-19 09:17:00 +00:00
This is a very simple single buffer delay node.
It provides an internal feedback and dry/wet mix.
"#;
2022-07-17 09:58:28 +00:00
pub const HELP: &'static str = r#"Delay - A Simple Delay Line
2021-06-19 09:17:00 +00:00
This node provides a very simple delay line with the bare minimum of
parameters. Most importantly a freely modulateable 'time' parameter
and a feedback 'fb' parameter.
Via the 'mix' parameter you can mix in the input signal to the output.
You can use this node to delay any kind of signal, from a simple control
signal to an audio signal.
For other kinds of delay/feedback please see also the 'FbWr'/'FbRd' nodes.
"#;
2021-06-19 08:50:39 +00:00
}
impl DspNode for Delay {
2022-07-17 09:58:28 +00:00
fn outputs() -> usize {
1
}
2021-06-19 08:50:39 +00:00
2021-06-19 10:17:22 +00:00
fn set_sample_rate(&mut self, srate: f32) {
self.buffer.set_sample_rate(srate);
}
fn reset(&mut self) {
self.buffer.reset();
2021-06-20 11:14:19 +00:00
self.clock.reset();
2021-06-19 10:17:22 +00:00
}
2021-06-19 08:50:39 +00:00
#[inline]
fn process<T: NodeAudioContext>(
2022-07-17 09:58:28 +00:00
&mut self,
ctx: &mut T,
_ectx: &mut NodeExecContext,
_nctx: &NodeContext,
2022-07-17 09:58:28 +00:00
atoms: &[SAtom],
inputs: &[ProcBuf],
outputs: &mut [ProcBuf],
ctx_vals: LedPhaseVals,
) {
use crate::dsp::{at, denorm, inp, out};
2021-06-19 08:50:39 +00:00
2022-07-17 09:58:28 +00:00
let buffer = &mut *self.buffer;
2021-06-19 10:17:22 +00:00
2021-06-20 11:14:19 +00:00
let mode = at::Delay::mode(atoms);
2022-07-17 09:58:28 +00:00
let inp = inp::Delay::inp(inputs);
2021-06-20 11:14:19 +00:00
let trig = inp::Delay::trig(inputs);
2021-06-19 10:17:22 +00:00
let time = inp::Delay::time(inputs);
2022-07-17 09:58:28 +00:00
let fb = inp::Delay::fb(inputs);
let mix = inp::Delay::mix(inputs);
let out = out::Delay::sig(outputs);
2021-06-19 10:17:22 +00:00
2021-06-20 11:14:19 +00:00
if mode.i() == 0 {
for frame in 0..ctx.nframes() {
let dry = inp.read(frame);
2021-06-19 10:17:22 +00:00
2022-07-17 09:58:28 +00:00
let out_sample = buffer.cubic_interpolate_at(denorm::Delay::time(time, frame));
2021-06-19 10:17:22 +00:00
buffer.feed(dry + out_sample * denorm::Delay::fb(fb, frame));
2022-07-17 09:58:28 +00:00
out.write(
frame,
crossfade(dry, out_sample, denorm::Delay::mix(mix, frame).clamp(0.0, 1.0)),
);
2021-06-20 11:14:19 +00:00
}
} else {
for frame in 0..ctx.nframes() {
let dry = inp.read(frame);
2022-07-17 09:58:28 +00:00
let clock_samples = self.clock.next(denorm::Delay::trig(trig, frame));
2021-06-20 11:14:19 +00:00
let out_sample = buffer.at(clock_samples as usize);
buffer.feed(dry + out_sample * denorm::Delay::fb(fb, frame));
2022-07-17 09:58:28 +00:00
out.write(
frame,
crossfade(dry, out_sample, denorm::Delay::mix(mix, frame).clamp(0.0, 1.0)),
);
2021-06-20 11:14:19 +00:00
}
2021-06-19 08:50:39 +00:00
}
2021-06-20 11:14:19 +00:00
let last_frame = ctx.nframes() - 1;
ctx_vals[0].set(out.read(last_frame));
2021-06-19 08:50:39 +00:00
}
}