I am using the MIDIUSB library on an Arduino Micro.
I try to calculate BPM with the timing clock (0xF8) coming from ableton live. As long as i only receive timing clock, everything works fine and the calculated BPM is pretty accurate. But as soon as some additional MIDI CCs are sent from ableton live, arduino micro is missing some timing clock messages. It also happens that MIDI CCs aren't received by the Arduino Micro.
Is this normal with this library or might it be a problem with ableton live?
This is the code:
#include <MIDIUSB.h>
#define MIDI_CMD_TIMINGCLOCK 0xF8
void setup() {
}
void loop() {
int16_t MIDIvalues[3];
readMidiBySerial(MIDIvalues);
if (MIDIvalues[0] == MIDI_CMD_TIMINGCLOCK) {
static uint8_t clockCount;
static uint32_t startTime;
if (clockCount == 0) {
startTime = millis();
}
clockCount++;
uint8_t maxCount = 24;
if (clockCount > maxCount) {
uint32_t elapsedTime = millis() - startTime;
float tic = elapsedTime / float(maxCount);
float bpm = 60000.0 / (tic * 24.0);
clockCount = 0;
Serial.print("---- BPM: ");
Serial.println(bpm);
}
}
}
void readMidiBySerial(int16_t *values) {
int value0 = -1; // command byte
int value1 = -1;
int value2 = -1;
midiEventPacket_t rx;
do {
rx = MidiUSB.read();
if (rx.header != 0) {
// rx.header ???
value0 = rx.byte1; //read command byte
value1 = rx.byte2; //read next byte
value2 = rx.byte3; //read final byte
}
}
while (rx.header != 0); //when at least three bytes available
values[0] = value0;
values[1] = value1;
values[2] = value2;
}