diff --git a/PlaybackThread.cpp b/PlaybackThread.cpp index 1bfa778..833281a 100644 --- a/PlaybackThread.cpp +++ b/PlaybackThread.cpp @@ -4,12 +4,15 @@ #include "config.h" #include "SharedState.h" +static Step local_sequence[NUM_TRACKS][NUM_STEPS]; +static Step local_nextSequence[NUM_TRACKS][NUM_STEPS]; + bool wasPlaying = false; static void handlePlayback() { bool nowPlaying = isPlaying; int tracksToPlay = (playMode == MODE_POLY) ? NUM_TRACKS : 1; - if (!wasPlaying && !nowPlaying) { + if (!wasPlaying && nowPlaying) { midi.sendRealtime(0xFA); // MIDI Start } else if (wasPlaying && !nowPlaying) { midi.sendRealtime(0xFC); // MIDI Stop @@ -17,12 +20,18 @@ static void handlePlayback() { } wasPlaying = nowPlaying; - if (!nowPlaying) return; + if (!nowPlaying) { + delay(1); // yield + return; + } unsigned long currentMicros = micros(); unsigned long clockInterval = 2500000 / tempo; // 60s * 1000000us / (tempo * 24ppqn) - if (currentMicros - lastClockTime >= clockInterval) { + if (currentMicros - lastClockTime < clockInterval) { + delay(1); // yield + return; + } else { lastClockTime += clockInterval; midi.sendRealtime(0xF8); // MIDI Clock @@ -32,9 +41,7 @@ static void handlePlayback() { clockCount = 0; midi.lock(); - Step local_sequence[NUM_TRACKS][NUM_STEPS]; memcpy(local_sequence, sequence, sizeof(local_sequence)); - Step local_nextSequence[NUM_TRACKS][NUM_STEPS]; memcpy(local_nextSequence, nextSequence, sizeof(local_nextSequence)); midi.unlock(); diff --git a/UIThread.cpp b/UIThread.cpp index 9154be9..34ae679 100644 --- a/UIThread.cpp +++ b/UIThread.cpp @@ -15,7 +15,7 @@ #include "UIThread.h" #include "SharedState.h" - +static Step local_sequence[NUM_TRACKS][NUM_STEPS]; static void handleInput(); static void drawUI(); @@ -106,8 +106,7 @@ static void generateSequenceData(int themeType, Step (*target)[NUM_STEPS]) { } void generateTheme(int themeType) { - Step local_sequence[NUM_TRACKS][NUM_STEPS]; - generateSequenceData(themeType, sequence); + generateSequenceData(themeType, local_sequence); midi.lock(); memcpy(sequence, local_sequence, sizeof(local_sequence)); @@ -353,7 +352,6 @@ static void drawUI() { bool local_trackMute[NUM_TRACKS]; int local_midiChannel; MelodyStrategy* local_strategy; - Step local_sequence[NUM_TRACKS][NUM_STEPS]; int local_playbackStep; int local_scaleNotes[12]; @@ -401,7 +399,6 @@ static void drawUI() { static void updateLeds() { // Make local copies of shared data inside a critical section // to avoid holding the lock during slow LED update operations. - Step local_sequence[NUM_TRACKS][NUM_STEPS]; int local_playbackStep; bool local_isPlaying; UIState local_currentState;