#!/bin/sh

# Plays an approximation of a GRUB tune (beeps) from within Linux.
# Needs sox, play (from sox), and bc (calculator)
# Initial idea from:
#  Ubuntu Forums post
#  Title "GRUB2 and previewing the GRUB_INIT_TUNE"
#  Posted 2011 APR 26 by user "Mozai"
#  https://ubuntuforums.org/showthread.php?t=1739495
#  Retrieved 2016 DEC 30


if [ $# -lt 3 ]; then
	echo "$0: Error: Insufficient number of arguments" >&2
	echo "$0: Usage: Specify tempo, followed by one-or-more pairs of frequency and duration." >&2
	echo "$0: Example: $0 480 440 1" >&2
	exit 1
fi

tempo=$1; shift

while [ -n "$*" ]; do
	freq=$1; shift
	dur=$1; shift 
	dur=$(echo "$dur*(60/$tempo)"|bc -l)
	# -V0 to hide warnings about clipping/volume
	# have to specify format and parameters for sox and play, since this is a pipe
	sox -V0 -e u-law -r 8000 -n -t raw - synth $dur sine $freq
done | play -q -e u-law -t raw -c1 -r 8000 -

