Added spline interpolation again.

Using Teensy biquad lowpass for filtering the modulator signal.
master
Holger Wirtz 5 years ago
parent e85dbd43b8
commit 6bd662b602
  1. 33
      MicroMDAEPiano.ino
  2. 9
      config.h
  3. 84
      effect_modulated_delay.cpp
  4. 9
      effect_modulated_delay.h
  5. 111
      spline.cpp
  6. 43
      spline.h

@ -57,29 +57,31 @@ AudioAmplifier inverter;
AudioEffectModulatedDelay modchorus_r;
AudioEffectModulatedDelay modchorus_l;
AudioSynthWaveform modulator;
AudioFilterBiquad modulator_filter;
AudioConnection patchCord0(queue_r, peak_r);
AudioConnection patchCord1(queue_l, peak_l);
AudioConnection patchCord2(queue_r, freeverb_r);
AudioConnection patchCord3(queue_l, freeverb_l);
AudioConnection patchCord4(queue_r, 0, modchorus_r, 0);
AudioConnection patchCord5(queue_l, 0, modchorus_l, 0);
AudioConnection patchCord6(modulator, 0, modchorus_r, 1);
AudioConnection patchCord7(modulator, inverter);
AudioConnection patchCord8(inverter, 0, modchorus_l, 1);
AudioConnection patchCord9(queue_r, 0, mixer_r, 0);
AudioConnection patchCord10(queue_l, 0, mixer_l, 0);
AudioConnection patchCord11(modchorus_r, 0, mixer_r, 2);
AudioConnection patchCord12(modchorus_l, 0, mixer_l, 2);
AudioConnection patchCord13(freeverb_r, 0, mixer_r, 1);
AudioConnection patchCord14(freeverb_l, 0, mixer_l, 1);
AudioConnection patchCord15(mixer_r, volume_r);
AudioConnection patchCord16(mixer_l, volume_l);
AudioConnection patchCord6(modulator, modulator_filter);
AudioConnection patchCord7(modulator_filter, 0, modchorus_r, 1);
AudioConnection patchCord8(modulator_filter, inverter);
AudioConnection patchCord9(inverter, 0, modchorus_l, 1);
AudioConnection patchCord10(queue_r, 0, mixer_r, 0);
AudioConnection patchCord11(queue_l, 0, mixer_l, 0);
AudioConnection patchCord12(modchorus_r, 0, mixer_r, 2);
AudioConnection patchCord13(modchorus_l, 0, mixer_l, 2);
AudioConnection patchCord14(freeverb_r, 0, mixer_r, 1);
AudioConnection patchCord15(freeverb_l, 0, mixer_l, 1);
AudioConnection patchCord16(mixer_r, volume_r);
AudioConnection patchCord17(mixer_l, volume_l);
AudioOutputUSB usb1;
AudioConnection patchCord17(volume_r, 0, usb1, 0);
AudioConnection patchCord18(volume_l, 0, usb1, 1);
AudioConnection patchCord18(volume_r, 0, usb1, 0);
AudioConnection patchCord19(volume_l, 0, usb1, 1);
AudioOutputI2S i2s1;
AudioConnection patchCord19(volume_r, 0, i2s1, 0);
AudioConnection patchCord20(volume_l, 0, i2s1, 1);
AudioConnection patchCord20(volume_r, 0, i2s1, 0);
AudioConnection patchCord21(volume_l, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
// Objects
@ -226,6 +228,7 @@ void setup()
modulator.begin(CHORUS_WAVEFORM);
modulator.phase(0);
modulator.offset(0.0);
modulator_filter.setLowpass(0, CHORUS_MODULATOR_FILTER_FRQ, CHORUS_MODULATOR_FILTER_Q);
inverter.gain(-1.0); // change phase for second moduleated delay
// internal mixing of original signal(0), reverb(1) and chorus(2)

@ -26,6 +26,7 @@
#include "midinotes.h"
#include <Arduino.h>
#include "spline.h"
// ATTENTION! For better latency you have to redefine AUDIO_BLOCK_SAMPLES from
// 128 to 64 in <ARDUINO-IDE-DIR>/cores/teensy3/AudioStream.h
@ -56,10 +57,12 @@
#define REDUCE_LOUDNESS 0
#define USE_XFADE_DATA 1
// CHORUS parameters
#define INTERPOLATION_WINDOW_SIZE 5 // use only odd numbers!!!
#define INTERPOLATE_MODE 11
#define CHORUS_WAVEFORM WAVEFORM_TRIANGLE // WAVEFORM_SINE WAVEFORM_TRIANGLE WAVEFORM_SAWTOOTH WAVEFORM_SAWTOOTH_REVERSE
#define CHORUS_DELAY_LENGTH_SAMPLES (15*AUDIO_BLOCK_SAMPLES) // one AUDIO_BLOCK_SAMPLES = 2.902ms; you need doubled length, e.g. delay point is 20ms, so you need up to 40ms delay!
#define CHORUS_WAVEFORM WAVEFORM_TRIANGLE // WAVEFORM_SINE WAVEFORM_TRIANGLE WAVEFORM_SAWTOOTH WAVEFORM_SAWTOOTH_REVERSE
#define CHORUS_MODULATOR_FILTER_FRQ 1000
#define CHORUS_MODULATOR_FILTER_Q 0.7
#define CHORUS_INTERPOLATION_MODE Catmull
#define CHORUS_INTERPOLATION_WINDOW_SIZE 11
//*************************************************************************************************
//* DEBUG OUTPUT SETTINGS

@ -25,6 +25,7 @@
#include <Audio.h>
#include "limits.h"
#include "effect_modulated_delay.h"
#include "spline.h"
#include "config.h"
/******************************************************************/
@ -57,12 +58,6 @@ boolean AudioEffectModulatedDelay::begin(short *delayline, int d_length)
_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);
}
@ -80,16 +75,19 @@ void AudioEffectModulatedDelay::update(void)
if (block && modulation)
{
int16_t *bp;
float *mp;
int16_t *mp;
float mod_idx;
float mod_number;
float mod_fraction;
#ifdef CHORUS_INTERPOLATION_MODE
int8_t j;
float x[CHORUS_INTERPOLATION_WINDOW_SIZE];
float y[CHORUS_INTERPOLATION_WINDOW_SIZE];
Spline spline(x, y, CHORUS_INTERPOLATION_WINDOW_SIZE, CHORUS_INTERPOLATION_MODE);
#endif
// (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;
mp = modulation->data;
for (uint16_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
{
@ -102,7 +100,23 @@ void AudioEffectModulatedDelay::update(void)
// 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);
#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
uint8_t c = 0;
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); j <= (CHORUS_INTERPOLATION_WINDOW_SIZE / 2); j++)
{
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);
c++;
}
*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;
@ -118,16 +132,7 @@ void AudioEffectModulatedDelay::update(void)
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
@ -148,40 +153,3 @@ 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);
}

