From 86f082b9cc85070e04ac7c9b720eb31a8d4fcc55 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Thu, 1 Nov 2018 09:35:05 +0100 Subject: [PATCH] FIxed the generator function. You have to use all available optimizations (especially LTO) at compilation for getting high performance. --- MicroMDAPiano.ino | 3 +- mdaEPiano.cpp | 73 ++++++++++++++++------------------------------- mdaEPiano.h | 4 ++- 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/MicroMDAPiano.ino b/MicroMDAPiano.ino index 150899f..1f756c2 100644 --- a/MicroMDAPiano.ino +++ b/MicroMDAPiano.ino @@ -215,7 +215,7 @@ void loop() } elapsedMicros t1; - ep->process(audio_buffer_l, audio_buffer_r, AUDIO_BLOCK_SAMPLES); + ep->process(audio_buffer_l, audio_buffer_r); uint32_t t2 = t1; if (t2 > audio_block_time_ms) // everything greater 2.9ms is a buffer underrun! xrun++; @@ -231,6 +231,7 @@ void loop() if (peak_l.read() > 0.99) peak++; } + #ifndef TEENSY_AUDIO_BOARD for (uint8_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++) { diff --git a/mdaEPiano.cpp b/mdaEPiano.cpp index 843db9a..69f1312 100644 --- a/mdaEPiano.cpp +++ b/mdaEPiano.cpp @@ -332,14 +332,14 @@ float mdaEPiano::getParameter(int32_t index) { } */ -void mdaEPiano::process(int16_t *outputs_r, int16_t *outputs_l, int32_t sampleFrames) +void mdaEPiano::process(int16_t *outputs_r, int16_t *outputs_l) { int16_t v; float x, l, r, od = overdrive; int32_t i; int16_t frame; - for (frame = 0; frame < sampleFrames; frame++) + for (frame = 0; frame < AUDIO_BLOCK_SAMPLES; frame++) { VOICE *V = voice; l = r = 0.0f; @@ -348,64 +348,41 @@ void mdaEPiano::process(int16_t *outputs_r, int16_t *outputs_l, int32_t sampleFr V->frac += V->delta; //integer-based linear interpolation V->pos += V->frac >> 16; V->frac &= 0xFFFF; - if (V->pos > V->end) V->pos -= V->loop; - //i = waves[V->pos]; - //i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000; //not working on intel mac !?! - //x = V->env * (*(float *)&i - 3.0f); //fast int->float - //x = V->env * (float)i / 32768.0f; + if (V->pos > V->end) + V->pos -= V->loop; i = waves[V->pos] + ((V->frac * (waves[V->pos + 1] - waves[V->pos])) >> 16); - //x = V->env * (float)i / 32768.0f; - + x = V->env * static_cast(i) / 32768.0f; V->env = V->env * V->dec; //envelope - - //if(x>0.0f) { x -= od * x * x; if(x < -V->env) x = -V->env; } //+= 0.5f * x * x; } //overdrive - - //l += V->outl * x; - //r += V->outr * x; + if (x > 0.0f) { + x -= od * x * x; //overdrive + if (x < -V->env) x = -V->env; + } + l += V->outl * x; + r += V->outr * x; V++; } - /* - tl += tfrq * (l - tl); //treble boost - tr += tfrq * (r - tr); - r += treb * (r - tr); - l += treb * (l - tl); - - lfo0 += dlfo * lfo1; //LFO for tremolo and autopan - lfo1 -= dlfo * lfo0; - l += l * lmod * lfo1; - r += r * rmod * lfo1; //worth making all these local variables? + tl += tfrq * (l - tl); //treble boost + tr += tfrq * (r - tr); + r += treb * (r - tr); + l += treb * (l - tl); - outputs_l[v] = int16_t(l * 0xFFFF) >> REDUCE_LOUDNESS; - outputs_r[v] = int16_t(r * 0xFFFF) >> REDUCE_LOUDNESS; - */ + lfo0 += dlfo * lfo1; //LFO for tremolo and autopan + lfo1 -= dlfo * lfo0; + l += l * lmod * lfo1; + r += r * rmod * lfo1; //worth making all these local variables? - outputs_l[v] = i >> REDUCE_LOUDNESS; - outputs_r[v] = i >> REDUCE_LOUDNESS; + outputs_l[frame] = static_cast(l) >> REDUCE_LOUDNESS; + outputs_r[frame] = static_cast(r) >> REDUCE_LOUDNESS; } - /* - if (frame < sampleFrames) - { - if (activevoices == 0 && programs[curProgram].param[4] > 0.5f) - { - lfo0 = -0.7071f; //reset LFO phase - good idea? - lfo1 = 0.7071f; - } - } - */ + if (fabs(tl) < 1.0e-10) tl = 0.0f; //anti-denormal if (fabs(tr) < 1.0e-10) tr = 0.0f; - for (v = 0; v < activevoices; v++) if (voice[v].env < SILENCE) voice[v] = voice[--activevoices]; - - /* - for (v = 0; v < sampleFrames; v++) - { - outputs_r[v] = int16_t(o0[v] * 0xFFFF) >> REDUCE_LOUDNESS; - outputs_l[v] = int16_t(o1[v] * 0xFFFF) >> REDUCE_LOUDNESS; - } - */ + for (v = 0; v < activevoices; v++) + if (voice[v].env < SILENCE) + voice[v] = voice[--activevoices]; } void mdaEPiano::noteOn(int32_t note, int32_t velocity) diff --git a/mdaEPiano.h b/mdaEPiano.h index 2a6f5d5..da47d88 100644 --- a/mdaEPiano.h +++ b/mdaEPiano.h @@ -61,7 +61,7 @@ public: mdaEPiano(); // mdaEPiano(audioMasterCallback audioMaster); ~mdaEPiano(); - virtual void process(int16_t *outputs_r, int16_t *outputs_l, int32_t sampleFrames); + virtual void process(int16_t *outputs_r, int16_t *outputs_l); virtual bool processMidiMessage(uint8_t type, uint8_t data1, uint8_t data2); virtual void setProgram(int32_t program); //virtual void setProgramName(char *name); @@ -98,6 +98,8 @@ private: float Fs, iFs; #define EVENTBUFFER 120 + #define EVENTS_DONE 99999999 + int32_t notes[EVENTBUFFER + 8]; //list of delta|note|velocity for current block ///global internal variables KGRP kgrp[34];