lhl2617 / VSLilyPond

VSCode Extension for LilyPond
https://marketplace.visualstudio.com/items?itemName=lhl2617.vslilypond
90 stars 7 forks source link

MIDI input is not debounced #341

Closed paradox460 closed 2 years ago

paradox460 commented 2 years ago

I am using a KORG microKEY AIR (bluetooth 25key midi keyboard) on MacOS 10.15.7, and when attempting to input notes, I get a "double note" effect. Pressing the key gets one note entered into the document, but releasing it gets the same note.

Additionally, there is some carry-over from previously entered notes.

Screenshot 2021-09-03 at 15 27 12

paradox460 commented 2 years ago

Investigating a bit more, it looks like this plugin reads the velocity of the midi message (msg[2]). This may work in testing because the virtual keyboard used emits a velocity of 0 for keyups, but my real MIDI keyboard does not.

Instead, the status byte should be referenced.

For a key down event, we see that we get a status byte of 144, a key number of 55, and a velocity of 62 (I pushed the key with a moderate amount of force, mezzo-forte if I were to judge) Screenshot 2021-09-03 at 20 36 12@2x

144, translated into hexadecimal, is 0x90, which is indicated on this page as a Key Down event. You can cross-check this against the official MIDI tables here, but they use binary, so you have to translate 144 into binary (1001 0000)

When we release the key, we see that we have a different status byte, 128 this time Screenshot 2021-09-03 at 20 39 44@2x

128, translated into hex, is 0x80, which indicates a key up event.

Patching the functions in midi-in.ts to use this information, instead of relying on velocity, nets us this happy result:

Screenshot 2021-09-03 at 20 47 03

I have fixed this in #342

lhl2617 commented 2 years ago

Thank you so much for this--merging into dev and I will be checking tomorrow before releasing a new version.