Skip to content

Instantly share code, notes, and snippets.

@ryanlaws
Created December 13, 2020 08:28
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ryanlaws/3b8819edad28f2a5b8690bb269601a84 to your computer and use it in GitHub Desktop.
-- based on @okyeron's hid_demo/hid-events.lua
-- check MIDI Devices list printed below to REPL for which port to choose
-- change these to suit your needs
local midi_port_id = 1 -- especially this one
local midi_channel = 1 -- and probably this one
local midi_velocity = 100
function init()
-- print some device data to REPL
print("-------------")
print("HID Devices:")
for v in pairs(hid.devices) do
print (v .. ": " .. hid.devices[v].name)
end
print("-------------")
print("MIDI Devices:")
for v in pairs(midi.devices) do
print (v .. ": " .. midi.devices[v].name)
end
print("-------------")
end
local keyb = hid.connect()
local midi_out = midi.devices[midi_port_id]
function keyb.event(_, code, val)
-- clamp to reasonable value
local note = math.max(0, math.min(code, 127))
-- key down = note on
-- key up = note off
local midi_type = (val == 1) and 'note_on' or 'note_off'
print(midi_type..' ', note)
-- selecting method dynamically
-- so explicit MIDI port needed as first argument
midi[midi_type](midi_out, note, midi_velocity, midi_channel)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment