if ! which beep >/dev/null 2>&1; then
>&2 echo "Required beep command is missing; quitting."
exit 1
fi
if [ -z "$TEMPO" ]; then
TEMPO="90"
fi
if [ -z "$BREATH" ]; then
BREATH="100"
fi
if [ -n "$DEBUG" ]; then
echo "TEMPO=60/$TEMPO"
echo "BREATH=$BREATH ms"
fi
function note {
local note="$1"
local frequency="$2"
local duration="$3"
if [ "$duration" == "" ]; then
duration="1"
fi
if [ "$frequency" == "0" ]; then
frequency="1"
fi
milliseconds="$(bc -l <<< "( 60 / $TEMPO ) * ( $duration ) * 1000")"
milliseconds="${milliseconds%\.*}"
echo "-f $frequency -l $milliseconds -D $BREATH"
if [ -n "$DEBUG" ]; then
>&2 echo "$note ($frequency Hz) $duration ($milliseconds ms)"
fi
}
function beep_command {
local beep_cmd=""
while read note; do
if [ -z "$beep_cmd" ]; then
beep_cmd="beep $($note)"
else
beep_cmd="$beep_cmd -n $($note)"
fi
done
echo "$beep_cmd"
}
function play {
local beep_cmd=$(beep_command)
if [ -n "$DEBUG" ]; then
>&2 echo "$beep_cmd"
fi
$beep_cmd
}
# Frequencies for equal-tempered scale, A4 = 440 Hz
# "Middle C" is C4
function REST {
note "REST" "0" "$@"
}
function C0 {
note "C0" "16.35" "$@"
}
function C#0 {
note "C
}
function D0 {
note "D0" "18.35" "$@"
}
function D
note "D#0" "19.45" "$@"
}
function E0 {
note "E0" "20.60" "$@"
}
function F0 {
note "F0" "21.83" "$@"
}
function F
note "F#0" "23.12" "$@"
}
function G0 {
note "G0" "24.50" "$@"
}
function G
note "G#0" "25.96" "$@"
}
function A0 {
note "A0" "27.50" "$@"
}
function A
note "A#0" "29.14" "$@"
}
function B0 {
note "B0" "30.87" "$@"
}
function C1 {
note "C1" "32.70" "$@"
}
function C
note "C#1" "34.65" "$@"
}
function D1 {
note "D1" "36.71" "$@"
}
function D
note "D#1" "38.89" "$@"
}
function E1 {
note "E1" "41.20" "$@"
}
function F1 {
note "F1" "43.65" "$@"
}
function F
note "F#1" "46.25" "$@"
}
function G1 {
note "G1" "49.00" "$@"
}
function G
note "G#1" "51.91" "$@"
}
function A1 {
note "A1" "55.00" "$@"
}
function A
note "A#1" "58.27" "$@"
}
function B1 {
note "B1" "61.74" "$@"
}
function C2 {
note "C2" "65.41" "$@"
}
function C
note "C#2" "69.30" "$@"
}
function D2 {
note "D2" "73.42" "$@"
}
function D
note "D#2" "77.78" "$@"
}
function E2 {
note "E2" "82.41" "$@"
}
function F2 {
note "F2" "87.31" "$@"
}
function F
note "F#2" "92.50" "$@"
}
function G2 {
note "G2" "98.00" "$@"
}
function G
note "G#2" "103.83" "$@"
}
function A2 {
note "A2" "110.00" "$@"
}
function A
note "A#2" "116.54" "$@"
}
function B2 {
note "B2" "123.47" "$@"
}
function C3 {
note "C3" "130.81" "$@"
}
function C
note "C#3" "138.59" "$@"
}
function D3 {
note "D3" "146.83" "$@"
}
function D
note "D#3" "155.56" "$@"
}
function E3 {
note "E3" "164.81" "$@"
}
function F3 {
note "F3" "174.61" "$@"
}
function F
note "F#3" "185.00" "$@"
}
function G3 {
note "G3" "196.00" "$@"
}
function G
note "G#3" "207.65" "$@"
}
function A3 {
note "A3" "220.00" "$@"
}
function A
note "A#3" "233.08" "$@"
}
function B3 {
note "B3" "246.94" "$@"
}
function C4 {
note "C4" "261.63" "$@"
}
function C
note "C#4" "277.18" "$@"
}
function D4 {
note "D4" "293.66" "$@"
}
function D
note "D#4" "311.13" "$@"
}
function E4 {
note "E4" "329.63" "$@"
}
function F4 {
note "F4" "349.23" "$@"
}
function F
note "F#4" "369.99" "$@"
}
function G4 {
note "G4" "392.00" "$@"
}
function G
note "G#4" "415.30" "$@"
}
function A4 {
note "A4" "440.00" "$@"
}
function A
note "A#4" "466.16" "$@"
}
function B4 {
note "B4" "493.88" "$@"
}
function C5 {
note "C5" "523.25" "$@"
}
function C
note "C#5" "554.37" "$@"
}
function D5 {
note "D5" "587.33" "$@"
}
function D
note "D#5" "622.25" "$@"
}
function E5 {
note "E5" "659.25" "$@"
}
function F5 {
note "F5" "698.46" "$@"
}
function F
note "F#5" "739.99" "$@"
}
function G5 {
note "G5" "783.99" "$@"
}
function G
note "G#5" "830.61" "$@"
}
function A5 {
note "A5" "880.00" "$@"
}
function A
note "A#5" "932.33" "$@"
}
function B5 {
note "B5" "987.77" "$@"
}
function C6 {
note "C6" "1046.50" "$@"
}
function C
note "C#6" "1108.73" "$@"
}
function D6 {
note "D6" "1174.66" "$@"
}
function D
note "D#6" "1244.51" "$@"
}
function E6 {
note "E6" "1318.51" "$@"
}
function F6 {
note "F6" "1396.91" "$@"
}
function F
note "F#6" "1479.98" "$@"
}
function G6 {
note "G6" "1567.98" "$@"
}
function G
note "G#6" "1661.22" "$@"
}
function A6 {
note "A6" "1760.00" "$@"
}
function A
note "A#6" "1864.66" "$@"
}
function B6 {
note "B6" "1975.53" "$@"
}
function C7 {
note "C7" "2093.00" "$@"
}
function C
note "C#7" "2217.46" "$@"
}
function D7 {
note "D7" "2349.32" "$@"
}
function D
note "D#7" "2489.02" "$@"
}
function E7 {
note "E7" "2637.02" "$@"
}
function F7 {
note "F7" "2793.83" "$@"
}
function F
note "F#7" "2959.96" "$@"
}
function G7 {
note "G7" "3135.96" "$@"
}
function G
note "G#7" "3322.44" "$@"
}
function A7 {
note "A7" "3520.00" "$@"
}
function A
note "A#7" "3729.31" "$@"
}
function B7 {
note "B7" "3951.07" "$@"
}
function C8 {
note "C8" "4186.01" "$@"
}
function C
note "C#8" "4434.92" "$@"
}
function D8 {
note "D8" "4698.63" "$@"
}
function D
note "D#8" "4978.03" "$@"
}
function E8 {
note "E8" "5274.04" "$@"
}
function F8 {
note "F8" "5587.65" "$@"
}
function F
note "F#8" "5919.91" "$@"
}
function G8 {
note "G8" "6271.93" "$@"
}
function G
note "G#8" "6644.88" "$@"
}
function A8 {
note "A8" "7040.00" "$@"
}
function A
note "A#8" "7458.62" "$@"
}
function B8 {
note "B8" "7902.13" "$@"
}