Skip to content

Commit

Permalink
[keyboard] Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 29, 2024
1 parent 9b358a4 commit ec27952
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions include/libremidi/backends/keyboard/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <libremidi/detail/midi_in.hpp>

#include <chrono>
#include <unordered_map>

namespace libremidi
{
Expand All @@ -21,7 +22,7 @@ class midi_in_kbd final
explicit midi_in_kbd(input_configuration&& conf, kbd_input_configuration&& apiconf)
: configuration{std::move(conf), std::move(apiconf)}
{
apiconf.set_input_scancode_callbacks(
configuration.set_input_scancode_callbacks(
[this](int v) { on_keypress(v); }, [this](int v) { on_keyrelease(v); });
}

Expand Down Expand Up @@ -52,7 +53,10 @@ class midi_in_kbd final

if (it->second >= kevent::NOTE_0 && it->second < (kevent::NOTE_0 + 128))
{
libremidi::channel_events::note_on(0, it->second - kevent::NOTE_0, m_current_velocity);
int note = it->second - kevent::NOTE_0 + 12 * m_current_octave;
this->configuration.on_message(
libremidi::channel_events::note_on(0, note, m_current_velocity));
m_current_notes_scancodes[scancode] = note;
}
else if (it->second >= kevent::VEL_0 && it->second < (kevent::VEL_0 + 128))
{
Expand All @@ -73,10 +77,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 - 10, 0, 127);
m_current_octave = std::clamp(m_current_octave - 1, 0, 127);
break;
case kevent::OCTAVE_PLUS:
m_current_octave = std::clamp(m_current_octave + 10, 0, 127);
m_current_octave = std::clamp(m_current_octave + 1, 0, 127);
break;
}
}
Expand All @@ -92,11 +96,17 @@ class midi_in_kbd final

if (it->second >= kevent::NOTE_0 && it->second < (kevent::NOTE_0 + 128))
{
libremidi::channel_events::note_off(0, it->second - kevent::NOTE_0, 0);
if (auto note_it = m_current_notes_scancodes.find(scancode);
note_it != m_current_notes_scancodes.end())
{
this->configuration.on_message(libremidi::channel_events::note_off(0, note_it->second, 0));
m_current_notes_scancodes.erase(note_it);
}
}
}

int m_current_octave{2};
int m_current_octave{3};
int m_current_velocity{80};
std::unordered_map<int, int> m_current_notes_scancodes;
};
}

0 comments on commit ec27952

Please sign in to comment.