2021-08-05 03:20:47 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
use crate::dsp::{DspNode, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom};
|
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
2022-08-07 19:28:45 +00:00
|
|
|
use synfx_dsp::*;
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_biqfilt_type {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "BtW LP",
|
|
|
|
1 => "Res",
|
|
|
|
_ => "?",
|
2021-08-05 03:20:47 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_biqfilt_ord {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "1",
|
|
|
|
1 => "2",
|
|
|
|
2 => "3",
|
|
|
|
3 => "4",
|
|
|
|
_ => "?",
|
2021-08-05 03:20:47 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
/// A simple amplifier
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct BiqFilt {
|
|
|
|
cascade: Vec<Biquad>,
|
2022-07-17 09:58:28 +00:00
|
|
|
srate: f32,
|
|
|
|
ofreq: f32,
|
|
|
|
oq: f32,
|
|
|
|
ogain: f32,
|
|
|
|
otype: u8,
|
2021-08-05 03:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BiqFilt {
|
|
|
|
pub fn new(_nid: &NodeId) -> Self {
|
|
|
|
Self {
|
|
|
|
cascade: vec![Biquad::new(); 4],
|
|
|
|
srate: 1.0 / 44100.0,
|
|
|
|
otype: 99, // value that can't be set by the user
|
|
|
|
ofreq: -2.0, // value that can't be set by the user
|
2022-07-17 09:58:28 +00:00
|
|
|
oq: -2.0, // value that can't be set by the user
|
2021-08-05 03:20:47 +00:00
|
|
|
ogain: -2.0, // value that can't be set by the user
|
|
|
|
}
|
|
|
|
}
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const inp: &'static str = "BiqFilt inp\nSignal input\nRange: (-1..1)\n";
|
|
|
|
pub const freq: &'static str = "BiqFilt freq\nFilter cutoff frequency.\nRange: (-1..1)\n";
|
|
|
|
pub const q: &'static str = "BiqFilt q\nFilter Q factor.\nRange: (0..1)\n";
|
|
|
|
pub const gain: &'static str = "BiqFilt gain\nFilter gain.\nRange: (0..1)\n";
|
|
|
|
pub const ftype: &'static str = "BiqFilt ftype\n'BtW LP' Butterworth Low-Pass, 'Res' Resonator";
|
|
|
|
pub const order: &'static str = "BiqFilt order\n";
|
|
|
|
pub const sig: &'static str = "BiqFilt sig\nFiltered signal output.\nRange: (-1..1)\n";
|
|
|
|
pub const DESC: &'static str = r#"Biquad Filter
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
This is the implementation of a biquad filter cascade.
|
|
|
|
It is not meant for fast automation. Please use other nodes
|
|
|
|
like eg. SFilter for that.
|
|
|
|
"#;
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const HELP: &'static str = r#"BiqFilt - Biquad Filter (Cascade)
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
This is the implementation of a biquad filter cascade.
|
|
|
|
It is not meant for fast automation and might blow up if you
|
|
|
|
treat it too rough. Please use other nodes like eg. SFilter for that.
|
|
|
|
"#;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DspNode for BiqFilt {
|
2022-07-17 09:58:28 +00:00
|
|
|
fn outputs() -> usize {
|
|
|
|
1
|
|
|
|
}
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
fn set_sample_rate(&mut self, srate: f32) {
|
|
|
|
self.srate = srate;
|
|
|
|
self.otype = 99; // cause recalculation of the filter
|
|
|
|
|
|
|
|
for b in &mut self.cascade {
|
|
|
|
b.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
for b in &mut self.cascade {
|
|
|
|
b.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
2022-07-17 09:58:28 +00:00
|
|
|
&mut self,
|
|
|
|
ctx: &mut T,
|
|
|
|
_ectx: &mut NodeExecContext,
|
2021-08-05 03:20:47 +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};
|
|
|
|
|
|
|
|
let inp = inp::BiqFilt::inp(inputs);
|
|
|
|
let freq = inp::BiqFilt::freq(inputs);
|
|
|
|
let q = inp::BiqFilt::q(inputs);
|
|
|
|
let gain = inp::BiqFilt::gain(inputs);
|
2021-08-05 03:20:47 +00:00
|
|
|
let ftype = at::BiqFilt::ftype(atoms);
|
|
|
|
let order = at::BiqFilt::order(atoms);
|
2022-07-17 09:58:28 +00:00
|
|
|
let out = out::BiqFilt::sig(outputs);
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
let ftype = ftype.i() as u8;
|
|
|
|
let cfreq = denorm::BiqFilt::freq(freq, 0);
|
|
|
|
let cfreq = cfreq.clamp(0.0, 22000.0);
|
2022-07-17 09:58:28 +00:00
|
|
|
let cq = denorm::BiqFilt::q(q, 0);
|
2021-08-05 03:20:47 +00:00
|
|
|
let cgain = denorm::BiqFilt::gain(gain, 0);
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
if ftype != self.otype
|
|
|
|
|| (cfreq - self.ofreq).abs() > 0.0001
|
|
|
|
|| (cq - self.oq).abs() > 0.0001
|
|
|
|
|| (cgain - self.ogain).abs() > 0.0001
|
2021-08-05 03:20:47 +00:00
|
|
|
{
|
|
|
|
// recalculate coeffs of all in the cascade
|
2022-07-17 09:58:28 +00:00
|
|
|
let coefs = match ftype {
|
|
|
|
1 => BiquadCoefs::resonator(self.srate, cfreq, cq),
|
|
|
|
_ => BiquadCoefs::butter_lowpass(self.srate, cfreq),
|
|
|
|
};
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
for o in &mut self.cascade {
|
|
|
|
o.set_coefs(coefs);
|
|
|
|
o.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.otype = ftype;
|
|
|
|
self.ofreq = cfreq;
|
2022-07-17 09:58:28 +00:00
|
|
|
self.oq = cq;
|
2021-08-05 03:20:47 +00:00
|
|
|
self.ogain = cgain;
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:25:10 +00:00
|
|
|
let mut order = order.i() as u8;
|
2022-07-17 09:58:28 +00:00
|
|
|
if ftype == 1 {
|
|
|
|
// The resonator just blows up with higher orders.
|
2021-08-05 03:25:10 +00:00
|
|
|
order = 0;
|
|
|
|
}
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
for frame in 0..ctx.nframes() {
|
2022-07-17 09:58:28 +00:00
|
|
|
// let freq = denorm::BiqFilt::freq(freq, frame);
|
|
|
|
// let freq = freq.clamp($minfreq, $maxfreq);
|
|
|
|
// let q = denorm::BiqFilt::q(q, frame);
|
|
|
|
// let gain = denorm::BiqFilt::gain(gain, frame);
|
2021-08-05 03:20:47 +00:00
|
|
|
|
|
|
|
let mut s = inp.read(frame);
|
2021-08-05 03:22:46 +00:00
|
|
|
for i in 0..=order {
|
2021-08-05 03:20:47 +00:00
|
|
|
s = self.cascade[i as usize].tick(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.write(frame, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx_vals[0].set(out.read(ctx.nframes() - 1));
|
|
|
|
}
|
|
|
|
}
|