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.

Saturday, 28 March 2009

Acer Crystal Eye image capture

Intro
I have been idly attempting to get a utility to grab a frame from my laptop webcam (an Acer Crystal Eye).

Whilst the webcam works fine with the likes of Cheese, the act of perfoming an automated grab with the likes of CamE was unsuccessful. There is, I believe a problem with CamE, Ubuntu, the web cam and V4l2 working in harmony.

After trawling through the repositories, I finally found fswebcam which worked a treat.

Installingsudo apt-get install fswebcam
Configuring


In your home directory, create a file .fswc.confinput 0
top-banner
title "Prawn Cam"
timestamp "%d-%m-%Y %H:%M:%S (%Z)"
font /usr/share/fonts/truetype/freefont/FreeMono.ttf

Running

To demonstrate the flexibility of fswebcam, I generated a shell script to save a time-stamped file and copy the time-stamped file to another machine using scp.#!/bin/sh

TARGET_DIR="$HOME/.webcam"
OFFSITE_DIR="remote.server.name:/path/to/.webcam"
CONFIG_FILE="$HOME/.fswc.conf"

EXT="jpg"
FILE_TIME=`date +%H%M%S`
FILE_NAME=$FILE_TIME.$EXT

if [ ! -d $TARGET_DIR ]; then

mkdir $TARGET_DIR

fi

cd $TARGET_DIR

fswebcam -c $CONFIG_FILE --save $FILE_NAME
scp $FILE_NAME $OFFSITE_DIR/$FILE_NAME


That's it. Setup crontab to run the task as frequently as you wish.crontab -e

*/1 * * * * /usr/local/bin/webcap.sh
Will run the task every minute. (A little extreme, perhaps).