A latency of 27ms doesn't seem right at all. I'm not familiar with the Adafruit libraries you mention, but I did write a MIDI BLE library for ESP32. The way I handle it there is by buffering the Bluetooth packets, rather than the MIDI messages, I think this should simplify the rest of the code. The ESP32 has FreeRTOS and two cores, so it's relatively easy to handle sending Bluetooth data asynchronously, on Teensy, it'll be a bit harder, you could use a timer interrupt, but that gets complicated quickly.
Basically, you have two threads, your main thread that wants to send MIDI messages, and a second thread that sends BLE packets in the background. When you write the first data to a BLE packet, the sender thread starts a timeout of a couple of milliseconds. As long as the timeout isn't reached yet, all incoming MIDI messages are added to the same BLE packet. When the timeout is reached, the sender thread sends the BLE packet. This means that the main thread never has to wait for the BLE transmission.
You can find my implementation here if you're interested:
https://github.com/tttapa/Control-S...Interfaces/BluetoothMIDI_Interface.cpp#L8-L80
Looking at the implementation of the Adafruit MIDI BLE library, it seems like most actual MIDI BLE stuff happens on the nRF51 itself, not on the “master” MCU.
Two things stood out to me though:
For communicating with the Bluefruit UART LE, the library seems to use SoftwareSerial @ 9600 baud. This is extremely slow. Since you probably have a free hardware UART on your Teensy, you could look into using that instead, although you might not be able to use a higher baud rate, see this comment
https://github.com/adafruit/Adafrui...7d7f5f2891/Adafruit_BluefruitLE_UART.cpp#L109. The overhead for sending MIDI data to the nRF51 is at least 12 bytes for the string “AT+BLEMIDITX”, then probably some other overhead, and at least 5 bytes of data (for a 3-byte MIDI message and 2 bytes for the header/timestamp). Let's say you need to transmit at least 20 bytes for a single 3-byte MIDI message, that'll take approximately 20 ms (1000×(8+2)×20/9600), just to send it over the serial link to the BLE adapter, that's not really acceptable IMHO.
Second, the MTU seems to be fixed to 20 bytes for some reason, see
https://github.com/adafruit/Adafrui...c85f3c7dc7baae/Adafruit_BLEMIDI.cpp#L116-L133. This will introduce extra latency as well. On my ESP32, I use an MTU of over 500 bytes, which allows you to fit much more MIDI data into a single BLE packet.
Adding a queue for buffering might prevent the rest of your code from being slowed down by the BLE code, but it won't solve the underlying problem with the Teensy <-> nRF51 link being way too slow to be usable for MIDI.