|
|
|
/* Audio Library for Teensy 3.X
|
|
|
|
Copyright (c) 2014, Pete (El Supremo)
|
|
|
|
Copyright (c) 2019, Holger Wirtz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include "limits.h"
|
|
|
|
#include "effect_modulated_chorus.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
|
|
|
|
// Based on; A u d i o E f f e c t C h o r u s
|
|
|
|
// Written by Pete (El Supremo) Jan 2014
|
|
|
|
// 140529 - change to handle mono stream - change modify() to voices()
|
|
|
|
// 140219 - correct storage class (not static)
|
|
|
|
// 190527 - adding modulation input (by Holger Wirtz)
|
|
|
|
|
|
|
|
boolean AudioModulatedEffectChorus::begin(short *delayline, int d_length)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
Serial.print("AudioModulatedEffectChorus.begin(Chorus delay line length = ");
|
|
|
|
Serial.print(d_length);
|
|
|
|
Serial.println(")");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
l_delayline = NULL;
|
|
|
|
delay_length = 0;
|
|
|
|
l_circ_idx = 0;
|
|
|
|
|
|
|
|
if (delayline == NULL) {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
if (d_length < 10) {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_delayline = delayline;
|
|
|
|
delay_length = d_length;
|
|
|
|
delay_length_half = d_length / 2;
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//int last_idx = 0;
|
|
|
|
void AudioModulatedEffectChorus::update(void)
|
|
|
|
{
|
|
|
|
audio_block_t *block;
|
|
|
|
audio_block_t *modulation;
|
|
|
|
short *bp;
|
|
|
|
short *mp;
|
|
|
|
float mod_idx;
|
|
|
|
|
|
|
|
if (l_delayline == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
block = receiveWritable(0);
|
|
|
|
modulation = receiveReadOnly(1);
|
|
|
|
|
|
|
|
if (block && modulation)
|
|
|
|
{
|
|
|
|
#ifdef INTERPOLATE
|
|
|
|
uint8_t j;
|
|
|
|
int16_t interpolation_idx;
|
|
|
|
interpolation modulation_interpolate;
|
|
|
|
modulation_interpolate.valuelenXY(INTERPOLATION_WINDOW_SIZE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bp = block->data;
|
|
|
|
mp = modulation->data;
|
|
|
|
|
|
|
|
for (int i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
|
|
|
|
{
|
|
|
|
#ifdef INTERPOLATE
|
|
|
|
float x[INTERPOLATION_WINDOW_SIZE];
|
|
|
|
float y[INTERPOLATION_WINDOW_SIZE];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// write data into circular buffer
|
|
|
|
if (l_circ_idx >= delay_length)
|
|
|
|
l_circ_idx = 0;
|
|
|
|
l_delayline[l_circ_idx] = *bp; // write signal into circular buffer
|
|
|
|
|
|
|
|
// calculate modulation index
|
|
|
|
mod_idx = float(*mp) / SHRT_MAX * delay_length_half + l_circ_idx; // calculate index with modulation as a float
|
|
|
|
if (mod_idx > delay_length)
|
|
|
|
mod_idx = mod_idx - delay_length;
|
|
|
|
else if (mod_idx < 0)
|
|
|
|
mod_idx = delay_length + mod_idx;
|
|
|
|
|
|
|
|
#ifdef INTERPOLATE
|
|
|
|
// get value with interpolation
|
|
|
|
for (j = 0; j < INTERPOLATION_WINDOW_SIZE; j++)
|
|
|
|
{
|
|
|
|
interpolation_idx = mod_idx + (INTERPOLATION_WINDOW_SIZE / -2) + j;
|
|
|
|
if (interpolation_idx > delay_length)
|
|
|
|
interpolation_idx = interpolation_idx - delay_length;
|
|
|
|
else if (interpolation_idx < 0)
|
|
|
|
interpolation_idx = delay_length + interpolation_idx;
|
|
|
|
|
|
|
|
x[j] = interpolation_idx;
|
|
|
|
y[j] = l_delayline[interpolation_idx];
|
|
|
|
}
|
|
|
|
modulation_interpolate.valueX(x);
|
|
|
|
modulation_interpolate.valueY(y);
|
|
|
|
modulation_interpolate.valueI(mod_idx);
|
|
|
|
|
|
|
|
#if INTERPOLATE == CUBIC
|
|
|
|
*bp = int(modulation_interpolate.CubicInterpolate() + 0.5);
|
|
|
|
#elif INTERPOLATE == LINEAR
|
|
|
|
*bp = int(modulation_interpolate.LinearInterpolate() + 0.5);
|
|
|
|
#elif INTERPOLATE == COSINE
|
|
|
|
*bp = int(modulation_interpolate.CosineInterpolate() + 0.5);
|
|
|
|
#elif INTERPOLATE == LAGRANGE
|
|
|
|
*bp = int(modulation_interpolate.LagrangeInterpolate() + 0.5);
|
|
|
|
#elif INTERPOLATE == QUDRATIC
|
|
|
|
*bp = int(modulation_interpolate.QuadraticInterpolate() + 0.5);
|
|
|
|
#else
|
|
|
|
// No interpolation - should sound really bad...
|
|
|
|
*bp = int(mod_idx + 0.5);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// No interpolation - should sound really bad...
|
|
|
|
*bp = int(mod_idx + 0.5);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bp++;
|
|
|
|
mp++;
|
|
|
|
l_circ_idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transmit the block
|
|
|
|
transmit(block, 0);
|
|
|
|
release(block);
|
|
|
|
release(modulation);
|
|
|
|
}
|
|
|
|
}
|