more moog filter code

This commit is contained in:
Weird Constructor 2021-07-18 14:04:26 +02:00
parent c4495d6039
commit b7349ccae4
3 changed files with 237 additions and 14 deletions

View file

@ -1049,19 +1049,20 @@ pub fn process_simper_svf(
// Stilson/Moog implementation partly translated from SynthV1 by rncbc
// https://github.com/rncbc/synthv1/blob/master/src/synthv1_filter.h#L103
// under GPLv2 or any later.
//
// It's also found on MusicDSP and has probably no proper license anyways.
// See also: https://github.com/ddiakopoulos/MoogLadders
// and https://github.com/ddiakopoulos/MoogLadders/blob/master/src/MusicDSPModel.h
#[inline]
pub fn process_stilson_moog(
input: f32, freq: f32, res: f32, israte: f32,
b0: &mut f32, b1: &mut f32, b2: &mut f32, b3: &mut f32, b4: &mut f32
) -> (f32, f32, f32, f32) {
// let cutoff = freq * israte;
// let cutoff = (std::f32::consts::PI * freq * israte).tan();
let cutoff = 2.0 * freq * israte;
let c = 1.0 - cutoff;
let p = cutoff + 0.8 * cutoff * c;
let f = p + p - 1.0;
// let f = 2.0 * cutoff.sin() - 1.0;
let q = res * (1.0 + 0.5 * c * (1.0 - c + 5.6 * c * c));
let inp = input - q * *b4;

View file

@ -73,17 +73,20 @@ impl SFilter {
pub const ftype : &'static str =
"SFilter ftype\nThe filter type, there are varying types of \
filters available. Please consult the node documentation for \
a complete list.";
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 =
"SFilter sig\nFiltered signal output.\nRange: (-1..1)\n";
pub const DESC : &'static str =
r#"Simple Audio Filter
r#"Simple Filter
This is a collection of more or less simple filters.
There are only two parameters: Filter cutoff 'freq' and the 'res'onance.
"#;
pub const HELP : &'static str =
r#"SFilter - Simple Audio Filter
r#"SFilter - 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'
@ -113,6 +116,19 @@ and less quirky than the Hal Chamberlin SVF.
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)
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
fall off per octave.
LP 24m - Low-pass Stilson/Moog filter (24dB)
HP 24m - High-pass Stilson/Moog filter (24dB)
BP 24m - Band-pass Stilson/Moog filter (24dB)
NO 24m - Notch Stilson/Moog filter (24dB)
"#;
}
@ -383,10 +399,42 @@ impl DspNode for SFilter {
input, freq, res, self.israte,
&mut self.z, &mut self.y, &mut self.k,
&mut self.h, &mut self.m);
low
});
},
14 => { // Stilson/Moog High Pass
process_filter_fun32!(
ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
let (_low, _band, high, _notch) =
process_stilson_moog(
input, freq, res, self.israte,
&mut self.z, &mut self.y, &mut self.k,
&mut self.h, &mut self.m);
high
});
},
15 => { // Stilson/Moog Band Pass
process_filter_fun32!(
ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
let (_low, band, _high, _notch) =
process_stilson_moog(
input, freq, res, self.israte,
&mut self.z, &mut self.y, &mut self.k,
&mut self.h, &mut self.m);
band
});
},
16 => { // Stilson/Moog Notch
process_filter_fun32!(
ctx.nframes(), inp, out, freq, res, 1.0, input, 22000.0, {
let (_low, _band, _high, notch) =
process_stilson_moog(
input, freq, res, self.israte,
&mut self.z, &mut self.y, &mut self.k,
&mut self.h, &mut self.m);
notch
});
},
_ => {},
}

View file

