HexoDSP/src/dsp/node_tslfo.rs

117 lines
3.7 KiB
Rust
Raw Normal View History

2021-08-06 03:49:03 +00:00
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.
use synfx_dsp::{TriSawLFO, Trigger};
2021-08-06 03:49:03 +00:00
use crate::dsp::{
2022-07-17 09:58:28 +00:00
DspNode, GraphAtomData, GraphFun, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom,
2021-08-06 03:49:03 +00:00
};
2022-07-17 09:58:28 +00:00
use crate::nodes::{NodeAudioContext, NodeExecContext};
2021-08-06 03:49:03 +00:00
#[derive(Debug, Clone)]
2021-08-13 03:13:17 +00:00
pub struct TsLFO {
2022-07-17 09:58:28 +00:00
lfo: Box<TriSawLFO<f64>>,
trig: Trigger,
2021-08-06 03:49:03 +00:00
}
2021-08-13 03:13:17 +00:00
impl TsLFO {
2021-08-06 03:49:03 +00:00
pub fn new(_nid: &NodeId) -> Self {
2022-07-17 09:58:28 +00:00
Self { lfo: Box::new(TriSawLFO::new()), trig: Trigger::new() }
2021-08-06 03:49:03 +00:00
}
2022-07-17 09:58:28 +00:00
pub const time: &'static str =
2021-08-13 03:13:17 +00:00
"TsLFO time\nThe frequency or period time of the LFO, goes all the \
2021-08-06 04:01:11 +00:00
way from 0.1ms up to 30s. Please note, that the text entry is always \
in milliseconds.\nRange: (0..1)\n";
2022-07-17 09:58:28 +00:00
pub const trig: &'static str =
2021-08-13 03:13:17 +00:00
"TsLFO trig\nTriggers a phase reset of the LFO.\nRange: (0..1)\n";
2022-07-17 09:58:28 +00:00
pub const rev: &'static str =
2021-08-13 03:13:17 +00:00
"TsLFO rev\nThe reverse point of the LFO waveform. At 0.5 the LFO \
2021-08-06 03:49:03 +00:00
will follow a triangle waveform. At 0.0 or 1.0 the LFO waveform will \
be (almost) a (reversed) saw tooth. Node: A perfect sawtooth can not be \
achieved with this oscillator, as there will always be a minimal \
rise/fall time.\nRange: (0..1)\n";
2022-07-17 09:58:28 +00:00
pub const sig: &'static str = "TsLFO sig\nThe LFO output.\nRange: (0..1)";
pub const DESC: &'static str = r#"TriSaw LFO
2021-08-06 03:49:03 +00:00
This simple LFO has a configurable waveform. You can blend between triangular to sawtooth waveforms using the 'rev' parameter.
"#;
2022-07-17 09:58:28 +00:00
pub const HELP: &'static str = r#"TsLFO - TriSaw LFO
2021-08-06 03:49:03 +00:00
This simple LFO has a configurable waveform. You can blend between
triangular to sawtooth waveforms using the 'rev' parameter.
Using the 'trig' input you can reset the LFO phase, which allows to use it
kind of like an envelope.
"#;
}
2021-08-13 03:13:17 +00:00
impl DspNode for TsLFO {
2022-07-17 09:58:28 +00:00
fn outputs() -> usize {
1
}
2021-08-06 03:49:03 +00:00
fn set_sample_rate(&mut self, srate: f32) {
self.lfo.set_sample_rate(srate as f64);
2021-08-06 03:49:03 +00:00
}
fn reset(&mut self) {
self.lfo.reset();
self.trig.reset();
}
#[inline]
fn process<T: NodeAudioContext>(
2022-07-17 09:58:28 +00:00
&mut self,
ctx: &mut T,
_ectx: &mut NodeExecContext,
2021-08-06 03:49:03 +00:00
_nctx: &NodeContext,
2022-07-17 09:58:28 +00:00
_atoms: &[SAtom],
inputs: &[ProcBuf],
outputs: &mut [ProcBuf],
ctx_vals: LedPhaseVals,
) {
use crate::dsp::{denorm, inp, out};
2021-08-06 03:49:03 +00:00
2021-08-13 03:13:17 +00:00
let time = inp::TsLFO::time(inputs);
let trig = inp::TsLFO::trig(inputs);
2022-07-17 09:58:28 +00:00
let rev = inp::TsLFO::rev(inputs);
let out = out::TsLFO::sig(outputs);
2021-08-06 03:49:03 +00:00
2021-08-17 01:53:43 +00:00
let lfo = &mut *self.lfo;
2021-08-06 03:49:03 +00:00
for frame in 0..ctx.nframes() {
2021-08-13 03:13:17 +00:00
if self.trig.check_trigger(denorm::TsLFO::trig(trig, frame)) {
2021-08-06 03:49:03 +00:00
lfo.reset();
}
2021-08-13 03:13:17 +00:00
let time_ms = denorm::TsLFO::time(time, frame).clamp(0.1, 300000.0);
2021-08-06 03:49:03 +00:00
2022-07-17 09:58:28 +00:00
lfo.set((1000.0 / time_ms) as f64, denorm::TsLFO::rev(rev, frame) as f64);
2021-08-06 03:49:03 +00:00
out.write(frame, lfo.next_unipolar() as f32);
}
ctx_vals[0].set(out.read(ctx.nframes() - 1));
}
fn graph_fun() -> Option<GraphFun> {
let mut lfo = TriSawLFO::new();
2021-08-06 04:01:11 +00:00
lfo.set_sample_rate(160.0);
2021-08-06 03:49:03 +00:00
2021-08-17 01:53:43 +00:00
Some(Box::new(move |gd: &dyn GraphAtomData, init: bool, _x: f32, _xn: f32| -> f32 {
2021-08-06 03:49:03 +00:00
if init {
lfo.reset();
2021-08-13 03:13:17 +00:00
let time_idx = NodeId::TsLFO(0).inp_param("time").unwrap().inp();
2022-07-17 09:58:28 +00:00
let rev_idx = NodeId::TsLFO(0).inp_param("rev").unwrap().inp();
2021-08-06 03:49:03 +00:00
let time = gd.get_norm(time_idx as u32).sqrt();
2022-07-17 09:58:28 +00:00
let rev = gd.get_norm(rev_idx as u32);
2021-08-06 04:01:11 +00:00
lfo.set(5.0 * (1.0 - time) + time * 1.0, rev);
2021-08-06 03:49:03 +00:00
}
lfo.next_unipolar() as f32
}))
}
}