💾 Archived View for midnight.pub › posts › 1513 captured on 2023-09-28 at 15:51:55. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-09-08)

➡️ Next capture (2023-11-04)

-=-=-=-=-=-=-

Midnight Pub

Terminal Metronome

~woland

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.

Write a reply

Replies

~morgan wrote (thread):

Very neat, thanks :)

Fish shell does floating point arithmetic, for what it's worth ;)

https://fishshell.com/docs/current/cmds/math.html

~bidgehop wrote (thread):

This is what happens when you drink too much beer at the pub, go home drunk and sit in front of the keyboard with beer flowing in your veins where you prefer writing a bash script to walking to your sister's room