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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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::TrigSignal;

#[macro_export]
macro_rules! fa_test_s {
    ($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",
            _ => "?",
        };
        write!($formatter, "{}", s)
    }};
}

/// A simple amplifier
#[derive(Debug, Clone)]
pub struct Test {
    trig_sig: TrigSignal,
    trigger: bool,
}

impl Test {
    pub fn new(_nid: &NodeId, _node_global: &NodeGlobalRef) -> Self {
        Self { trigger: false, trig_sig: TrigSignal::new() }
    }

    pub const f: &'static str = "F Test";
    pub const p: &'static str = "An unsmoothed parameter for automated tests.";
    pub const trig: &'static str =
        "A trigger input, that will create a short pulse on the ~~tsig~~ output.";
    pub const sig: &'static str = "The output of p as signal";
    pub const tsig: &'static str =
        "A short trigger pulse will be generated when the ~~trig~~ input is triggered.";
    pub const out2: &'static str =
        "A test output that will emit **1.0** if output ~~sig~~ is connected.";
    pub const out3: &'static str =
        "A test output that will emit **1.0** if input ~~f~~ is connected.";
    pub const out4: &'static str = "";
    pub const outc: &'static str =
        "Emits a number that defines the out_connected bitmask. Used only for testing!";

    pub const DESC: &'static str = r#""#;
    pub const HELP: &'static str = r#""#;

    pub fn graph_fun() -> Option<GraphFun> {
        Some(Box::new(|_gd: &dyn GraphAtomData, _init: bool, x: f32, _xn: f32| -> f32 { x }))
    }
}

impl DspNode for Test {
    fn set_sample_rate(&mut self, srate: f32) {
        self.trig_sig.set_sample_rate(srate);
    }

    fn reset(&mut self) {
        self.trig_sig.reset();
    }

    #[inline]
    fn process(
        &mut self,
        ctx: &mut dyn NodeAudioContext,
        _ectx: &mut NodeExecContext,
        nctx: &NodeContext,
        atoms: &[SAtom],
        _inputs: &[ProcBuf],
        outputs: &mut [ProcBuf],
        _led: LedPhaseVals,
    ) {
        use crate::dsp::{at, is_in_con, is_out_con, out_buf, out_idx};

        let p = at::Test::p(atoms);
        let trig = at::Test::trig(atoms);
        let tsig = out_idx::Test::tsig();

        let mut out2 = out_buf::Test::out2(outputs);
        let mut out3 = out_buf::Test::out3(outputs);
        let mut outc = out_buf::Test::outc(outputs);

        let (out, tsig) = outputs.split_at_mut(tsig);
        let out = &mut out[0];
        let tsig = &mut tsig[0];

        let mut trigger = trig.i();
        if !self.trigger && trigger > 0 {
            self.trigger = true;
        } else if !self.trigger && trigger == 0 {
            self.trigger = false;
        } else if self.trigger {
            trigger = 0;
        }

        for frame in 0..ctx.nframes() {
            if trigger > 0 {
                self.trig_sig.trigger();
                trigger = 0;
            }

            out.write(frame, p.f());
            let t = self.trig_sig.next();
            tsig.write(frame, t);

            out2.write(frame, if is_out_con::Test::sig(nctx) { 1.0 } else { 0.0 });
            out3.write(frame, if is_in_con::Test::f(nctx) { 1.0 } else { 0.0 });
            outc.write(frame, nctx.out_connected as f32);
        }
    }
}