2021-07-10 20:16:55 +00:00
|
|
|
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
|
|
|
|
// This is a part of HexoDSP. Released under (A)GPLv3 or any later.
|
|
|
|
// See README.md and COPYING for details.
|
|
|
|
|
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
|
|
|
use crate::dsp::{NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext};
|
2021-07-10 20:45:08 +00:00
|
|
|
use crate::dsp::helpers::{
|
|
|
|
process_1pole_lowpass,
|
|
|
|
process_1pole_highpass,
|
|
|
|
process_1pole_tpt_lowpass,
|
|
|
|
process_1pole_tpt_highpass,
|
2021-07-12 17:54:01 +00:00
|
|
|
process_hal_chamberlin_svf,
|
2021-07-15 03:08:06 +00:00
|
|
|
process_simper_svf,
|
2021-07-10 20:45:08 +00:00
|
|
|
};
|
2021-07-10 20:16:55 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! fa_sfilter_type { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
|
|
|
let s =
|
|
|
|
match ($v.round() as usize) {
|
2021-07-12 17:54:01 +00:00
|
|
|
0 => "LP 1p",
|
|
|
|
1 => "LP 1pt",
|
|
|
|
2 => "HP 1p",
|
|
|
|
3 => "HP 1pt",
|
2021-07-14 03:47:05 +00:00
|
|
|
4 => "LP 12c",
|
|
|
|
5 => "HP 12c",
|
|
|
|
6 => "BP 12c",
|
|
|
|
7 => "NO 12c",
|
2021-07-15 03:08:06 +00:00
|
|
|
8 => "LP 12s",
|
|
|
|
9 => "HP 12s",
|
|
|
|
10 => "BP 12s",
|
|
|
|
11 => "NO 12s",
|
|
|
|
12 => "PK 12s",
|
2021-07-10 20:16:55 +00:00
|
|
|
_ => "?",
|
|
|
|
};
|
|
|
|
write!($formatter, "{}", s)
|
|
|
|
} } }
|
|
|
|
|
|
|
|
/// A simple amplifier
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct SFilter {
|
2021-07-15 04:28:44 +00:00
|
|
|
israte: f32,
|
|
|
|
z: f32,
|
|
|
|
y: f32,
|
|
|
|
k: f32,
|
|
|
|
h: f32,
|
2021-07-12 17:54:01 +00:00
|
|
|
otype: i8,
|
2021-07-10 20:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SFilter {
|
|
|
|
pub fn new(_nid: &NodeId) -> Self {
|
|
|
|
Self {
|
|
|
|
israte: 1.0 / 44100.0,
|
|
|
|
z: 0.0,
|
2021-07-10 20:34:03 +00:00
|
|
|
y: 0.0,
|
2021-07-15 04:28:44 +00:00
|
|
|
k: 0.0,
|
|
|
|
h: 0.0,
|
2021-07-12 17:54:01 +00:00
|
|
|
otype: -1,
|
2021-07-10 20:16:55 +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";
|
2021-07-12 17:54:01 +00:00
|
|
|
pub const res : &'static str =
|
|
|
|
"SFilter res\nFilter resonance.\nRange: (0..1)\n";
|
2021-07-10 20:16:55 +00:00
|
|
|
pub const ftype : &'static str =
|
2021-07-11 09:59:13 +00:00
|
|
|
"SFilter ftype\nThe filter type, there are varying types of \
|
|
|
|
filters available. Please consult the node documentation for \
|
|
|
|
a complete list.";
|
2021-07-10 20:16:55 +00:00
|
|
|
pub const sig : &'static str =
|
|
|
|
"SFilter sig\nFiltered signal output.\nRange: (-1..1)\n";
|
|
|
|
pub const DESC : &'static str =
|
|
|
|
r#"Simple Audio Filter
|
|
|
|
|
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
|
|
|
"#;
|
|
|
|
pub const HELP : &'static str =
|
|
|
|
r#"SFilter - Simple Audio Filter
|
|
|
|
|
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:
|
|
|
|
|
2021-07-12 17:54:01 +00:00
|
|
|
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 03:08:06 +00:00
|
|
|
|
2021-07-15 04:48:05 +00:00
|
|
|
The (Andrew) Simper state variable filter is a newer design
|
|
|
|
and stable up to 22kHz at 44.1kHz sampling rate.
|
2021-07-15 03:08:06 +00:00
|
|
|
|
|
|
|
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-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,
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-15 03:08:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
} };
|
2021-07-15 03:09:48 +00:00
|
|
|
($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);
|
|
|
|
}
|
|
|
|
} };
|
2021-07-15 03:08:06 +00:00
|
|
|
($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);
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
2021-07-10 20:16:55 +00:00
|
|
|
impl DspNode for SFilter {
|
|
|
|
fn outputs() -> usize { 1 }
|
|
|
|
|
|
|
|
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) {
|
2021-07-12 17:54:01 +00:00
|
|
|
self.z = 0.0;
|
|
|
|
self.y = 0.0;
|
2021-07-15 04:28:44 +00:00
|
|
|
self.k = 0.0;
|
|
|
|
self.h = 0.0;
|
2021-07-12 17:54:01 +00:00
|
|
|
self.otype = -1;
|
2021-07-10 20:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
|
|
|
&mut self, ctx: &mut T, _ectx: &mut NodeExecContext,
|
|
|
|
_nctx: &NodeContext,
|
|
|
|
atoms: &[SAtom], inputs: &[ProcBuf],
|
|
|
|
outputs: &mut [ProcBuf], ctx_vals: LedPhaseVals)
|
|
|
|
{
|
|
|
|
use crate::dsp::{out, inp, denorm, at};
|
|
|
|
|
|
|
|
let inp = inp::SFilter::inp(inputs);
|
|
|
|
let freq = inp::SFilter::freq(inputs);
|
2021-07-12 17:54:01 +00:00
|
|
|
let res = inp::SFilter::res(inputs);
|
2021-07-10 20:16:55 +00:00
|
|
|
let ftype = at::SFilter::ftype(atoms);
|
|
|
|
let out = out::SFilter::sig(outputs);
|
|
|
|
|
2021-07-12 17:54:01 +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;
|
2021-07-12 17:54:01 +00:00
|
|
|
self.otype = ftype;
|
|
|
|
}
|
|
|
|
|
|
|
|
match ftype {
|
|
|
|
0 => { // Lowpass
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
ctx.nframes(), inp, out, freq, input, 22000.0, {
|
2021-07-10 20:45:08 +00:00
|
|
|
process_1pole_lowpass(
|
|
|
|
input, freq, self.israte, &mut self.z)
|
2021-07-15 03:08:06 +00:00
|
|
|
})
|
2021-07-10 20:16:55 +00:00
|
|
|
},
|
2021-07-12 17:54:01 +00:00
|
|
|
1 => { // Lowpass TPT
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
ctx.nframes(), inp, out, freq, input, 22000.0, {
|
2021-07-10 20:45:08 +00:00
|
|
|
process_1pole_tpt_lowpass(
|
|
|
|
input, freq, self.israte, &mut self.z)
|
2021-07-15 03:08:06 +00:00
|
|
|
})
|
2021-07-10 20:45:08 +00:00
|
|
|
},
|
2021-07-12 17:54:01 +00:00
|
|
|
2 => { // Highpass
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
ctx.nframes(), inp, out, freq, input, 22000.0, {
|
2021-07-10 20:45:08 +00:00
|
|
|
process_1pole_highpass(
|
|
|
|
input, freq, self.israte, &mut self.z, &mut self.y)
|
2021-07-15 03:08:06 +00:00
|
|
|
})
|
2021-07-10 20:45:08 +00:00
|
|
|
},
|
2021-07-12 17:54:01 +00:00
|
|
|
3 => { // Highpass TPT
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
ctx.nframes(), inp, out, freq, input, 22000.0, {
|
2021-07-10 20:45:08 +00:00
|
|
|
process_1pole_tpt_highpass(
|
|
|
|
input, freq, self.israte, &mut self.z)
|
2021-07-15 03:08:06 +00:00
|
|
|
})
|
2021-07-10 20:34:03 +00:00
|
|
|
},
|
2021-07-12 17:54:01 +00:00
|
|
|
4 => { // Low Pass Hal Chamberlin SVF
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
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
|
|
|
|
});
|
2021-07-12 17:54:01 +00:00
|
|
|
},
|
|
|
|
5 => { // High Pass Hal Chamberlin SVF
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
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
|
|
|
|
});
|
2021-07-12 17:54:01 +00:00
|
|
|
},
|
|
|
|
6 => { // Band Pass Hal Chamberlin SVF
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
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
|
|
|
|
});
|
2021-07-12 17:54:01 +00:00
|
|
|
},
|
|
|
|
7 => { // Notch Hal Chamberlin SVF
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:08:06 +00:00
|
|
|
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
|
2021-07-15 04:28:44 +00:00
|
|
|
process_filter_fun32!(
|
2021-07-15 03:09:48 +00:00
|
|
|
ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
|
2021-07-15 03:08:06 +00:00
|
|
|
let (low, _band, _high) =
|
|
|
|
process_simper_svf(
|
|
|
|
input, freq, res, self.israte,
|
2021-07-15 04:28:44 +00:00
|
|
|
&mut self.k, &mut self.h);
|
2021-07-15 03:08:06 +00:00
|
|
|
low
|
|
|
|
});
|
2021-07-12 17:54:01 +00:00
|
|
|
},
|
2021-07-10 20:16:55 +00:00
|
|
|
_ => {},
|
|
|
|
}
|
2021-07-12 04:23:45 +00:00
|
|
|
|
|
|
|
ctx_vals[0].set(out.read(ctx.nframes() - 1));
|
2021-07-10 20:16:55 +00:00
|
|
|
}
|
|
|
|
}
|