login | register
Wed 09 of Jul, 2008 [07:37 UTC]

voip-info.org

History

Convert WAV audio files for use in Asterisk

Created by: tims,Last modification on Mon 10 of Mar, 2008 [18:48 UTC] by xtronics

Converting WAV files

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

You just recorded a fabulous audio file to use as you 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.)

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

  (Is there a way to save in this format from aacity on a Linux box?) 

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 -w -c 1 `echo $a|sed "s/.wav/.sln/"` resample -ql; done

Converting your WAV files to good GSM files is easier than you might think if you have the program Sox installed. 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 "`echo $a|sed -e s/wav//`gsm" resample -ql; done

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)

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



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

Comments

Comments Filter
222

333Converting from SLN to WAV

by 8020, Thursday 22 of February, 2007 [07:05:26 UTC]
In case anyone is interested in editing an existing SLN file, here the command to convert SLN to WAV

sox -t raw -r 8000 -s -w -c 1 {inputfile}.sln {outputfile}.wav
222

333WAV file requirements

by johnnyb, Friday 11 of March, 2005 [15:02:41 UTC]
I modified the section on creating asterisk-compatible wav files to take into account the wav file requirements as listed in the source code. The .wav format, as the source code requires it, must be of AudioFormat 1 (signed linear — that's the -s switch), Frequency 8000 (-r 8000), 1 audio channel (-c 1) and 16 bits per sample (-w).

The wav format gives MUCH BETTER sound quality, especially if you are using a high dynamic range. For instance, our answering message includes both music and voice, and GSM gets really crackely. WAV does it perfectly.
222

333Wave 16-bit Mono 8000Hz

by qagwaai, Tuesday 26 of October, 2004 [15:13:26 UTC]
Regardless of the tool or the initial format of the sound file, this seems to be a vanilla wav format which will work.
222

333Audio Format Question

by sparcusa, Thursday 16 of September, 2004 [18:24:26 UTC]
Anybody know where to find a good primer on the subject of audio quality and formats. I'm interested in getting a cursory understanding of this subject. A short explaination of which formats are best for telephone use would be helpful also.

thx
222

333GSM to Wav Conversion

by , Wednesday 08 of September, 2004 [15:11:37 UTC]
similarly u can convert a gsm file into wav

sox input.gsm -r 8000 -c 1 -w -s ouput.wav

-r for rate
-c for number of channels
-s for Signed
-w for 16 bit
222

333Conversion script

by zeta, Friday 05 of March, 2004 [18:08:27 UTC]
I wrote a very simple php script to help automate the process of converting wav to gsm. Just execute the script in the directory that you want to convert and it will recursively grab an convert your wav to gsm files. I wrote it after cutting up a 9 minute prompt from Allison.
http://zeta.subculture.org/asterisk/wavgsm.phps
Jesse Janzer