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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// 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::{
    process_1pole_highpass, process_1pole_lowpass, process_1pole_tpt_highpass,
    process_1pole_tpt_lowpass, process_hal_chamberlin_svf, process_simper_svf,
    process_stilson_moog,
};

#[macro_export]
macro_rules! fa_sfilter_type {
    ($formatter: expr, $v: expr, $denorm_v: expr) => {{
        let s = match ($v.round() as usize) {
            0 => "LP 1p",
            1 => "LP 1pt",
            2 => "HP 1p",
            3 => "HP 1pt",
            4 => "LP 12c",
            5 => "HP 12c",
            6 => "BP 12c",
            7 => "NO 12c",
            8 => "LP 12s",
            9 => "HP 12s",
            10 => "BP 12s",
            11 => "NO 12s",
            12 => "PK 12s",
            13 => "LP 24m",
            _ => "?",
        };
        write!($formatter, "{}", s)
    }};
}

/// A simple amplifier
#[derive(Debug, Clone)]
pub struct SFilter {
    israte: f32,
    z: f32,
    y: f32,
    k: f32,
    h: f32,
    delay: [f32; 4],
    otype: i8,
}

impl SFilter {
    pub fn new(_nid: &NodeId, _node_global: &NodeGlobalRef) -> Self {
        Self { israte: 1.0 / 44100.0, z: 0.0, y: 0.0, k: 0.0, h: 0.0, delay: [0.0; 4], otype: -1 }
    }
    pub const inp: &'static str = "Signal input";
    pub const freq: &'static str = "Filter cutoff frequency.";
    pub const res: &'static str = "Filter resonance.";
    pub const ftype: &'static str = "The filter type, there are varying types of \
        filters available. Please consult the node documentation for \
        a complete list.\n\
        Types: **1p/1pt**=one poles, **12c**=Hal Chamberlin SVF,\n\
        **12s**=Simper SVF, **24m**=Moog\n\
        Outputs: **LP**=Low-,**HP**=High-,**BP**=Band-Pass,**NO**=Notch,**PK**=Peak";
    pub const sig: &'static str = "Filtered signal output.";
    pub const DESC: &'static str = r#"Simple Filter

This is a collection of more or less simple filters.
There are only two parameters: Filter cutoff ~~freq~~ and the ~~res~~ resonance.
"#;
    pub const HELP: &'static str = r#"Simple Audio Filter Collection

This is a collection of a few more or less simple filters
of varying types. There are only few parameters for you to change: ~~freq~~
and ~~res~~ resonance. You can switch between the types with the ~~ftype~~.
There are currently following filters available:

- **HP 1p** - One pole low-pass filter (6db)
- **HP 1pt** - One pole low-pass filter (6db) (TPT form)
- **LP 1p** - One pole high-pass filter (6db)
- **LP 1pt** - One pole high-pass filter (6db) (TPT form)

The Hal Chamberlin filters are an older state variable filter design,
that is limited to max cutoff frequency of 16kHz. For a more stable
filter use the "12s" variants.

- **LP 12c** - Low-pass Hal Chamberlin state variable filter (12dB)
- **HP 12c** - High-pass Hal Chamberlin state variable filter (12dB)
- **BP 12c** - Band-pass Hal Chamberlin state variable filter (12dB)
- **NO 12c** - Notch Hal Chamberlin state variable filter (12dB)

The (Andrew) Simper state variable filter is a newer design
and stable up to 22kHz at 44.1kHz sampling rate. It's overall more precise
and less quirky than the Hal Chamberlin SVF.

- **LP 12s** - Low-pass Simper state variable filter (12dB)
- **HP 12s** - High-pass Simper state variable filter (12dB)
- **BP 12s** - Band-pass Simper state variable filter (12dB)
- **NO 12s** - Notch Simper state variable filter (12dB)
- **PK 12s** - Peak Simper state variable filter (12dB)

For a more colored filter reach for the Stilson/Moog filter with a 24dB
fall off per octave. Beware high cutoff frequencies for this filter,
as it can become quite unstable.

- **LP 24m** - Low-pass Stilson/Moog filter (24dB)

"#;

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

macro_rules! process_filter_fun32 {
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
     $input: ident, $minfreq: expr, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame);
            let $freq = denorm::SFilter::freq($freq, frame);
            let $freq = $freq.clamp($minfreq, $maxfreq);
            let $res = denorm::SFilter::res($res, frame);
            let $res = $res.clamp(0.0, 0.99);
            let s = $block;
            $out.write(frame, s);
        }
    }};
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
     $input: ident, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame);
            let $freq = denorm::SFilter::freq($freq, frame);
            let $freq = $freq.clamp(1.0, $maxfreq);
            let $res = denorm::SFilter::res($res, frame);
            let $res = $res.clamp(0.0, 0.99);
            let s = $block;
            $out.write(frame, s);
        }
    }};
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
     $maxres: expr, $input: ident, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame);
            let $freq = denorm::SFilter::freq($freq, frame);
            let $freq = $freq.clamp(1.0, $maxfreq);
            let $res = denorm::SFilter::res($res, frame);
            let $res = $res.clamp(0.0, $maxres);
            let s = $block;
            $out.write(frame, s);
        }
    }};
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident,
     $input: ident, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame);
            let $freq = denorm::SFilter::freq($freq, frame);
            let $freq = $freq.clamp(1.0, $maxfreq);
            let s = $block;
            $out.write(frame, s);
        }
    }};
}

