limit frequency of reading & parsing MIDI data

To improve note accuracy
This commit is contained in:
Peter Cai 2021-06-11 10:23:53 +08:00
parent 7d2f555ee4
commit 2a4670009b
1 changed files with 20 additions and 2 deletions

View File

@ -5,6 +5,10 @@
#define NUM_MOTORS 4
// Minimal interval between reading two chunks of MIDI data
// This improves frequency accuracy by throttling non-synthesizing actions
#define MIDI_CMD_INTERVAL_MIN 15 * 1000 // 15ms
using namespace midi;
MotorControl motors[NUM_MOTORS] = {
@ -73,10 +77,24 @@ int main() {
MIDI.setHandleNoteOn(midi_note_on);
MIDI.setHandleNoteOff(midi_note_off);
unsigned long cur_micros;
unsigned long last_read_micros = 0;
while (true) {
unsigned long cur_micros = timer2.get_count() / 2ul; // The unit of get_count is 0.5us
cur_micros = timer2.get_count() / 2ul; // The unit of get_count is 0.5us
handle_tick(cur_micros);
MIDI.read();
if (cur_micros - last_read_micros >= MIDI_CMD_INTERVAL_MIN) {
last_read_micros = cur_micros;
// Read all available MIDI data
// Note that the host is expected to send MIDI data only when
// it is supposed to be played; therefore we don't need
// to worry about the host sending data prematurely
while (Serial.available()) {
MIDI.read();
}
}
}
return 0;