You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
291 lines
9.1 KiB
291 lines
9.1 KiB
7 years ago
|
/*
|
||
|
* AudioEffectAnalogDelay.cpp
|
||
|
*
|
||
|
* Created on: Jan 7, 2018
|
||
|
* Author: slascos
|
||
|
*/
|
||
|
#include <new>
|
||
|
#include "AudioEffectAnalogDelay.h"
|
||
|
|
||
|
namespace BAGuitar {
|
||
|
|
||
7 years ago
|
constexpr int MIDI_NUM_PARAMS = 4;
|
||
|
constexpr int MIDI_CHANNEL = 0;
|
||
|
constexpr int MIDI_CONTROL = 1;
|
||
|
|
||
7 years ago
|
constexpr int MIDI_BYPASS = 0;
|
||
7 years ago
|
constexpr int MIDI_DELAY = 1;
|
||
|
constexpr int MIDI_FEEDBACK = 2;
|
||
|
constexpr int MIDI_MIX = 3;
|
||
|
|
||
7 years ago
|
// BOSS DM-3 Filters
|
||
|
constexpr unsigned NUM_IIR_STAGES = 4;
|
||
|
constexpr unsigned IIR_COEFF_SHIFT = 2;
|
||
|
constexpr int32_t DEFAULT_COEFFS[5*NUM_IIR_STAGES] = {
|
||
|
536870912, 988616936, 455608573, 834606945, -482959709,
|
||
|
536870912, 1031466345, 498793368, 965834205, -467402235,
|
||
|
536870912, 1105821939, 573646688, 928470657, -448083489,
|
||
|
2339, 5093, 2776, 302068995, 4412722
|
||
|
};
|
||
|
|
||
7 years ago
|
|
||
7 years ago
|
AudioEffectAnalogDelay::AudioEffectAnalogDelay(float maxDelayMs)
|
||
7 years ago
|
: AudioStream(1, m_inputQueueArray)
|
||
|
{
|
||
7 years ago
|
m_memory = new AudioDelay(maxDelayMs);
|
||
|
m_maxDelaySamples = calcAudioSamples(maxDelayMs);
|
||
7 years ago
|
m_iir = new IirBiQuadFilterHQ(NUM_IIR_STAGES, reinterpret_cast<const int32_t *>(&DEFAULT_COEFFS), IIR_COEFF_SHIFT);
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
AudioEffectAnalogDelay::AudioEffectAnalogDelay(size_t numSamples)
|
||
7 years ago
|
: AudioStream(1, m_inputQueueArray)
|
||
|
{
|
||
7 years ago
|
m_memory = new AudioDelay(numSamples);
|
||
7 years ago
|
m_maxDelaySamples = numSamples;
|
||
7 years ago
|
m_iir = new IirBiQuadFilterHQ(NUM_IIR_STAGES, reinterpret_cast<const int32_t *>(&DEFAULT_COEFFS), IIR_COEFF_SHIFT);
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// requires preallocated memory large enough
|
||
7 years ago
|
AudioEffectAnalogDelay::AudioEffectAnalogDelay(ExtMemSlot *slot)
|
||
7 years ago
|
: AudioStream(1, m_inputQueueArray)
|
||
|
{
|
||
7 years ago
|
m_memory = new AudioDelay(slot);
|
||
7 years ago
|
m_maxDelaySamples = slot->size();
|
||
7 years ago
|
m_externalMemory = true;
|
||
7 years ago
|
m_iir = new IirBiQuadFilterHQ(NUM_IIR_STAGES, reinterpret_cast<const int32_t *>(&DEFAULT_COEFFS), IIR_COEFF_SHIFT);
|
||
7 years ago
|
}
|
||
|
|
||
|
AudioEffectAnalogDelay::~AudioEffectAnalogDelay()
|
||
|
{
|
||
|
if (m_memory) delete m_memory;
|
||
7 years ago
|
if (m_iir) delete m_iir;
|
||
7 years ago
|
}
|
||
|
|
||
|
void AudioEffectAnalogDelay::update(void)
|
||
|
{
|
||
7 years ago
|
audio_block_t *inputAudioBlock = receiveReadOnly(); // get the next block of input samples
|
||
7 years ago
|
|
||
7 years ago
|
// Check is block is disabled
|
||
7 years ago
|
if (m_enable == false) {
|
||
7 years ago
|
// do not transmit or process any audio, return as quickly as possible.
|
||
|
if (inputAudioBlock) release(inputAudioBlock);
|
||
7 years ago
|
|
||
7 years ago
|
// release all held memory resources
|
||
7 years ago
|
if (m_previousBlock) {
|
||
|
release(m_previousBlock); m_previousBlock = nullptr;
|
||
|
}
|
||
|
if (!m_externalMemory) {
|
||
7 years ago
|
// when using internal memory we have to release all references in the ring buffer
|
||
7 years ago
|
while (m_memory->getRingBuffer()->size() > 0) {
|
||
|
audio_block_t *releaseBlock = m_memory->getRingBuffer()->front();
|
||
|
m_memory->getRingBuffer()->pop_front();
|
||
|
if (releaseBlock) release(releaseBlock);
|
||
|
}
|
||
|
}
|
||
7 years ago
|
return;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// 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);
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
transmit(inputAudioBlock, 0);
|
||
|
release(inputAudioBlock);
|
||
|
return;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Otherwise perform normal processing
|
||
7 years ago
|
// 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);
|
||
|
|
||
|
// If using DMA, we need something else to do while that read executes, so
|
||
|
// move on to input preprocessing
|
||
|
|
||
7 years ago
|
// Preprocessing
|
||
|
audio_block_t *preProcessed = allocate();
|
||
7 years ago
|
// mix the input with the feedback path in the pre-processing stage
|
||
7 years ago
|
m_preProcessing(preProcessed, inputAudioBlock, m_previousBlock);
|
||
7 years ago
|
|
||
7 years ago
|
// consider doing the BBD post processing here to use up more time while waiting
|
||
|
// for the read data to come back
|
||
7 years ago
|
audio_block_t *blockToRelease = m_memory->addBlock(preProcessed);
|
||
7 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
// BACK TO OUTPUT PROCESSING
|
||
|
// audio_block_t *blockToOutput = nullptr;
|
||
|
// blockToOutput = allocate();
|
||
7 years ago
|
|
||
|
// copy the output data
|
||
7 years ago
|
// if (!blockToOutput) return; // skip this time due to failure
|
||
|
// // copy over data
|
||
|
// m_memory->getSamples(blockToOutput, m_delaySamples);
|
||
|
|
||
7 years ago
|
// Check if external DMA, if so, we need to be sure the read is completed
|
||
7 years ago
|
if (m_externalMemory && m_memory->getSlot()->isUseDma()) {
|
||
|
// Using DMA
|
||
7 years ago
|
unsigned loopCount = 0;
|
||
7 years ago
|
while (m_memory->getSlot()->isReadBusy()) {}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// perform the wet/dry mix mix
|
||
7 years ago
|
m_postProcessing(blockToOutput, inputAudioBlock, blockToOutput);
|
||
|
transmit(blockToOutput);
|
||
|
|
||
|
release(inputAudioBlock);
|
||
|
release(m_previousBlock);
|
||
|
m_previousBlock = blockToOutput;
|
||
7 years ago
|
|
||
7 years ago
|
if (m_externalMemory && m_memory->getSlot()->isUseDma()) {
|
||
|
// Using DMA
|
||
7 years ago
|
if (m_blockToRelease) release(m_blockToRelease);
|
||
|
m_blockToRelease = blockToRelease;
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
void AudioEffectAnalogDelay::delay(float milliseconds)
|
||
7 years ago
|
{
|
||
|
size_t delaySamples = calcAudioSamples(milliseconds);
|
||
|
|
||
7 years ago
|
if (!m_memory) { Serial.println("delay(): m_memory is not valid"); }
|
||
|
|
||
7 years ago
|
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();
|
||
7 years ago
|
|
||
|
if (!slot) { Serial.println("ERROR: slot ptr is not valid"); }
|
||
7 years ago
|
if (!slot->isEnabled()) {
|
||
|
slot->enable();
|
||
7 years ago
|
Serial.println("WEIRD: slot was not enabled");
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
m_delaySamples = delaySamples;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
void AudioEffectAnalogDelay::delay(size_t delaySamples)
|
||
7 years ago
|
{
|
||
7 years ago
|
if (!m_memory) { Serial.println("delay(): m_memory is not valid"); }
|
||
|
|
||
7 years ago
|
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
|
||
7 years ago
|
Serial.println(String("CONFIG: delay:") + delaySamples);
|
||
7 years ago
|
ExtMemSlot *slot = m_memory->getSlot();
|
||
|
if (!slot->isEnabled()) {
|
||
|
slot->enable();
|
||
|
}
|
||
|
}
|
||
7 years ago
|
m_delaySamples= delaySamples;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
|
||
7 years ago
|
void AudioEffectAnalogDelay::processMidi(int channel, int control, int value)
|
||
|
{
|
||
|
float val = (float)value / 127.0f;
|
||
|
|
||
|
if ((m_midiConfig[MIDI_DELAY][MIDI_CHANNEL] == channel) &&
|
||
|
(m_midiConfig[MIDI_DELAY][MIDI_CONTROL] == control)) {
|
||
|
// Delay
|
||
7 years ago
|
m_maxDelaySamples = m_memory->getSlot()->size();
|
||
|
Serial.println(String("AudioEffectAnalogDelay::delay: ") + val + String(" out of ") + m_maxDelaySamples);
|
||
|
delay((size_t)(val * (float)m_maxDelaySamples));
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
if ((m_midiConfig[MIDI_BYPASS][MIDI_CHANNEL] == channel) &&
|
||
|
(m_midiConfig[MIDI_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); }
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if ((m_midiConfig[MIDI_FEEDBACK][MIDI_CHANNEL] == channel) &&
|
||
|
(m_midiConfig[MIDI_FEEDBACK][MIDI_CONTROL] == control)) {
|
||
|
// Feedback
|
||
|
Serial.println(String("AudioEffectAnalogDelay::feedback: ") + val);
|
||
|
feedback(val);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ((m_midiConfig[MIDI_MIX][MIDI_CHANNEL] == channel) &&
|
||
|
(m_midiConfig[MIDI_MIX][MIDI_CONTROL] == control)) {
|
||
|
// Mix
|
||
|
Serial.println(String("AudioEffectAnalogDelay::mix: ") + val);
|
||
|
mix(val);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
void AudioEffectAnalogDelay::mapMidiDelay(int control, int channel)
|
||
|
{
|
||
|
m_midiConfig[MIDI_DELAY][MIDI_CHANNEL] = channel;
|
||
|
m_midiConfig[MIDI_DELAY][MIDI_CONTROL] = control;
|
||
|
}
|
||
|
|
||
7 years ago
|
void AudioEffectAnalogDelay::mapMidiBypass(int control, int channel)
|
||
7 years ago
|
{
|
||
7 years ago
|
m_midiConfig[MIDI_BYPASS][MIDI_CHANNEL] = channel;
|
||
|
m_midiConfig[MIDI_BYPASS][MIDI_CONTROL] = control;
|
||
7 years ago
|
}
|
||
|
|
||
|
void AudioEffectAnalogDelay::mapMidiFeedback(int control, int channel)
|
||
|
{
|
||
|
m_midiConfig[MIDI_FEEDBACK][MIDI_CHANNEL] = channel;
|
||
|
m_midiConfig[MIDI_FEEDBACK][MIDI_CONTROL] = control;
|
||
|
}
|
||
|
|
||
|
void AudioEffectAnalogDelay::mapMidiMix(int control, int channel)
|
||
|
{
|
||
|
m_midiConfig[MIDI_MIX][MIDI_CHANNEL] = channel;
|
||
|
m_midiConfig[MIDI_MIX][MIDI_CONTROL] = control;
|
||
|
}
|
||
|
|
||
|
|
||
7 years ago
|
void AudioEffectAnalogDelay::m_preProcessing(audio_block_t *out, audio_block_t *dry, audio_block_t *wet)
|
||
7 years ago
|
{
|
||
7 years ago
|
if ( out && dry && wet) {
|
||
|
alphaBlend(out, dry, wet, m_feedback);
|
||
7 years ago
|
m_iir->process(out->data, out->data, AUDIO_BLOCK_SAMPLES);
|
||
7 years ago
|
} 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)
|
||
|
{
|
||
|
if ( out && dry && wet) {
|
||
7 years ago
|
// Simulate the LPF IIR nature of the analog systems
|
||
7 years ago
|
//m_iir->process(wet->data, wet->data, AUDIO_BLOCK_SAMPLES);
|
||
7 years ago
|
alphaBlend(out, dry, wet, m_mix);
|
||
|
} else if (dry) {
|
||
|
memcpy(out->data, dry->data, sizeof(int16_t) * AUDIO_BLOCK_SAMPLES);
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|