kshoji / BLE-MIDI-for-Android

MIDI over Bluetooth LE driver for Android 4.3 or later
Apache License 2.0
123 stars 41 forks source link

Event coordination code seems incorrect #19

Open Astronought opened 4 years ago

Astronought commented 4 years ago

Hi,

Not sure if this library is still maintained, but I've found a potential problem around how event timings are coordinated.

Specifically the code that starts here.

For example lets imagine I have a lastTimestamp of 8000 and a timestamp of 5000, i.e. there was 5192 ms between the last midi event and the current midi event. Now the condition of the if statement is false because 5000 + (8192 / 2) = 9096 which obviously isn't < 8000 (lastTimestamp) and so no adjustment is made to the current timestamp.

This is now an issue, because when the final result is calculated, part of the calculation is adjustedTimestamp - lastTimestamp or 5000 - 8000, which when we take the system time into account means we end up with an event timing that is less than the previous event, despite the current event taking place 5192ms after the previous.

I suggest all that needs to happen instead is we need to take the MAX_TIMESTAMP, subtract the lastTimestamp from it and add the difference onto the current timestamp to get the proper adjustment. E.g. 8192 - 8000 + 5000 = 5192ms, this can then be added onto the last system time recorded and we get correct event ordering.

Please let me know if I've missed something completely obvious, otherwise I am happy to put up a PR.

EDIT: BlueZ has a fantastic implementation of this here