/* 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 #include #include "limits.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; _circ_idx = 0; if (delayline == NULL) { return (false); } if (d_length < 10) { return (false); } _delayline = delayline; _delay_length = _max_delay_length = d_length; // init filter filter.numStages = 1; filter.pState = filter_state; filter.pCoeffs = filter_coeffs; calcModFilterCoeff(500.0); 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; // (Filter implementation: https://web.fhnw.ch/technik/projekte/eit/Fruehling2016/MuelZum/html/parametric__equalizer__example_8c_source.html) arm_q15_to_float(modulation->data, modulation_f32, AUDIO_BLOCK_SAMPLES); arm_biquad_cascade_df1_f32(&filter, modulation_f32, modulation_f32, AUDIO_BLOCK_SAMPLES); bp = block->data; mp = modulation_f32; for (uint16_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++) { // write data into circular buffer if (_circ_idx >= _delay_length) _circ_idx = 0; _delayline[_circ_idx] = *bp; // Calculate modulation index as a float, for interpolation later. // The index is located around the half of the delay length multiplied by the current amount of the modulator mod_idx = *mp * float(_delay_length >> 1); mod_fraction = modff(mod_idx, &mod_number); // Simple interpolation int16_t c_mod_idx = (_circ_idx + int(round(mod_idx))) % _delay_length; float value1, value2; if (c_mod_idx < 0) { value1 = _delayline[_delay_length + c_mod_idx - 1]; value2 = _delayline[_delay_length + c_mod_idx]; } else { value1 = _delayline[c_mod_idx - 1]; value2 = _delayline[c_mod_idx]; } *bp = int(round(mod_fraction * value1 + (1.0 - mod_fraction) * value2)); #ifdef DEBUG float m = (value2 - value1) / (SHRT_MAX >> 1); if (m > 1.0 || m < -1.0) { Serial.print(F("WARNING m=")); Serial.println(m, 4); } #endif bp++; // next audio data mp++; // next modulation data _circ_idx++; // next circular buffer index } } if (modulation) release(modulation); if (block) { transmit(block, 0); release(block); } } void AudioEffectModulatedDelay::setDelay(float milliseconds) { _delay_length = min(AUDIO_SAMPLE_RATE * milliseconds / 500, _max_delay_length); } void AudioEffectModulatedDelay::calcModFilterCoeff(float32_t cFrq) { const float sqrt2 = 1.4142135623730950488; float QcRaw = (2 * PI * cFrq) / AUDIO_SAMPLE_RATE_EXACT; // Find cutoff frequency in [0..PI] float QcWarp = tan(QcRaw); // Warp cutoff frequency float gain = 1 / (1 + sqrt2 / QcWarp + 2 / (QcWarp * QcWarp)); filter_coeffs[2] = (1 - sqrt2 / QcWarp + 2 / (QcWarp * QcWarp)) * gain; filter_coeffs[1] = (2 - 2 * 2 / (QcWarp * QcWarp)) * gain; filter_coeffs[0] = 1; filter_coeffs[3] = 1 * gain; filter_coeffs[4] = 2 * gain; /* // 1.1kHz 2nd order Butterworth lowpass filter coefficients // calculated with Iowa IIR FIlter Designer 6.5 float32_t b0 = 0.072959657268266670; float32_t b1 = 0.072959657268266670; float32_t b2 = 0.0; float32_t a0 = 1.000000000000000000; float32_t a1 = -0.854080685463466605; float32_t a2 = 0.0; // Normalize so a0 = 1 filter_coeffs[0] = b0 / a0; filter_coeffs[1] = b1 / a0; filter_coeffs[2] = b2 / a0; filter_coeffs[3] = -a1 / a0; filter_coeffs[4] = -a2 / a0; */ } void AudioEffectModulatedDelay::setModFilter(float cFrq) { calcModFilterCoeff(cFrq); }