adafruit / adafruit-bluefruit-le-desktop

Desktop application to interact with Bluefruit LE and other Bluetooth low energy devices on Mac OSX, Windows, and Linux.
Other
138 stars 35 forks source link

incorrect timestamps on MIDI packets over BLE? #33

Open myklemykle opened 5 years ago

myklemykle commented 5 years ago

HI,

I've been working on a BLE MIDI controller, based on the Bluefruit LE, using code borrowed from this canonical example: https://github.com/adafruit/Adafruit_BluefruitLE_nRF51/tree/master/examples/midi

On OSX 10.11, I connect to the Bluefruit board using Bluefruit LE Connect.

The MIDI events sent over BLE by the Bluefruit board are read by an OS X application written in Faust (http://faust.grame.fr). While testing, I found my compiled Faust application was not receiving all MIDI events sent by the board, if the time between NOTEON and NOTEOFF messages was very brief (5-6ms).

Later I confirmed that this is only an issue with rapid MIDI notes from the Bluefruit board; Faust can receive MIDI notes with 5ms time spacing (or even less) from other sources. Only the notes I send from the Bluefruit have this problem.

I contacted one of the Faust developers, Stéphan Letz, who looked at the problem ... he is of the belief that either the Bluefruit LE app or the Adafruit BLE-MIDI libraries are causing incorrect timestamps to be put on MIDI events. If so, this may be causing lost notes or timing problems for other uses of BLE MIDI.

I will invite Stéphane to describe the problem in more detail here.

sletz commented 5 years ago

We debugged the issue by comparing the timestamp put in the CoreMIDI packets (expressed in "host time" and that can be converted in nanosec using AudioConvertHostTimeToNanos function) with the current date (also expressed in "host time" a) when the MIDI input callback is called.

We have seen huge deltas between the 2, about 400 ==> 450 milliseconds, which seems highly suspicious. Doing the same comparaison on a CoreAudio IAC bus, we see deltas in the order of 60 ==> 100 microseconds, so more than 3 order of magnitude less.