Exchanged main loop.

Added better MIDI test functions.
pull/4/head
Holger Wirtz 7 years ago
parent 1d282f5d81
commit 83611463ad
  1. 115
      MicroDexed.ino

@ -16,11 +16,11 @@
#include <looper.h> #include <looper.h>
#include "dexed.h" #include "dexed.h"
#define AUDIO_MEM 8 #define AUDIO_MEM 32
#define AUDIO_BUFFER_SIZE 128 #define AUDIO_BUFFER_SIZE 128
#define SAMPLEAUDIO_BUFFER_SIZE 44100 #define SAMPLEAUDIO_BUFFER_SIZE 44100
#define MIDI_QUEUE_LOCK_TIMEOUT_MS 5 #define MIDI_QUEUE_LOCK_TIMEOUT_MS 0
//#define INIT_AUDIO_QUEUE 1 #define INIT_AUDIO_QUEUE 1
#define TEST_MIDI 1 #define TEST_MIDI 1
#define TEST_NOTE1 60 #define TEST_NOTE1 60
@ -62,7 +62,7 @@ void setup()
AudioMemory(AUDIO_MEM); AudioMemory(AUDIO_MEM);
sgtl5000_1.enable(); sgtl5000_1.enable();
sgtl5000_1.volume(0.4); sgtl5000_1.volume(0.2);
// Initialize processor and memory measurements // Initialize processor and memory measurements
AudioProcessorUsageMaxReset(); AudioProcessorUsageMaxReset();
@ -84,17 +84,10 @@ void setup()
dexed->activate(); dexed->activate();
#ifdef TEST_MIDI #ifdef TEST_MIDI
midi_queue_t m; threads.addThread(midi_test_thread, 1);
m.cmd = 0x90;
m.data1 = TEST_NOTE1;
m.data2 = 100;
midi_queue.enqueue(m);
m.data1 = TEST_NOTE2;
midi_queue.enqueue(m);
m.cmd = 0xb0;
#endif #endif
threads.addThread(audio_thread, 1); threads.addThread(midi_thread, 1);
sched.addJob(cpu_and_mem_usage, 1000); sched.addJob(cpu_and_mem_usage, 1000);
@ -103,63 +96,85 @@ void setup()
void loop() void loop()
{ {
#ifdef TEST_MIDI int16_t* audio_buffer; // pointer to 128 * int16_t
if (millis() > 3000 && millis() < 3050) bool break_for_calculation;
dexed->ProcessMidiMessage(0x80, TEST_NOTE1, 0);
if (millis() > 5000 && millis() < 5050)
dexed->ProcessMidiMessage(0x80, TEST_NOTE2, 0);
#endif
// process midi->audio audio_buffer = queue1.getBuffer();
while (MIDI.read()) if (audio_buffer == NULL)
{
Serial.println(F("audio_buffer allocation problems!"));
return;
}
while (!midi_queue.isEmpty())
{ {
midi_queue_t m;
m.cmd = MIDI.getType();
m.data1 = MIDI.getData1();
m.data2 = MIDI.getData2();
if (midi_queue_lock.lock(MIDI_QUEUE_LOCK_TIMEOUT_MS)) if (midi_queue_lock.lock(MIDI_QUEUE_LOCK_TIMEOUT_MS))
{ {
midi_queue.enqueue(m); midi_queue_t m = midi_queue.dequeue();
break_for_calculation = dexed->ProcessMidiMessage(m.cmd, m.data1, m.data2);
midi_queue_lock.unlock(); midi_queue_lock.unlock();
if (break_for_calculation == true)
break;
} }
else
break;
} }
dexed->GetSamples(AUDIO_BUFFER_SIZE, audio_buffer);
queue1.playBuffer();
sched.scheduler(); sched.scheduler();
} }
void audio_thread(void) void midi_test_thread(void)
{ {
int16_t* audio_buffer; // pointer to 128 * int16_t delay(500);
bool break_for_calculation; queue_midi_event(0x90, TEST_NOTE1, 100);
queue_midi_event(0x90, TEST_NOTE2, 100);
delay(1000);
queue_midi_event(0x80, TEST_NOTE1, 100);
delay(1000);
queue_midi_event(0x80, TEST_NOTE2, 100);
delay(500);
for (uint8_t i = 0; i < 16; i++)
{
queue_midi_event(0x90, 55 + i, 100);
delay(100);
}
delay(1000);
for (uint8_t i = 0; i < 16; i++)
{
queue_midi_event(0x80, 55 + i, 100);
}
threads.yield();
}
Serial.println(F("audio thread start")); void midi_thread(void)
{
Serial.println(F("midi thread start"));
while (42 == 42) // Don't panic! while (42 == 42) // Don't panic!
{ {
audio_buffer = queue1.getBuffer(); while (MIDI.read())
if (audio_buffer == NULL)
{ {
Serial.println(F("audio_buffer allocation problems!")); queue_midi_event(MIDI.getType(), MIDI.getData1(), MIDI.getData2());
continue;
}
while (!midi_queue.isEmpty())
{
if (midi_queue_lock.lock(MIDI_QUEUE_LOCK_TIMEOUT_MS))
{
midi_queue_t m = midi_queue.dequeue();
break_for_calculation = dexed->ProcessMidiMessage(m.cmd, m.data1, m.data2);
midi_queue_lock.unlock();
if (break_for_calculation == true)
break;
}
else
break;
} }
}
}
bool queue_midi_event(uint8_t type, uint8_t data1, uint8_t data2)
{
midi_queue_t m;
m.cmd = type;
m.data1 = data1;
m.data2 = data2;
dexed->GetSamples(AUDIO_BUFFER_SIZE, audio_buffer); if (midi_queue_lock.lock(MIDI_QUEUE_LOCK_TIMEOUT_MS))
queue1.playBuffer(); {
midi_queue.enqueue(m);
midi_queue_lock.unlock();
return (true);
} }
return (false);
} }
void cpu_and_mem_usage(void) void cpu_and_mem_usage(void)

Loading…
Cancel
Save