Skip to content

Commit

Permalink
Merge pull request #357 from shorepine/usbmidifix
Browse files Browse the repository at this point in the history
stop parsing USB/IAC midi messagees once parsed
  • Loading branch information
bwhitman authored Sep 7, 2024
2 parents 2d525c5 + 6227de1 commit 11b5397
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion amy
Submodule amy updated 6 files
+2 −15 juno.py
+11 −14 src/amy.c
+1 −1 src/amy.h
+128 −128 src/patches.h
+6 −6 test.py
+ tests/ref/TestPortamento.wav
2 changes: 1 addition & 1 deletion tulip/esp32s3/usb_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static void midi_transfer_cb(usb_transfer_t *transfer) {
p[i], p[i + 1], p[i + 2], p[i + 3]);
// Strip the first byte which is the USB-MIDI virtual wire ID,
// rest is MIDI message (at least for 3-byte messages).
convert_midi_bytes_to_messages(p + i + 1, 3);
convert_midi_bytes_to_messages(p + i + 1, 3, 1);
}
esp_err_t err = usb_host_transfer_submit(transfer);
if (err != ESP_OK) {
Expand Down
2 changes: 1 addition & 1 deletion tulip/macos/virtualmidi.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void midi_out(uint8_t * bytes, uint16_t len) {
data[0] = bytes[2];
data[1] = bytes[1];
data[2] = bytes[0];
convert_midi_bytes_to_messages(data, 3);
convert_midi_bytes_to_messages(data, 3, 1);
} else {
//printf("bytes[3] was not 0x20\n");
}
Expand Down
12 changes: 8 additions & 4 deletions tulip/shared/midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ void callback_midi_message_received(uint8_t *data, size_t len) {
1 0xFF reset | XXXX
*/

void convert_midi_bytes_to_messages(uint8_t * data, size_t len) {
void convert_midi_bytes_to_messages(uint8_t * data, size_t len, uint8_t usb) {
// i take any amount of bytes and add messages
// remember this can start in the middle of a midi message, so act accordingly
// running status is handled by keeping the status byte around after getting a message.
// remember that USB midi always comes in groups of 3 here, even if it's just a one byte message
// so we have USB (and mac IAC) set a usb flag so we know to end the loop once a message is parsed
for(size_t i=0;i<len;i++) {

uint8_t byte = data[i];
Expand All @@ -83,7 +85,8 @@ void convert_midi_bytes_to_messages(uint8_t * data, size_t len) {
current_midi_message[0] = byte;
if(byte == 0xF4 || byte == 0xF5 || byte == 0xF6 || byte == 0xF8 || byte == 0xF9 ||
byte == 0xFA || byte == 0xFB || byte == 0xFC || byte == 0xFD || byte == 0xFE || byte == 0xFF) {
callback_midi_message_received(current_midi_message, 1);
callback_midi_message_received(current_midi_message, 1);
if(usb) i = len+1; // exit the loop if usb
} else if(byte == 0xF0) { // sysex start
// We will ignore sysex for now, but we have to understand it. We'll tell the parser to ignore anything up to F7
sysex_flag = 1;
Expand All @@ -107,6 +110,7 @@ void convert_midi_bytes_to_messages(uint8_t * data, size_t len) {
} else if (status == 0xC0 || status == 0xD0 || current_midi_message[0] == 0xF3 || current_midi_message[0] == 0xF1) {
current_midi_message[1] = byte;
callback_midi_message_received(current_midi_message, 2);
if(usb) i = len+1; // exit the loop if usb
}
}
}
Expand All @@ -116,7 +120,7 @@ void convert_midi_bytes_to_messages(uint8_t * data, size_t len) {


void midi_local(uint8_t * bytes, uint16_t len) {
convert_midi_bytes_to_messages(bytes, len);
convert_midi_bytes_to_messages(bytes, len, 0);
}

#ifdef ESP_PLATFORM
Expand Down Expand Up @@ -175,7 +179,7 @@ void run_midi() {
// fprintf(stderr, "%02x ", data[i]);
//}
//fprintf(stderr, "\n");
convert_midi_bytes_to_messages(data,length);
convert_midi_bytes_to_messages(data,length,0);
}
} // end loop forever
}
Expand Down
2 changes: 1 addition & 1 deletion tulip/shared/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern QueueHandle_t uart_queue;
#define MIDI_SLOTS 4
#include "py/mphal.h"
#include "py/runtime.h"
void convert_midi_bytes_to_messages(uint8_t * data, size_t len);
void convert_midi_bytes_to_messages(uint8_t * data, size_t len, uint8_t usb);
extern mp_obj_t midi_callback;


Expand Down

0 comments on commit 11b5397

Please sign in to comment.