From dbdda4d5e2ac1e80e8ba1c8edc9256901277255c Mon Sep 17 00:00:00 2001 From: dronus Date: Tue, 27 Mar 2018 18:10:16 +0200 Subject: [PATCH 1/4] Replaced tanhf by fast approximation. --- filter_moog_f32.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/filter_moog_f32.cpp b/filter_moog_f32.cpp index 1d488aa..813ea4e 100644 --- a/filter_moog_f32.cpp +++ b/filter_moog_f32.cpp @@ -27,18 +27,26 @@ #include "filter_moog_f32.h" +__inline__ float tanh_fast(float x) +{ + // rational tanh aproximation + float x2 = x * x; + float a = x * (135135.0f + x2 * (17325.0f + x2 * (378.0f + x2))); + float b = 135135.0f + x2 * (62370.0f + x2 * (3150.0f + x2 * 28.0f)); + return a / b; +} #if defined(KINETISK) void AudioFilterMoog_F32::update_fixed(const float *in,float *lp) { for (int i = 0; i < 2*AUDIO_BLOCK_SAMPLES; i++) { - float cs = tanhf(in[i/2] * driv); - y_a = y_a + g * (tanhf(cs - q * ((y_d_1 + y_d)/2) - tanhf(y_a))); - y_b = y_b + g * (tanhf(y_a) - tanhf(y_b)); - y_c = y_c + g * (tanhf(y_b) - tanhf(y_c)); + float cs = tanh_fast(in[i/2] * driv); + y_a = y_a + g * (tanh_fast(cs - q * ((y_d_1 + y_d)/2) - tanh_fast(y_a))); + y_b = y_b + g * (tanh_fast(y_a) - tanh_fast(y_b)); + y_c = y_c + g * (tanh_fast(y_b) - tanh_fast(y_c)); y_d_1 = y_d; - y_d = y_d + g * (tanhf(y_c) - tanhf(y_d)); + y_d = y_d + g * (tanh_fast(y_c) - tanh_fast(y_d)); lp[i/2] = y_d; } } @@ -50,12 +58,12 @@ void AudioFilterMoog_F32::update_variable(const float *in,const float *ctl, floa float nf=basef*(exp2f(ctl[0/over]*oct)); frequency(nf,false); for (int i = 0; i < 2*AUDIO_BLOCK_SAMPLES; i++) { - float cs = tanhf(in[i/over] * driv); - y_a = y_a + g * (tanhf(cs - q * ((y_d_1 + y_d)/2) - tanhf(y_a))); - y_b = y_b + g * (tanhf(y_a) - tanhf(y_b)); - y_c = y_c + g * (tanhf(y_b) - tanhf(y_c)); + float cs = tanh_fast(in[i/over] * driv); + y_a = y_a + g * (tanh_fast(cs - q * ((y_d_1 + y_d)/2) - tanh_fast(y_a))); + y_b = y_b + g * (tanh_fast(y_a) - tanh_fast(y_b)); + y_c = y_c + g * (tanh_fast(y_b) - tanh_fast(y_c)); y_d_1 = y_d; - y_d = y_d + g * (tanhf(y_c) - tanhf(y_d)); + y_d = y_d + g * (tanh_fast(y_c) - tanh_fast(y_d)); lp[i/over] = y_d; } } From 4e904f521465915ef7589e145abe3864abb9fe96 Mon Sep 17 00:00:00 2001 From: dronus Date: Tue, 27 Mar 2018 18:13:18 +0200 Subject: [PATCH 2/4] Corrected case typo in include directive --- filter_moog_f32.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filter_moog_f32.h b/filter_moog_f32.h index 072f6fa..65b16e5 100644 --- a/filter_moog_f32.h +++ b/filter_moog_f32.h @@ -28,7 +28,7 @@ #define filter_moog_f32_h_ #include "Arduino.h" -#include "AudioStream_f32.h" +#include "AudioStream_F32.h" #include "utility/dspinst.h" #include "arm_math.h" From e5c592151d77e10a367b5188ed46520b9cbcbff2 Mon Sep 17 00:00:00 2001 From: dronus Date: Tue, 27 Mar 2018 18:15:07 +0200 Subject: [PATCH 3/4] Renamed octave() to octaveControl() making this filter a drop-in replacement for Teensy Audio's State Variable Filter. --- filter_moog_f32.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/filter_moog_f32.h b/filter_moog_f32.h index 65b16e5..30ab716 100644 --- a/filter_moog_f32.h +++ b/filter_moog_f32.h @@ -52,7 +52,7 @@ public: frequency(1000); resonance(1); drive(1); - octave(1); + octaveControl(1); y_a = 0; y_b = 0; y_c = 0; @@ -72,7 +72,7 @@ public: else if (qi > 5.0) qi = 5.0; q=qi; } - void octave(float n) { + void octaveControl(float n) { if (n < 0.0) n = 0.0; else if (n > 6.9999) n = 6.9999; oct=n; From 07048b543ea710da0fa7632244bf8059d283d3d5 Mon Sep 17 00:00:00 2001 From: dronus Date: Tue, 27 Mar 2018 18:16:12 +0200 Subject: [PATCH 4/4] allow frequency() to store basef outside the limits to allow full range modulation via control in. --- filter_moog_f32.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/filter_moog_f32.h b/filter_moog_f32.h index 30ab716..9e6c179 100644 --- a/filter_moog_f32.h +++ b/filter_moog_f32.h @@ -60,12 +60,12 @@ public: y_d_1 = 0; } void frequency(float freq,bool setf=true) { + if(setf) + basef=freq; if (freq < 20.0) freq = 20.0; else if (freq > AUDIO_SAMPLE_RATE_EXACT/2.5) freq = AUDIO_SAMPLE_RATE_EXACT/2.5; g = 1 - expf(-2 * tanf(2 * M_PI * freq/(2 * AUDIO_SAMPLE_RATE_EXACT))); // Serial.println(freq); - if(setf) - basef=freq; } void resonance(float qi) { if (qi < 0.7) qi = 0.7;