1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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 crate::dsp::{
    DspNode, GraphAtomData, GraphFun, LedPhaseVals, NodeContext, NodeGlobalRef, NodeId, ProcBuf,
    SAtom,
};
use crate::nodes::{NodeAudioContext, NodeExecContext};
use synfx_dsp::{TriSawLFO, Trigger};

#[derive(Debug, Clone)]
pub struct TsLFO {
    lfo: Box<TriSawLFO<f64>>,
    trig: Trigger,
}

impl TsLFO {
    pub fn new(_nid: &NodeId, _node_global: &NodeGlobalRef) -> Self {
        Self { lfo: Box::new(TriSawLFO::new()), trig: Trigger::new() }
    }

    pub const time: &'static str = "The frequency or period time of the LFO, goes all the \
        way from **0.1ms** up to **30s**. Please note, that the text entry is always \
        in milliseconds.";
    pub const trig: &'static str = "Triggers a phase reset of the LFO.";
    pub const rev: &'static str = "The reverse point of the LFO waveform. At **0.5** the LFO \
        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.";
    pub const sig: &'static str = "The LFO output.";
    pub const DESC: &'static str = r#"TriSaw LFO

This simple LFO has a configurable waveform.
You can blend between triangular to sawtooth waveforms using the ~~rev~~ parameter.
"#;
    pub const HELP: &'static str = r#"TriSaw LFO

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.
"#;

    pub fn graph_fun() -> Option<GraphFun> {
        let mut lfo = TriSawLFO::new();
        lfo.set_sample_rate(160.0);

        Some(Box::new(move |gd: &dyn GraphAtomData, init: bool, _x: f32, _xn: f32| -> f32 {
            if init {
                lfo.reset();
                let time_idx = NodeId::TsLFO(0).inp_param("time").unwrap().inp();
                let rev_idx = NodeId::TsLFO(0).inp_param("rev").unwrap().inp();

                let time = gd.get_norm(time_idx as u32).sqrt();
                let rev = gd.get_norm(rev_idx as u32);
                lfo.set(5.0 * (1.0 - time) + time * 1.0, rev);
            }

            lfo.next_unipolar() as f32
        }))
    }
}

impl DspNode for TsLFO {
    fn set_sample_rate(&mut self, srate: f32) {
        self.lfo.set_sample_rate(srate as f64);
    }

    fn reset(&mut self) {
        self.lfo.reset();
        self.trig.reset();
    }

    #[inline]
    fn process(
        &mut self,
        ctx: &mut dyn NodeAudioContext,
        _ectx: &mut NodeExecContext,
        _nctx: &NodeContext,
        _atoms: &[SAtom],
        inputs: &[ProcBuf],
        outputs: &mut [ProcBuf],
        ctx_vals: LedPhaseVals,
    ) {
        use crate::dsp::{denorm, inp, out};

        let time = inp::TsLFO::time(inputs);
        let trig = inp::TsLFO::trig(inputs);
        let rev = inp::TsLFO::rev(inputs);
        let out = out::TsLFO::sig(outputs);

        let lfo = &mut *self.lfo;

        for frame in 0..ctx.nframes() {
            if self.trig.check_trigger(denorm::TsLFO::trig(trig, frame)) {
                lfo.reset();
            }

            let time_ms = denorm::TsLFO::time(time, frame).clamp(0.1, 300000.0);

            lfo.set((1000.0 / time_ms) as f64, denorm::TsLFO::rev(rev, frame) as f64);

            out.write(frame, lfo.next_unipolar() as f32);
        }

        ctx_vals[0].set(out.read(ctx.nframes() - 1));
    }
}