forked from wirtz/BALibrary
Feature/add lfo (#1)
* Basic working tremolo * LFO now works as a vector * Added standard tremolo demomaster
parent
d0a4ff5b3c
commit
1b2ccda033
@ -0,0 +1,131 @@ |
|||||||
|
/*************************************************************************
|
||||||
|
* 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 AudioEffectsTremolo effect. It can |
||||||
|
* be controlled using USB MIDI. You can get a free USB MIDI Controller |
||||||
|
* appliation at
|
||||||
|
* http://www.blackaddr.com/downloads/BAMidiTester/
|
||||||
|
* or the source code at |
||||||
|
* https://github.com/Blackaddr/BAMidiTester
|
||||||
|
*
|
||||||
|
* Even if you don't control the guitar effect with USB MIDI, you must set |
||||||
|
* the Arduino IDE USB-Type under Tools to "Serial + MIDI" |
||||||
|
*/ |
||||||
|
#include <MIDI.h> |
||||||
|
#include "BALibrary.h" |
||||||
|
#include "BAEffects.h" |
||||||
|
|
||||||
|
using namespace midi; |
||||||
|
using namespace BAEffects; |
||||||
|
using namespace BALibrary; |
||||||
|
|
||||||
|
AudioInputI2S i2sIn; |
||||||
|
AudioOutputI2S i2sOut; |
||||||
|
BAAudioControlWM8731 codec; |
||||||
|
|
||||||
|
/// IMPORTANT /////
|
||||||
|
// YOU MUST USE TEENSYDUINO 1.41 or greater
|
||||||
|
// YOU MUST COMPILE THIS DEMO USING Serial + Midi
|
||||||
|
|
||||||
|
//#define USE_EXT // uncomment this line to use External MEM0
|
||||||
|
#define MIDI_DEBUG // uncomment to see raw MIDI info in terminal
|
||||||
|
|
||||||
|
AudioEffectTremolo tremolo; |
||||||
|
|
||||||
|
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 tremolo, and the output
|
||||||
|
// to both i2s channels
|
||||||
|
AudioConnection input(i2sIn,0, tremolo,0); |
||||||
|
AudioConnection tremoloOut(tremolo, 0, cabFilter, 0); |
||||||
|
AudioConnection leftOut(cabFilter,0, i2sOut, 0); |
||||||
|
AudioConnection rightOut(cabFilter,0, i2sOut, 1); |
||||||
|
|
||||||
|
int loopCount = 0; |
||||||
|
|
||||||
|
void setup() { |
||||||
|
delay(100); |
||||||
|
Serial.begin(57600); // Start the serial port
|
||||||
|
|
||||||
|
// Disable the codec first
|
||||||
|
codec.disable(); |
||||||
|
delay(100); |
||||||
|
AudioMemory(128); |
||||||
|
delay(5); |
||||||
|
|
||||||
|
// Enable the codec
|
||||||
|
Serial.println("Enabling codec...\n"); |
||||||
|
codec.enable(); |
||||||
|
delay(100); |
||||||
|
|
||||||
|
// Configure which MIDI CC's will control the effect parameters
|
||||||
|
tremolo.mapMidiControl(AudioEffectTremolo::BYPASS,16); |
||||||
|
tremolo.mapMidiControl(AudioEffectTremolo::RATE,20); |
||||||
|
tremolo.mapMidiControl(AudioEffectTremolo::DEPTH,21); |
||||||
|
tremolo.mapMidiControl(AudioEffectTremolo::VOLUME,22); |
||||||
|
|
||||||
|
// Besure to enable the tremolo. When disabled, audio is is completely blocked
|
||||||
|
// to minimize resources to nearly zero.
|
||||||
|
tremolo.enable();
|
||||||
|
|
||||||
|
// Set some default values.
|
||||||
|
// These can be changed by sending MIDI CC messages over the USB using
|
||||||
|
// the BAMidiTester application.
|
||||||
|
tremolo.rate(0.5f); // initial LFO rate of 1/2 max which is about 10 Hz
|
||||||
|
tremolo.bypass(false); |
||||||
|
tremolo.depth(0.5f); // 50% depth modulation
|
||||||
|
|
||||||
|
// 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 OnControlChange(byte channel, byte control, byte value) { |
||||||
|
tremolo.processMidi(channel, control, value); |
||||||
|
#ifdef MIDI_DEBUG |
||||||
|
Serial.print("Control Change, ch="); |
||||||
|
Serial.print(channel, DEC); |
||||||
|
Serial.print(", control="); |
||||||
|
Serial.print(control, DEC); |
||||||
|
Serial.print(", value="); |
||||||
|
Serial.print(value, DEC); |
||||||
|
Serial.println(); |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
|
void loop() { |
||||||
|
// usbMIDI.read() needs to be called rapidly from loop(). When
|
||||||
|
// each MIDI messages arrives, it return true. The message must
|
||||||
|
// be fully processed before usbMIDI.read() is called again.
|
||||||
|
|
||||||
|
if (loopCount % 524288 == 0) { |
||||||
|
Serial.print("Processor Usage, Total: "); Serial.print(AudioProcessorUsage()); |
||||||
|
Serial.print("% "); |
||||||
|
Serial.print(" tremolo: "); Serial.print(tremolo.processorUsage()); |
||||||
|
Serial.println("%"); |
||||||
|
} |
||||||
|
loopCount++; |
||||||
|
|
||||||
|
// check for new MIDI from USB
|
||||||
|
if (usbMIDI.read()) { |
||||||
|
// this code entered only if new MIDI received
|
||||||
|
byte type, channel, data1, data2, cable; |
||||||
|
type = usbMIDI.getType(); // which MIDI message, 128-255
|
||||||
|
channel = usbMIDI.getChannel(); // which MIDI channel, 1-16
|
||||||
|
data1 = usbMIDI.getData1(); // first data byte of message, 0-127
|
||||||
|
data2 = usbMIDI.getData2(); // second data byte of message, 0-127
|
||||||
|
Serial.println(String("Received a MIDI message on channel ") + channel); |
||||||
|
|
||||||
|
if (type == MidiType::ControlChange) { |
||||||
|
// if type is 3, it's a CC MIDI Message
|
||||||
|
// Note: the Arduino MIDI library encodes channels as 1-16 instead
|
||||||
|
// of 0 to 15 as it should, so we must subtract one.
|
||||||
|
OnControlChange(channel-1, data1, data2); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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 |
||||||
|
}; |
@ -0,0 +1,192 @@ |
|||||||
|
/*************************************************************************
|
||||||
|
* 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 BAAudioEffectsTremolo effect. It can |
||||||
|
* be controlled using the Blackaddr Audio "Expansion Control Board". |
||||||
|
*
|
||||||
|
* POT1 (left) controls amount of delay |
||||||
|
* POT2 (right) controls amount of feedback |
||||||
|
* 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; |
||||||
|
|
||||||
|
AudioEffectTremolo tremolo; |
||||||
|
|
||||||
|
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, tremolo,0); |
||||||
|
AudioConnection delayOut(tremolo, 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 volume
|
||||||
|
// - 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 the waveforms
|
||||||
|
// - 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 waveformIndex = 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, waveformHandle, rateHandle, depthHandle, volumeHandle, 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
|
||||||
|
waveformHandle = 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);
|
||||||
|
volumeHandle = 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
|
||||||
|
|
||||||
|
// Besure to enable the tremolo. When disabled, audio is is completely blocked by the effect
|
||||||
|
// to minimize resource usage to nearly to nearly zero.
|
||||||
|
tremolo.enable();
|
||||||
|
|
||||||
|
// Set some default values.
|
||||||
|
// These can be changed using the controls on the Blackaddr Audio Expansion Board
|
||||||
|
tremolo.bypass(false); |
||||||
|
tremolo.rate(0.0f); |
||||||
|
tremolo.depth(1.0f); |
||||||
|
|
||||||
|
//////////////////////////////////
|
||||||
|
// Waveform selection //
|
||||||
|
// These are commented out, in this example we'll use SW2 to cycle through the different filters
|
||||||
|
//tremolo.setWaveform(Waveform::SINE); // The default waveform
|
||||||
|
|
||||||
|
// 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 = tremolo.isBypass(); // get the current state
|
||||||
|
bypass = !bypass; // change it
|
||||||
|
tremolo.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 waveforms
|
||||||
|
controls.setOutput(led2Handle, controls.getSwitchValue(led2Handle)); |
||||||
|
if (controls.isSwitchToggled(waveformHandle)) { |
||||||
|
waveformIndex = (waveformIndex + 1) % static_cast<unsigned>(Waveform::NUM_WAVEFORMS); |
||||||
|
// cast the index
|
||||||
|
tremolo.setWaveform(static_cast<Waveform>(waveformIndex)); |
||||||
|
Serial.println(String("Waveform set to ") + waveformIndex); |
||||||
|
} |
||||||
|
|
||||||
|
// Use POT1 (left) to control the rate setting
|
||||||
|
if (controls.checkPotValue(rateHandle, potValue)) { |
||||||
|
// Pot has changed
|
||||||
|
Serial.println(String("New RATE setting: ") + potValue); |
||||||
|
tremolo.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); |
||||||
|
tremolo.depth(potValue); |
||||||
|
} |
||||||
|
|
||||||
|
// Use POT3 (centre) to control the volume setting
|
||||||
|
if (controls.checkPotValue(volumeHandle, potValue)) { |
||||||
|
// Pot has changed
|
||||||
|
Serial.println(String("New VOLUME setting: ") + potValue); |
||||||
|
tremolo.volume(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<float>(headphoneVolume) / static_cast<float>(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) {
|
||||||
|
if (loopCount % 25000 == 0) { |
||||||
|
Serial.print("Processor Usage, Total: "); Serial.print(AudioProcessorUsage()); |
||||||
|
Serial.print("% "); |
||||||
|
Serial.print(" tremolo: "); Serial.print(tremolo.processorUsage()); |
||||||
|
Serial.println("%"); |
||||||
|
} |
||||||
|
loopCount++; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,126 @@ |
|||||||
|
/**************************************************************************//**
|
||||||
|
* @file |
||||||
|
* @author Steve Lascos |
||||||
|
* @company Blackaddr Audio |
||||||
|
* |
||||||
|
* Tremolo is a classic volume modulate effect. |
||||||
|
* |
||||||
|
* @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__BAEFFECTS_AUDIOEFFECTDELAYEXTERNAL_H; 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*****************************************************************************/ |
||||||
|
|
||||||
|
#ifndef __BAEFFECTS_AUDIOEFFECTTREMOLO_H |
||||||
|
#define __BAEFFECTS_AUDIOEFFECTTREMOLO_H |
||||||
|
|
||||||
|
#include <Audio.h> |
||||||
|
#include "LibBasicFunctions.h" |
||||||
|
|
||||||
|
namespace BAEffects { |
||||||
|
|
||||||
|
/**************************************************************************//**
|
||||||
|
* AudioEffectTremolo |
||||||
|
*****************************************************************************/ |
||||||
|
class AudioEffectTremolo : public AudioStream { |
||||||
|
public: |
||||||
|
|
||||||
|
///< List of AudioEffectTremolo MIDI controllable parameters
|
||||||
|
enum { |
||||||
|
BYPASS = 0, ///< controls effect bypass
|
||||||
|
RATE, ///< controls the rate of the modulation
|
||||||
|
DEPTH, ///< controls the depth of the modulation
|
||||||
|
WAVEFORM, ///< select the modulation waveform
|
||||||
|
VOLUME, ///< controls the output volume level
|
||||||
|
NUM_CONTROLS ///< this can be used as an alias for the number of MIDI controls
|
||||||
|
}; |
||||||
|
|
||||||
|
// *** CONSTRUCTORS ***
|
||||||
|
AudioEffectTremolo(); |
||||||
|
|
||||||
|
virtual ~AudioEffectTremolo(); ///< Destructor
|
||||||
|
|
||||||
|
// *** PARAMETERS ***
|
||||||
|
void rate(float rateValue); |
||||||
|
|
||||||
|
void depth(float depthValue) { m_depth = depthValue; } |
||||||
|
|
||||||
|
void setWaveform(BALibrary::Waveform waveform); |
||||||
|
|
||||||
|
|
||||||
|
/// 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 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); |
||||||
|
|
||||||
|
|
||||||
|
virtual void update(void); ///< update automatically called by the Teesny Audio Library
|
||||||
|
|
||||||
|
private: |
||||||
|
audio_block_t *m_inputQueueArray[1]; |
||||||
|
BALibrary::LowFrequencyOscillatorVector<float> m_osc; |
||||||
|
int m_midiConfig[NUM_CONTROLS][2]; // stores the midi parameter mapping
|
||||||
|
bool m_isOmni = false; |
||||||
|
bool m_bypass = true; |
||||||
|
bool m_enable = false; |
||||||
|
|
||||||
|
float m_rate = 0.0f; |
||||||
|
float m_depth = 0.0f; |
||||||
|
BALibrary::Waveform m_waveform = BALibrary::Waveform::SINE; |
||||||
|
float m_volume = 1.0f; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
#endif /* __BAEFFECTS_AUDIOEFFECTTREMOLO_H */ |
@ -0,0 +1,145 @@ |
|||||||
|
/*
|
||||||
|
* LowFrequencyOscillator.cpp |
||||||
|
* |
||||||
|
* Created on: October 12, 2018 |
||||||
|
* Author: Steve Lascos |
||||||
|
* |
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
|
||||||
|
#include <assert.h> |
||||||
|
#include "Audio.h" |
||||||
|
#include "LibBasicFunctions.h" |
||||||
|
|
||||||
|
namespace BALibrary { |
||||||
|
|
||||||
|
template <class T> |
||||||
|
void LowFrequencyOscillatorVector<T>::m_initPhase(T radiansPerSample) |
||||||
|
{ |
||||||
|
// Initialize the phase vector starting at 0 radians, and incrementing
|
||||||
|
// by radiansPerSample for each element in the vector.
|
||||||
|
T initialPhase[AUDIO_BLOCK_SAMPLES]; |
||||||
|
for (auto i=0; i<AUDIO_BLOCK_SAMPLES; i++) { |
||||||
|
initialPhase[i] = (T)i * radiansPerSample; |
||||||
|
} |
||||||
|
m_radiansPerBlock = radiansPerSample * (T)AUDIO_BLOCK_SAMPLES; |
||||||
|
|
||||||
|
// there could be different threads controlling the LFO rate and consuming
|
||||||
|
// the LFO output, so we need to protected the m_phaseVec for thread-safety.
|
||||||
|
while (m_phaseLock.test_and_set()) {} |
||||||
|
memcpy(m_phaseVec, initialPhase, sizeof(T)*AUDIO_BLOCK_SAMPLES); |
||||||
|
m_phaseLock.clear(); |
||||||
|
} |
||||||
|
|
||||||
|
// This function takes in the frequency of the LFO in hertz and uses knowledge
|
||||||
|
// about the the audio sample rate to calcuate the correct radians per sample.
|
||||||
|
template <class T> |
||||||
|
void LowFrequencyOscillatorVector<T>::setRateAudio(float frequencyHz) |
||||||
|
{ |
||||||
|
T radiansPerSample; |
||||||
|
if (frequencyHz == 0) { |
||||||
|
radiansPerSample = 0; |
||||||
|
} else { |
||||||
|
T periodSamples = AUDIO_SAMPLE_RATE_EXACT / frequencyHz; |
||||||
|
radiansPerSample = (T)TWO_PI_F / periodSamples; |
||||||
|
} |
||||||
|
m_initPhase(radiansPerSample); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// This function is used when the LFO is being called at some rate other than
|
||||||
|
// the audio rate. Here you can manually set the radians per sample as a fraction
|
||||||
|
// of 2*PI
|
||||||
|
template <class T> |
||||||
|
void LowFrequencyOscillatorVector<T>::setRateRatio(float ratio) |
||||||
|
{ |
||||||
|
T radiansPerSample; |
||||||
|
if (ratio == 0) { |
||||||
|
radiansPerSample = 0; |
||||||
|
} else { |
||||||
|
radiansPerSample = (T)TWO_PI_F * ratio; |
||||||
|
} |
||||||
|
m_initPhase(radiansPerSample); |
||||||
|
} |
||||||
|
|
||||||
|
// When this function is called, it will update the phase vector by incrementing by
|
||||||
|
// radians per block which is radians per sample * block size.
|
||||||
|
template <class T> |
||||||
|
inline void LowFrequencyOscillatorVector<T>::m_updatePhase() |
||||||
|
{ |
||||||
|
if (m_phaseLock.test_and_set()) { return; } |
||||||
|
|
||||||
|
if (m_phaseVec[0] > TWO_PI_F) { |
||||||
|
arm_offset_f32(m_phaseVec, -TWO_PI_F + m_radiansPerBlock, m_phaseVec, AUDIO_BLOCK_SAMPLES); |
||||||
|
} else { |
||||||
|
arm_offset_f32(m_phaseVec, m_radiansPerBlock, m_phaseVec, AUDIO_BLOCK_SAMPLES); |
||||||
|
} |
||||||
|
m_phaseLock.clear(); |
||||||
|
} |
||||||
|
|
||||||
|
// This function will compute the vector of samples for the output waveform using
|
||||||
|
// the current phase vector.
|
||||||
|
template <class T> |
||||||
|
T *LowFrequencyOscillatorVector<T>::getNextVector() |
||||||
|
{ |
||||||
|
switch(m_waveform) { |
||||||
|
case Waveform::SINE : |
||||||
|
for (auto i=0; i<AUDIO_BLOCK_SAMPLES; i++) { |
||||||
|
m_outputVec[i] = arm_sin_f32(m_phaseVec[i]); |
||||||
|
} |
||||||
|
break; |
||||||
|
case Waveform::SQUARE : |
||||||
|
for (auto i=0; i<AUDIO_BLOCK_SAMPLES; i++) { |
||||||
|
if (m_phaseVec[i] > 3*PI_F) { |
||||||
|
m_outputVec[i] = 0.0f; |
||||||
|
} |
||||||
|
else if (m_phaseVec[i] > 2*PI_F) { |
||||||
|
m_outputVec[i] = 1.0f; |
||||||
|
} |
||||||
|
else if (m_phaseVec[i] > PI_F) { |
||||||
|
m_outputVec[i] = 0.0f; |
||||||
|
} else { |
||||||
|
m_outputVec[i] = 1.0f; |
||||||
|
} |
||||||
|
} |
||||||
|
break; |
||||||
|
case Waveform::TRIANGLE : |
||||||
|
// for (auto i=0; i<AUDIO_BLOCK_SAMPLES; i++) {
|
||||||
|
// if (m_phaseVec[i] > 3*PI_F) {
|
||||||
|
// m_outputVec[i] = ;
|
||||||
|
// }
|
||||||
|
// else if (m_phaseVec[i] > 2*PI_F) {
|
||||||
|
// m_outputVec[i] = 1.0f;
|
||||||
|
// }
|
||||||
|
// else if (m_phaseVec[i] > PI_F) {
|
||||||
|
// m_outputVec[i] = 0.0f;
|
||||||
|
// } else {
|
||||||
|
// m_outputVec[i] = 1.0f;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
break; |
||||||
|
case Waveform::RANDOM : |
||||||
|
break; |
||||||
|
default : |
||||||
|
assert(0); // This occurs if a Waveform type is missing from the switch statement
|
||||||
|
} |
||||||
|
|
||||||
|
m_updatePhase(); |
||||||
|
return m_outputVec; |
||||||
|
} |
||||||
|
|
||||||
|
template class LowFrequencyOscillatorVector<float>; |
||||||
|
|
||||||
|
} // namespace BALibrary
|
||||||
|
|
@ -0,0 +1,163 @@ |
|||||||
|
/*
|
||||||
|
* AudioEffectTremolo.cpp |
||||||
|
* |
||||||
|
* Created on: Jan 7, 2018 |
||||||
|
* Author: slascos |
||||||
|
*/ |
||||||
|
#include <cmath> // std::roundf |
||||||
|
#include "AudioEffectTremolo.h" |
||||||
|
|
||||||
|
using namespace BALibrary; |
||||||
|
|
||||||
|
namespace BAEffects { |
||||||
|
|
||||||
|
constexpr int MIDI_CHANNEL = 0; |
||||||
|
constexpr int MIDI_CONTROL = 1; |
||||||
|
|
||||||
|
constexpr float MAX_RATE_HZ = 20.0f; |
||||||
|
|
||||||
|
AudioEffectTremolo::AudioEffectTremolo() |
||||||
|
: AudioStream(1, m_inputQueueArray) |
||||||
|
{ |
||||||
|
m_osc.setWaveform(m_waveform); |
||||||
|
} |
||||||
|
|
||||||
|
AudioEffectTremolo::~AudioEffectTremolo() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
void AudioEffectTremolo::update(void) |
||||||
|
{ |
||||||
|
audio_block_t *inputAudioBlock = receiveWritable(); // 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); |
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
// DO PROCESSING
|
||||||
|
// apply modulation wave
|
||||||
|
float *mod = m_osc.getNextVector(); |
||||||
|
for (auto i=0; i<AUDIO_BLOCK_SAMPLES; i++) { |
||||||
|
mod[i] = (mod[i] + 1.0f) / 2.0f; |
||||||
|
mod[i] = (1.0f - m_depth) + mod[i]*m_depth; |
||||||
|
mod[i] = m_volume * mod[i]; |
||||||
|
float sample = std::roundf(mod[i] * (float)inputAudioBlock->data[i]); |
||||||
|
inputAudioBlock->data[i] = (int16_t)sample; |
||||||
|
} |
||||||
|
//Serial.println(String("mod: ") + mod[0]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//float mod = (m_osc.getNext()+1.0f)/2.0f; // value between -1.0 and +1.0f
|
||||||
|
//float modVolume = (1.0f - m_depth) + mod*m_depth; // value between 0 to depth
|
||||||
|
//float finalVolume = m_volume * modVolume;
|
||||||
|
|
||||||
|
// Set the output volume
|
||||||
|
//gainAdjust(inputAudioBlock, inputAudioBlock, finalVolume, 1);
|
||||||
|
|
||||||
|
transmit(inputAudioBlock); |
||||||
|
release(inputAudioBlock); |
||||||
|
} |
||||||
|
|
||||||
|
void AudioEffectTremolo::rate(float rateValue) |
||||||
|
{ |
||||||
|
float rateAudioBlock = rateValue * MAX_RATE_HZ; |
||||||
|
m_osc.setRateAudio(rateAudioBlock); |
||||||
|
} |
||||||
|
|
||||||
|
void AudioEffectTremolo::setWaveform(Waveform waveform) |
||||||
|
{ |
||||||
|
m_waveform = waveform; |
||||||
|
m_osc.setWaveform(waveform); |
||||||
|
} |
||||||
|
|
||||||
|
void AudioEffectTremolo::processMidi(int channel, int control, int value) |
||||||
|
{ |
||||||
|
|
||||||
|
float val = (float)value / 127.0f; |
||||||
|
|
||||||
|
if ((m_midiConfig[BYPASS][MIDI_CHANNEL] == channel) && |
||||||
|
(m_midiConfig[BYPASS][MIDI_CONTROL] == control)) { |
||||||
|
// Bypass
|
||||||
|
if (value >= 65) { bypass(false); Serial.println(String("AudioEffectTremolo::not bypassed -> ON") + value); } |
||||||
|
else { bypass(true); Serial.println(String("AudioEffectTremolo::bypassed -> OFF") + value); } |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if ((m_midiConfig[RATE][MIDI_CHANNEL] == channel) && |
||||||
|
(m_midiConfig[RATE][MIDI_CONTROL] == control)) { |
||||||
|
// Rate
|
||||||
|
rate(val); |
||||||
|
Serial.println(String("AudioEffectTremolo::rate: ") + m_rate); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if ((m_midiConfig[DEPTH][MIDI_CHANNEL] == channel) && |
||||||
|
(m_midiConfig[DEPTH][MIDI_CONTROL] == control)) { |
||||||
|
// Depth
|
||||||
|
depth(val); |
||||||
|
Serial.println(String("AudioEffectTremolo::depth: ") + m_depth); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if ((m_midiConfig[WAVEFORM][MIDI_CHANNEL] == channel) && |
||||||
|
(m_midiConfig[WAVEFORM][MIDI_CONTROL] == control)) { |
||||||
|
// Waveform
|
||||||
|
if (value < 16) { |
||||||
|
m_waveform = Waveform::SINE; |
||||||
|
} else if (value < 32) { |
||||||
|
m_waveform = Waveform::TRIANGLE; |
||||||
|
} else if (value < 48) { |
||||||
|
m_waveform = Waveform::SQUARE; |
||||||
|
} else if (value < 64) { |
||||||
|
m_waveform = Waveform::SAWTOOTH; |
||||||
|
} else if (value < 80) { |
||||||
|
m_waveform = Waveform::RANDOM; |
||||||
|
} |
||||||
|
|
||||||
|
Serial.println(String("AudioEffectTremolo::waveform: ") + static_cast<unsigned>(m_waveform)); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if ((m_midiConfig[VOLUME][MIDI_CHANNEL] == channel) && |
||||||
|
(m_midiConfig[VOLUME][MIDI_CONTROL] == control)) { |
||||||
|
// Volume
|
||||||
|
Serial.println(String("AudioEffectTremolo::volume: ") + 100*val + String("%")); |
||||||
|
volume(val); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void AudioEffectTremolo::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; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in new issue