@ -1042,8 +1042,8 @@ fn check_node_sfilter_moog_lowpass() {
avg_fft_freqs(4.0, &[
100, 500, 1000, 2000, 3500, 4000, 5000, 6000, 8000, 12000
], &fft[..]), vec![
(0, 24), (100, 16), (500, 20), (1000, 20), (2000, 36), (3500, 332),
(4000, 164), (5000, 20), (6000, 8), (8000, 0)
(0, 4), (100, 4), (500, 4), (1000, 4), (2000, 4),
(3500, 20), (4000, 52), (5000, 4), (6000, 0), (8000, 0)
]);
// Low Pass Stilson/Moog @ 4000Hz RES=0.0
@ -1053,7 +1053,7 @@ fn check_node_sfilter_moog_lowpass() {
100, 500, 1000, 2000, 3500, 4000, 5000, 6000, 8000, 12000
], &fft[..]), vec![
(0, 20), (100, 12), (500, 16), (1000, 16), (2000, 12), (3500, 8),
(4000, 8), (5000, 4), (6000, 4), (8000, 0)
(4000, 4), (5000, 4), (6000, 0), (8000, 0)
]);
// Low Pass Stilson/Moog @ 22050Hz RES=0.0
@ -1061,15 +1061,15 @@ fn check_node_sfilter_moog_lowpass() {
assert_eq!(
avg_fft_freqs(8.0, &[100, 1000, 4000, 12000, 16000, 20000, 22050, 22051], &fft[..]), vec![
(0, 16), (100, 16), (1000, 16), (4000, 16), (12000, 16),
(16000, 16), (20000, 16), (22050, 0)
(16000, 16), (20000, 8), (22050, 0)
]);
// Low Pass Stilson/Moog @ 22050Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 13, 22050.0, 1.0);
assert_eq!(
avg_fft_freqs(8.0, &[100, 1000, 4000, 12000, 16000, 20000, 22050, 22051], &fft[..]), vec![
(0, 8), (100, 16), (1000, 16), (4000, 16), (12000, 16),
(16000, 16), (20000, 16), (22050, 0)
(0, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0),
(16000, 0), (20000, 0), (22050, 0)
]);
// Low Pass Stilson/Moog @ 0Hz RES=0.0
@ -1084,8 +1084,182 @@ fn check_node_sfilter_moog_lowpass() {
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 13, 0.0, 1.0);
assert_eq!(
avg_fft_freqs(4.0, &[1, 5, 10, 100, 1000, 4000, 12000, 22050, 22051], &fft[..]), vec![
(0, 68), (1, 0), (5, 0), (10, 4), (100, 0), (1000, 0),
(0, 0), (1, 0), (5, 0), (10, 0), (100, 0), (1000, 0),
(4000, 0), (12000, 0), (22050, 0)
]);
}
#[test]
fn check_node_sfilter_moog_highpass() {
let (mut matrix, mut node_exec) = setup_sfilter_matrix();
// High Pass Stilson/Moog @ 1000Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 1000.0, 1.0);
assert_eq!(
avg_fft_freqs(10.0, &[
500, 700, 900, 1000, 1500, 2000, 3000, 4000, 12000
], &fft[..]), vec![
(0, 0), (500, 10), (700, 20), (900, 40), (1000, 140),
(1500, 30), (2000, 10), (3000, 10), (4000, 10)
]);
// High Pass Stilson/Moog @ 1000Hz RES=0.5
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 1000.0, 0.5);
assert_eq!(
avg_fft_freqs(10.0, &[
500, 700, 900, 1000, 1500, 2000, 3000, 4000, 12000
], &fft[..]), vec![
(0, 0), (500, 10), (700, 40), (900, 50), (1000, 40),
(1500, 20), (2000, 20), (3000, 10), (4000, 10)
]);
// High Pass Stilson/Moog @ 1000Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 1000.0, 0.0);
assert_eq!(
avg_fft_freqs(10.0, &[
500, 700, 900, 1000, 1500, 2000, 3000, 4000, 12000
], &fft[..]), vec![
(0, 10), (500, 20), (700, 20), (900, 20), (1000, 20),
(1500, 20), (2000, 10), (3000, 20), (4000, 10)
]);
// High Pass Stilson/Moog @ 4000Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 4000.0, 1.0);
assert_eq!(
avg_fft_freqs(4.0, &[
100, 500, 1000, 2000, 3500, 4000, 5000, 6000, 8000, 12000
], &fft[..]), vec![
(0, 0), (100, 0), (500, 0), (1000, 4), (2000, 16),
(3500, 68), (4000, 200), (5000, 36), (6000, 24), (8000, 16)
]);
// High Pass Stilson/Moog @ 4000Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 4000.0, 0.0);
assert_eq!(
avg_fft_freqs(4.0, &[
100, 500, 1000, 2000, 3500, 4000, 5000, 6000, 8000, 12000
], &fft[..]), vec![
(0, 0), (100, 0), (500, 8), (1000, 16), (2000, 24), (3500, 28),
(4000, 24), (5000, 20), (6000, 20), (8000, 20)
]);
// High Pass Stilson/Moog @ 22050Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 22050.0, 0.0);
assert_eq!(
avg_fft_freqs(8.0, &[100, 1000, 4000, 12000, 16000, 20000, 22050], &fft[..]),
vec![
(0, 0), (100, 0), (1000, 0), (4000, 0),
(12000, 0), (16000, 0), (20000, 8)
]);
// High Pass Stilson/Moog @ 22050Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 22050.0, 1.0);
assert_eq!(
avg_fft_freqs(8.0, &[100, 1000, 4000, 12000, 16000, 20000, 22050], &fft[..]),
vec![
(0, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0),
(16000, 0), (20000, 0)
]);
// High Pass Stilson/Moog @ 0Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 0.0, 0.0);
assert_eq!(
avg_fft_freqs(4.0, &[10, 100, 1000, 4000, 12000, 22050], &fft[..]), vec![
(0, 0), (10, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0)
]);
// High Pass Stilson/Moog @ 0Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 14, 0.0, 1.0);
assert_eq!(
avg_fft_freqs(4.0, &[10, 100, 1000, 4000, 12000, 22050], &fft[..]), vec![
(0, 0), (10, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0)
]);
}
#[test]
fn check_node_sfilter_moog_bandpass() {
let (mut matrix, mut node_exec) = setup_sfilter_matrix();
// Band Pass Simper SVF @ 1000Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 1000.0, 1.0);
assert_eq!(
avg_fft_freqs(10.0, &[
250, 500, 700, 900, 1000, 1500, 2000, 3000, 4000, 12000
], &fft[..]), vec![
(0, 0), (250, 0), (500, 10), (700, 40), (900, 230),
(1000, 70), (1500, 10), (2000, 0), (3000, 0), (4000, 0)
]);
// Band Pass Simper SVF @ 1000Hz RES=0.5
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 1000.0, 0.5);
assert_eq!(
avg_fft_freqs(10.0, &[
250, 500, 700, 900, 1000, 1500, 2000, 3000, 4000, 12000
], &fft[..]), vec![
(0, 0), (250, 0), (500, 10), (700, 10), (900, 10),
(1000, 10), (1500, 10), (2000, 0), (3000, 0), (4000, 0)
]);
// Band Pass Simper SVF @ 1000Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 1000.0, 0.0);
assert_eq!(
avg_fft_freqs(4.0, &[
250, 500, 700, 900, 1000, 1500, 2000, 3000, 4000, 12000
], &fft[..]), vec![
(0, 0), (250, 0), (500, 8), (700, 8), (900, 8),
(1000, 8), (1500, 4), (2000, 4), (3000, 4), (4000, 0)
]);
// Band Pass Simper SVF @ 4000Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 4000.0, 1.0);
assert_eq!(
avg_fft_freqs(10.0, &[
100, 500, 1000, 2000, 3500, 4000, 5000, 6000, 8000, 12000
], &fft[..]), vec![
(0, 0), (100, 0), (500, 0), (1000, 0), (2000, 20),
(3500, 320), (4000, 170), (5000, 20), (6000, 10), (8000, 0)
]);
// Band Pass Simper SVF @ 4000Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 4000.0, 0.0);
assert_eq!(
avg_fft_freqs(10.0, &[
100, 500, 1000, 2000, 3500, 4000, 5000, 6000, 8000, 12000
], &fft[..]), vec![
(0, 0), (100, 0), (500, 0), (1000, 0), (2000, 0), (3500, 10),
(4000, 0), (5000, 0), (6000, 0), (8000, 0)
]);
// Band Pass Simper SVF @ 22050Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 22050.0, 0.0);
assert_eq!(
avg_fft_freqs(8.0, &[100, 1000, 4000, 12000, 16000, 20000, 22050], &fft[..]),
vec![
(0, 0), (100, 0), (1000, 0), (4000, 0),
(12000, 0), (16000, 0), (20000, 0)
]);
// Band Pass Simper SVF @ 22050Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 22050.0, 1.0);
assert_eq!(
avg_fft_freqs(8.0, &[100, 1000, 4000, 12000, 16000, 20000, 22050], &fft[..]),
vec![
(0, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0),
(16000, 0), (20000, 0)
]);
// Band Pass Simper SVF @ 0Hz RES=0.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 0.0, 0.0);
assert_eq!(
avg_fft_freqs(4.0, &[10, 100, 1000, 4000, 12000, 22050], &fft[..]), vec![
(0, 4), (10, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0)
]);
// Band Pass Simper SVF @ 0Hz RES=1.0
let fft = fft_with_freq_res_type(&mut matrix, &mut node_exec, 15, 0.0, 1.0);
assert_eq!(
avg_fft_freqs(4.0, &[10, 100, 1000, 4000, 12000, 22050], &fft[..]), vec![
(0, 12), (10, 0), (100, 0), (1000, 0), (4000, 0), (12000, 0)
]);
}