macro_rules! process_filter_fun {
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
     $input: ident, $minfreq: expr, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame) as f64;
            let $freq = denorm::SFilter::freq($freq, frame) as f64;
            let $freq = $freq.clamp($minfreq, $maxfreq);
            let $res = denorm::SFilter::res($res, frame) as f64;
            let $res = $res.clamp(0.0, 0.99);
            let s = $block;
            $out.write(frame, s as f32);
        }
    }};
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
     $input: ident, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame) as f64;
            let $freq = denorm::SFilter::freq($freq, frame) as f64;
            let $freq = $freq.clamp(1.0, $maxfreq);
            let $res = denorm::SFilter::res($res, frame) as f64;
            let $res = $res.clamp(0.0, 0.99);
            let s = $block;
            $out.write(frame, s as f32);
        }
    }};
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
     $maxres: expr, $input: ident, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame) as f64;
            let $freq = denorm::SFilter::freq($freq, frame) as f64;
            let $freq = $freq.clamp(1.0, $maxfreq);
            let $res = denorm::SFilter::res($res, frame) as f64;
            let $res = $res.clamp(0.0, $maxres);
            let s = $block;
            $out.write(frame, s as f32);
        }
    }};
    ($nframes: expr, $inp: expr, $out: ident, $freq: ident,
     $input: ident, $maxfreq: expr, $block: block) => {{
        for frame in 0..$nframes {
            let $input = $inp.read(frame) as f64;
            let $freq = denorm::SFilter::freq($freq, frame) as f64;
            let $freq = $freq.clamp(1.0, $maxfreq);
            let s = $block;
            $out.write(frame, s as f32);
        }
    }};
}

impl DspNode for SFilter {
    fn set_sample_rate(&mut self, srate: f32) {
        self.israte = 1.0 / srate;
    }
    fn reset(&mut self) {
        self.z = 0.0;
        self.y = 0.0;
        self.k = 0.0;
        self.h = 0.0;
        self.delay = [0.0; 4];
        self.otype = -1;
    }

    #[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::{at, denorm, inp, out};

        let inp = inp::SFilter::inp(inputs);
        let freq = inp::SFilter::freq(inputs);
        let res = inp::SFilter::res(inputs);
        let ftype = at::SFilter::ftype(atoms);
        let out = out::SFilter::sig(outputs);

        let ftype = ftype.i() as i8;

