HexoDSP/src/dsp/node_sfilter.rs

403 lines
15 KiB
Rust
Raw Normal View History

2021-07-10 20:16:55 +00:00
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
2021-08-04 01:58:43 +00:00
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
2021-07-10 20:16:55 +00:00
// See README.md and COPYING for details.
2022-08-07 19:28:45 +00:00
use crate::dsp::{DspNode, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom};
use crate::nodes::{NodeAudioContext, NodeExecContext};
use synfx_dsp::{
2022-07-17 09:58:28 +00:00
process_1pole_highpass, process_1pole_lowpass, process_1pole_tpt_highpass,
process_1pole_tpt_lowpass, process_hal_chamberlin_svf, process_simper_svf,
process_stilson_moog,
};
2021-07-10 20:16:55 +00:00
#[macro_export]
2022-07-17 09:58:28 +00:00
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",
_ => "?",
2021-07-10 20:16:55 +00:00
};
2022-07-17 09:58:28 +00:00
write!($formatter, "{}", s)
}};
}
2021-07-10 20:16:55 +00:00
/// A simple amplifier
#[derive(Debug, Clone)]
pub struct SFilter {
2021-07-15 04:28:44 +00:00
israte: f32,
2022-07-17 09:58:28 +00:00
z: f32,
y: f32,
k: f32,
h: f32,
delay: [f32; 4],
otype: i8,
2021-07-10 20:16:55 +00:00
}
impl SFilter {
pub fn new(_nid: &NodeId) -> Self {
2022-07-17 09:58:28 +00:00
Self { israte: 1.0 / 44100.0, z: 0.0, y: 0.0, k: 0.0, h: 0.0, delay: [0.0; 4], otype: -1 }
2021-07-10 20:16:55 +00:00
}
2022-07-17 09:58:28 +00:00
pub const inp: &'static str = "SFilter inp\nSignal input\nRange: (-1..1)\n";
pub const freq: &'static str = "SFilter freq\nFilter cutoff frequency.\nRange: (-1..1)\n";
pub const res: &'static str = "SFilter res\nFilter resonance.\nRange: (0..1)\n";
pub const ftype: &'static str = "SFilter ftype\nThe filter type, there are varying types of \
2021-07-11 09:59:13 +00:00
filters available. Please consult the node documentation for \
2021-07-18 12:04:26 +00:00
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";
2022-07-17 09:58:28 +00:00
pub const sig: &'static str = "SFilter sig\nFiltered signal output.\nRange: (-1..1)\n";
pub const DESC: &'static str = r#"Simple Filter
2021-07-10 20:16:55 +00:00
2021-07-11 09:59:13 +00:00
This is a collection of more or less simple filters.
There are only two parameters: Filter cutoff 'freq' and the 'res'onance.
2021-07-10 20:16:55 +00:00
"#;
2022-07-17 09:58:28 +00:00
pub const HELP: &'static str = r#"SFilter - Simple Audio Filter Collection
2021-07-10 20:16:55 +00:00
2021-07-11 09:59:13 +00:00
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'onance. 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)
2021-07-14 03:47:05 +00:00
The Hal Chamberlin filters are an older state variable filter design,
2021-07-15 04:48:05 +00:00
that is limited to max cutoff frequency of 16kHz. For a more stable
filter use the "12s" variants.
2021-07-14 03:47:05 +00:00
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)
2021-07-15 04:48:05 +00:00
The (Andrew) Simper state variable filter is a newer design
2021-07-17 08:03:10 +00:00
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)
2021-07-18 12:04:26 +00:00
Next page: more filters (eg. Moog)
---page---
SFilter - Simple Audio Filter Collection
For a more colored filter reach for the Stilson/Moog filter with a 24dB
2021-08-06 03:05:47 +00:00
fall off per octave. Beware high cutoff frequencies for this filter,
as it can become quite unstable.
2021-07-18 12:04:26 +00:00
LP 24m - Low-pass Stilson/Moog filter (24dB)
2021-07-10 20:16:55 +00:00
"#;
}
2021-07-15 04:28:44 +00:00
macro_rules! process_filter_fun32 {
($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
2022-07-17 09:58:28 +00:00
$input: ident, $minfreq: expr, $maxfreq: expr, $block: block) => {{
2021-07-15 04:28:44 +00:00
for frame in 0..$nframes {
let $input = $inp.read(frame);
2022-07-17 09:58:28 +00:00
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);
2021-07-15 04:28:44 +00:00
let s = $block;
$out.write(frame, s);
}
2022-07-17 09:58:28 +00:00
}};
2021-07-15 04:28:44 +00:00
($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
2022-07-17 09:58:28 +00:00
$input: ident, $maxfreq: expr, $block: block) => {{
2021-07-15 04:28:44 +00:00
for frame in 0..$nframes {
let $input = $inp.read(frame);
2022-07-17 09:58:28 +00:00
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);
2021-07-15 04:28:44 +00:00
let s = $block;
$out.write(frame, s);
}
2022-07-17 09:58:28 +00:00
}};
2021-07-15 04:28:44 +00:00
($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
2022-07-17 09:58:28 +00:00
$maxres: expr, $input: ident, $maxfreq: expr, $block: block) => {{
2021-07-15 04:28:44 +00:00
for frame in 0..$nframes {
let $input = $inp.read(frame);
2022-07-17 09:58:28 +00:00
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);
2021-07-15 04:28:44 +00:00
let s = $block;
$out.write(frame, s);
}
2022-07-17 09:58:28 +00:00
}};
2021-07-15 04:28:44 +00:00
($nframes: expr, $inp: expr, $out: ident, $freq: ident,
2022-07-17 09:58:28 +00:00
$input: ident, $maxfreq: expr, $block: block) => {{
2021-07-15 04:28:44 +00:00
for frame in 0..$nframes {
let $input = $inp.read(frame);
2022-07-17 09:58:28 +00:00
let $freq = denorm::SFilter::freq($freq, frame);
let $freq = $freq.clamp(1.0, $maxfreq);
2021-07-15 04:28:44 +00:00
let s = $block;
$out.write(frame, s);
}
2022-07-17 09:58:28 +00:00
}};
2021-07-15 04:28:44 +00:00
}
macro_rules! process_filter_fun {
($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
2022-07-17 09:58:28 +00:00
$input: ident, $minfreq: expr, $maxfreq: expr, $block: block) => {{
for frame in 0..$nframes {
let $input = $inp.read(frame) as f64;
2022-07-17 09:58:28 +00:00
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);
}
2022-07-17 09:58:28 +00:00
}};
($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
2022-07-17 09:58:28 +00:00
$input: ident, $maxfreq: expr, $block: block) => {{
for frame in 0..$nframes {
let $input = $inp.read(frame) as f64;
2022-07-17 09:58:28 +00:00
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);
}
2022-07-17 09:58:28 +00:00
}};
2021-07-15 03:09:48 +00:00
($nframes: expr, $inp: expr, $out: ident, $freq: ident, $res: ident,
2022-07-17 09:58:28 +00:00
$maxres: expr, $input: ident, $maxfreq: expr, $block: block) => {{
2021-07-15 03:09:48 +00:00
for frame in 0..$nframes {
let $input = $inp.read(frame) as f64;
2022-07-17 09:58:28 +00:00
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);
2021-07-15 03:09:48 +00:00
let s = $block;
$out.write(frame, s as f32);
}
2022-07-17 09:58:28 +00:00
}};
($nframes: expr, $inp: expr, $out: ident, $freq: ident,
2022-07-17 09:58:28 +00:00
$input: ident, $maxfreq: expr, $block: block) => {{
for frame in 0..$nframes {
let $input = $inp.read(frame) as f64;
2022-07-17 09:58:28 +00:00
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);
}
2022-07-17 09:58:28 +00:00
}};
}
2021-07-10 20:16:55 +00:00
impl DspNode for SFilter {
2022-07-17 09:58:28 +00:00
fn outputs() -> usize {
1
}
2021-07-10 20:16:55 +00:00
fn set_sample_rate(&mut self, srate: f32) {
2021-07-15 04:28:44 +00:00
self.israte = 1.0 / srate;
2021-07-10 20:16:55 +00:00
}
fn reset(&mut self) {
2022-07-17 09:58:28 +00:00
self.z = 0.0;
self.y = 0.0;
self.k = 0.0;
self.h = 0.0;
self.delay = [0.0; 4];
self.otype = -1;
2021-07-10 20:16:55 +00:00
}
#[inline]
fn process<T: NodeAudioContext>(
2022-07-17 09:58:28 +00:00
&mut self,
ctx: &mut T,
_ectx: &mut NodeExecContext,
2021-07-10 20:16:55 +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::{at, denorm, inp, out};
2021-07-10 20:16:55 +00:00
2022-07-17 09:58:28 +00:00
let inp = inp::SFilter::inp(inputs);
let freq = inp::SFilter::freq(inputs);
let res = inp::SFilter::res(inputs);
2021-07-10 20:16:55 +00:00
let ftype = at::SFilter::ftype(atoms);
2022-07-17 09:58:28 +00:00
let out = out::SFilter::sig(outputs);
2021-07-10 20:16:55 +00:00
let ftype = ftype.i() as i8;
if ftype != self.otype {
self.y = 0.0;
self.z = 0.0;
2021-07-15 04:28:44 +00:00
self.k = 0.0;
self.h = 0.0;
self.delay = [0.0; 4];
self.otype = ftype;
}
match ftype {
2022-07-17 09:58:28 +00:00
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,
)
});
}
_ => {}
2021-07-10 20:16:55 +00:00
}
ctx_vals[0].set(out.read(ctx.nframes() - 1));
2021-07-10 20:16:55 +00:00
}
}