Skip to content

Instantly share code, notes, and snippets.

@marcinantkiewicz
Forked from fred-stripe/lpd8.lua
Last active March 29, 2023 05:07
Show Gist options
  • Save marcinantkiewicz/fffc606f3ecef01bc2b47e2fe611dc21 to your computer and use it in GitHub Desktop.
Save marcinantkiewicz/fffc606f3ecef01bc2b47e2fe611dc21 to your computer and use it in GitHub Desktop.
https://github.com/Hammerspoon/hammerspoon/discussions/3379
https://www.guerrilladigital.cc/2021/02/18/no-elgato-stream-deck-no-problem-i-reprogrammed-my-akai-lpd8-to-work-as-an-obs-scene-switcher/
-- 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