From 7cbb2608ecc469993814984de44a12a5978a5e00 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Wed, 16 May 2018 09:48:53 +0200 Subject: [PATCH] Simplified audio calculation function. Small fixes. --- MicroDexed.ino | 4 +- dexed.cpp | 119 +++++++++++-------------------------------------- dexed.h | 2 - synth.h | 6 +-- 4 files changed, 30 insertions(+), 101 deletions(-) diff --git a/MicroDexed.ino b/MicroDexed.ino index ca22b25..487fa84 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -21,7 +21,7 @@ //#define SHOW_DEXED_TIMING 1 #define SHOW_XRUN 1 #define SHOW_CPU_LOAD_MSEC 5000 -#define MAX_NOTES 10 +#define MAX_NOTES 11 #define TEST_MIDI 1 #define TEST_NOTE 40 #define TEST_VEL 60 @@ -76,7 +76,7 @@ void setup() dexed->activate(); dexed->setMaxNotes(MAX_NOTES); - + #ifdef TEST_MIDI queue_midi_event(0x90, TEST_NOTE, TEST_VEL); // 1 diff --git a/dexed.cpp b/dexed.cpp index 866646d..bf04d92 100644 --- a/dexed.cpp +++ b/dexed.cpp @@ -71,11 +71,6 @@ Dexed::Dexed(int rate) voices[i].live = false; } - /* for(i=0;i(data[i]); - }*/ - max_notes = 16; currentNote = 0; controllers.values_[kControllerPitch] = 0x2000; @@ -95,11 +90,11 @@ Dexed::Dexed(int rate) sustain = false; - extra_buf_size_ = 0; - memset(&voiceStatus, 0, sizeof(VoiceStatus)); setEngineType(DEXED_ENGINE_MODERN); + //setEngineType(DEXED_ENGINE_MARKI); + //setEngineType(DEXED_ENGINE_OPL); } Dexed::~Dexed() @@ -140,100 +135,38 @@ void Dexed::GetSamples(uint16_t n_samples, int16_t* buffer) refreshVoice = false; } - // flush first events - for (i = 0; i < n_samples && i < extra_buf_size_; i++) { - buffer[i] = extra_buf_[i]; - } + for (i = 0; i < n_samples; i += _N_) { + AlignedBuf audiobuf; + float sumbuf[_N_]; - // remaining buffer is still to be processed - if (extra_buf_size_ > n_samples) { - for (uint16_t j = 0; j < extra_buf_size_ - n_samples; j++) { - extra_buf_[j] = extra_buf_[j + n_samples]; + for (uint8_t j = 0; j < _N_; ++j) { + audiobuf.get()[j] = 0; + sumbuf[j] = 0.0; } - extra_buf_size_ -= n_samples; - } - else - { - for (; i < n_samples; i += _N_) { - AlignedBuf audiobuf; - float sumbuf[_N_]; - for (uint8_t j = 0; j < _N_; ++j) { - audiobuf.get()[j] = 0; - sumbuf[j] = 0.0; - } - - int32_t lfovalue = lfo.getsample(); - int32_t lfodelay = lfo.getdelay(); - - for (uint8_t note = 0; note < max_notes; ++note) { - if (voices[note].live) { - voices[note].dx7_note->compute(audiobuf.get(), lfovalue, lfodelay, &controllers); - for (uint8_t j = 0; j < _N_; ++j) { - int32_t val = audiobuf.get()[j]; - val = val >> 4; - int32_t clip_val = val < -(1 << 24) ? 0x8000 : val >= (1 << 24) ? 0x7fff : val >> 9; - float f = static_cast(clip_val >> 1) / 0x8000; - if (f > 1) f = 1; - if (f < -1) f = -1; - sumbuf[j] += f; - audiobuf.get()[j] = 0; - } + int32_t lfovalue = lfo.getsample(); + int32_t lfodelay = lfo.getdelay(); + + for (uint8_t note = 0; note < max_notes; ++note) { + if (voices[note].live) { + voices[note].dx7_note->compute(audiobuf.get(), lfovalue, lfodelay, &controllers); + for (uint8_t j = 0; j < _N_; ++j) { + int32_t val = audiobuf.get()[j]; + val = val >> 4; + int32_t clip_val = val < -(1 << 24) ? 0x8000 : val >= (1 << 24) ? 0x7fff : val >> 9; + float f = static_cast(clip_val >> 1) / 0x8000; + if (f > 1) f = 1; + if (f < -1) f = -1; + sumbuf[j] += f; + audiobuf.get()[j] = 0; } } + } - uint16_t jmax = n_samples - i; - for (uint8_t j = 0; j < _N_; ++j) { - if (j < jmax) - { - buffer[i + j] = static_cast(sumbuf[j] * 0x8000); - } - else - extra_buf_[j - jmax] = static_cast(sumbuf[j] * 0x8000); - } + for (uint8_t j = 0; j < _N_; ++j) { + buffer[i + j] = static_cast(sumbuf[j] * 0x8000); } - extra_buf_size_ = i - n_samples; } - - /* if (++_k_rate_counter == 0 && !monoMode) - { - uint8_t op_carrier = controllers.core->get_carrier_operators(data[134]); // look for carriers - - for (i = 0; i < max_notes; i++) - { - if (voices[i].live == true) - { - uint8_t op_amp = 0; - uint8_t op_carrier_num = 0; - - voices[i].dx7_note->peekVoiceStatus(voiceStatus); - - for (uint8_t op = 0; op < 6; op++) - { - uint8_t op_bit = static_cast(pow(2, op)); - - if ((op_carrier & op_bit) > 0) - { - // this voice is a carrier! - op_carrier_num++; - - //TRACE("Voice[%2d] OP [%d] amp=%ld,amp_step=%d,pitch_step=%d",i,op,voiceStatus.amp[op],voiceStatus.ampStep[op],voiceStatus.pitchStep); - - if (voiceStatus.amp[op] <= 1069 && voiceStatus.ampStep[op] == 4) // this voice produces no audio output - op_amp++; - } - } - if (op_amp == op_carrier_num) - { - // all carrier-operators are silent -> disable the voice - voices[i].live = false; - voices[i].sustained = false; - voices[i].keydown = false; - TRACE("Shutted down Voice[%2d]", i); - } - } - } - } */ } bool Dexed::ProcessMidiMessage(uint8_t type, uint8_t data1, uint8_t data2) diff --git a/dexed.h b/dexed.h index 1f5f525..a9c3f07 100644 --- a/dexed.h +++ b/dexed.h @@ -90,8 +90,6 @@ class Dexed FmCore* engineMsfa; EngineMkI* engineMkI; EngineOpl* engineOpl; - int16_t extra_buf_[_N_]; - uint32_t extra_buf_size_; private: uint8_t _k_rate_counter; diff --git a/synth.h b/synth.h index ad60d1e..f24aab5 100644 --- a/synth.h +++ b/synth.h @@ -17,8 +17,8 @@ #ifndef __SYNTH_H #define __SYNTH_H -#include -#define SUPER_PRECISE +//#include +//#define SUPER_PRECISE // This IS not be present on MSVC. // See http://stackoverflow.com/questions/126279/c99-stdint-h-header-and-ms-visual-studio @@ -50,7 +50,6 @@ typedef __int16 SInt16; #define SynthMemoryBarrier() #endif -/* template inline static T min(const T& a, const T& b) { return a < b ? a : b; @@ -60,7 +59,6 @@ template inline static T max(const T& a, const T& b) { return a > b ? a : b; } -*/ #define QER(n,b) ( ((float)n)/(1<