Last active
July 7, 2020 03:36
-
-
Save llimllib/137c587eba2ce9b0eff2c9af4d0a5e29 to your computer and use it in GitHub Desktop.
A script to use the first knob on my novation keyboard to control my mac's volume
This file contains hidden or 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
-- This is a hammerspoon (http://www.hammerspoon.org/) script to hook the | |
-- volume knob on my keyboard up to my system's volume control | |
-- https://easings.net/ is great for easing functions | |
-- I tried easeInExpo, but it was too sharp (return 2 ^ (10 * x - 10)) | |
-- experimentation suggested cubic was the most pleasant. Linear was expectedly | |
-- awful | |
function easeIn(x) | |
return x * x * x | |
end | |
function onMidiMessage(object, deviceName, commandType, description, metadata) | |
-- hs.alert.show(deviceName .. commandType) | |
if commandType == "controlChange" then | |
-- print(hs.inspect(metadata)) | |
if metadata["controllerNumber"] == 21 then | |
-- 16256 is the max value for reasons that are unclear to me | |
-- it's the first seven bytes of a fourteen-bit number: 11111110000000 | |
-- I suppose I should try to understand midi or something | |
local vol = easeIn(metadata["fourteenBitValue"] / 16256) * 100 | |
if vol < 0.00001 then | |
print("muting") | |
hs.audiodevice.defaultOutputDevice():setMuted(true) | |
else | |
print("volume "..vol) | |
hs.audiodevice.defaultOutputDevice():setMuted(false) | |
hs.audiodevice.defaultOutputDevice():setVolume(vol) | |
end | |
end | |
elseif commandType == "noteOn" then | |
-- This lets you mash any of the drum pad buttons to play/pause | |
-- spotify, could be more specific but why I guess | |
if metadata["channel"] == 9 then | |
-- print("play/pause") | |
-- send a play/pause keypress. I got 205 from | |
-- karabiner-eventviewer, but it seems like this isn't a legit | |
-- keypress so mac just ignores it | |
-- | |
-- hs.eventtap.event.newKeyEvent(205, true):post() | |
-- hs.eventtap.event.newKeyEvent(205, false):post() | |
-- | |
-- so, since computers are awful, just play/pause spotify. There is | |
-- hs.itunes, I don't use that, and screw Apple. | |
hs.spotify.playpause() | |
end | |
end | |
end | |
local devices = hs.midi.devices() | |
device = nil | |
for key, deviceName in pairs(devices) do | |
if deviceName.find(deviceName, "Launchkey") then | |
hs.alert.show(deviceName) | |
device = hs.midi.new(deviceName) | |
device:callback(onMidiMessage) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment