January 12, 2022
Building a one-line metronome in the OSX terminal
#!/bin/zsh
# my one line metronome
BPM=120 && while do; printf '\a' && sleep `bc <<< "scale=4; 60/${BPM}"`; done;
I recently was working on a project where I needed to break out an ASCII table. If you're not familiar, it's a table that takes letters, numbers, and common punctuation, and assigns them number values, either decimal or hexidecimal.
For example; the capital letter N is represented as the decimal value 78, or the hexidecimal value 4E.
The Bell Code
In addition to the standard letters or numbers you expect, there's a series of control characters, each used in the early days of telecommunication - but not too much anymore. One of those control characters is called a bell code - lucky number 7 in the decimal system.
This code was used to trigger a machine to ring a small bell, alerting an operator on the other end of the line.
Turns out, on POSIX systems like OSX, you can enter:
printf '\a'
in the terminal, and hear a mac's version of a bell. There's not too much we can do with this, but since my terminal's bell kind of sounds like a click - I want to build a one-line metronome.
My One Line Metronome
Here's my one-line metronome, tested with zsh and OSX.
#!/bin/zsh
BPM=120 && while do; printf '\a' && sleep `bc <<< "scale=4; 60/${BPM}"`; done;
Simply change the BPM value at the start and paste it into your OSX terminal to hear the standard error sound played at the beats per minute desired.
More or less useless, but undeniably nerdy.
Let's Work Together
I help organizations solve complex technical challenges, in the simplest ways possible.