From e17ba53d41e81e06d5d897a704702bd53d884afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Tue, 22 Sep 2026 01:43:50 -0400 Subject: [PATCH] keyboard: keep the computed note inside the MIDI range The octave was clamped to 0..127 while note_on takes a uint8_t, so from the default octave seven presses of OCTAVE_PLUS produced notes above 127, which truncated on the way out. Nine is the highest octave the default map fits inside the range; the note is checked as well, since the scancode map is the caller's to replace. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01PWVTQ1pm3X2Uz7goFgULtC --- include/libremidi/backends/keyboard/midi_in.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/libremidi/backends/keyboard/midi_in.hpp b/include/libremidi/backends/keyboard/midi_in.hpp index deab4caa..1c0c3c74 100644 --- a/include/libremidi/backends/keyboard/midi_in.hpp +++ b/include/libremidi/backends/keyboard/midi_in.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -54,7 +55,10 @@ class midi_in_kbd final if (it->second >= kevent::NOTE_0 && it->second < (kevent::NOTE_0 + 128)) { - int note = it->second - kevent::NOTE_0 + 12 * m_current_octave; + const int note = it->second - kevent::NOTE_0 + 12 * m_current_octave; + if (note > 127) + return; + this->configuration.on_message( libremidi::channel_events::note_on(0, note, m_current_velocity)); m_current_notes_scancodes[scancode] = note; @@ -65,7 +69,7 @@ class midi_in_kbd final } else if (it->second >= kevent::OCT_0 && it->second < (kevent::OCT_0 + 128)) { - m_current_octave = it->second - kevent::OCT_0; + m_current_octave = std::min(it->second - kevent::OCT_0, max_octave); } else { @@ -78,10 +82,10 @@ class midi_in_kbd final m_current_velocity = std::clamp(m_current_velocity + 10, 0, 127); break; case kevent::OCTAVE_MINUS: - m_current_octave = std::clamp(m_current_octave - 1, 0, 127); + m_current_octave = std::clamp(m_current_octave - 1, 0, max_octave); break; case kevent::OCTAVE_PLUS: - m_current_octave = std::clamp(m_current_octave + 1, 0, 127); + m_current_octave = std::clamp(m_current_octave + 1, 0, max_octave); break; } } @@ -106,6 +110,7 @@ class midi_in_kbd final } } + static constexpr int max_octave = 9; int m_current_octave{3}; int m_current_velocity{80}; std::unordered_map m_current_notes_scancodes;