VoIPowering Your Office with Asterisk: Soothing the Savages with Hold Music

Thursday Jul 6th 2006 by Carla Schroder

Ever hated what you had to listen to on hold? Here's your chance to be the on-hold disk jockey.

One of the more fun aspects of running an Asterisk server is choosing your own hold music. Hopefully your callers are not sitting on hold for long periods of time, but as long as they are it's nice to give them something pleasant to listen to. It can be music, inspirational speeches, comedy routines—even Internet radio.

Linux comes with all the tools you need to record sound files and convert them to the Asterisk-friendly .gsm format. Audacity is an excellent sound recorder and editor that runs on Linux, Macintosh, and Windows. As fun as it is, we're not getting into sound recording today, though, just managing existing sound files.

Asterisk versions 1.2 and later includes their own music player, so you can ignore all the documentation that tells you how to add one. Asterisk can handle several different sound file formats on its own, but decoding and encoding sound files eats up CPU cycles, so it's more efficient to convert them yourself.

Converting sound files with lame sox
Using both sox and lame gives you the ability to convert just about any sound file format. sox does not directly support .gsm or .mp3, two common formats used in Asterisk. If you do not have these already installed, install them with this command on CentOS or Fedora Linux:

# yum install lame sox

You might need to fetch lame from RPMforge.net.

Spoken-word sound files can be converted to the more efficient .gsm format. To convert an .mp3 file use these commands, substituting your own filename:

$ lame --decode soundfile1.mp3 soundfile1.wav
$ sox -V soundfile1.wav -r 8000 -c 1 soundfile1.gsm resample -ql

The easy way to convert a batch of .mp3 files is to place them all in a single directory, then run these commands exactly as shown:

$ for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; done
$ for i in *.wav; do sox $i -r 8000 -c 1 $(basename $i .wav).gsm resample -ql; done

If your files are already in .wav format, just use the sox commands.

.mp3 music files should be converted to raw format for efficiency:

$ lame --decode musicfile.mp3 musicfile.wav
$ sox -V musicfile.wav -r 8000 -c 1 -w musicfile.raw

Use these commands as shown for batch conversions:

$ for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; done
$ for i in *.wav; do echo $i; sox $i -r 8000 -c 1 -w ${i%%.wav}.raw ; echo ${i%%.wav}.raw; done

Configuring Asterisk
To use your nicely converted sound files, copy them to the /var/lib/asterisk/mohmp3 directory on your Asterisk server. Then edit /etc/asterisk/musiconhold.conf:

; Music on Hold 
Asterisk will play them in sequence if you leave off the "random=yes" option.

Testing your new Hold Music
Create a test extension, using a high-numbered extension to avoid conflict with your real extensions. Add these lines to /etc/asterisk/extensions.conf:

exten => 9000,1,Answer
exten => 9000,n,SetMusicOnHold(default)
exten => 9000,n,WaitMusicOnHold(15)
exten => 9000,n,Hangup
Asterisk will answer this extension with music from the "Default" class for 15 seconds, then hang up.

Now restart Asterisk from the Asterisk CLI (see VoIPowering Your Office With Asterisk: Moving to the Grownup Version to learn about basic commands) to load the new sound files:

# asterisk -r
*CLI> restart gracefully

Now call extension 9000 and listen to your customized tunes.

Different sounds for different occasions
You may create a variety of directories containing sound files for different occasions. This is called creating music classes. Create a new sound files directory, then edit /etc/asterisk/musiconhold.conf to add the new class:

# mkdir /var/lib/asterisk/mohmp3/newclass

You can test this new class just like the default class:

exten => 9000,1,Answer
exten => 9000,n,SetMusicOnHold(newclass)
exten => 9000,n,WaitMusicOnHold(15)
exten => 9000,n,Hangup
Sound files can go into any directory, as long as you enter the correct directory in the class definition.

Using streaming audio
If you have a reliable MP3 streaming audio source, such as Internet radio, you can pipe it into your Asterisk hold music. First create an empty directory, which in this example is /var/lib/asterisk/mohmp3/streaming. Then create the class this way, using the IP address of the stream:

Of course it's never quite this simple, unless you have your own streaming audio server, because the source IP address can be long and complex. See this Nerdvittles article to learn more about hooking up Internet radio to your Asterisk server.

man sox
man lame
How to Manipulate Sound Files with SoX
The Music Frontier: Taming Streaming Audio for Music on Hold with Asterisk

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved