diff --git a/MicroDexed.ino b/MicroDexed.ino index b886dae..1f5414b 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -46,6 +46,8 @@ MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); Dexed* dexed = new Dexed(SAMPLE_RATE); bool sd_card_available = false; uint8_t bank = DEFAULT_SYSEXBANK; +uint32_t xrun = 0; +uint32_t overload = 0; #ifdef MASTER_KEY_MIDI bool master_key_enabled = false; @@ -159,16 +161,11 @@ void loop() if (!queue1.available()) continue; -#if defined(SHOW_DEXED_TIMING) || defined(SHOW_XRUN) elapsedMicros t1; -#endif dexed->getSamples(AUDIO_BLOCK_SAMPLES, audio_buffer); - -#ifdef SHOW_XRUN uint32_t t2 = t1; if (t2 > 2900) // everything greater 2.9ms is a buffer underrun! - Serial.println(F("XRUN")); -#endif + xrun++; #ifdef SHOW_DEXED_TIMING Serial.println(t1, DEC); #endif @@ -209,8 +206,7 @@ void handle_midi_input(void) void note_on(void) { randomSeed(analogRead(A0)); - queue_midi_event(0x90, TEST_NOTE, random(TEST_VEL_MIN, TEST_VEL_MAX)); - // 1 + queue_midi_event(0x90, TEST_NOTE, random(TEST_VEL_MIN, TEST_VEL_MAX)); // 1 queue_midi_event(0x90, TEST_NOTE + 5, random(TEST_VEL_MIN, TEST_VEL_MAX)); // 2 queue_midi_event(0x90, TEST_NOTE + 8, random(TEST_VEL_MIN, TEST_VEL_MAX)); // 3 queue_midi_event(0x90, TEST_NOTE + 12, random(TEST_VEL_MIN, TEST_VEL_MAX)); // 4 @@ -328,12 +324,12 @@ bool queue_midi_event(uint8_t type, uint8_t data1, uint8_t data2) if (type == 0x80 && data1 == MASTER_KEY_MIDI) // Master key released { master_key_enabled = false; - Serial.println("Master key disabled"); + Serial.println(F("Master key disabled")); } else if (type == 0x90 && data1 == MASTER_KEY_MIDI) // Master key pressed { master_key_enabled = true; - Serial.println("Master key enabled"); + Serial.println(F("Master key enabled")); } else { @@ -469,6 +465,10 @@ void show_cpu_and_mem_usage(void) Serial.print(AudioMemoryUsage(), DEC); Serial.print(F(" MEM MAX:")); Serial.print(AudioMemoryUsageMax(), DEC); + Serial.print(F(" XRUN:")); + Serial.print(xrun, DEC); + Serial.print(F(" OVERLOAD:")); + Serial.print(overload, DEC); Serial.println(); AudioProcessorUsageMaxReset(); AudioMemoryUsageMaxReset(); diff --git a/config.h b/config.h index 89548dd..31293cd 100644 --- a/config.h +++ b/config.h @@ -21,9 +21,10 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "midinotes.h" //#define TEST_MIDI 1 -#define TEST_NOTE 40 +#define TEST_NOTE MIDI_E2 #define TEST_VEL_MIN 60 #define TEST_VEL_MAX 110 @@ -43,12 +44,12 @@ #define DEFAULT_SYSEXBANK 0 #define DEFAULT_SYSEXSOUND 0 -#define MASTER_KEY_MIDI 84 // C6 -#define MASTER_NUM1 24 // C1 -#define MASTER_BANK_SELECT 83 // B5 +#define MASTER_KEY_MIDI MIDI_C6 +#define MASTER_NUM1 MIDI_C1 #define USE_TEENSY_DSP 1 #define SUM_UP_AS_INT 1 +#define REDUCE_LOUDNESS 2 // Use these with the Teensy Audio Shield #define SDCARD_CS_PIN 10 diff --git a/dexed.cpp b/dexed.cpp index 7c9cb95..0fa453b 100644 --- a/dexed.cpp +++ b/dexed.cpp @@ -39,6 +39,7 @@ #endif extern uint8_t bank; +extern uint32_t overload; extern bool load_sysex(uint8_t bank, uint8_t voice_number); Dexed::Dexed(int rate) @@ -142,6 +143,9 @@ void Dexed::getSamples(uint16_t n_samples, int16_t* buffer) int32_t lfovalue = lfo.getsample(); int32_t lfodelay = lfo.getdelay(); +#ifdef SUM_UP_AS_INT + int32_t sum; +#endif for (uint8_t note = 0; note < max_notes; ++note) { if (voices[note].live) { @@ -154,24 +158,34 @@ void Dexed::getSamples(uint16_t n_samples, int16_t* buffer) #else int32_t clip_val = val < -(1 << 24) ? 0x8000 : val >= (1 << 24) ? 0x7fff : val >> 9; #endif + if (clip_val != (val>>9)) + overload++; #ifdef SUM_UP_AS_INT - int32_t tmp = buffer[i + j] + (clip_val >> 1); - if (buffer[i + j] > 0 && clip_val > 0) + sum = buffer[i + j] + (clip_val >> REDUCE_LOUDNESS); +/* + if (buffer[i + j] > 0 && clip_val > 0 && sum < 0) { - if ((tmp < buffer[i + j]) && (tmp < clip_val)) - tmp = 0x8000; + sum = 0x8000; } - else if (buffer[i + j] < 0 && clip_val < 0) + else if (buffer[i + j] < 0 && clip_val < 0 && sum > 0) { - if ((tmp > buffer[i + j]) && (tmp > clip_val)) - tmp = 0x7FFF; + sum = 0x7FFF; } - buffer[i + j] = tmp; + */ + buffer[i + j] = sum; audiobuf.get()[j] = 0; #else - float f = static_cast(clip_val >> 1) / 0x8000; - if (f > 1) f = 1; - if (f < -1) f = -1; + float f = static_cast(clip_val >> REDUCE_LOUDNESS) / 0x8000; + if (f > 1) + { + f = 1; + overload++; + } + else if (f < -1) + { + f = -1; + overload++; + } sumbuf[j] += f; audiobuf.get()[j] = 0; #endif diff --git a/midinotes.h b/midinotes.h new file mode 100644 index 0000000..c62e4a0 --- /dev/null +++ b/midinotes.h @@ -0,0 +1,97 @@ +/************************************************* + * MIDI note values + *************************************************/ + +#ifndef _MIDINOTES_H +#define _MIDINOTES_H + +#define MIDI_A0 21 +#define MIDI_AIS0 22 +#define MIDI_B0 23 +#define MIDI_C1 24 +#define MIDI_CIS1 25 +#define MIDI_D1 26 +#define MIDI_DIS1 27 +#define MIDI_E1 28 +#define MIDI_F1 29 +#define MIDI_FIS1 30 +#define MIDI_G1 31 +#define MIDI_GIS1 32 +#define MIDI_A1 33 +#define MIDI_AIS1 34 +#define MIDI_B1 35 +#define MIDI_C2 36 +#define MIDI_CIS2 37 +#define MIDI_D2 38 +#define MIDI_DIS2 39 +#define MIDI_E2 40 +#define MIDI_F2 41 +#define MIDI_FIS2 42 +#define MIDI_G2 43 +#define MIDI_GIS2 44 +#define MIDI_A2 45 +#define MIDI_AIS2 46 +#define MIDI_B2 47 +#define MIDI_C3 48 +#define MIDI_CIS3 49 +#define MIDI_D3 50 +#define MIDI_DIS3 51 +#define MIDI_E3 52 +#define MIDI_F3 53 +#define MIDI_FIS3 54 +#define MIDI_G3 55 +#define MIDI_GIS3 56 +#define MIDI_A3 57 +#define MIDI_AIS3 58 +#define MIDI_B3 59 +#define MIDI_C4 60 +#define MIDI_CIS4 61 +#define MIDI_D4 62 +#define MIDI_DIS4 63 +#define MIDI_E4 64 +#define MIDI_F4 65 +#define MIDI_FIS4 66 +#define MIDI_G4 67 +#define MIDI_GIS4 68 +#define MIDI_A4 69 +#define MIDI_AIS4 70 +#define MIDI_B4 71 +#define MIDI_C5 72 +#define MIDI_CIS5 73 +#define MIDI_D5 74 +#define MIDI_DIS5 75 +#define MIDI_E5 76 +#define MIDI_F5 77 +#define MIDI_FIS5 78 +#define MIDI_G5 79 +#define MIDI_GIS5 80 +#define MIDI_A5 81 +#define MIDI_AIS5 82 +#define MIDI_B5 83 +#define MIDI_C6 84 +#define MIDI_CIS6 85 +#define MIDI_D6 86 +#define MIDI_DIS6 87 +#define MIDI_E6 88 +#define MIDI_F6 89 +#define MIDI_FIS6 90 +#define MIDI_G6 91 +#define MIDI_GIS6 92 +#define MIDI_A6 93 +#define MIDI_AIS6 94 +#define MIDI_B6 95 +#define MIDI_C7 96 +#define MIDI_CIS7 97 +#define MIDI_D7 98 +#define MIDI_DIS7 99 +#define MIDI_E7 100 +#define MIDI_F7 101 +#define MIDI_FIS7 102 +#define MIDI_G7 103 +#define MIDI_GIS7 104 +#define MIDI_A7 105 +#define MIDI_AIS7 106 +#define MIDI_B7 107 +#define MIDI_C8 108 + +#endif