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
// 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, GraphFun, LedPhaseVals, NodeContext, NodeGlobalRef, NodeId, ProcBuf, SAtom,
};
use crate::nodes::{NodeAudioContext, NodeExecContext};
use synfx_dsp::AllPass;

/// A simple amplifier
#[derive(Debug, Clone)]
pub struct AllP {
    allpass: Box<AllPass<f64>>,
}

impl AllP {
    pub fn new(_nid: &NodeId, _node_global: &NodeGlobalRef) -> Self {
        Self { allpass: Box::new(AllPass::new()) }
    }

    pub const inp: &'static str = "The signal input for the allpass filter.";
    pub const g: &'static str = "The internal factor for the allpass filter.";
    pub const time: &'static str = "The allpass delay time.";
    pub const sig: &'static str = "The output of allpass filter.";

    pub const DESC: &'static str = r#"Single Allpass Filter
This is an allpass filter that can be used to build reverbs
or anything you might find it useful for.
"#;
    pub const HELP: &'static str = r#"A Simple Single Allpass Filter

This is an allpass filter that can be used to build reverbs
or anything you might find it useful for.

Typical arrangements are (Schroeder Reverb):

```text
                    t=4.5ms
                    g=0.7   -> Comb
    AllP -> AllP -> AllP -> -> Comb
    t=42ms  t=13.5ms        -> Comb
    g=0.7   g=0.7           -> Comb
```

Or:

```text
    Comb ->                 t=0.48ms
    Comb ->                 g=0.7
    Comb -> AllP -> AllP -> AllP
    Comb -> t=5ms   t=1.68ms
            g=0.7   g=0.7
```

Typical values for the comb filters are in the range ~~g~~=**0.6** to **0.9**
and time in the range of **30ms** to **250ms**.

Feel free to deviate from this and experiment around.

Building your own reverbs is fun!

(And don't forget that you can create feedback using the `FbWr` and `FbRd` nodes!)
"#;

    pub fn graph_fun() -> Option<GraphFun> {
        None
    }
}

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

    fn reset(&mut self) {
        self.allpass.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 inp = inp::AllP::inp(inputs);
        let time = inp::AllP::time(inputs);
        let g = inp::AllP::g(inputs);
        let out = out::AllP::sig(outputs);

        let ap = &mut *self.allpass;

        for frame in 0..ctx.nframes() {
            let v = inp.read(frame);

            out.write(
                frame,
                ap.next(
                    denorm::AllP::time(time, frame) as f64,
                    denorm::AllP::g(g, frame) as f64,
                    v as f64,
                ) as f32,
            );
        }

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