diff --git a/MicroDexed.ino b/MicroDexed.ino index 97268fa..041b7a6 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -1,66 +1,63 @@ +// // MicroDexed +// +// A port of the Dexed sound engine (https://github.com/asb2m10/dexed) for the Teensy-3.5/3.6 with audio shield +// (c)2018 H. Wirtz +// +#include #include #include "dexed.h" -#define RATE 128 -#define SAMPLERATE 44100 -#define TEENSY 1 +#define AUDIO_BUFFER_SIZE 128 +#define SAMPLEAUDIO_BUFFER_SIZE 44100 #define TEST_MIDI 1 -#define TEST_NOTE1 59 -#define TEST_NOTE2 60 +#define TEST_NOTE1 60 +#define TEST_NOTE2 68 -#ifdef TEENSY #include #include #include #include #include +#include -// GUItool: begin automatically generated code +typedef struct +{ + uint8_t cmd; + uint8_t data1; + uint8_t data2; +} midi_queue_t; + +// GUItool: begin automatically geneAUDIO_BUFFER_SIZEd code AudioPlayQueue queue1; //xy=266,484 -//AudioEffectReverb reverb1; //xy=486,545 AudioOutputI2S i2s1; //xy=739,486 -//AudioConnection patchCord1(queue1, reverb1); -//AudioConnection patchCord2(reverb1, 0, i2s1, 0); -//AudioConnection patchCord3(reverb1, 0, i2s1, 1); AudioConnection patchCord2(queue1, 0, i2s1, 0); AudioConnection patchCord3(queue1, 0, i2s1, 1); AudioControlSGTL5000 sgtl5000_1; //xy=384,610 -// GUItool: end automatically generated code - -/* - // GUItool: begin automatically generated code - AudioPlayQueue queue1; //xy=811,259 - AudioOutputI2S i2s1; //xy=1185,252 - AudioConnection patchCord1(queue1, 0, i2s1, 0); - AudioConnection patchCord2(queue1, 0, i2s1, 1); - AudioControlSGTL5000 sgtl5000_1; //xy=830,376 - // GUItool: end automatically generated code -*/ - -#endif +// GUItool: end automatically geneAUDIO_BUFFER_SIZEd code MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); - -Dexed* dexed = new Dexed(SAMPLERATE); +Dexed* dexed = new Dexed(SAMPLEAUDIO_BUFFER_SIZE); +QueueArray midi_queue; +Threads::Mutex midi_queue_lock; void setup() { Serial.begin(115200); //while (!Serial) ; // wait for Arduino Serial Monitor - delay(200); + delay(300); Serial.println(F("MicroDexed")); + Serial.println(F("setup start")); MIDI.begin(MIDI_CHANNEL_OMNI); -#ifdef TEENSY // Audio connections require memory to work. For more // detailed information, see the MemoryAndCpuUsage example AudioMemory(16); sgtl5000_1.enable(); - sgtl5000_1.volume(0.3); + sgtl5000_1.volume(0.5); // Initialize processor and memory measurements //AudioProcessorUsageMaxReset(); @@ -72,11 +69,10 @@ void setup() int16_t* audio_buffer = queue1.getBuffer(); if (audio_buffer != NULL) { - memset(audio_buffer,0,RATE); + memset(audio_buffer, 0, AUDIO_BUFFER_SIZE); queue1.playBuffer(); } } -#endif dexed->activate(); @@ -85,15 +81,13 @@ void setup() dexed->ProcessMidiMessage(0x90, TEST_NOTE2, 60); #endif - //reverb1.reverbTime(5.0); + threads.addThread(audio_thread, 1); - Serial.println("Go"); + Serial.println(F("setup end")); } void loop() { - int16_t* audio_buffer; // pointer for 128 * int16_t - #ifdef TEST_MIDI if (millis() > 3000 && millis() < 3050) dexed->ProcessMidiMessage(0x80, TEST_NOTE1, 0); @@ -101,48 +95,43 @@ void loop() dexed->ProcessMidiMessage(0x80, TEST_NOTE2, 0); #endif -#ifdef TEENSY - audio_buffer = queue1.getBuffer(); - if (audio_buffer == NULL) + // process midi->audio + while (MIDI.read()) { - Serial.println("audio_buffer allocation problems!"); - return; + midi_queue_t m; + m.cmd = MIDI.getType(); + m.data1 = MIDI.getData1(); + m.data2 = MIDI.getData2(); + + while (!midi_queue_lock.try_lock()); + midi_queue.enqueue(m); + midi_queue_lock.unlock(); } -#endif +} - // process midi->audio - if (MIDI.read()) +void audio_thread(void) +{ + int16_t* audio_buffer; // pointer for 128 * int16_t + + Serial.println(F("audio thread start")); + + while (42 == 42) { - /* Serial.print("Type: "); - Serial.print(MIDI.getType(), DEC); - Serial.print(" Data1: "); - Serial.print(MIDI.getData1(), DEC); - Serial.print(" Data2: "); - Serial.println(MIDI.getData2(), DEC); */ - dexed->ProcessMidiMessage(MIDI.getType(), MIDI.getData1(), MIDI.getData2()); + audio_buffer = queue1.getBuffer(); + if (audio_buffer == NULL) + { + Serial.println("audio_buffer allocation problems!"); + return; + } + while (!midi_queue.isEmpty ()) + { + while (!midi_queue_lock.try_lock()); + midi_queue_t m = midi_queue.dequeue(); + dexed->ProcessMidiMessage(m.cmd, m.data1, m.data2); + midi_queue_lock.unlock(); + } + + dexed->GetSamples(AUDIO_BUFFER_SIZE, audio_buffer); + queue1.playBuffer(); } - dexed->GetSamples(RATE, audio_buffer); - - /* uint8_t i = 0; - for (i = 0; i < 128; i++) - { - if ((i % 16) == 0) - Serial.println(); - - if (i < 10) - Serial.print(" "); - if (i > 9 && i < 100) - Serial.print(" "); - Serial.print("["); - Serial.print(i, DEC); - Serial.print("]:"); - Serial.print(audio_buffer[i]); - Serial.print(" "); - } - Serial.println();*/ - -#ifdef TEENSY - queue1.playBuffer(); -#endif } -