6.2. musiconhold.conf

Let's begin with the simplest part: setting up music-on-hold.
Allowed configuration settings are described in the sample musiconhold.conf. Additional information on MP3 music support through mpg123 may be found in doc/mp3.txt (1.4). The optimum sample rate for MP3 files is 8000 Hz, mono.
Here is the simplest possible configuration, using only one music class (default):
[default]
mode=quietmp3
directory=/var/lib/asterisk/mohmp3
Now we stop and start Asterisk, for example, using asterisk -rx "restart now", to activate the new extension and the audio streams (a reload is not sufficient). Now, if you call extension 222 (as defined above), you should hear music-on-hold (a default Asterisk installation will have have three royalty-free music files in the /var/lib/asterisk/mohmp3 directory).
Still, the following is a better configuration, for which we create another music directory moh-native in /var/lib/asterisk/:
[default]
mode=files   ; Read music files in an Asterisk native format
directory=/var/lib/asterisk/moh-native
random=yes   ; Play music files in random order
For this, it is necessary to convert the source MP3 files into an Asterisk compatible format, which can be accomplished using sox[30] and lame[31]like so:
$  lame --decode music.mp3 music.wav
$  sox -V music.wav -r 8000 -c 1 -w music.raw
$  sox -V music.wav -r 8000 -c 1 -w music.gsm
This converts our (dummy, only for demonstration) music.mp3 file into WAV, and then into RAW and GSM formats (with a sample rate of 8 kHz). We can also make versions of the file in alaw and ulaw formats (by using the .al and .ul file extensions with sox) - Asterisk will automatically choose the sound file requiring the least processing. The important common factor is that the sample rate always be 8000 Hz.

Wichtig

If you do not specify autoload in modules.conf, you must ensure that the modules for the formats you wish to use here are loaded before res_musiconhold.
For our test, we copied the WAV files provided in moh/ to moh-native/, transcoded them to RAW and GSM formats using sox, and then deleted the WAV files (which caused us problems) from the directory:
$  cd /var/lib/asterisk/moh-native/
$  for i in *.wav; do \
sox $i -r 8000 -c 1 $(basename $i .wav).raw; \
sox $i -r 8000 -c 1 $(basename $i .wav).gsm; \
done
$  rm *.wav
For CPU-critical installations (e.g., large installations), you should take time to read contrib/utils/README.rawplayer, which may be found in the Asterisk source directory.
Additional music classes may be defined. For example, in addition to [default], you might choose to classify by genre, e.g. [rock], and provide a different file path (perhaps a sub-path) where files of that genre are located. You might also classify by intended audience, particularly if you have custom files with announcements directed at specific types of customer. As always: don't forget to reload!


[30] sox (http://sox.sourceforge.net/) is available as a Debian package, and is installed thus:

$  apt-get install sox

For MacOS X, if you have installed Darwinports (http://darwinports.opendarwin.org/ or http://darwinports.com/) there is also a package:
$  port install sox
Many other distributions have sox packages; in the absence of packages, you must install it from source.
lame (http://lame.sourceforge.net/) is not available as a Debian package, so we must download and install it using the familiar ./configure, make and make install commands.

[31] For MacOS X, there is a Darwinports package (see sox):

$  port install lame
or via Fink (http://fink.sourceforge.net/):
$  fink install lame
Of course, you can do this with the FinkCommander GUI also.