I made a bash script to calculate bpm times

Just wrote my first script in bash so I thought I’d share it here in case it is useful for anyone else. It asks you for a bpm and then gives you odd and even divisions in milliseconds. Pretty basic but I got annoyed with having to find and open a website when I can access a drop down terminal with one key so I thought it would be a good opportunity to learn how to script something myself.

You can grab it here if you want to try it.

3 Likes

Sorry, I couldn’t resist.

BPM Calc.vcvs (8.7 KB)

I’ll see myself out :stuck_out_tongue:

1 Like

That’s a lot easier! I was actually working in Bitwig and wondered what the ballpark would be for sidechain compression times and got frustrated with how long it took to open a tab, search for it, open it, and then type in the tempo.

1 Like

I had some old bash exercises lying around and thought I’d share an adapted version of your script which goes off a command line argument (e.g. just type bpm 120 and it does its stuff) and adds some other things.

This is in no way meant to be an “oooo look at me and my fancy pants bash skillz”) thing. I’m sharing as it’s more fun to compute. :slight_smile: (I may have gone overkill with the error checking paranoia, but what the hey.)

#!/bin/bash

# function which prints some help text, if needed
helptext ()
{
	cat << STUFF
bpm: Prints common beats per minute divisions
Usage: bpm NUMBER (integer)
e.g.: bpm 120
STUFF
}

# function which checks if the suppied value is a number
numcheck ()
{
	case "$1" in
	    ''|*[!0-9]|[!\-0-9]*) echo 0 ;; # NAN
	    *) echo 1 ;;
	esac
}

# function which does the bpm division for the supplied value
# in the function the supplied value is referred to as the variable $1
do_them_divisions () {
	# uses the bc command to do float division
	ms=$(bc <<< "scale = 10; 60000.0 / ($bpm) / ($1)")

	# uses printf to format and print out the division result
	# 
	# something like:
	# 1/2:	250.000 ms
	# 
	# in printf:
	# %d prints an integer
	# \t is a tab character
	# %.3f prints a floating point number with three decimals places
	# \n is a new line character
	printf "1/%d:\t%.3f ms\n" "$1" "$ms"
}

# check if any command line arguments have been provided
# exits it not
if [[ $# -eq 0 ]]; then
    echo "No bpm entered"
    helptext
    exit 1
fi

# assigns the first command line argument to the bpm variable
# ignores any other arguments
bpm=$1

# check if bpm is a number
# exits it not
if [[ $(numcheck $bpm) -eq 0 ]]; then
    echo "bpm is not a number"
    helptext
    exit 2
fi

# prints the output (what the script originally did)

echo "EVEN"
# prints out 1
do_them_divisions 1
# prints out 2 to 16
for (( i = 2; i <= 16; i+=2 )); do
	do_them_divisions $i
done

echo "ODD"
# prints out 3 to 15
for (( i = 3; i <= 15; i+=2 )); do
	do_them_divisions $i
done

# The livin' end

There’s also a BPM Calc module or 2.

1 Like

Wow that’s really interesting and definitely how it should be done! I’ll have to study it a bit but it definitely looks like a cleaner albeit more complex way to do it.

This is in no way meant to be an “oooo look at me and my fancy pants bash skillz”) thing.

No, don’t worry! This is really useful to me to study and to see how else it can be approached so thank you for posting it.

1 Like

Story of my life. :smiley:

1 Like