diff --git a/examples/Modulation/AnalogChorusDemoExpansion/AnalogChorusDemoExpansion.ino b/examples/Modulation/AnalogChorusDemoExpansion/AnalogChorusDemoExpansion.ino new file mode 100644 index 0000000..5f293d6 --- /dev/null +++ b/examples/Modulation/AnalogChorusDemoExpansion/AnalogChorusDemoExpansion.ino @@ -0,0 +1,217 @@ +/************************************************************************* + * This demo uses the BALibrary library to provide enhanced control of + * the TGA Pro board. + * + * The latest copy of the BA Guitar library can be obtained from + * https://github.com/Blackaddr/BALibrary + * + * This example demonstrates teh BAAudioEffectsAnalogChorus effect. It can + * be controlled using the Blackaddr Audio "Expansion Control Board". + * + * POT1 (left) controls the modulation rate + * POT2 (right) controls the modulation depth + * POT3 (center) controls the wet/dry mix + * SW1 will enable/bypass the audio effect. LED1 will be on when effect is enabled. + * SW2 will cycle through the 3 pre-programmed analog filters. LED2 will be on when SW2 is pressed. + * + * Using the Serial Montitor, send 'u' and 'd' characters to increase or decrease + * the headphone volume between values of 0 and 9. + */ +#define TGA_PRO_REVB // Set which hardware revision of the TGA Pro we're using +#define TGA_PRO_EXPAND_REV2 // pull in the pin definitions for the Blackaddr Audio Expansion Board. + +#include "BALibrary.h" +#include "BAEffects.h" + +using namespace BAEffects; +using namespace BALibrary; + +AudioInputI2S i2sIn; +AudioOutputI2S i2sOut; +BAAudioControlWM8731 codec; + +//#define USE_EXT // uncomment this line to use External MEM0 + +#ifdef USE_EXT +// If using external SPI memory, we will instantiate a SRAM +// manager and create an external memory slot to use as the memory +// for our audio delay +ExternalSramManager externalSram; +ExtMemSlot delaySlot; // Declare an external memory slot. + +// Instantiate the AudioEffectAnalogChorus to use external memory by +/// passing it the delay slot. +AudioEffectAnalogChorus analogChorus(&delaySlot); +#else +AudioEffectAnalogChorus analogChorus; // default chorus delays +#endif + +AudioFilterBiquad cabFilter; // We'll want something to cut out the highs and smooth the tone, just like a guitar cab. + +// Simply connect the input to the delay, and the output +// to both i2s channels +AudioConnection input(i2sIn,0, analogChorus,0); +AudioConnection chorusOut(analogChorus, 0, cabFilter, 0); +AudioConnection leftOut(cabFilter,0, i2sOut, 0); +AudioConnection rightOut(cabFilter,0, i2sOut, 1); + + +////////////////////////////////////////// +// SETUP PHYSICAL CONTROLS +// - POT1 (left) will control the rate +// - POT2 (right) will control the depth +// - POT3 (centre) will control the wet/dry mix. +// - SW1 (left) will be used as a bypass control +// - LED1 (left) will be illuminated when the effect is ON (not bypass) +// - SW2 (right) will be used to cycle through the three built in analog filter styles available. +// - LED2 (right) will illuminate when pressing SW2. +////////////////////////////////////////// +// To get the calibration values for your particular board, first run the +// BAExpansionCalibrate.ino example and +constexpr int potCalibMin = 1; +constexpr int potCalibMax = 1018; +constexpr bool potSwapDirection = true; + +// Create a control object using the number of switches, pots, encoders and outputs on the +// Blackaddr Audio Expansion Board. +BAPhysicalControls controls(BA_EXPAND_NUM_SW, BA_EXPAND_NUM_POT, BA_EXPAND_NUM_ENC, BA_EXPAND_NUM_LED); + +int loopCount = 0; +unsigned filterIndex = 0; // variable for storing which analog filter we're currently using. +constexpr unsigned MAX_HEADPHONE_VOL = 10; +unsigned headphoneVolume = 8; // control headphone volume from 0 to 10. + +// BAPhysicalControls returns a handle when you register a new control. We'll uses these handles when working with the controls. +int bypassHandle, filterHandle, rateHandle, depthHandle, mixHandle, led1Handle, led2Handle; // Handles for the various controls + +void setup() { + delay(100); // wait a bit for serial to be available + Serial.begin(57600); // Start the serial port + delay(100); + + // Setup the controls. The return value is the handle to use when checking for control changes, etc. + // pushbuttons + bypassHandle = controls.addSwitch(BA_EXPAND_SW1_PIN); // will be used for bypass control + filterHandle = controls.addSwitch(BA_EXPAND_SW2_PIN); // will be used for stepping through filters + // pots + rateHandle = controls.addPot(BA_EXPAND_POT1_PIN, potCalibMin, potCalibMax, potSwapDirection); // control the amount of delay + depthHandle = controls.addPot(BA_EXPAND_POT2_PIN, potCalibMin, potCalibMax, potSwapDirection); + mixHandle = controls.addPot(BA_EXPAND_POT3_PIN, potCalibMin, potCalibMax, potSwapDirection); + // leds + led1Handle = controls.addOutput(BA_EXPAND_LED1_PIN); + led2Handle = controls.addOutput(BA_EXPAND_LED2_PIN); // will illuminate when pressing SW2 + + // Disable the audio codec first + codec.disable(); + AudioMemory(128); + + // Enable and configure the codec + Serial.println("Enabling codec...\n"); + codec.enable(); + codec.setHeadphoneVolume(1.0f); // Max headphone volume + + // If using external memory request request memory from the manager + // for the slot + #ifdef USE_EXT + Serial.println("Using EXTERNAL memory"); + // We have to request memory be allocated to our slot. + externalSram.requestMemory(&delaySlot, 40.0f, MemSelect::MEM0, true); // 40 ms is enough to handle the full range of the chorus delay + #else + Serial.println("Using INTERNAL memory"); + #endif + + // Besure to enable the delay. When disabled, audio is is completely blocked by the effect + // to minimize resource usage to nearly to nearly zero. + analogChorus.enable(); + + // Set some default values. + // These can be changed using the controls on the Blackaddr Audio Expansion Board + analogChorus.bypass(false); + analogChorus.rate(0.5f); + analogChorus.mix(0.5f); + analogChorus.depth(1.0f); + + ////////////////////////////////// + // AnalogChorus filter selection // + // These are commented out, in this example we'll use SW2 to cycle through the different filters + //analogChorus.setFilter(AudioEffectAnalogChorus::Filter::CE2); // The default filter. Naturally bright echo (highs stay, lows fade away) + //analogChorus.setFilter(AudioEffectAnalogChorus::Filter::WARM); // A warm filter with a smooth frequency rolloff above 2Khz + //analogChorus.setFilter(AudioEffectAnalogChorus::Filter::DARK); // A very dark filter, with a sharp rolloff above 1Khz + + // Guitar cabinet: 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); +} + +void loop() { + + float potValue; + + // Check if SW1 has been toggled (pushed) + if (controls.isSwitchToggled(bypassHandle)) { + bool bypass = analogChorus.isBypass(); // get the current state + bypass = !bypass; // change it + analogChorus.bypass(bypass); // set the new state + controls.setOutput(led1Handle, !bypass); // Set the LED when NOT bypassed + Serial.println(String("BYPASS is ") + bypass); + } + + // Use SW2 to cycle through the filters + controls.setOutput(led2Handle, controls.getSwitchValue(led2Handle)); + if (controls.isSwitchToggled(filterHandle)) { + filterIndex = (filterIndex + 1) % 3; // update and potentionall roll the counter 0, 1, 2, 0, 1, 2, ... + // cast the index between 0 to 2 to the enum class AudioEffectAnalogChorus::Filter + analogChorus.setFilter(static_cast(filterIndex)); // will cycle through 0 to 2 + Serial.println(String("Filter set to ") + filterIndex); + } + + // Use POT1 (left) to control the rate setting + if (controls.checkPotValue(rateHandle, potValue)) { + // Pot has changed + Serial.println(String("New RATE setting: ") + potValue); + analogChorus.rate(potValue); + } + + // Use POT2 (right) to control the depth setting + if (controls.checkPotValue(depthHandle, potValue)) { + // Pot has changed + Serial.println(String("New DEPTH setting: ") + potValue); + analogChorus.depth(potValue); + } + + // Use POT3 (centre) to control the mix setting + if (controls.checkPotValue(mixHandle, potValue)) { + // Pot has changed + Serial.println(String("New MIX setting: ") + potValue); + analogChorus.mix(potValue); + } + + // Use the 'u' and 'd' keys to adjust volume across ten levels. + if (Serial) { + if (Serial.available() > 0) { + while (Serial.available()) { + char key = Serial.read(); + if (key == 'u') { + headphoneVolume = (headphoneVolume + 1) % MAX_HEADPHONE_VOL; + Serial.println(String("Increasing HEADPHONE volume to ") + headphoneVolume); + } + else if (key == 'd') { + headphoneVolume = (headphoneVolume - 1) % MAX_HEADPHONE_VOL; + Serial.println(String("Decreasing HEADPHONE volume to ") + headphoneVolume); + } + codec.setHeadphoneVolume(static_cast(headphoneVolume) / static_cast(MAX_HEADPHONE_VOL)); + } + } + } + + // Use the loopCounter to roughly measure human timescales. Every few seconds, print the CPU usage + // to the serial port. About 500,000 loops! + if (loopCount % 524288 == 0) { + Serial.print("Processor Usage, Total: "); Serial.print(AudioProcessorUsage()); + Serial.print("% "); + Serial.print(" AnalogChorus: "); Serial.print(analogChorus.processorUsage()); + Serial.println("%"); + } + loopCount++; + +} diff --git a/examples/Modulation/AnalogChorusDemoExpansion/name.c b/examples/Modulation/AnalogChorusDemoExpansion/name.c new file mode 100644 index 0000000..5ea00fe --- /dev/null +++ b/examples/Modulation/AnalogChorusDemoExpansion/name.c @@ -0,0 +1,19 @@ +// To give your project a unique name, this code must be +// placed into a .c file (its own tab). It can not be in +// a .cpp file or your main sketch (the .ino file). + +#include "usb_names.h" + +// Edit these lines to create your own name. The length must +// match the number of characters in your custom name. + +#define MIDI_NAME {'B','l','a','c','k','a','d','d','r',' ','A','u','d','i','o',' ','T','G','A',' ','P','r','o'} +#define MIDI_NAME_LEN 23 + +// Do not change this part. This exact format is required by USB. + +struct usb_string_descriptor_struct usb_string_product_name = { + 2 + MIDI_NAME_LEN * 2, + 3, + MIDI_NAME +}; diff --git a/src/AudioEffectAnalogChorus.h b/src/AudioEffectAnalogChorus.h new file mode 100644 index 0000000..2d87cfb --- /dev/null +++ b/src/AudioEffectAnalogChorus.h @@ -0,0 +1,196 @@ +/**************************************************************************//** + * @file + * @author Steve Lascos + * @company Blackaddr Audio + * + * AudioEffectAnalogChorus is a class for simulating a classic BBD based chorus + * like the Boss CE-2. This class works with either internal RAM, or external + * SPI RAM. The external RAM uses DMA to minimize load on the + * CPU. + * + * @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 . + *****************************************************************************/ + +#ifndef __BAEFFECTS_BAAUDIOEFFECTANALOGCHORUS_H +#define __BAEFFECTS_BAAUDIOEFFECTANALOGCHORUS_H + +#include +#include "LibBasicFunctions.h" + +namespace BAEffects { + +/**************************************************************************//** + * AudioEffectAnalogChorus models BBD based analog chorus. It provides controls + * for rate, depth, mix and output level. All parameters can be + * controlled by MIDI. The class supports internal memory, or external SPI + * memory by providing an ExtMemSlot. External memory access uses DMA to reduce + * process load. + *****************************************************************************/ +class AudioEffectAnalogChorus : public AudioStream { +public: + + ///< List of AudioEffectAnalogChorus MIDI controllable parameters + enum { + BYPASS = 0, ///< controls effect bypass + RATE, ///< controls the modulate rate of the LFO + DEPTH, ///< controls the depth of modulation of the LFO + MIX, ///< controls the the mix of input and chorus signals + VOLUME, ///< controls the output volume level + NUM_CONTROLS ///< this can be used as an alias for the number of MIDI controls + }; + + enum class Filter { + CE2 = 0, + WARM, + DARK + }; + + + /// Construct an analog chorus using internal memory. The chorus will have the + /// default average delay. + AudioEffectAnalogChorus(); + + /// Construct an analog chorus using external SPI via an ExtMemSlot. The chorus will have + /// the default average delay. + /// @param slot A pointer to the ExtMemSlot to use for the delay. + AudioEffectAnalogChorus(BALibrary::ExtMemSlot *slot); // requires sufficiently sized pre-allocated memory + + virtual ~AudioEffectAnalogChorus(); ///< Destructor + + // *** PARAMETERS *** + + /// Set the chorus average delay in milliseconds + /// The value should be between 0.0f and 1.0f + void setDelayConfig(float averageDelayMs, float delayRangeMs); + + /// Set the chorus average delay in number of audio samples + /// The value should be between 0.0f and 1.0f + void setDelayConfig(size_t averageDelayNumSamples, size_t delayRangeNumSamples); + + /// Bypass the effect. + /// @param byp when true, bypass wil disable the effect, when false, effect is enabled. + /// Note that audio still passes through when bypass is enabled. + void bypass(bool byp) { m_bypass = byp; } + + /// Get if the effect is bypassed + /// @returns true if bypassed, false if not bypassed + bool isBypass() { return m_bypass; } + + /// Toggle the bypass effect + void toggleBypass() { m_bypass = !m_bypass; } + + /// Set the LFO frequency where 0.0f is MIN and 1.0f is MAX + void rate(float rate); + + /// Set the depth of LFO modulation. + /// @param lfoDepth must be a float between 0.0f and 1.0f + void depth(float lfoDepth) { m_lfoDepth = lfoDepth; } + + /// Set the amount of blending between dry and wet (echo) at the output. + /// @param mix When 0.0, output is 100% dry, when 1.0, output is 100% wet. When + /// 0.5, output is 50% Dry, 50% Wet. + void mix(float mix) { m_mix = mix; } + + /// Set the output volume. This affect both the wet and dry signals. + /// @details The default is 1.0. + /// @param vol Sets the output volume between -1.0 and +1.0 + void volume(float vol) {m_volume = vol; } + + // ** ENABLE / DISABLE ** + + /// Enables audio processing. Note: when not enabled, CPU load is nearly zero. + void enable() { m_enable = true; } + + /// Disables audio process. When disabled, CPU load is nearly zero. + void disable() { m_enable = false; } + + // ** MIDI ** + + /// Sets whether MIDI OMNI channel is processig on or off. When on, + /// all midi channels are used for matching CCs. + /// @param isOmni when true, all channels are processed, when false, channel + /// must match configured value. + void setMidiOmni(bool isOmni) { m_isOmni = isOmni; } + + /// Configure an effect parameter to be controlled by a MIDI CC + /// number on a particular channel. + /// @param parameter one of the parameter names in the class enum + /// @param midiCC the CC number from 0 to 127 + /// @param midiChannel the effect will only response to the CC on this channel + /// when OMNI mode is off. + void mapMidiControl(int parameter, int midiCC, int midiChannel = 0); + + /// process a MIDI Continous-Controller (CC) message + /// @param channel the MIDI channel from 0 to 15) + /// @param midiCC the CC number from 0 to 127 + /// @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 AudioEffectAnalogChorus::Filter + /// for options. + /// @details See AudioEffectAnalogChorusFIlters.h for more details. + /// @param filter the preset filter. E.g. AudioEffectAnalogChorus::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. + /// 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 + /// (0.75 * 2^31) = 1610612736 and coeffShift = 1. + /// @param numStages the actual number of filter stages you want to use. Must be <= MAX_NUM_FILTER_STAGES. + /// @param coeffs pointer to an integer array of coefficients in q31 format. + /// @param coeffShift Coefficient scaling factor = 2^coeffShift. + void setFilterCoeffs(int numStages, const int32_t *coeffs, int coeffShift); + + virtual void update(void); ///< update automatically called by the Teesny Audio Library + +private: + static constexpr float m_DEFAULT_DELAY_MS = 20.0f; ///< default average delay of chorus in milliseconds + static constexpr float m_DELAY_RANGE = 15.0f; ///< default range of delay variation in milliseconds + static constexpr float m_LFO_MIN_RATE = 2.0f; ///< slowest possible LFO rate in milliseconds + static constexpr float m_LFO_RANGE = 8.0f; ///< fastest possible LFO rate in milliseconds + + audio_block_t *m_inputQueueArray[1]; + bool m_isOmni = false; + bool m_bypass = true; + bool m_enable = false; + bool m_externalMemory = false; + BALibrary::AudioDelay *m_memory = nullptr; + BALibrary::LowFrequencyOscillatorVector m_lfo; + size_t m_maxDelaySamples = 0; + audio_block_t *m_previousBlock = nullptr; + audio_block_t *m_blockToRelease = nullptr; + BALibrary::IirBiQuadFilterHQ *m_iir = nullptr; + + // Controls + int m_midiConfig[NUM_CONTROLS][2]; // stores the midi parameter mapping + size_t m_delaySamples = 0; + float m_lfoDepth = 0.0f; + float m_mix = 0.0f; + float m_volume = 1.0f; + + void m_preProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet); + void m_postProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet); + + // Coefficients + void m_constructFilter(void); +}; + +} + +#endif /* __BAEFFECTS_BAAUDIOEFFECTAnalogChorus_H */ diff --git a/src/BAEffects.h b/src/BAEffects.h index 50b92b0..c331137 100644 --- a/src/BAEffects.h +++ b/src/BAEffects.h @@ -26,5 +26,6 @@ #include "AudioEffectAnalogDelay.h" #include "AudioEffectSOS.h" #include "AudioEffectTremolo.h" +#include "AudioEffectAnalogChorus.h" #endif /* __BAEFFECTS_H */ diff --git a/src/effects/AudioEffectAnalogDelay.cpp.interpolated b/src/effects/AudioEffectAnalogChorus.cpp similarity index 51% rename from src/effects/AudioEffectAnalogDelay.cpp.interpolated rename to src/effects/AudioEffectAnalogChorus.cpp index b5d41cb..5d67f6b 100644 --- a/src/effects/AudioEffectAnalogDelay.cpp.interpolated +++ b/src/effects/AudioEffectAnalogChorus.cpp @@ -1,40 +1,32 @@ /* - * AudioEffectAnalogDelay.cpp + * AudioEffectAnalogChorus.cpp * * Created on: Jan 7, 2018 * Author: slascos */ #include -#include "AudioEffectAnalogDelayFilters.h" -#include "AudioEffectAnalogDelay.h" +#include "AudioEffectAnalogChorusFilters.h" +#include "AudioEffectAnalogChorus.h" using namespace BALibrary; -#define INTERPOLATED_DELAY Uncomment this line to test the inteprolated delay which adds 1/10th of a sample +//#define INTERPOLATED_DELAY Uncomment this line to test the inteprolated delay which adds 1/10th of a sample namespace BAEffects { constexpr int MIDI_CHANNEL = 0; constexpr int MIDI_CONTROL = 1; -AudioEffectAnalogDelay::AudioEffectAnalogDelay(float maxDelayMs) +AudioEffectAnalogChorus::AudioEffectAnalogChorus() : AudioStream(1, m_inputQueueArray) { - m_memory = new AudioDelay(maxDelayMs); - m_maxDelaySamples = calcAudioSamples(maxDelayMs); - m_constructFilter(); -} - -AudioEffectAnalogDelay::AudioEffectAnalogDelay(size_t numSamples) -: AudioStream(1, m_inputQueueArray) -{ - m_memory = new AudioDelay(numSamples); - m_maxDelaySamples = numSamples; - m_constructFilter(); + m_memory = new AudioDelay(m_DEFAULT_DELAY_MS + m_DELAY_RANGE); + m_maxDelaySamples = calcAudioSamples(m_DEFAULT_DELAY_MS + m_DELAY_RANGE); + m_constructFilter(); } // requires preallocated memory large enough -AudioEffectAnalogDelay::AudioEffectAnalogDelay(ExtMemSlot *slot) +AudioEffectAnalogChorus::AudioEffectAnalogChorus(ExtMemSlot *slot) : AudioStream(1, m_inputQueueArray) { m_memory = new AudioDelay(slot); @@ -43,25 +35,25 @@ AudioEffectAnalogDelay::AudioEffectAnalogDelay(ExtMemSlot *slot) m_constructFilter(); } -AudioEffectAnalogDelay::~AudioEffectAnalogDelay() +AudioEffectAnalogChorus::~AudioEffectAnalogChorus() { if (m_memory) delete m_memory; if (m_iir) delete m_iir; } // This function just sets up the default filter and coefficients -void AudioEffectAnalogDelay::m_constructFilter(void) +void AudioEffectAnalogChorus::m_constructFilter(void) { - // Use DM3 coefficients by default - m_iir = new IirBiQuadFilterHQ(DM3_NUM_STAGES, reinterpret_cast(&DM3), DM3_COEFF_SHIFT); + // Use CE2 coefficients by default + m_iir = new IirBiQuadFilterHQ(CE2_NUM_STAGES, reinterpret_cast(&CE2), CE2_COEFF_SHIFT); } -void AudioEffectAnalogDelay::setFilterCoeffs(int numStages, const int32_t *coeffs, int coeffShift) +void AudioEffectAnalogChorus::setFilterCoeffs(int numStages, const int32_t *coeffs, int coeffShift) { m_iir->changeFilterCoeffs(numStages, coeffs, coeffShift); } -void AudioEffectAnalogDelay::setFilter(Filter filter) +void AudioEffectAnalogChorus::setFilter(Filter filter) { switch(filter) { case Filter::WARM : @@ -70,14 +62,14 @@ void AudioEffectAnalogDelay::setFilter(Filter filter) case Filter::DARK : m_iir->changeFilterCoeffs(DARK_NUM_STAGES, reinterpret_cast(&DARK), DARK_COEFF_SHIFT); break; - case Filter::DM3 : + case Filter::CE2 : default: - m_iir->changeFilterCoeffs(DM3_NUM_STAGES, reinterpret_cast(&DM3), DM3_COEFF_SHIFT); + m_iir->changeFilterCoeffs(CE2_NUM_STAGES, reinterpret_cast(&CE2), CE2_COEFF_SHIFT); break; } } -void AudioEffectAnalogDelay::update(void) +void AudioEffectAnalogChorus::update(void) { audio_block_t *inputAudioBlock = receiveReadOnly(); // get the next block of input samples @@ -179,92 +171,18 @@ void AudioEffectAnalogDelay::update(void) m_blockToRelease = blockToRelease; } -void AudioEffectAnalogDelay::delay(float milliseconds) -{ - size_t delaySamples = calcAudioSamples(milliseconds); - - if (delaySamples > m_memory->getMaxDelaySamples()) { - // this exceeds max delay value, limit it. - delaySamples = m_memory->getMaxDelaySamples(); - } - - if (!m_memory) { Serial.println("delay(): m_memory is not valid"); } - - if (!m_externalMemory) { - // internal memory - //QueuePosition queuePosition = calcQueuePosition(milliseconds); - //Serial.println(String("CONFIG: delay:") + delaySamples + String(" queue position ") + queuePosition.index + String(":") + queuePosition.offset); - } else { - // external memory - //Serial.println(String("CONFIG: delay:") + delaySamples); - ExtMemSlot *slot = m_memory->getSlot(); - - if (!slot) { Serial.println("ERROR: slot ptr is not valid"); } - if (!slot->isEnabled()) { - slot->enable(); - Serial.println("WEIRD: slot was not enabled"); - } - } - - m_delaySamples = delaySamples; -} - -void AudioEffectAnalogDelay::delay(size_t delaySamples) -{ - if (!m_memory) { Serial.println("delay(): m_memory is not valid"); } - - if (!m_externalMemory) { - // internal memory - //QueuePosition queuePosition = calcQueuePosition(delaySamples); - //Serial.println(String("CONFIG: delay:") + delaySamples + String(" queue position ") + queuePosition.index + String(":") + queuePosition.offset); - } else { - // external memory - //Serial.println(String("CONFIG: delay:") + delaySamples); - ExtMemSlot *slot = m_memory->getSlot(); - if (!slot->isEnabled()) { - slot->enable(); - } - } - m_delaySamples = delaySamples; -} - -void AudioEffectAnalogDelay::delayFractionMax(float delayFraction) -{ - size_t delaySamples = static_cast(static_cast(m_memory->getMaxDelaySamples()) * delayFraction); - - if (delaySamples > m_memory->getMaxDelaySamples()) { - // this exceeds max delay value, limit it. - delaySamples = m_memory->getMaxDelaySamples(); - } - - if (!m_memory) { Serial.println("delay(): m_memory is not valid"); } - - if (!m_externalMemory) { - // internal memory - //QueuePosition queuePosition = calcQueuePosition(delaySamples); - //Serial.println(String("CONFIG: delay:") + delaySamples + String(" queue position ") + queuePosition.index + String(":") + queuePosition.offset); - } else { - // external memory - //Serial.println(String("CONFIG: delay:") + delaySamples); - ExtMemSlot *slot = m_memory->getSlot(); - if (!slot->isEnabled()) { - slot->enable(); - } - } - m_delaySamples = delaySamples; -} - -void AudioEffectAnalogDelay::m_preProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet) +void AudioEffectAnalogChorus::m_preProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet) { - if ( out && dry && wet) { - alphaBlend(out, dry, wet, m_feedback); - m_iir->process(out->data, out->data, AUDIO_BLOCK_SAMPLES); - } else if (dry) { - memcpy(out->data, dry->data, sizeof(int16_t) * AUDIO_BLOCK_SAMPLES); - } + memcpy(out->data, dry->data, sizeof(int16_t) * AUDIO_BLOCK_SAMPLES); +// if ( out && dry && wet) { +// alphaBlend(out, dry, wet, m_feedback); +// m_iir->process(out->data, out->data, AUDIO_BLOCK_SAMPLES); +// } else if (dry) { +// memcpy(out->data, dry->data, sizeof(int16_t) * AUDIO_BLOCK_SAMPLES); +// } } -void AudioEffectAnalogDelay::m_postProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet) +void AudioEffectAnalogChorus::m_postProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet) { if (!out) return; // no valid output buffer @@ -280,43 +198,97 @@ void AudioEffectAnalogDelay::m_postProcessing(audio_block_t *out, audio_block_t } +void AudioEffectAnalogChorus::setDelayConfig(float averageDelayMs, float delayRangeMs) +{ + size_t delaySamples = calcAudioSamples(averageDelayMs + delayRangeMs); + + if (delaySamples > m_memory->getMaxDelaySamples()) { + // this exceeds max delay value, limit it. + delaySamples = m_memory->getMaxDelaySamples(); + } + + if (!m_memory) { Serial.println("delay(): m_memory is not valid"); } + + if (!m_externalMemory) { + // internal memory + // Do nothing + } else { + // external memory + ExtMemSlot *slot = m_memory->getSlot(); + + if (!slot) { Serial.println("ERROR: slot ptr is not valid"); } + if (!slot->isEnabled()) { + slot->enable(); + Serial.println("WEIRD: slot was not enabled"); + } + } +} + +void AudioEffectAnalogChorus::setDelayConfig(size_t averageDelayNumSamples, size_t delayRangeNumSamples) +{ + size_t delaySamples = averageDelayNumSamples + delayRangeNumSamples; + + if (delaySamples > m_memory->getMaxDelaySamples()) { + // this exceeds max delay value, limit it. + delaySamples = m_memory->getMaxDelaySamples(); + } + + if (!m_memory) { Serial.println("delay(): m_memory is not valid"); } + + if (!m_externalMemory) { + // internal memory + // Do nothing + } else { + // external memory + ExtMemSlot *slot = m_memory->getSlot(); + + if (!slot) { Serial.println("ERROR: slot ptr is not valid"); } + if (!slot->isEnabled()) { + slot->enable(); + Serial.println("WEIRD: slot was not enabled"); + } + } +} + +void AudioEffectAnalogChorus::rate(float rate) +{ + // update the LFO by mapping the rate into the MIN/MAX range, pass to LFO in milliseconds + m_lfo.setRateAudio(m_LFO_MIN_RATE + (rate * m_LFO_RANGE)); +} -void AudioEffectAnalogDelay::processMidi(int channel, int control, int value) +void AudioEffectAnalogChorus::processMidi(int channel, int control, int value) { float val = (float)value / 127.0f; - if ((m_midiConfig[DELAY][MIDI_CHANNEL] == channel) && - (m_midiConfig[DELAY][MIDI_CONTROL] == control)) { - // Delay - if (m_externalMemory) { m_maxDelaySamples = m_memory->getSlot()->size() / sizeof(int16_t); } - size_t delayVal = (size_t)(val * (float)m_maxDelaySamples); - delay(delayVal); - Serial.println(String("AudioEffectAnalogDelay::delay (ms): ") + calcAudioTimeMs(delayVal) - + String(" (samples): ") + delayVal + String(" out of ") + m_maxDelaySamples); - return; + if ((m_midiConfig[RATE][MIDI_CHANNEL] == channel) && + (m_midiConfig[RATE][MIDI_CONTROL] == control)) { + // Rate + Serial.println(String("AudioEffectAnalogChorus::rate: ") + 100*val + String("%")); + rate(val); + return; } if ((m_midiConfig[BYPASS][MIDI_CHANNEL] == channel) && (m_midiConfig[BYPASS][MIDI_CONTROL] == control)) { // Bypass - if (value >= 65) { bypass(false); Serial.println(String("AudioEffectAnalogDelay::not bypassed -> ON") + value); } - else { bypass(true); Serial.println(String("AudioEffectAnalogDelay::bypassed -> OFF") + value); } + if (value >= 65) { bypass(false); Serial.println(String("AudioEffectAnalogChorus::not bypassed -> ON") + value); } + else { bypass(true); Serial.println(String("AudioEffectAnalogChorus::bypassed -> OFF") + value); } return; } - if ((m_midiConfig[FEEDBACK][MIDI_CHANNEL] == channel) && - (m_midiConfig[FEEDBACK][MIDI_CONTROL] == control)) { - // Feedback - Serial.println(String("AudioEffectAnalogDelay::feedback: ") + 100*val + String("%")); - feedback(val); + if ((m_midiConfig[DEPTH][MIDI_CHANNEL] == channel) && + (m_midiConfig[DEPTH][MIDI_CONTROL] == control)) { + // depth + Serial.println(String("AudioEffectAnalogChorus::depth: ") + 100*val + String("%")); + depth(val); return; } if ((m_midiConfig[MIX][MIDI_CHANNEL] == channel) && (m_midiConfig[MIX][MIDI_CONTROL] == control)) { // Mix - Serial.println(String("AudioEffectAnalogDelay::mix: Dry: ") + 100*(1-val) + String("% Wet: ") + 100*val ); + Serial.println(String("AudioEffectAnalogChorus::mix: Dry: ") + 100*(1-val) + String("% Wet: ") + 100*val ); mix(val); return; } @@ -324,14 +296,14 @@ void AudioEffectAnalogDelay::processMidi(int channel, int control, int value) if ((m_midiConfig[VOLUME][MIDI_CHANNEL] == channel) && (m_midiConfig[VOLUME][MIDI_CONTROL] == control)) { // Volume - Serial.println(String("AudioEffectAnalogDelay::volume: ") + 100*val + String("%")); + Serial.println(String("AudioEffectAnalogChorus::volume: ") + 100*val + String("%")); volume(val); return; } } -void AudioEffectAnalogDelay::mapMidiControl(int parameter, int midiCC, int midiChannel) +void AudioEffectAnalogChorus::mapMidiControl(int parameter, int midiCC, int midiChannel) { if (parameter >= NUM_CONTROLS) { return ; // Invalid midi parameter diff --git a/src/effects/AudioEffectAnalogChorusFilters.h b/src/effects/AudioEffectAnalogChorusFilters.h new file mode 100644 index 0000000..308ffcc --- /dev/null +++ b/src/effects/AudioEffectAnalogChorusFilters.h @@ -0,0 +1,83 @@ +/**************************************************************************//** + * @file + * @author Steve Lascos + * @company Blackaddr Audio + * + * This file constains precomputed co-efficients for the AudioEffectAnalogChorus + * 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 BAEffects { + +// 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. AudioEffectAnalogChorus 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 CE2_NUM_STAGES = 4; +constexpr unsigned CE2_COEFF_SHIFT = 2; +constexpr int32_t CE2[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 +}; + +};