Andrew Cooke | Contents | Latest | RSS | Previous |

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Choochoo Training Diary

Last 100 entries

© 2006-2017 Andrew Cooke (site) / post authors (content).

[Programming,Exercise] Simple Timer Script

From: andrew cooke <andrew@...>

Date: Sun, 29 Mar 2020 17:29:17 -0300

The following may be useful to someone doing exercises at home during
quarantine.  It will only work on Linux since it uses ALSA for sound.

For example

  time 1 -x 3 -d 5

will give 3 beeps, 1 second apart, 5 seconds after you hit return.

If there is a delay the start is indicated by a high-pitched beep.
The last interval is ended with a low-pitched beep.

Andrew


#!/bin/bash

function beep() {
    FREQ=$1
    TIME=$2
    ( speaker-test -Dsysdefault -t sine -f $FREQ 1> /dev/null 2> /dev/null)& pid=$! ; sleep $TIME ; kill $pid 2>&1
}

COUNT=1
DELAY=0

while (( "$#" )); do
    if [ "$1" == "-h" ]; then
	echo
	echo "timer (mainly for timing exercises)"
	echo
	echo "usage:"
	echo
	echo " $0 TIME [-x N] [-d SECS]"
	echo
	echo " -x N     repeat N times"
	echo " -d SECS  wait SECS before starting"
	echo
	exit 1
    elif [ "$1" == "-x" ]; then
	shift
	COUNT="$1"
    elif [ "$1" == "-d" ]; then
	shift
	DELAY="$1"
    else
	DURATION="$1"
    fi
    shift
done

if [ -z ${DURATION+x} ]; then
    echo "no duration (see $0 -h)"
    exit 2
fi

echo
echo "duration: ${DURATION}s"
echo "delay: ${DELAY}s"
echo "count: ${COUNT}"
echo

if [ $DELAY -gt 0 ]; then
    sleep $DELAY
    beep 2000 0.5
fi

LOOP=0
while [ $LOOP -lt $COUNT ]; do
    LOOP=$(( $LOOP + 1 ));
    echo "$LOOP"
    sleep $DURATION
    if [ $LOOP -eq $COUNT ]; then
	beep 500 0.1
    else
	beep 1000 0.1
    fi
done

Comment on this post