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.


tetris

That's really cool!

reply

morgan

Very neat, thanks :)

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

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

reply

woland

fish has some nice features, alas its not posix no compatible with any other shell

reply

bidgehop

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

reply

woland

I guess I should <touch> some grass

reply