Created
March 31, 2020 13:09
-
-
Save fred-stripe/03fd08d126d6ab7808c4216461e18b4c to your computer and use it in GitHub Desktop.
lpd8 + hammerspoon.midi -> new buttons for your computer machine https://images.reverb.com/image/upload/s--TanFLw05--/t_card-square/v1571420337/zzyfza1vf8wsxjynymgr.jpg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A quick hammerspoon to use the buttons on my Akai LPD8 | |
-- which is a pretty fun little drum pad that can be had | |
-- for $40-50 bucks if you shop around: | |
-- https://images.reverb.com/image/upload/s--TanFLw05--/t_card-square/v1571420337/zzyfza1vf8wsxjynymgr.jpg | |
devices = hs.midi.virtualSources() | |
lpd8 = hs.midi.new(devices[1]) | |
held = {} | |
-- row,col mapping of the 8 drum pads on the lpd-8 | |
-- I prefer to use program 3 which by default is | |
-- this scale. | |
pads = { | |
r1c1=67, r1c2=69, r1c3=71, r1c4=72, | |
r2c1=60, r2c2=62, r2c3=64, r2c4=65, | |
} | |
--[[ TODO: there are also 8 parameter knobs that could be mapped ]]-- | |
_noteToPad = {} | |
for pad, note in pairs(pads) do | |
_noteToPad[note] = pad | |
end | |
-- debug utility | |
function aa(fmt, ...) | |
local args = {...} | |
hs.alert.show(string.format(fmt, table.unpack(args))) | |
end | |
function noteToPad(note) | |
--aa("%s, %s", note, _noteToPad[note]) | |
return _noteToPad[note] | |
end | |
-- event name -> pad -> {handler, handler, …} | |
handlers = { | |
padPressed={ | |
r1c1={(function (event) | |
hs.alert.show(event.pad) | |
end)} | |
} | |
} | |
function dispatch(evt) | |
handlers[evt.event] = handlers[evt.event] or {} | |
handlers[evt.event][evt.pad] = handlers[evt.event][evt.pad] or {} | |
for target, handler in ipairs(handlers[evt.event][evt.pad]) do | |
handler(evt) | |
end | |
end | |
lpd8:callback(function (object, deviceName, commandType, description, metadata) | |
held[deviceName] = held[deviceName] or {} | |
local note, ts = metadata.note, metadata.timestamp | |
if commandType == 'noteOff' then | |
held[deviceName][note] = false | |
dispatch({event='padPressed', pad=noteToPad(note), when=ts}) | |
elseif commandType == 'noteOn' then | |
held[deviceName][note] = ts | |
end | |
for device, notes in pairs(held) do | |
local keyset = {} | |
for k,v in pairs(notes) do | |
if notes[k] then | |
keyset[#keyset + 1] = k | |
end | |
end | |
hs.alert.show(string.format("◊%s → %d [%s]", device, #notes, table.concat(keyset, ","))) | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment