/* 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; _delay_offset = 0.0; _modulation_intensity = 0.0; _circ_idx = 0; if (delayline == NULL) { return (false); } if (d_length < 10) { return (false); } _delayline = delayline; _delay_length = d_length; #ifdef CHORUS_INTERPOLATION_MODE spline = new Spline; spline->setPoints(x, y, int(CHORUS_INTERPOLATION_WINDOW_SIZE)); spline->setDegree(CHORUS_INTERPOLATION_MODE); #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; int16_t *mp; float mod_idx; float mod_number; float mod_fraction; #ifdef CHORUS_INTERPOLATION_MODE int8_t j; uint8_t c; #endif bp = block->data; mp = modulation->data; 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 the modulation index as a float in the interval [-1.0,1.0] for interpolation later. // The index is located around the half of the delay length multiplied by the current amount of the modulator mod_idx = _delay_offset + ((float(*mp) / SHRT_MAX) * _modulation_intensity); mod_fraction = modff(mod_idx, &mod_number); #ifdef CHORUS_INTERPOLATION_MODE // Spline interpolation // Generate a an array with the size of CHORUS_INTERPOLATION_WINDOW_SIZE of x/y values around mod_idx for interpolation int16_t c_mod_idx = _circ_idx - int(round(mod_idx)); // This is the pointer to the value in the circular buffer at the current modulation index for (j = (CHORUS_INTERPOLATION_WINDOW_SIZE / -2), c = 0; j <= (CHORUS_INTERPOLATION_WINDOW_SIZE / 2); j++, c++) { int16_t jc_mod_idx = (c_mod_idx + j) % _delay_length; // The modulation index pointer plus the value of the current window pointer if (jc_mod_idx < 0) // check for negative offsets and correct them y[c] = float(_delayline[_delay_length + jc_mod_idx]); else y[c] = float(_delayline[jc_mod_idx]); x[c] = float(j); } *bp = int(round(spline->value(mod_fraction))); // use spline interpolated value #else // 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]; } if (mod_fraction < 0) *bp = int(round((1.0 + mod_fraction) * value1 + fabs(mod_fraction) * value2)); else *bp = int(round(mod_fraction * value1 + (1.0 - mod_fraction) * value2)); #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); } } float AudioEffectModulatedDelay::offset(float offset_value) { 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); } void AudioEffectModulatedDelay::intensity(float intensity_value) { if (intensity_value > 1.0) intensity_value = 1.0; else if (intensity_value < 0.0) intensity_value = 0.0; _modulation_intensity = intensity_value * (1 - MODULATION_MAX_FACTOR) * 2 * _delay_length; }