From c4cf09efed8d5830739ae4ac0124beaf174ddbbf Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Tue, 15 Jan 2019 11:25:29 +0100 Subject: [PATCH] Disabled BAPhysicalControls. Added AudioEffectSimpleChorus, created from AudioEffectAnalogDelay. --- src/.swp | Bin 0 -> 12288 bytes src/AudioEffectSimpleChorus.h | 159 ++++++++++++++ src/BAEffects.h | 1 + src/BALibrary.h | 2 +- src/effects/AudioEffectSimpleChorus.cpp | 198 ++++++++++++++++++ ...lControls.cpp => BAPhysicalControls.cpp.O} | 2 +- 6 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 src/.swp create mode 100644 src/AudioEffectSimpleChorus.h create mode 100644 src/effects/AudioEffectSimpleChorus.cpp rename src/peripherals/{BAPhysicalControls.cpp => BAPhysicalControls.cpp.O} (99%) diff --git a/src/.swp b/src/.swp new file mode 100644 index 0000000000000000000000000000000000000000..bb2f40dd8ac1adf2ce04842e0a46c37f91c73b67 GIT binary patch literal 12288 zcmeI%&r8EF6bJB3@Zd#71+P!ND2SVS@h}fl2M+8u_Cxd}X`60kTUwh8ofoej{F6NU z&-hQ6#dU`jc9adif!9aV@qG;kL zeZGX}!67rf5vi~vd0%A;-jwUDTU#<)jSr%s z)Z=2dL?DFka<4~+iRcJ. + *****************************************************************************/ + +#ifndef __BAEFFECTS_BAAUDIOEFFECTSIMPLECHORUS_H +#define __BAEFFECTS_BAAUDIOEFFECTSIMPLECHORUS_H + +#include +#include "LibBasicFunctions.h" + +namespace BAEffects { + +/**************************************************************************//** + * AudioEffectSimpleChorus models BBD based analog delays. It provides controls + * for delay, feedback (or regen), 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 AudioEffectSimpleChorus : public AudioStream { +public: + + ///< List of AudioEffectSimpleChorus MIDI controllable parameters + enum { + BYPASS = 0, ///< controls effect bypass + FREQUENCY, ///< controls the amount of delay + INTENSITY, ///< controls the amount of echo feedback (regen) + MIX, ///< controls the the mix of input and echo signals + NUM_CONTROLS ///< this can be used as an alias for the number of MIDI controls + }; + + // *** CONSTRUCTORS *** + AudioEffectSimpleChorus() = delete; + + /// Construct an analog delay using internal memory by specifying the maximum + /// delay in milliseconds. + /// @param maxDelayMs maximum delay in milliseconds. Larger delays use more memory. + AudioEffectSimpleChorus(float maxDelayMs); + + /// Construct an analog delay using internal memory by specifying the maximum + + virtual ~AudioEffectSimpleChorus(); ///< Destructor + + // *** PARAMETERS *** + + /// 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 amount of echo feedback (a.k.a regeneration). + /// @param feedback a floating point number between 0.0 and 1.0. + void frequency(float frequency) { m_frequency = frequency; } + + /// Set the amount of echo feedback (a.k.a regeneration). + /// @param feedback a floating point number between 0.0 and 1.0. + void intensity(float intensity) { m_intensity = intensity; } + + /// 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; } + + // ** 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); + + virtual void update(void); ///< update automatically called by the Teesny Audio Library + +private: + /// Set the delay in milliseconds. + /// @param milliseconds the request delay in milliseconds. Must be less than max delay. + void delay(float milliseconds); + + /// Set the delay in number of audio samples. + /// @param delaySamples the request delay in audio samples. Must be less than max delay. + void delay(size_t delaySamples); + + /// Set the delay as a fraction of the maximum delay. + /// The value should be between 0.0f and 1.0f + void delayFractionMax(float delayFraction); + + audio_block_t *m_inputQueueArray[1]; + bool m_isOmni = false; + bool m_bypass = true; + bool m_enable = false; + BALibrary::AudioDelay *m_memory = nullptr; + size_t m_maxDelaySamples = 0; + audio_block_t *m_previousBlock = nullptr; + audio_block_t *m_blockToRelease = nullptr; + BALibrary::LowFrequencyOscillatorVector lfo; + + // Controls + int m_midiConfig[NUM_CONTROLS][2]; // stores the midi parameter mapping + size_t m_delaySamples = 0; + float m_frequency = 1.0f; + float m_intensity = 1.0f; + float m_mix = 0.0f; + + void m_postProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet); + +}; + +} + +#endif /* __BAEFFECTS_BAAUDIOEFFECTANALOGDELAY_H */ diff --git a/src/BAEffects.h b/src/BAEffects.h index 50b92b0..8741b05 100644 --- a/src/BAEffects.h +++ b/src/BAEffects.h @@ -26,5 +26,6 @@ #include "AudioEffectAnalogDelay.h" #include "AudioEffectSOS.h" #include "AudioEffectTremolo.h" +#include "AudioEffectSimpleChorus.h" #endif /* __BAEFFECTS_H */ diff --git a/src/BALibrary.h b/src/BALibrary.h index 9a0a3d3..0df21e4 100644 --- a/src/BALibrary.h +++ b/src/BALibrary.h @@ -29,6 +29,6 @@ #include "BAAudioControlWM8731.h" // Codec Control #include "BASpiMemory.h" #include "BAGpio.h" -#include "BAPhysicalControls.h" +//#include "BAPhysicalControls.h" #endif /* __BALIBRARY_H */ diff --git a/src/effects/AudioEffectSimpleChorus.cpp b/src/effects/AudioEffectSimpleChorus.cpp new file mode 100644 index 0000000..f41f1b9 --- /dev/null +++ b/src/effects/AudioEffectSimpleChorus.cpp @@ -0,0 +1,198 @@ +/* + * AudioEffectSimpleChorus.cpp + * + * Created on: Jan 7, 2018 + * Author: slascos + */ +#include +#include // std::roundf +#include "AudioEffectAnalogDelayFilters.h" +#include "AudioEffectSimpleChorus.h" + +using namespace BALibrary; + +namespace BAEffects { + +constexpr int MIDI_CHANNEL = 0; +constexpr int MIDI_CONTROL = 1; + +AudioEffectSimpleChorus::AudioEffectSimpleChorus(float maxDelayMs) +: AudioStream(1, m_inputQueueArray) +{ + delay(maxDelayMs); + m_memory = new AudioDelay(maxDelayMs); + m_maxDelaySamples = calcAudioSamples(maxDelayMs); + lfo.setRateAudio(m_frequency); +} + +AudioEffectSimpleChorus::~AudioEffectSimpleChorus() +{ + if (m_memory) delete m_memory; +} + +void AudioEffectSimpleChorus::update(void) +{ + audio_block_t *inputAudioBlock = receiveReadOnly(); // get the next block of input samples + + // Check is block is disabled + if (m_enable == false) { + // do not transmit or process any audio, return as quickly as possible. + if (inputAudioBlock) release(inputAudioBlock); + + // release all held memory resources + if (m_previousBlock) { + release(m_previousBlock); m_previousBlock = nullptr; + } + // when using internal memory we have to release all references in the ring buffer + while (m_memory->getRingBuffer()->size() > 0) { + audio_block_t *releaseBlock = m_memory->getRingBuffer()->front(); + m_memory->getRingBuffer()->pop_front(); + if (releaseBlock) release(releaseBlock); + } + return; + } + + // Check is block is bypassed, if so either transmit input directly or create silence + if (m_bypass == true) { + // transmit the input directly + if (!inputAudioBlock) { + // create silence + inputAudioBlock = allocate(); + if (!inputAudioBlock) { return; } // failed to allocate + else { + clearAudioBlock(inputAudioBlock); + } + } + transmit(inputAudioBlock, 0); + release(inputAudioBlock); + return; + } + + // Otherwise perform normal processing + // In order to make use of the SPI DMA, we need to request the read from memory first, + // then do other processing while it fills in the back. + audio_block_t *blockToOutput = nullptr; // this will hold the output audio + blockToOutput = allocate(); + if (!blockToOutput) return; // skip this update cycle due to failure + + // get the data. If using external memory with DMA, this won't be filled until + // later. + m_memory->getSamples(blockToOutput, m_delaySamples); + + //audio_block_t *blockToRelease = m_memory->addBlock(blockToOutput); + + // If using DMA, we need something else to do while that read executes, so + // move on to input preprocessing + + // Chorus + float *mod = lfo.getNextVector(); + for(uint8_t i=0;idata[i])+(m_delaySamples/2); + //inputAudioBlock->data[i] = (int16_t)sample/2+inputAudioBlock->data[i]/2; + blockToOutput->data[i]=(float(inputAudioBlock->data[i])*mod[i]); + } + + // BACK TO OUTPUT PROCESSING + + // perform the wet/dry mix mix + //m_postProcessing(blockToOutput, inputAudioBlock, blockToOutput); + transmit(blockToOutput); + + release(inputAudioBlock); + release(m_previousBlock); + m_previousBlock = blockToOutput; + + //if (m_blockToRelease) release(m_blockToRelease); + //m_blockToRelease = blockToRelease; +} + +void AudioEffectSimpleChorus::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"); } + + m_delaySamples = delaySamples; +} + +void AudioEffectSimpleChorus::delay(size_t delaySamples) +{ + if (!m_memory) { Serial.println("delay(): m_memory is not valid"); } + + m_delaySamples = delaySamples; +} + +void AudioEffectSimpleChorus::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"); } + + m_delaySamples = delaySamples; +} + +void AudioEffectSimpleChorus::m_postProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet) +{ + if (!out) return; // no valid output buffer + + if ( out && dry && wet) { + // Simulate the LPF IIR nature of the analog systems + alphaBlend(out, dry, wet, m_mix); + } else if (dry) { + memcpy(out->data, dry->data, sizeof(int16_t) * AUDIO_BLOCK_SAMPLES); + } +} + +void AudioEffectSimpleChorus::processMidi(int channel, int control, int value) +{ + float val = (float)value / 127.0f; + + if ((m_midiConfig[FREQUENCY][MIDI_CHANNEL] == channel) && + (m_midiConfig[FREQUENCY][MIDI_CONTROL] == control)) { + // Frequency + frequency(value/10); + Serial.println(String("AudioEffectSimpleChorus::frequency (Hz): ") + calcAudioTimeMs(value/10)); + return; + } + + if ((m_midiConfig[BYPASS][MIDI_CHANNEL] == channel) && + (m_midiConfig[BYPASS][MIDI_CONTROL] == control)) { + // Bypass + if (value >= 65) { bypass(false); Serial.println(String("AudioEffectSimpleChorus::not bypassed -> ON") + value); } + else { bypass(true); Serial.println(String("AudioEffectSimpleChorus::bypassed -> OFF") + value); } + return; + } + + if ((m_midiConfig[MIX][MIDI_CHANNEL] == channel) && + (m_midiConfig[MIX][MIDI_CONTROL] == control)) { + // Mix + Serial.println(String("AudioEffectSimpleChorus::mix: Dry: ") + 100*(1-val) + String("% Wet: ") + 100*val ); + mix(val); + return; + } +} + +void AudioEffectSimpleChorus::mapMidiControl(int parameter, int midiCC, int midiChannel) +{ + if (parameter >= NUM_CONTROLS) { + return ; // Invalid midi parameter + } + m_midiConfig[parameter][MIDI_CHANNEL] = midiChannel; + m_midiConfig[parameter][MIDI_CONTROL] = midiCC; +} + +} + + diff --git a/src/peripherals/BAPhysicalControls.cpp b/src/peripherals/BAPhysicalControls.cpp.O similarity index 99% rename from src/peripherals/BAPhysicalControls.cpp rename to src/peripherals/BAPhysicalControls.cpp.O index 4b45874..7016997 100644 --- a/src/peripherals/BAPhysicalControls.cpp +++ b/src/peripherals/BAPhysicalControls.cpp.O @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "BAPhysicalControls.h" +//#include "BAPhysicalControls.h" // These calls must be define in order to get vector to work on arduino namespace std {