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.
176 lines
5.5 KiB
176 lines
5.5 KiB
/* 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 <Audio.h>
|
|
#include "arm_math.h"
|
|
#include "effect_modulated_delay.h"
|
|
#include "config.h"
|
|
|
|
/******************************************************************/
|
|
|
|
// Based on; A u d i o E f f e c t D e l a y
|
|
// Written by Pete (El Supremo) Jan 2014
|
|
// 140529 - change to handle mono stream - change modify() to voices()
|
|
// 140219 - correct storage class (not static)
|
|
// 190527 - added modulation input (by Holger Wirtz)
|
|
|
|
boolean AudioEffectModulatedDelay::begin(short *delayline, int d_length)
|
|
{
|
|
#if 0
|
|
Serial.print(F("AudioEffectModulatedDelay.begin(Chorus delay line length = "));
|
|
Serial.print(d_length);
|
|
Serial.println(F(")"));
|
|
#endif
|
|
|
|
_delayline = NULL;
|
|
_delay_length = 0;
|
|
_delay_offset = 0.0;
|
|
_circ_idx = 0;
|
|
|
|
if (delayline == NULL) {
|
|
return (false);
|
|
}
|
|
if (d_length < 10) {
|
|
return (false);
|
|
}
|
|
|
|
_delayline = delayline;
|
|
_delay_length = d_length;
|
|
|
|
#ifdef CHORUS_MODULATOR_BIQUAD
|
|
mod_lp_coeffs[0] = -1.98982427; // https://arachnoid.com/BiQuadDesigner/ Lopass Fc=50Hz, Q=0.7
|
|
mod_lp_coeffs[1] = 0.98987476;
|
|
mod_lp_coeffs[2] = 1.26228228e-5;
|
|
mod_lp_coeffs[3] = 2.52456456e-5;
|
|
mod_lp_coeffs[4] = 1.26228228e-5;
|
|
biquad_mod = {1, mod_lp_state, mod_lp_coeffs};
|
|
#endif
|
|
#ifdef CHORUS_OUTUT_BIQUAD // https://web.fhnw.ch/technik/projekte/eit/Fruehling2016/MuelZum/html/parametric__equalizer__example_8c_source.html
|
|
out_lp_coeffs[0] = -0.10987036; // https://arachnoid.com/BiQuadDesigner/ Lopass Fc=10000Hz, Q=0.3
|
|
out_lp_coeffs[1] = -0.24497694;
|
|
out_lp_coeffs[2] = 0.16128817;
|
|
out_lp_coeffs[3] = 0.32257635;
|
|
out_lp_coeffs[4] = 0.16128817;
|
|
biquad_out = {1, out_lp_state, out_lp_coeffs, 0};
|
|
#endif
|
|
return (true);
|
|
}
|
|
|
|
void AudioEffectModulatedDelay::update(void)
|
|
{
|
|
audio_block_t *block;
|
|
audio_block_t *modulation;
|
|
|
|
if (_delayline == NULL)
|
|
return;
|
|
|
|
block = receiveWritable(0);
|
|
modulation = receiveReadOnly(1);
|
|
|
|
if (block && modulation)
|
|
{
|
|
int16_t *bp;
|
|
float *mp;
|
|
float mod_idx;
|
|
float mod_number;
|
|
float mod_fraction;
|
|
float mod_float[AUDIO_BLOCK_SAMPLES];
|
|
|
|
bp = block->data;
|
|
arm_q15_to_float(modulation->data, mod_float, AUDIO_BLOCK_SAMPLES);
|
|
mp = mod_float;
|
|
#ifdef CHORUS_MODULATOR_BIQUAD
|
|
arm_biquad_cascade_df1_f32(&biquad_mod, mod_float, mod_float, AUDIO_BLOCK_SAMPLES);
|
|
#endif
|
|
|
|
for (uint16_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
|
|
{
|
|
// write data into circular buffer (delayline)
|
|
if (_circ_idx >= _delay_length)
|
|
_circ_idx = 0;
|
|
_delayline[_circ_idx] = *bp;
|
|
|
|
// Calculate the modulation-index as a floating point number for interpolation
|
|
mod_idx = *mp * (1 - MODULATION_MAX_FACTOR) * _delay_length;
|
|
mod_fraction = modff(mod_idx, &mod_number);
|
|
|
|
// calculate modulation index into circular buffer
|
|
c_mod_idx = (_circ_idx - _delay_offset - int(mod_number)) % _delay_length;
|
|
if (c_mod_idx < 0) // check for negative offsets and correct them
|
|
c_mod_idx += _delay_length;
|
|
|
|
// linear interpolation
|
|
if (c_mod_idx < 1)
|
|
{
|
|
idx[0] = _delay_length - 1;
|
|
idx[1] = 0;
|
|
}
|
|
else if (c_mod_idx + 1 >= _delay_length)
|
|
{
|
|
idx[0] = 0;
|
|
idx[1] = c_mod_idx;
|
|
}
|
|
else
|
|
{
|
|
idx[0] = c_mod_idx + 1;
|
|
idx[1] = c_mod_idx;
|
|
}
|
|
if (mod_idx < 0.0)
|
|
*bp = round(float(_delayline[idx[0]]) * (mod_fraction) + float(_delayline[idx[1]]) * (1.0 - mod_fraction));
|
|
else
|
|
*bp = round(float(_delayline[idx[0]]) * (1.0 - mod_fraction) + float(_delayline[idx[1]]) * mod_fraction);
|
|
|
|
|
|
// push the pointers forward
|
|
bp++; // next audio data
|
|
mp++; // next modulation data
|
|
_circ_idx++; // next circular buffer index
|
|
}
|
|
}
|
|
|
|
if (modulation)
|
|
release(modulation);
|
|
|
|
if (block)
|
|
{
|
|
#ifdef CHORUS_OUTPUT_BIQUAD
|
|
arm_biquad_cascade_df1_fast_q15(&biquad_out, (q15_t*)block, (q15_t*)block, AUDIO_BLOCK_SAMPLES);
|
|
#endif
|
|
transmit(block, 0);
|
|
release(block);
|
|
}
|
|
}
|
|
|
|
float AudioEffectModulatedDelay::offset(float offset_value) // in ms
|
|
{
|
|
uint16_t offset_frames = (offset_value / 1000) * AUDIO_SAMPLE_RATE;
|
|
if (offset_frames > _delay_length * MODULATION_MAX_FACTOR)
|
|
_delay_offset = _delay_length * MODULATION_MAX_FACTOR;
|
|
else if (offset_frames <= _delay_length * (1 - MODULATION_MAX_FACTOR))
|
|
_delay_offset = _delay_length * (1 - MODULATION_MAX_FACTOR);
|
|
else
|
|
_delay_offset = offset_frames;
|
|
|
|
return (offset_frames / AUDIO_SAMPLE_RATE * 1000);
|
|
}
|
|
|