Upgrade 3CX to v18 and get it hosted free!

Convert WAV audio files for use in Asterisk

Author image

Converting WAV files

You just recorded a fabulous audio file to use as your main voice menu. Then you realize that Asterisk does not use WAV format audio for the Playback or Background applications. So what do you do? How can you convert your WAV files into GSM files that still have good sound quality? (This is partially false, Asterisk can play anything it has a format and codec for, including some wav files. See below.)

What is Asterisk

Asterisk is an open source framework for building your own communication applications. It equips your business with a comprehensive PBX solution and can be run on Linux, BSD and OS X. It requires sufficient Linux and telephony knowledge for setup, but it pays off with a number of sophisticated features such as automatic call distribution, IVR systems, codec translation among others.

Asterisk 1.4

In asterisk 1.4 is a conversion application built in, Asterisk file convert.
The command converts between different code formats.

file convert <file_in.format> <file_out.format>

A shell script example:

    #!/bin/bash
    # Converts a audio file from alaw to a ulaw
    rasterisk -x "file convert /tmp/file_in.alaw /tmp/file_out.ulaw"

Convert files from the CLI

Note the differences!

gsm: raw gsm encoding, good for VoIP
wav: MS wav format, 16 bit linear
WAV: MS wav format, gsm encoded (wav49)

Converting to sln format

Starting from Asterisk 1.2.0, the .sln (SLINEAR) format seems to be the preferred format.
To convert wav file to sln, use the following command:

    sox foo-in.wav -t raw -r 8000 -s -w -c 1 foo-out.sln

Note that sox v14.3.0 and above (installed in Ubuntu 9.10), the -w option has changed to -2

    sox foo-in.wav -t raw -r 8000 -s -2 -c 1 foo-out.sln

If you have a directory full of .wav files to convert, try this command. It uses sed to automatically rename the files with the .sln extension (assuming incoming wav files at a sample rate other than 8khz.)

 for a in *.wav; do  sox "$a" -t raw -r 8000 -s -c 1 ${a/.wav/.sln/} -q; done

Converting your WAV files to good GSM files is easier than you might think if you have the program sox installed (on Debian systems the libsox-fmt-gsm package is required in addition to sox). From the shell prompt, enter this command:

    sox foo.wav -r 8000 foo.gsm resample -ql

and hit the <ENTER> key. Note that the sox option ‘-ql’ (lower case L) modifies the resample option. It is not a number one (1). In a few moments you will have a new GSM format file in the same directory as the original WAV file. In this example “foo.wav” is your main voice menu audio file in WAV format, and “foo.gsm” is the same file converted to GSM format. If you wanted to, you could use “main-voice-menu.gsm” as the name in place of “foo.gsm”: what matters here is the second file name you use in this command ends in “.gsm”.

If your WAV file was in stereo, add the -c1 option to convert to mono, or the output will sound very strange.

    sox foo.wav -r 8000 -c1 foo.gsm resample -ql

You may get better results if you record your WAV file in 16 bit 8000 Hz mono and then run

    sox foo.wav foo.gsm

If you have multiple WAV files in one directory and you want to convert them all, use this command:

    for a in *.wav; do sox "$a" -r 8000 -c1 ${a/.wav/.gsm} resample -ql; done

You can also put a bash script in /usr/bin and name it wav-gsm-convert. The content can be like this

#!/bin/bash
wav_file=$1
gsm_file=${1/.wav/.gsm}
if [ $# -ne 1 ]; then
    echo "Usage: $0 filename.wav"
    exit
fi
if [ ! -f $wav_file ]; then
    echo "WAV file is missing"
    exit 1
fi
if [ -f $gsm_file ]; then
    echo -en "The destination file $gsm_file is existing\nDo you want to remove it(Y/N): "
    read ans
    if [[ "$ans" =~ /Yy/ ]]; then
            rm -f $gsm_file
    fi
fi
sox -t wav $wav_file -r 8000 -c1 -t gsm $gsm_file resample -ql

Next, move your new foo.gsm file to the directory: /var/lib/asterisk/sounds

Now you can easily use the applications Playback and Background in your extensions.conf file to play your fabulous main voice menu. For example:
exten => s,1,Background(foo)
or
exten => s,1,Background(main-voice-menu)
or
exten => s,1,Playback(foo)
or
exten => s,1,Playback(main-voice-menu)

Playing .sln files from the command line

You can play sln files using sox from the command line (play is part of sox):

    play -t raw -r 8000 -s -w -c 1 file.sln

Using WAV files

Asterisk has codecs for wav (pcm), gsm, g729, g726, and wav49, all of which can be used for Playback and Background. However, Asterisk does not understand ADPCM WAV files. To convert your WAV files to a format which Asterisk can understand, use the following command:

    sox foo-in.wav -r 8000 -c 1 -s -w foo-out.wav resample -ql

Note that sox v14.3.0 and above (installed in Ubuntu 9.10), resample is no longer used, remix is used or leave it as:

   sox foo-in.wav -r 8k -c 1 -s -w foo-out.wav

All so if your using sox v14.3.foo and above and you are getting check_header errors with play back try this:

   sox foo-in.wav -r 8k -c 1 -e gsm foo-out.wav

This also works converting mp3, just make sure you have libsox-fmt-mp3 installed.

Normalizing volume and reducing volume fluxuations

Using the sox command’s “compand” filter, you can reduce or eliminate flutters in volume level, and you can normalize the volume of your sound files. This is called dynamic range compression. The effect is desirable for a PBX system where changes in volume, or voices that are too quiet to understand would be considered unprofessional. It is important to perform the compand effect BEFORE you resample it as to preserve as much quality as possible.

An example command to perform some appropriate dynamic range compression and normalization is shown below:

    sox "foo-in.wav" -r 8000 -c1 "foo-out.gsm" lowpass 4000 compand 0.02,0.05 -60,-60,-30,-10,-20,-8,-5,-8,-2,-8 -8 -7 0.05 resample -ql

In this example, foo-in.wav is a 16-bit mono 44khz uncompressed pcm WAV file.

Converting WAV files to MP3

Here is a bash script that can take all of your call recordings in /var/spool/asterisk/monitor and convert them to MP3 files with identical timestamps and they are compatible to be played back from Asterisk.

Converting to a CD writable format

So, you’ve decided to do your call recording in GSM format as you don’t care about quality and you don’t want to stuff your disks full, but how do you write that file to an audio CD to send to somebody who wants to listen to the call?

    sox infile.gsm -r 44100 -a outfile.wav

creates a file in a format Nero can write to CD. There are probably better ways of doing it, but it works for me!

See also


Back to Asterisk tips and tricks


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.