💾 Archived View for tilde.team › ~woland › terminal-metronome.gmi captured on 2024-05-26 at 15:02:56. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2024-02-05)
-=-=-=-=-=-=-
So today I sat down to practice guitar and I realized that I left both my
metronomes at my sister's room. Obviously I wasn't going to get up to go and
fetch one, so I picked up a bash spell tome (man SoX) and with a little bash
magic, made a basic metronome.
Here is the actual code:
tempo () { play -n -c1 synth 0.001 sine 1000 pad $(awk "BEGIN { print 60/$1 -.001 }") repeat 999999 }
To explain how the command is constructed, 'play' is a utility that gets
installed by SoX, you can also play almost every audio format with 'play' in
your shell. 'play -n' tells SoX to generate the audio from the following
parameters and not read an input audio file, '-c1' sets the audio channel to
mono. 'synth 0.001 sine 1000' create a sine wave with the freq of 1000 and the
duration of 0.001. 'pad' creates silence between the beats and then we repeat
the pattern for 999999.
The AWK bit should be pretty much self-explanatory. We essentially calculate
the amount of silence there needs to be between repetitions, given the fact
that bash (nor any other shell) can do decimal number arithmetic, I used AWK to
perform the math.
I also wanted to make the terminal flash on beat with tput flash, but couldn’t
sync the beat with the flash for the life of me.