diff --git a/examples/Delay/AnalogDelayDemo/AnalogDelayDemo.ino b/examples/Delay/AnalogDelayDemo/AnalogDelayDemo.ino index 1083140..bb6a910 100644 --- a/examples/Delay/AnalogDelayDemo/AnalogDelayDemo.ino +++ b/examples/Delay/AnalogDelayDemo/AnalogDelayDemo.ino @@ -94,6 +94,13 @@ void setup() { analogDelay.mix(0.5f); analogDelay.feedback(0.0f); + ////////////////////////////////// + // AnalogDelay filter selection // + // Uncomment to tryout the 3 different built-in filters. + //analogDelay.setFilter(AudioEffectAnalogDelay::Filter::DM3); // The default filter. Naturally bright echo (highs stay, lows fade away) + //analogDelay.setFilter(AudioEffectAnalogDelay::Filter::WARM); // A warm filter with a smooth frequency rolloff above 2Khz + //analogDelay.setFilter(AudioEffectAnalogDelay::Filter::DARK); // A very dark filter, with a sharp rolloff above 1Khz + // Setup 2-stages of LPF, cutoff 4500 Hz, Q-factor 0.7071 (a 'normal' Q-factor) cabFilter.setLowpass(0, 4500, .7071); cabFilter.setLowpass(1, 4500, .7071); diff --git a/src/AudioEffectAnalogDelay.h b/src/AudioEffectAnalogDelay.h index d84ad79..49b6e90 100644 --- a/src/AudioEffectAnalogDelay.h +++ b/src/AudioEffectAnalogDelay.h @@ -30,10 +30,6 @@ namespace BAGuitar { -/// The number of stages in the analog-response Biquad filter -constexpr unsigned MAX_NUM_FILTER_STAGES = 4; -constexpr unsigned NUM_COEFFS_PER_STAGE = 5; - /**************************************************************************//** * AudioEffectAnalogDelay models BBD based analog delays. It provides controls * for delay, feedback (or regen), mix and output level. All parameters can be @@ -54,6 +50,12 @@ public: NUM_CONTROLS ///< this can be used as an alias for the number of MIDI controls }; + enum class Filter { + DM3 = 0, + WARM, + DARK + }; + // *** CONSTRUCTORS *** AudioEffectAnalogDelay() = delete; @@ -133,9 +135,17 @@ public: /// @param value the CC value from 0 to 127 void processMidi(int channel, int midiCC, int value); + // ** FILTER COEFFICIENTS ** + + /// Set the filter coefficients to one of the presets. See AudioEffectAnalogDelay::Filter + /// for options. + /// @details See AudioEffectAnalogDelayFIlters.h for more details. + /// @param filter the preset filter. E.g. AudioEffectAnalogDelay::Filter::WARM + void setFilter(Filter filter); + /// Override the default coefficients with your own. The number of filters stages affects how /// much CPU is consumed. - /// @details The effect uses the CMSIS-DSP library for biquads which requires coefficents + /// @details The effect uses the CMSIS-DSP library for biquads which requires coefficents. /// be in q31 format, which means they are 32-bit signed integers representing -1.0 to slightly /// less than +1.0. The coeffShift parameter effectively multiplies the coefficients by 2^shift.
/// Example: If you really want +1.5, must instead use +0.75 * 2^1, thus 0.75 in q31 format is @@ -171,11 +181,6 @@ private: // Coefficients void m_constructFilter(void); -// int m_numStages; -// int m_coeffShift; -// int m_coeffs[MAX_NUM_FILTER_STAGES*NUM_COEFFS_PER_STAGE] = {}; - - //size_t m_callCount = 0; }; } diff --git a/src/effects/AudioEffectAnalogDelay.cpp b/src/effects/AudioEffectAnalogDelay.cpp index f822a6e..6cf1ead 100644 --- a/src/effects/AudioEffectAnalogDelay.cpp +++ b/src/effects/AudioEffectAnalogDelay.cpp @@ -5,6 +5,7 @@ * Author: slascos */ #include +#include "AudioEffectAnalogDelayFilters.h" #include "AudioEffectAnalogDelay.h" namespace BAGuitar { @@ -12,16 +13,6 @@ namespace BAGuitar { constexpr int MIDI_CHANNEL = 0; constexpr int MIDI_CONTROL = 1; -// BOSS DM-3 Filters -constexpr unsigned DM3_COEFF_SHIFT = 2; -constexpr int32_t DM3[5*MAX_NUM_FILTER_STAGES] = { - 536870912, 988616936, 455608573, 834606945, -482959709, - 536870912, 1031466345, 498793368, 965834205, -467402235, - 536870912, 1105821939, 573646688, 928470657, -448083489, - 2339, 5093, 2776, 302068995, 4412722 -}; - - AudioEffectAnalogDelay::AudioEffectAnalogDelay(float maxDelayMs) : AudioStream(1, m_inputQueueArray) { @@ -58,7 +49,7 @@ AudioEffectAnalogDelay::~AudioEffectAnalogDelay() void AudioEffectAnalogDelay::m_constructFilter(void) { // Use DM3 coefficients by default - m_iir = new IirBiQuadFilterHQ(MAX_NUM_FILTER_STAGES, reinterpret_cast(&DM3), DM3_COEFF_SHIFT); + m_iir = new IirBiQuadFilterHQ(DM3_NUM_STAGES, reinterpret_cast(&DM3), DM3_COEFF_SHIFT); } void AudioEffectAnalogDelay::setFilterCoeffs(int numStages, const int32_t *coeffs, int coeffShift) @@ -66,6 +57,22 @@ void AudioEffectAnalogDelay::setFilterCoeffs(int numStages, const int32_t *coeff m_iir->changeFilterCoeffs(numStages, coeffs, coeffShift); } +void AudioEffectAnalogDelay::setFilter(Filter filter) +{ + switch(filter) { + case Filter::WARM : + m_iir->changeFilterCoeffs(WARM_NUM_STAGES, reinterpret_cast(&WARM), WARM_COEFF_SHIFT); + break; + case Filter::DARK : + m_iir->changeFilterCoeffs(DARK_NUM_STAGES, reinterpret_cast(&DARK), DARK_COEFF_SHIFT); + break; + case Filter::DM3 : + default: + m_iir->changeFilterCoeffs(DM3_NUM_STAGES, reinterpret_cast(&DM3), DM3_COEFF_SHIFT); + break; + } +} + void AudioEffectAnalogDelay::update(void) { audio_block_t *inputAudioBlock = receiveReadOnly(); // get the next block of input samples diff --git a/src/effects/AudioEffectAnalogDelayFilters.h b/src/effects/AudioEffectAnalogDelayFilters.h new file mode 100644 index 0000000..71f70bd --- /dev/null +++ b/src/effects/AudioEffectAnalogDelayFilters.h @@ -0,0 +1,83 @@ +/**************************************************************************//** + * @file + * @author Steve Lascos + * @company Blackaddr Audio + * + * This file constains precomputed co-efficients for the AudioEffectAnalogDelay + * class. + * + * @copyright This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ +#include + +namespace BAGuitar { + +// The number of stages in the analog-response Biquad filter +constexpr unsigned MAX_NUM_FILTER_STAGES = 4; +constexpr unsigned NUM_COEFFS_PER_STAGE = 5; + +// Matlab/Octave can be helpful to design a filter. Once you have the IIR filter (bz,az) coefficients +// in the z-domain, they can be converted to second-order-sections. AudioEffectAnalogDelay is designed +// to accept up to a maximum of an 8th order filter, broken into four, 2nd order stages. +// +// Second order sections can be created with: +// [sos] = tf2sos(bz,az); +// The results coefficents must be converted the Q31 format required by the ARM CMSIS-DSP library. This means +// all coefficients must lie between -1.0 and +0.9999. If your (bz,az) coefficients exceed this, you must divide +// them down by a power of 2. For example, if your largest magnitude coefficient is -3.5, you must divide by +// 2^shift where 4=2^2 and thus shift = 2. You must then mutliply by 2^31 to get a 32-bit signed integer value +// that represents the required Q31 coefficient. + +// BOSS DM-3 Filters +// b(z) = 1.0e-03 * (0.0032 0.0257 0.0900 0.1800 0.2250 0.1800 0.0900 0.0257 0.0032) +// a(z) = 1.0000 -5.7677 14.6935 -21.3811 19.1491 -10.5202 3.2584 -0.4244 -0.0067 +constexpr unsigned DM3_NUM_STAGES = 4; +constexpr unsigned DM3_COEFF_SHIFT = 2; +constexpr int32_t DM3[5*MAX_NUM_FILTER_STAGES] = { + 536870912, 988616936, 455608573, 834606945, -482959709, + 536870912, 1031466345, 498793368, 965834205, -467402235, + 536870912, 1105821939, 573646688, 928470657, -448083489, + 2339, 5093, 2776, 302068995, 4412722 +}; + + +// Blackaddr WARM Filter +// Butterworth, 8th order, cutoff = 2000 Hz +// Matlab/Octave command: [bz, az] = butter(8, 2000/44100/2); +// b(z) = 1.0e-05 * (0.0086 0.0689 0.2411 0.4821 0.6027 0.4821 0.2411 0.0689 0.0086_ +// a(z) = 1.0000 -6.5399 18.8246 -31.1340 32.3473 -21.6114 9.0643 -2.1815 0.2306 +constexpr unsigned WARM_NUM_STAGES = 4; +constexpr unsigned WARM_COEFF_SHIFT = 2; +constexpr int32_t WARM[5*MAX_NUM_FILTER_STAGES] = { + 536870912,1060309346,523602393,976869875,-481046241, + 536870912,1073413910,536711084,891250612,-391829326, + 536870912,1087173998,550475248,835222426,-333446881, + 46,92,46,807741349,-304811072 +}; + +// Blackaddr DARK Filter +// Chebychev Type II, 8th order, stopband = 60db, cutoff = 1000 Hz +// Matlab command: [bz, az] = cheby2(8, 60, 1000/44100/2); +// b(z) = 0.0009 -0.0066 0.0219 -0.0423 0.0522 -0.0423 0.0219 -0.0066 0.0009 +// a(z) = 1.0000 -7.4618 24.3762 -45.5356 53.1991 -39.8032 18.6245 -4.9829 0.5836 +constexpr unsigned DARK_NUM_STAGES = 4; +constexpr unsigned DARK_COEFF_SHIFT = 1; +constexpr int32_t DARK[5*MAX_NUM_FILTER_STAGES] = { + 1073741824,-2124867808,1073741824,2107780229,-1043948409, + 1073741824,-2116080466,1073741824,2042553796,-979786242, + 1073741824,-2077777790,1073741824,1964779896,-904264933, + 957356,-1462833,957356,1896884898,-838694612 +}; + +};