[asterisk-users] play alaw file with .wav extension

Steve Edwards asterisk.org at sedwards.com
Wed Jul 21 14:45:48 CDT 2010


Un-top-posting and trying to regurgitate into a cohesive thread...

On Wed, 21 Jul 2010, Quy Pham Sy wrote:

> I have to play a alaw file with .wav ext. How can I do this?

On Wed, 21 Jul 2010, Danny Nicholas wrote:

> Asterisk won’t be “happy” trying to play foobar.wav if it is actually a 
> .alaw file. Since you can’t rename the existing files, there’s no law 
> that says you can’t copy them and play them correctly. Assuming that 
> your calls are using the alaw codec, this snippet would do the trick
>
> Exten => 1234,1,answer
> Exten => 1234,n,System(/bin/cp foobar.wav /tmp/foobar.alaw)
> Exten => 1234,n,playback(/tmp/foobar)
> Exten => 1234,n,System(/bin/rm /tmp/foobar.alaw)

On Wed, 21 Jul 2010, Kevin P. Fleming wrote:

> No, that won't work either, because a WAV file has a header, and a raw 
> alaw file does not... so Asterisk will try to play the contents of that 
> header as alaw data, presumably producing terrible noise.
>
> The best you can do is to use sox to convert them from 
> alaw-in-WAV-container to raw-alaw.

On Wed, 21 Jul 2010, Quy Pham Sy wrote:

> >> Exten => 1234,n,System(/bin/cp foobar.wav /tmp/foobar.alaw)
>
> it actually works, I made a link to the .wav file instead of copying it 
> ln -s foobar.wav foobar.alaw, and it works well.
>
> My .wav files are alaw file indeed. Here is the output from file command
>
> $file 53.wav
> 53.wav: RIFF (little-endian) data, WAVE audio, ITU G.711 A-law, mono 
> 8000 Hz
>
> they've just named as xxx.wav so I guess there is no problems with 
> copying or linking solutions.

It only "appears" to be working because you can't hear the problem.

Your files are not "mis-named," they are formatted in a way that Asterisk 
doesn't handle. Asterisk understands A-LAW encoding, just not when it's in 
a WAV "container."

(There is no such thing as an "alaw file." You may be thinking of a
raw (header-less) file containing A-LAW encoded data.)

By "tricking" Asterisk into playing the file as a header-less file, 
Asterisk is processing the WAV header as A-LAW encoded data. A WAV file 
has a 44 byte header. An A-LAW sample is 1 byte (not real sure about 
that). The sample rate is 8,000 per second. The 44 "samples" are played in 
about 1/200th of a second so you don't hear the "noise" at the beginning 
of the file.

You can create an "A-LAW in WAV" file using:

 	sox\
 		/var/lib/asterisk/sounds/demo-congrats.wav\
 		-A\
 		-t wav\
 		alaw-in-wav.wav

If you extract the header using:

 	dd\
 		bs=44\
 		count=1\
 		if=alaw-in-wav.wav\
 		of=header.wav

And then concatenate a bunch of them:

 	for ((IDX = 0; IDX < 200; ++IDX))
 	do
 	cat header.wav
 	done >noise.alaw

And then convert this into a "more normal" WAV file:

 	sox -t al noise.alaw -s -w noise.wav

You can play this in most audio players and hear about 1 second of a not 
too annoying buzz.

The "proper" way to handle this would be to enhance 
format_wav.c/format_wav_gsm.c to handle A-LAW encoded data.

Another approach would be to write an AGI (playback-alaw-in-wav?) to 
"wrap" the create a link, play the file, delete the link band-aid. You 
could do in dialplan, I just prefer writing code where I have more 
flexibility and better error handling.

-- 
Thanks in advance,
-------------------------------------------------------------------------
Steve Edwards       sedwards at sedwards.com      Voice: +1-760-468-3867 PST
Newline                                              Fax: +1-760-731-3000


More information about the asterisk-users mailing list