From 0e3d9e93635a897932cf6a7e05ef89c1607fa598 Mon Sep 17 00:00:00 2001 From: datalore Date: Tue, 5 Aug 2025 21:18:40 +0200 Subject: [PATCH] fix(midi): Now treating NoteOn with velocity 0 as NoteOff, see README.md --- README.md | 4 ++++ mapping.go | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c13733..a97cc86 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # midi-hid This software allows mapping and translating of MIDI commands to HID inputs on Linux. + +## Known issues + +The midi library used seems to recognise NoteOff messages as NoteOn messages. However, they can still be recognised by checking the velocity, which is always 0 in NoteOff messages. A workaround has been implemented. diff --git a/mapping.go b/mapping.go index a5aa9fd..6909856 100644 --- a/mapping.go +++ b/mapping.go @@ -34,10 +34,15 @@ func (m ButtonMapping) Is(msg midi.Message) bool { func (m ButtonMapping) TriggerIfMatch(msg midi.Message, virtGamepad uinput.Gamepad) error { if m.Is(msg) { + var velocity uint8 + msg.GetNoteOn(nil, nil, &velocity) switch msg.Type() { case midi.NoteOnMsg: - log.Printf("%s: Button down\n", m.comment) - return virtGamepad.ButtonDown(m.gamepadKey) + if velocity != 0 { + log.Printf("%s: Button down\n", m.comment) + return virtGamepad.ButtonDown(m.gamepadKey) + } + fallthrough // if reached here, velocity is 0 -> NoteOff case midi.NoteOffMsg: log.Printf("%s: Button up\n", m.comment) return virtGamepad.ButtonUp(m.gamepadKey)