working on win32 uwp! and libremidi update
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
#include <libremidi/config.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
namespace libremidi
|
||||
{
|
||||
/**
|
||||
* Used to set up keyboard input.
|
||||
* Your app should pass a function that will give you a callback
|
||||
* that you should call whenever you are getting a key input (press or release).
|
||||
*/
|
||||
struct kbd_input_configuration
|
||||
{
|
||||
using scancode_callback = std::function<void(int)>;
|
||||
// First argument is on key press, second on key release
|
||||
std::function<void(scancode_callback, scancode_callback)> set_input_scancode_callbacks
|
||||
= [](scancode_callback, scancode_callback) {};
|
||||
|
||||
enum kbd_event
|
||||
{
|
||||
NOTE_0 = 0x0, // C
|
||||
VEL_0 = NOTE_0 + 128, // Set velocity to 0
|
||||
OCT_0 = VEL_0 + 128, // Set octave to 0
|
||||
OCTAVE_PLUS = OCT_0 + 128,
|
||||
OCTAVE_MINUS,
|
||||
VELOCITY_PLUS,
|
||||
VELOCITY_MINUS,
|
||||
};
|
||||
|
||||
// Default map
|
||||
//
|
||||
// ,---,---,---,---,---,---,---,---,---,---,---,---,---,-------,
|
||||
// | V0| V1| V2| V3| V4| V5| V6| V7| V8| V9|V10|V11|V12| <- |
|
||||
// |---'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-----|
|
||||
// | ->| | | C#| D#| | F#| G#| A#| | C#| D#| | F#| |
|
||||
// |-----',--',--',--',--',--',--',--',--',--',--',--',--'| |
|
||||
// | Caps | C | D | E | F | G | A | B | C | D | E | F | G | |
|
||||
// |----,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'-,-'---'----|
|
||||
// | -^ | | O-| O+| V-| V+| | | | | | | ----^ |
|
||||
// |----'-,-',--'--,'---'---'---'---'---'---'-,-'---',--,------|
|
||||
// | ctrl | | alt | |altgr | | ctrl |
|
||||
// '------' '-----'--------------------------'------' '------'
|
||||
|
||||
// clang-format off
|
||||
// Use: https://kbdlayout.info/KBDUSX/scancodes
|
||||
std::map<int, int> scancode_map{
|
||||
{ 0x1E, NOTE_0}, // C0
|
||||
{ 0x11, NOTE_0 + 1},
|
||||
{ 0x1F, NOTE_0 + 2},
|
||||
{ 0x12, NOTE_0 + 3},
|
||||
{ 0x20, NOTE_0 + 4},
|
||||
{ 0x21, NOTE_0 + 5},
|
||||
{ 0x14, NOTE_0 + 6},
|
||||
{ 0x22, NOTE_0 + 7},
|
||||
{ 0x15, NOTE_0 + 8},
|
||||
{ 0x23, NOTE_0 + 9},
|
||||
{ 0x16, NOTE_0 + 10},
|
||||
{ 0x24, NOTE_0 + 11},
|
||||
{ 0x25, NOTE_0 + 12}, // C1
|
||||
{ 0x18, NOTE_0 + 13},
|
||||
{ 0x26, NOTE_0 + 14},
|
||||
{ 0x19, NOTE_0 + 15},
|
||||
{ 0x27, NOTE_0 + 16},
|
||||
{ 0x28, NOTE_0 + 17},
|
||||
{ 0x1B, NOTE_0 + 18},
|
||||
{ 0x2B, NOTE_0 + 19},
|
||||
|
||||
{ 0x29, VEL_0 + int(0)},
|
||||
{ 0x02, VEL_0 + int(1 * 127 / 12)},
|
||||
{ 0x03, VEL_0 + int(2 * 127 / 12)},
|
||||
{ 0x04, VEL_0 + int(3 * 127 / 12)},
|
||||
{ 0x05, VEL_0 + int(4 * 127 / 12)},
|
||||
{ 0x06, VEL_0 + int(5 * 127 / 12)},
|
||||
{ 0x07, VEL_0 + int(6 * 127 / 12)},
|
||||
{ 0x08, VEL_0 + int(7 * 127 / 12)},
|
||||
{ 0x09, VEL_0 + int(8 * 127 / 12)},
|
||||
{ 0x0A, VEL_0 + int(9 * 127 / 12)},
|
||||
{ 0x0B, VEL_0 + int(10 * 127 / 12)},
|
||||
{ 0x0C, VEL_0 + int(11 * 127 / 12)},
|
||||
{ 0x0D, VEL_0 + int(127)},
|
||||
|
||||
{ 0x2C, OCTAVE_MINUS },
|
||||
{ 0x2D, OCTAVE_PLUS },
|
||||
{ 0x2E, VELOCITY_MINUS },
|
||||
{ 0x2F, VELOCITY_PLUS},
|
||||
};
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
#include <libremidi/backends/keyboard/config.hpp>
|
||||
#include <libremidi/detail/midi_in.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace libremidi
|
||||
{
|
||||
class midi_in_kbd final
|
||||
: public midi1::in_api
|
||||
, public error_handler
|
||||
{
|
||||
public:
|
||||
using midi_api::client_open_;
|
||||
struct
|
||||
: input_configuration
|
||||
, kbd_input_configuration
|
||||
{
|
||||
} configuration;
|
||||
|
||||
explicit midi_in_kbd(input_configuration&& conf, kbd_input_configuration&& apiconf)
|
||||
: configuration{std::move(conf), std::move(apiconf)}
|
||||
{
|
||||
configuration.set_input_scancode_callbacks(
|
||||
[this](int v) { on_keypress(v); }, [this](int v) { on_keyrelease(v); });
|
||||
}
|
||||
|
||||
~midi_in_kbd() override { }
|
||||
|
||||
libremidi::API get_current_api() const noexcept override { return libremidi::API::KEYBOARD; }
|
||||
|
||||
stdx::error open_port(const input_port&, std::string_view) override { return stdx::error{}; }
|
||||
|
||||
stdx::error open_virtual_port(std::string_view) override { return stdx::error{}; }
|
||||
|
||||
stdx::error close_port() override { return stdx::error{}; }
|
||||
|
||||
stdx::error set_port_name(std::string_view) override { return stdx::error{}; }
|
||||
|
||||
timestamp absolute_timestamp() const noexcept override
|
||||
{
|
||||
return std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
}
|
||||
|
||||
void on_keypress(int scancode)
|
||||
{
|
||||
using kevent = kbd_input_configuration::kbd_event;
|
||||
|
||||
auto it = configuration.scancode_map.find(scancode);
|
||||
if (it == configuration.scancode_map.end())
|
||||
return;
|
||||
|
||||
if (it->second >= kevent::NOTE_0 && it->second < (kevent::NOTE_0 + 128))
|
||||
{
|
||||
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))
|
||||
{
|
||||
m_current_velocity = it->second - kevent::VEL_0;
|
||||
}
|
||||
else if (it->second >= kevent::OCT_0 && it->second < (kevent::OCT_0 + 128))
|
||||
{
|
||||
m_current_octave = it->second - kevent::OCT_0;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (it->second)
|
||||
{
|
||||
case kevent::VELOCITY_MINUS:
|
||||
m_current_velocity = std::clamp(m_current_velocity - 10, 0, 127);
|
||||
break;
|
||||
case kevent::VELOCITY_PLUS:
|
||||
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);
|
||||
break;
|
||||
case kevent::OCTAVE_PLUS:
|
||||
m_current_octave = std::clamp(m_current_octave + 1, 0, 127);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void on_keyrelease(int scancode)
|
||||
{
|
||||
using kevent = kbd_input_configuration::kbd_event;
|
||||
|
||||
auto it = configuration.scancode_map.find(scancode);
|
||||
if (it == configuration.scancode_map.end())
|
||||
return;
|
||||
|
||||
if (it->second >= kevent::NOTE_0 && it->second < (kevent::NOTE_0 + 128))
|
||||
{
|
||||
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{3};
|
||||
int m_current_velocity{80};
|
||||
std::unordered_map<int, int> m_current_notes_scancodes;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user