Auto-detect time signature from SPP deltas (bar boundaries)
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
volatile uint32_t midi_tick_count = 0;
|
volatile uint32_t midi_tick_count = 0;
|
||||||
volatile uint16_t last_spp_position = 0;
|
volatile uint16_t last_spp_position = 0;
|
||||||
volatile bool spp_valid = false;
|
volatile bool spp_valid = false;
|
||||||
|
extern volatile uint8_t beats_per_bar;
|
||||||
|
|
||||||
static Adafruit_USBD_MIDI usb_midi;
|
static Adafruit_USBD_MIDI usb_midi;
|
||||||
|
|
||||||
@@ -80,9 +81,22 @@ void UsbMidiTransport::update() {
|
|||||||
}
|
}
|
||||||
} else if (cin == 0x03 && packet[1] == 0xF2) {
|
} else if (cin == 0x03 && packet[1] == 0xF2) {
|
||||||
// Song Position Pointer
|
// Song Position Pointer
|
||||||
|
uint16_t prev_spp = last_spp_position;
|
||||||
last_spp_position = (packet[3] << 7) | packet[2];
|
last_spp_position = (packet[3] << 7) | packet[2];
|
||||||
spp_valid = true;
|
spp_valid = true;
|
||||||
Serial.printf("[CLK] SPP=%d\n", last_spp_position);
|
Serial.printf("[CLK] SPP=%d\n", last_spp_position);
|
||||||
|
|
||||||
|
// Auto-detect time signature from SPP delta (sent at bar boundaries)
|
||||||
|
if (prev_spp > 0 && last_spp_position > prev_spp) {
|
||||||
|
uint16_t delta = last_spp_position - prev_spp;
|
||||||
|
if (delta >= 8 && delta <= 64 && delta % 4 == 0) {
|
||||||
|
uint8_t detected = delta / 4;
|
||||||
|
if (detected >= 2 && detected <= 16 && detected != beats_per_bar) {
|
||||||
|
beats_per_bar = detected;
|
||||||
|
Serial.printf("[CLK] Auto-detected %d/4 time from SPP delta=%d\n", detected, delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
MidiEvent event;
|
MidiEvent event;
|
||||||
parse_midi_packet(packet, 4, event);
|
parse_midi_packet(packet, 4, event);
|
||||||
|
|||||||
Reference in New Issue
Block a user