Tuesday, 31 March 2009

Simple CLI alarm clock using crontab and mplayer

Well, it had to be done, I suppose. I fancied an alarm clock which would wake me up with a music file of my choice and which would start off quietly and gradually get louder.

Getting started


In order to get the volume to increase gradually from the command line, you need to install aumix. If you don't have mplayer installed, then install it as well unless you wish to use something else.sudo apt-get install aumix mplayer

Volume Script


Next, create a script /usr/local/bin/wakeup-volume to increase the volume over time. Amend the sleep intervals and increments of I to suit.
#!/bin/bash
for((I=1;I<=100;I+=1)); do
/usr/bin/aumix -v$I -p100 -w100
sleep 5
done

Playing Script


Then create a script /usr/local/bin/alarm-clock and select your command line and music file you wish to wake up to. I am using mplayer and looping the music file five times in this example.

This script kills all wakeup-volume processes and starts a new wakeup-volume process before playing your tune of choice.
#!/bin/bash
PLAYER=/usr/bin/mplayer
SONG="$HOME/Music/alarm clock song.ogg"
killall wakeup-volume
/usr/local/bin/wakeup-volume&
$PLAYER -loop 5 "$SONG"
Last of all, set up crontab to wake you up. I have set mine to wake me up at 06:59 Monday to Friday.crontab -e59 6 * * 1-5 /usr/bin/X11/xterm -display :0 \
-bg black -fg green -e /usr/local/bin/alarm-clock
Note: crontab expects the above to be on one line. I have put a continuation escape character in because I don't have enough width on this here blog.

Don't forget to set execute rights on the two scripts...sudo chmod 755 /usr/local/bin/alarm-clock
sudo chmod 755 /usr/local/bin/wakeup-volume

Alarm clock in action



Further Reading:

mplayer documentation,
man aumix.

Sweet dreams.

No comments: