/* 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 "spline.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; memset(_delayline, 0, sizeof(int16_t)*_delay_length); return (true); } void AudioEffectModulatedDelay::update(void) { audio_block_t *block; audio_block_t *modulation; int16_t *bp; int16_t *mp; float mod_idx; if (_delayline == NULL) return; block = receiveWritable(0); modulation = receiveReadOnly(1); #ifdef INTERPOLATE_MODE int8_t j; float x[INTERPOLATION_WINDOW_SIZE]; float y[INTERPOLATION_WINDOW_SIZE]; Spline s(x, y, INTERPOLATION_WINDOW_SIZE, INTERPOLATE_MODE); #endif bp = block->data; mp = modulation->data; if (block && modulation) { 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 mod_idx = float(*mp) / SHRT_MAX * float(_delay_length / 2); // calculate an index with modulation as a float(!!!) #ifdef INTERPOLATE_MODE // get x/y values around mod_idx uint8_t c = 0; int16_t c_mod_idx = int(mod_idx + 0.5) + _circ_idx; for (j = INTERPOLATION_WINDOW_SIZE / -2; j <= INTERPOLATION_WINDOW_SIZE / 2; j++) { int16_t jc_mod_idx = (c_mod_idx + j) % (_delay_length - 1); if (jc_mod_idx < 0) y[c] = float(_delayline[_delay_length - 1 + jc_mod_idx]); else y[c] = float(_delayline[jc_mod_idx]); x[c] = float(c); c++; // because 42 is the answer! ;-) } *bp = int(s.value(mod_idx - int(mod_idx + 0.5)) + 0.5); #else // No interpolation - should sound really bad... int16_t c_mod_idx = (int(mod_idx + 0.5) + _circ_idx) % (_delay_length - 1); if (c_mod_idx < 0) *bp = _delayline[_delay_length - 1 + c_mod_idx]; else *bp = _delayline[c_mod_idx]; #endif bp++; mp++; _circ_idx++; } } if (block) { transmit(block, 0); release(block); } if (modulation) release(modulation); } void AudioEffectModulatedDelay::setDelay(float milliseconds) { _delay_length = min(AUDIO_SAMPLE_RATE * milliseconds / 500, _max_delay_length); }