        if ftype != self.otype {
            self.y = 0.0;
            self.z = 0.0;
            self.k = 0.0;
            self.h = 0.0;
            self.delay = [0.0; 4];
            self.otype = ftype;
        }

        match ftype {
            0 => {
                // Lowpass
                process_filter_fun32!(ctx.nframes(), inp, out, freq, input, 22000.0, {
                    process_1pole_lowpass(input, freq, self.israte, &mut self.z)
                })
            }
            1 => {
                // Lowpass TPT
                process_filter_fun32!(ctx.nframes(), inp, out, freq, input, 22000.0, {
                    process_1pole_tpt_lowpass(input, freq, self.israte, &mut self.z)
                })
            }
            2 => {
                // Highpass
                process_filter_fun32!(ctx.nframes(), inp, out, freq, input, 22000.0, {
                    process_1pole_highpass(input, freq, self.israte, &mut self.z, &mut self.y)
                })
            }
            3 => {
                // Highpass TPT
                process_filter_fun32!(ctx.nframes(), inp, out, freq, input, 22000.0, {
                    process_1pole_tpt_highpass(input, freq, self.israte, &mut self.z)
                })
            }
            4 => {
                // Low Pass Hal Chamberlin SVF
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, input, 2.0, 16000.0, {
                    let (_high, _notch) = process_hal_chamberlin_svf(
                        input,
                        freq,
                        res,
                        self.israte,
                        &mut self.z,
                        &mut self.y,
                    );
                    self.y
                });
            }
            5 => {
                // High Pass Hal Chamberlin SVF
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, input, 16000.0, {
                    let (high, _notch) = process_hal_chamberlin_svf(
                        input,
                        freq,
                        res,
                        self.israte,
                        &mut self.z,
                        &mut self.y,
                    );
                    high
                });
            }
            6 => {
                // Band Pass Hal Chamberlin SVF
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, input, 16000.0, {
                    let (_high, _notch) = process_hal_chamberlin_svf(
                        input,
                        freq,
                        res,
                        self.israte,
                        &mut self.z,
                        &mut self.y,
                    );
                    self.z
                });
            }
            7 => {
                // Notch Hal Chamberlin SVF
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, input, 16000.0, {
                    let (_high, notch) = process_hal_chamberlin_svf(
                        input,
                        freq,
                        res,
                        self.israte,
                        &mut self.z,
                        &mut self.y,
                    );
                    notch
                });
            }
            8 => {
                // Simper SVF Low Pass
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
                    let (low, _band, _high) =
                        process_simper_svf(input, freq, res, self.israte, &mut self.k, &mut self.h);
                    low
                });
            }
            9 => {
                // Simper SVF High Pass
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
                    let (_low, _band, high) =
                        process_simper_svf(input, freq, res, self.israte, &mut self.k, &mut self.h);
                    high
                });
            }
            10 => {
                // Simper SVF Band Pass
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
                    let (_low, band, _high) =
                        process_simper_svf(input, freq, res, self.israte, &mut self.k, &mut self.h);
                    band
                });
            }
            11 => {
                // Simper SVF Notch
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
                    let (low, _band, high) =
                        process_simper_svf(input, freq, res, self.israte, &mut self.k, &mut self.h);
                    low + high
                });
            }
            12 => {
                // Simper SVF Peak
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
                    let (low, _band, high) =
                        process_simper_svf(input, freq, res, self.israte, &mut self.k, &mut self.h);
                    low - high
                });
            }
            13 => {
                // Stilson/Moog Low Pass
                process_filter_fun32!(ctx.nframes(), inp, out, freq, res, 1.0, input, 20000.0, {
                    // Clip here, to prevent blowups, because the
                    // moog filter is quite touchy...
                    let input = input.clamp(-1.0, 1.0);
                    process_stilson_moog(
                        input,
                        freq,
                        res,
                        self.israte,
                        &mut self.z,
                        &mut self.y,
                        &mut self.k,
                        &mut self.h,
                        &mut self.delay,
                    )
                });
            }
            _ => {}
        }

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