Tutorial - Set up an MP3/OGG Alarm Clock using Linux

By: Federico Pistono

20 Feb 2007

Alarm Clock

Tip: you might be interested in my DVD ripping guide ^_^

Why should you spend money for an alarm clock or a software that does an equivalent job, when you can just use you Linux box? Well, most likely you use cellphone... but if you have the computer switched on all day long you can also try and do it yourself. You may already have all the software that you need to make it work, it's done by you, it's highly customisable, and above all it's more fun. This short tutorial will guide you step by step in creating an MP3/ogg alarm clock using your computer, it works with all Linux systems, and can easily be modified for MAC OS X, *BSD, well you got it.

System requirements.

Let us now begin. In order to create our alarm clock we’ll be needing the following packages:

I take that you all know how to install a package, I recommend, depending on your system, to use your distribution's repository. So if you have a debian based distro you will have to type as root something like:

apt-get install cron bash find amarok

Mandriva uses urpmi instead of apt-get, Gentoo has its beautiful emerge and so on. I strongly suggest to compile the MPlayer package yourself, and specifically from SVN. There are several reasons for this choice.

  1. The SVN package contains all the newest patches and functionalities.
  2. MPlayer runs much faster if compiled, the MPlayer developers themselves encourage to compile from source, quoting them:

    "The recommended way to install MPlayer is to compile from source. Look at the unofficial packages section of our projects page if you do not wish to compile from source and/or are looking for packages that may be more tightly integrated with your platform. Just remember that we do not support any unofficial packages except the official ones listed below."

  3. MPlayer is a fast growing project. even the smallest change in the code can mean a precious help, if you had to install a new version every time you'd go crazy, using the CVS version allows you to upgrade the package in a matter of seconds.

Issue the following command to get the latest sources:

svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer

A directory named mplayer will be created in the current directory. You can later update your sources by typing

svn update

from within that directory.

Getting started, using cron

The command crontab -l shows you the content of your personal crontab file. It may be empty, depending on your distribution. In case you are not able to modify your crontab file, just execute as root the following command

echo <your-username> >>/etc/cron.allow

this will allow you username to modify the cron events, vice versa for the /etc/cron.deny file, make sure you do not create a conflict of names in these files (user_A cannot be present in both files).

If the file is not empty something will appear, mine is like this:

#m h dom mon dow command
42 07 * * 1-5 /usr/local/bin/alarm

Let me explain what is the meaning of this line. A crontab entry consist of seven fields separated by spaces. Each field is detailed below.

  • minute – This controls what minute of the hour the command will run on, and is between 0 and 59
  • hour – This controls what hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)
  • dom – This is the Day of Month, that you want the command run on, e.g. to run a command on the 19th of each month, the dom would be 19.
  • month – This is the month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)
  • dow – This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. sun).
  • cmd – This is the command that you want run. This field may contain multiple words or spaces.

So, this crontab entry executes the command alarm at 07 42 (7:42a.m.) every day of every month that falls on Monday through Friday. Now what you need is a bash script called alarm that will actually play some music at the desired time to make sure you make up. if you want to modify you contrab entry all you have to do is to type

crontab -e

as normal user, then insert the line a posted before, according to your needs of course.

I tend to have all my music in ogg format. The same piece of music, if it's Vorbis is smaller in filesize and it sounds better than an mp3 or anything else. However, this script will work with either MP3 or ogg.

Open a terminal and type (you can just copy-paste it):

 find ~/ -iname "*.mp3" -o -iname "*.ogg" > .playlist

This will find all the music you have in your home directory. If the music is placed somewhere else simply replace the ~/ with the absolute path of your directory, you can concatenate other commands for aac or mp4 files in a similar way. A playlist called .playlist is created and place in your home directory as a hidden file, so that it does not bother you.

We have configured cron to start the command alarm, but we don't have it yet, we need a script! Open your favourite text editor (I personally use Vim, but you can use whatever you like, Emacs, Gedit, Kate, Kwrite, Scite...) and type:

#!/bin/sh

# Uncomment if you want to use amaroK
#/usr/bin/dcop /usr/bin/amarok player play

#Comment if you do not want to use MPlayer
/usr/bin/X11/xterm -display :0 -bg black -fg white \
-e /usr/local/bin/MPlayer -shuffle -playlist ~/.playlist

# Eterm solution#/usr/bin/Eterm -0 -e /usr/local/bin/MPlayer \
# -shuffle -playlist ~/.playlist

Now in a terminal:

chmod 700 alarm
mv alarm /usr/local/bin

The script has two possibilities listed. The first one is commented and it allows the amaroK player to start using the DCOP engine. The advantage of this method is that you will be able to fully use amaroK's huge potential, and not merely start some music. I will leave apart the description of all the incredible functionalities that amaroK has, because it is not the purpose of this tutorial, I will just redirect you to its website for further details.

