Photo by mali maeder from Pexels

Turn Your Keyboard into a Soundboard

Jan-Hendrik Kuperus

--

Are the only sounds on your office floor the random rickety-clicks of your colleagues’ keyboards? Are meetings dull and devoid of humor? Then pick yourself up, find some sound fragments and start bringing a new vibe to your work floor. (And probably annoy the heck out of some of your co-workers 😇)

Back when I started playing with Hammerspoon, I browsed its entire API to see what things I could do with it. My eyes fell on the category hs.sound and I knew at that moment I had to build a soundboard. Once I had setup my Hyper-key and several really useful key-bindings, I went to work.

Not long after, I could complete a co-worker’s pun with a “BA-DUM-TISS” sound at the press of a button. Or end a meeting with the Warcraft’s Peon saying “WORK WORK”. My personal favourites were a piece of completely arbitrary “elevator music” and the introduction of “Driving home for Christmas”.

The Setup

Trust me when I say, the code is not the hard part. The hard part is finding good audio fragments to use. If you’re looking for inspiration, have a look the My Instants website. They have a brilliant collection of small sound snippets from various movies, songs and even games.

Once you’ve collected some sounds, you can start building your soundboard with Hammerspoon. To keep things manageable, the mappings between keys and sound-files are stored in a table. This table is then read by a function, binding the sound-file to the specified key. Here’s a part of my setup:

local soundBoard = {
badumtiss = { key = '1', file = 'badumtiss.mp3' },
metalgearsolid = { key = '2', file = 'metalgearsolid.mp3' },
winError = { key = '3', file = 'windows-error.mp3' },
sadtrombone = { key = '4', file = 'sadtrombone.mp3' },
crickets = { key = '5', file = 'crickets.mp3' },
batman = { key = '6', file = 'batman-transition.mp3' },
shocked = { key = '7', file = 'shocked.mp3' },
inception = { key = '8', file = 'inceptionbutton.mp3' },
haGay = { key = '9', file = 'ha-gay.mp3' },
workwork = { key = '0', file = 'wc-work-work.mp3' },
workComplete = { key = '-', file = 'wc-work-complete.mp3' },
ns = { key = '=', file = 'ns-ding-dong.mp3' },
-- ... many more here
}
-- Initialise Soundboard!
for n,s in pairs(soundBoard) do
-- Change the path here to where your sound files are stored:
s.sound = hs.sound.getByFile(os.getenv("HOME") .. '/.hammerspoon-assets/soundboard/' .. s.file)
-- Bind to Hyper+Cmd+s.key
hyper.bindKeyWithModifiers(s.key, {'cmd'}, function()
s.sound:stop()
s.sound:play()
end)
end

The table in my file holds entries for almost all keys, but the bulk of them have been omitted here, because the idea is illustrated well enough in this sample. The for-loop below it (where it says -- Initialise Soundboard!) loops over this table and binds the specified key to a function that plays the given sound. Make sure you either put the sounds in the path specified (~/.hammerspoon-assets/soundboard/) or adjust the path accordingly.

If you place this snippet in your ~/.hammerspoon/init.lua you will now be able to play sounds by pressing Hyper+⌘+<anything>. Please remember the above snippet references hyper, which is the hyper key created in an earlier post. If you don’t use the Hyper key, change the hyper.bindKeyWithModifiers call to your appropriate key-combination.

A fun thing to note with this soundboard is the fact that Hammerspoon is able to play a lot of sounds simultaneously. If you press different key-combo’s quickly after one another, they will start playing together. That is a particularly fun way to use this soundboard if you are skilled in putting samples together. You might just create your next dubstep-hit with just the keyboard of your Mac.

Have fun annoying people around you! 😇🙊

— JH

--

--

Jan-Hendrik Kuperus

Hi! I’m the Founder and Director of Yoink. I love writing code, tweaking it, beautifying it. I'm an all-round coder and a Professional Amateur Baker 😁🎂