From cf3218a09ce0b82b559ebed88caadbd889ba5d32 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Fri, 23 Aug 2019 14:40:09 +0200 Subject: [PATCH] Added function for coeff-calculation. --- effect_modulated_delay.cpp | 37 +++++++++++++++++++++++++++++-------- effect_modulated_delay.h | 1 + 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/effect_modulated_delay.cpp b/effect_modulated_delay.cpp index b06a3d5..b7c69c0 100644 --- a/effect_modulated_delay.cpp +++ b/effect_modulated_delay.cpp @@ -27,6 +27,8 @@ #include "effect_modulated_delay.h" #include "config.h" +extern config_t configuration; + /******************************************************************/ // Based on; A u d i o E f f e c t D e l a y @@ -58,18 +60,37 @@ boolean AudioEffectModulatedDelay::begin(short *delayline, int d_length) _delayline = delayline; _delay_length = d_length; + set_modulator_filter_coeffs(1.0, configuration.chorus_frequency / 10, 1.0); // gain, center frerquency + modulator_filter_data = {1, &modulator_filter_state, modulator_filter_coeffs}; + + return (true); +} + +void AudioEffectModulatedDelay::set_modulator_filter_coeffs(float gain, float fc, float width) +{ // modulator filter // coefficients calculated with "IOWA Hills IIR Filter Designer 6.5", http://www.iowahills.com/8DownloadPage.html - // OmegaC = 0.5, SR = 44117.64706, Fc = 11 kHz, N=2 // Example: https://web.fhnw.ch/technik/projekte/eit/Fruehling2016/MuelZum/html/parametric_equalizer_example_8c-example.html - modulator_filter_coeffs[0] = 0.291938819295525787; // b0 - modulator_filter_coeffs[1] = 0.582700575839973478; // b1 - modulator_filter_coeffs[2] = 0.291938819295525787; // b2 - modulator_filter_coeffs[3] = 0.005242268129729576; // -a1 - modulator_filter_coeffs[4] = -0.171820482560754689; // -a2 - modulator_filter_data = {1, &modulator_filter_state, modulator_filter_coeffs}; - return (true); + float32_t A = sqrt(powf(10, gain / 20.0f)); + float32_t w0 = 2.0f * PI * fc / ((float32_t)AUDIO_SAMPLE_RATE_EXACT); + float32_t cosw0 = cosf(w0); + float32_t sinw0 = sinf(w0); + float32_t alpha = sinw0 / (2.0f * width); + float32_t a0 = 1.0f + alpha / A; + + modulator_filter_coeffs[0] = (1.0f + alpha * A) / a0; // b0 + modulator_filter_coeffs[1] = (-2.0f * cosw0) / a0; // b1 + modulator_filter_coeffs[2] = (1.0f - alpha * A) / a0; // b2 + modulator_filter_coeffs[3] = -(2.0f * cosw0) / -a0; // -a1 + modulator_filter_coeffs[4] = (1.0f - alpha / A) / -a0; // -a2 + + // OmegaC = 0.5, SR = 44117.64706, Fc = 11 kHz, N=2 + /* modulator_filter_coeffs[0] = 0.291938819295525787; // b0 + modulator_filter_coeffs[1] = 0.582700575839973478; // b1 + modulator_filter_coeffs[2] = 0.291938819295525787; // b2 + modulator_filter_coeffs[3] = 0.005242268129729576; // -a1 + modulator_filter_coeffs[4] = -0.171820482560754689; // -a2 */ } void AudioEffectModulatedDelay::update(void) diff --git a/effect_modulated_delay.h b/effect_modulated_delay.h index 2c9d04b..c882ec4 100644 --- a/effect_modulated_delay.h +++ b/effect_modulated_delay.h @@ -49,6 +49,7 @@ class AudioEffectModulatedDelay : virtual float offset(float offset_value); private: + void set_modulator_filter_coeffs(float gain, float fc, float width); audio_block_t *inputQueueArray[2]; int16_t *_delayline; // pointer for the circular buffer uint16_t _cb_index; // current write pointer of the circular buffer