The second one uses the omnipotent MPlayer. MPlayer is fast, works every
time and with basically every existing format, it's easy to recompile and it depends from very few packages. The script will open an xterm and then execute MPlayer in shuffle quiet mode, so that you will be able to control it once opened. Just pick one of the two possibilities, it depends from your needs, I personally know many people that the only command that starts with K on their computer is kill, others have 767 programs starting with K. Just a matter of opinion. Obviously there is no such a thing as "the perfect option", it may vary from system to system, either choose the one that suits you better or create one of your own.

You may create custom playlists, depending on your mood. This is an example of how it can be accomplished, but with some little bash knowledge you will understand that the number of possibilities is infinite. Imagine that you would like to listen to your Radiohead songs.

find ~/ -iname "*.mp3" -o -iname "*.ogg" | grep Radiohead > .playlist_radiohead

You may even automate this process and easily create custom playlists. Take this:

#!/bin/sh
TITLE="Alarm-Custom"
PGM="Alarm version 0.01"
BAND="$1"

echo ""$TITLE" "$PGM", playing "$BAND"
find ~/ -iname "*.mp3" -o -iname "*.ogg" | grep "$BAND" > ~/.playlist
/usr/bin/Eterm -O -e /usr/local/bin/MPlayer -shuffle -playlist ~/.playlist

Save the script as calarm and follow the standard procedure described above. When lanching the script you will provide one argument, that is the name of the band. When launching the program, something like thsi will appear:

¤ calarm Radiohead
calarm - Custom Alarm Alarm version 0.01, playing Radiohead

If no argument is provided, the script will run all your collection. This process is supposed to be much slower, because every time it searches for new music, but in reality... this is my benchmark.

¤ time calarm
calarm - Custom Alarm Alarm version 0.01, playing

real 0m0.133s
user 0m0.044s
sys 0m0.056s

¤ wc ~/.playlist 
 840 6878 86735 /home/phate/.playlist

This means that it took 0.1 seconds to scan and create a collection of 840 songs. Even if you have 20 thousand songs, it would take no more than three seconds.

If you’d like to learn more about the inner workings of cron read the Newbie Intro to cron and the Manpage of cron.

There is no meaning in automating the process, so I did not create a script that does all the job, as I did for the DVD ripping Guide, since most of it cannot be done independently from each person's system. Furthermore, it's more fun this way, and you get to understand what actually happens.

For further information, suggestions, bug fix, send me an email.

This tutorial was inspired by Philip McClure's article.

25 Responses to Tutorial - Set up an MP3/OGG Alarm Clock using Linux

agntyellow (not verified)

Tuesday, September 4, 2007 - 10:30   reply  quote 

Very cool, I am going to see if it wakes me up tomorrow morning. Good Luck!

Federico Pistono

Tuesday, September 4, 2007 - 13:43   reply  quote 

Thanks!

Choose a good music, it could be traumatic.

chart (not verified)

Tuesday, February 26, 2008 - 13:07   reply  quote 

Really good, I am go to watch if it wakes me up today tonight

Federico Pistono

Wednesday, February 27, 2008 - 16:39   reply  quote 

Thanks, it worked quite nicely with me. I was thinking of making an improved version with an easy-wake system (progressive raise of volume) and possibly an amaroK integration as well.

Stay tuned. ^_^

pepito (not verified)

Saturday, January 30, 2010 - 12:45   reply  quote 

Federico Pistono wrote:
Thanks, it worked quite nicely with me. I was thinking of making an improved version with an easy-wake system (progressive raise of volume) and possibly an amaroK integration as well.

Stay tuned. ^_^

Hummm... did you get this done? I am quite interested. It would also be interesting to find a way to turn the computer off/hybernate/whatever during the night and turn it on automatically in the morning to start the alarm clock. Just to give you excuses to play with something :-)

dog forums (not verified)

Monday, November 10, 2008 - 22:30   reply  quote 

Wow very neat idea federico. You are very talented with computers and technology.

Federico Pistono

Wednesday, November 12, 2008 - 20:19   reply  quote 

Thanks, the idea is not original, but I tried to give it a personal touch. ^_^

FX Trading System (not verified)

Wednesday, October 3, 2007 - 17:28   reply  quote 

Choose a bad music then it would be traumatic.

Rems (not verified)

Sunday, November 4, 2007 - 01:35   reply  quote 

FX Trading System wrote:
Choose a bad music then it would be traumatic.

Well, it also depends on how loud you play the music and what kind of music it is. Last time I used it, it was with a nice power metal song and I nearly died of a heart attack when it woke me up :) (my heart was at least at 200 beats/sec)

