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.
169 lines
4.8 KiB
169 lines
4.8 KiB
6 years ago
|
/* 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>
|
||
6 years ago
|
#include "limits.h"
|
||
6 years ago
|
#include "effect_modulated_delay.h"
|
||
6 years ago
|
#include "interpolation.h"
|
||
6 years ago
|
#include "config.h"
|
||
6 years ago
|
|
||
|
/******************************************************************/
|
||
|
|
||
|
// 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)
|
||
6 years ago
|
// 190527 - added modulation input (by Holger Wirtz)
|
||
6 years ago
|
|
||
6 years ago
|
boolean AudioEffectModulatedDelay::begin(short *delayline, int d_length)
|
||
6 years ago
|
{
|
||
|
#if 0
|
||
6 years ago
|
Serial.print("AudioEffectModulatedDelay.begin(Chorus delay line length = ");
|
||
6 years ago
|
Serial.print(d_length);
|
||
|
Serial.println(")");
|
||
|
#endif
|
||
|
|
||
6 years ago
|
_delayline = NULL;
|
||
|
_delay_length = 0;
|
||
|
_circ_idx = 0;
|
||
6 years ago
|
|
||
|
if (delayline == NULL) {
|
||
|
return (false);
|
||
|
}
|
||
|
if (d_length < 10) {
|
||
|
return (false);
|
||
|
}
|
||
|
|
||
6 years ago
|
_delayline = delayline;
|
||
|
_delay_length = d_length;
|
||
|
_delay_length_half = d_length / 2;
|
||
|
|
||
6 years ago
|
memset(_delayline, 0, sizeof(int16_t)*_delay_length);
|
||
6 years ago
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
//int last_idx = 0;
|
||
6 years ago
|
void AudioEffectModulatedDelay::update(void)
|
||
6 years ago
|
{
|
||
|
audio_block_t *block;
|
||
|
audio_block_t *modulation;
|
||
6 years ago
|
|
||
6 years ago
|
int16_t *bp;
|
||
|
int16_t *mp;
|
||
6 years ago
|
float mod_idx;
|
||
|
|
||
6 years ago
|
if (_delayline == NULL)
|
||
|
return;
|
||
|
|
||
6 years ago
|
#ifdef INTERPOLATE
|
||
6 years ago
|
interpolation modulation_interpolate;
|
||
6 years ago
|
#endif
|
||
|
|
||
6 years ago
|
block = receiveWritable(0);
|
||
|
modulation = receiveReadOnly(1);
|
||
|
|
||
6 years ago
|
#ifdef INTERPOLATE
|
||
6 years ago
|
int8_t j;
|
||
|
float x[INTERPOLATION_WINDOW_SIZE];
|
||
|
float y[INTERPOLATION_WINDOW_SIZE];
|
||
|
modulation_interpolate.valuelenXY(INTERPOLATION_WINDOW_SIZE);
|
||
|
modulation_interpolate.valueX(x);
|
||
|
modulation_interpolate.valueY(y);
|
||
6 years ago
|
#endif
|
||
6 years ago
|
|
||
6 years ago
|
bp = block->data;
|
||
|
mp = modulation->data;
|
||
6 years ago
|
|
||
6 years ago
|
if (block && modulation)
|
||
|
{
|
||
|
for (uint16_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
|
||
6 years ago
|
{
|
||
6 years ago
|
// write data into circular buffer
|
||
6 years ago
|
if (_circ_idx >= _delay_length)
|
||
|
_circ_idx = 0;
|
||
|
_delayline[_circ_idx] = *bp;
|
||
6 years ago
|
|
||
6 years ago
|
// calculate modulation index
|
||
6 years ago
|
mod_idx = float(*mp) / SHRT_MAX * _delay_length_half; // calculate index with modulation as a float(!!!)
|
||
6 years ago
|
|
||
6 years ago
|
#ifdef INTERPOLATE
|
||
6 years ago
|
// get x/y values around mod_idx
|
||
6 years ago
|
uint8_t c = 0;
|
||
6 years ago
|
int16_t c_mod_idx = int(mod_idx + 0.5) + _circ_idx;
|
||
6 years ago
|
for (j = INTERPOLATION_WINDOW_SIZE / -2; j <= INTERPOLATION_WINDOW_SIZE / 2; j++)
|
||
6 years ago
|
{
|
||
6 years ago
|
int16_t jc_mod_idx = (c_mod_idx + j) % _delay_length;
|
||
|
if (jc_mod_idx < 0)
|
||
|
y[c] = float(_delayline[_delay_length + jc_mod_idx]);
|
||
|
else
|
||
|
y[c] = float(_delayline[jc_mod_idx]);
|
||
|
x[c] = float(c);
|
||
6 years ago
|
c++; // because 42 is the answer! ;-)
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
modulation_interpolate.valueI(mod_idx - int(mod_idx + 0.5));
|
||
6 years ago
|
|
||
|
#if INTERPOLATE == CUBIC
|
||
6 years ago
|
*bp = int(modulation_interpolate.CubicInterpolate() + 0.5);
|
||
6 years ago
|
#elif INTERPOLATE == LINEAR
|
||
6 years ago
|
*bp = int(modulation_interpolate.LinearInterpolate() + 0.5);
|
||
6 years ago
|
#elif INTERPOLATE == COSINE
|
||
6 years ago
|
*bp = int(modulation_interpolate.CosineInterpolate() + 0.5);
|
||
6 years ago
|
#elif INTERPOLATE == LAGRANGE
|
||
6 years ago
|
*bp = int(modulation_interpolate.LagrangeInterpolate() + 0.5);
|
||
6 years ago
|
#elif INTERPOLATE == QUDRATIC
|
||
6 years ago
|
*bp = int(modulation_interpolate.QuadraticInterpolate() + 0.5);
|
||
6 years ago
|
#else
|
||
|
// No interpolation - should sound really bad...
|
||
6 years ago
|
int16_t c_mod_idx = int(mod_idx + 0.5) + _circ_idx;
|
||
|
if (c_mod_idx < 0)
|
||
|
*bp = _delayline[_delay_length + c_mod_idx];
|
||
|
else
|
||
|
*bp = _delayline[c_mod_idx];
|
||
6 years ago
|
#endif
|
||
|
#else
|
||
|
// No interpolation - should sound really bad...
|
||
6 years ago
|
int16_t c_mod_idx = int(mod_idx + 0.5) + _circ_idx;
|
||
|
if (c_mod_idx < 0)
|
||
|
*bp = _delayline[_delay_length + c_mod_idx];
|
||
|
else
|
||
|
*bp = _delayline[c_mod_idx];
|
||
6 years ago
|
#endif
|
||
6 years ago
|
|
||
6 years ago
|
bp++;
|
||
|
mp++;
|
||
6 years ago
|
_circ_idx++;
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
if (block)
|
||
|
{
|
||
|
transmit(block, 0);
|
||
|
release(block);
|
||
|
}
|
||
|
|
||
|
if (modulation)
|
||
|
release(modulation);
|
||
6 years ago
|
}
|