@ -45,22 +45,13 @@ class AudioEffectModulatedDelay :
boolean begin(short *delayline, int delay_length);
virtual void update(void);
virtual void setDelay(float milliseconds);
virtual void setModFilter(float cFrq);
private:
virtual void calcModFilterCoeff(float32_t cFrq);
audio_block_t *inputQueueArray[2];
int16_t *_delayline;
uint16_t _circ_idx;
uint16_t _max_delay_length;
uint16_t _delay_length;
// filter data
arm_biquad_casd_df1_inst_f32 filter;
float32_t modulation_f32[AUDIO_BLOCK_SAMPLES];
float32_t filter_coeffs[5];
float32_t filter_state[4];
};
#endif

@ -0,0 +1,111 @@
// From: https://raw.githubusercontent.com/kerinin/arduino-splines/master/spline.cpp
#include "Arduino.h"
#include "spline.h"
#include <math.h>
Spline::Spline(void) {
_prev_point = 0;
}
Spline::Spline( float x[], float y[], int numPoints, int degree )
{
setPoints(x,y,numPoints);
setDegree(degree);
_prev_point = 0;
}
Spline::Spline( float x[], float y[], float m[], int numPoints )
{
setPoints(x,y,m,numPoints);
setDegree(Hermite);
_prev_point = 0;
}
void Spline::setPoints( float x[], float y[], int numPoints ) {
_x = x;
_y = y;
_length = numPoints;
}
void Spline::setPoints( float x[], float y[], float m[], int numPoints ) {
_x = x;
_y = y;
_m = m;
_length = numPoints;
}
void Spline::setDegree( int degree ){
_degree = degree;
}
float Spline::value( float x )
{
if( _x[0] > x ) {
return _y[0];
}
else if ( _x[_length-1] < x ) {
return _y[_length-1];
}
else {
for(int i = 0; i < _length; i++ )
{
int index = ( i + _prev_point ) % _length;
if( _x[index] == x ) {
_prev_point = index;
return _y[index];
} else if( (_x[index] < x) && (x < _x[index+1]) ) {
_prev_point = index;
return calc( x, index );
}
}
}
}
float Spline::calc( float x, int i )
{
switch( _degree ) {
case 0:
return _y[i];
case 1:
if( _x[i] == _x[i+1] ) {
// Avoids division by 0
return _y[i];
} else {
return _y[i] + (_y[i+1] - _y[i]) * ( x - _x[i]) / ( _x[i+1] - _x[i] );
}
case Hermite:
return hermite( ((x-_x[i]) / (_x[i+1]-_x[i])), _y[i], _y[i+1], _m[i], _m[i+1], _x[i], _x[i+1] );
case Catmull:
if( i == 0 ) {
// x prior to spline start - first point used to determine tangent
return _y[1];
} else if( i == _length-2 ) {
// x after spline end - last point used to determine tangent
return _y[_length-2];
} else {
float t = (x-_x[i]) / (_x[i+1]-_x[i]);
float m0 = (i==0 ? 0 : catmull_tangent(i) );
float m1 = (i==_length-1 ? 0 : catmull_tangent(i+1) );
return hermite( t, _y[i], _y[i+1], m0, m1, _x[i], _x[i+1]);
}
}
}
float Spline::hermite( float t, float p0, float p1, float m0, float m1, float x0, float x1 ) {
return (hermite_00(t)*p0) + (hermite_10(t)*(x1-x0)*m0) + (hermite_01(t)*p1) + (hermite_11(t)*(x1-x0)*m1);
}
float Spline::hermite_00( float t ) { return (2*pow(t,3)) - (3*pow(t,2)) + 1;}
float Spline::hermite_10( float t ) { return pow(t,3) - (2*pow(t,2)) + t; }
float Spline::hermite_01( float t ) { return (3*pow(t,2)) - (2*pow(t,3)); }
float Spline::hermite_11( float t ) { return pow(t,3) - pow(t,2); }
float Spline::catmull_tangent( int i )
{
if( _x[i+1] == _x[i-1] ) {
// Avoids division by 0
return 0;
} else {
return (_y[i+1] - _y[i-1]) / (_x[i+1] - _x[i-1]);
}
}

@ -0,0 +1,43 @@
// From https://raw.githubusercontent.com/kerinin/arduino-splines/master/spline.h
/*
Library for 1-d splines
Copyright Ryan Michael
Licensed under the LGPLv3
*/
#ifndef spline_h
#define spline_h
#include "Arduino.h"
#define Hermite 10
#define Catmull 11
class Spline
{
public:
Spline( void );
Spline( float x[], float y[], int numPoints, int degree = 1 );
Spline( float x[], float y[], float m[], int numPoints );
float value( float x );
void setPoints( float x[], float y[], int numPoints );
void setPoints( float x[], float y[], float m[], int numPoints );
void setDegree( int degree );
private:
float calc( float, int);
float* _x;
float* _y;
float* _m;
int _degree;
int _length;
int _prev_point;
float hermite( float t, float p0, float p1, float m0, float m1, float x0, float x1 );
float hermite_00( float t );
float hermite_10( float t );
float hermite_01( float t );
float hermite_11( float t );
float catmull_tangent( int i );
};
#endif
Loading…
Cancel
Save