Carlton Houston (not verified)

Tuesday, April 15, 2008 - 04:55   reply  quote 

I don't know why "Woke up with Wood" by Z Z Top is not hard coded in. :)

WON SEO-UL (not verified)

Sunday, June 29, 2008 - 20:17   reply  quote 

Thanks for sharing the info ...
"Why should you spend money for an alarm clock" a couple $. But it will be a junk in a home..
I guess everyone has a mobile phone to use as an alarm clock :)

Thanks Again..

Federico Pistono

Monday, June 30, 2008 - 10:25   reply  quote 

Well, yes, you can use your mobile phone, just as a million other things... it was just to illustrate a possible way to use cron. In this case it was an alarm clock, it could used for just about anything :)

edinburgh accommodation (not verified)

Wednesday, October 8, 2008 - 17:50   reply  quote 

I think this article done a great job.What a best way to describe your view. Thanks for sharing with us. Really like your informative article. Hopefully we will get more interesting topic from you in future.

Federico Pistono

Thursday, October 9, 2008 - 22:27   reply  quote 

Thanks, glad to see it was useful. ^_^

Daniel (not verified)

Wednesday, December 17, 2008 - 17:39   reply  quote 

if you want an interesting sound try the THX "Deep note"
http://en.wikipedia.org/wiki/Deep_Note

biznes (not verified)

Thursday, March 5, 2009 - 15:51   reply  quote 

Yeah it was useful :) thanks :)

Federico Pistono

Saturday, March 7, 2009 - 17:21   reply  quote 

You're welcome!

seo otimização (not verified)

Thursday, April 2, 2009 - 15:24   reply  quote 

That works just great!
It took me some time to set up, but it works perfectly.
Thanks for the tutorial!!

Excellent post!

Harbin blog (not verified)

Monday, May 18, 2009 - 15:26   reply  quote 

Could you recommend a method that use the operating system - windows, I don't know how to use linux, I am from China, linux is not very popular here.

Bjorn (not verified)

Monday, November 16, 2009 - 22:54   reply  quote 

Here is an old version I made for Win3.1 (Works for XP, too. Likely works under Vista.)

It uses a batch file to run wav files. This version uses ms-windows Scheduled Tasks to set time, so it very flexible.

Alarm stops when any key is pressed (as long as Alarm selected as active window.)

The wav files provided are selected to be more and more annoying and loud. You can, of course, use your own.

As with Fede's alarm no setup/installer has been made as it is more fun/instructive to set up your self. (Also no one has needed one.)

Free download here:

http://islandnet.com/~blarsen/

The script could be re done with exact=ON, possibly allowing lower case letters in filemanes. It is setup as it is as it is old :^D

A version using standard *.bat files can be made using WPLAYANY.EXE or such--batch (*.bsh) is used as it plays WAV's directly.

Other software would allow modern file formats such as OGG, WMF, or MP3.

Enjoy!

transportadora rj (not verified)

Thursday, May 21, 2009 - 14:25   reply  quote 

Harbin,

I highly recommend to use Linux.
Windows do not works well. Try Linux, its free!

Thanks.

Moda (not verified)

Tuesday, May 26, 2009 - 15:46   reply  quote 

Linux to server, and windows to work.
really!

Dylan Hayward (not verified)

Wednesday, July 1, 2009 - 08:46   reply  quote 

Great tutorial - thank you kindly for posting this.

I encountered some minor problems so I suspect other newbs like myself might as well (using Ubuntu 9.04) - probably obvious to most but ->

1) Prefix all terminal commands with "sudo".
2) Use "chmod +rwx alarm" instead of "chmod 700 alarm".
3) MPlayer located at "/usr/bin/mplayer" not "/usr/local/bin/MPlayer" - adjust the script accordingly.
4) For whatever reason the forward slash in the alarm script didn't work. Just delete the "\" and merge the two lines together.

Modified script looks like this:

#!/bin/sh

/usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.playlist

Hope this helps someone.

Anonymous (not verified)

Wednesday, November 4, 2009 - 08:26   reply  quote 

Curious about methods people use to shut the alarm off. Thinking about trying this on a headless system. I'd probably just make the script start mpd at the specific time, and then I'll have to use some sort of mpd client along with maybe a lirc controller, but it'd be cool if the script could be "play music until any keyboard key is pressed". Obviously headless I'm not running X, but mplayer still works well for playing music.

mobiliario (not verified)

Sunday, January 24, 2010 - 17:43   reply  quote 

This is a really good discussion! It is interesting that it has spanned over a few years and is still relevant. Thanks for the viewpoints – Happy 2010!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><div>
  • Lines and paragraphs break automatically.
  • You may quote other posts using [quote] tags.

More information about formatting options


My Communities

iSupport

User login