Asterisk

Converting Audio Files for Asterisk: Formats, sox and Sample Rates

Himanshu Pal

Himanshu Pal

Why a perfectly good file will not play

You record a greeting, drop it into the sounds directory, and it plays as static, plays at the wrong speed, or does not play at all. Nothing is wrong with the file — it is simply not in a form Asterisk can use.

Asterisk does not decode arbitrary media. It expects audio at telephony parameters, and anything else must be converted first. The requirements are narrow and worth memorising: 8000 Hz sample rate, mono, 16-bit for standard narrowband prompts.

A typical file from a phone or editing program is 44.1 kHz stereo. Handed to Asterisk unconverted, the sample-rate mismatch is what produces chipmunk-speed or garbled playback.

Converting with sox

sox (Sound eXchange) is the standard tool. To produce a GSM file — compact and widely used for prompts:

sox inputfile.wav -r 8000 -c 1 outputfile.gsm resample -ql

-r 8000 sets the sample rate, -c 1 forces mono, and resample -ql controls resampling quality. The same thing expressed with an explicit output type:

sox -t wav input.wav -r 8000 -c1 -t gsm output.gsm resample -ql

For raw signed linear (SLN), which Asterisk handles natively with no decoding cost:

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

Watch the version difference here — it catches people out. In sox 14.3.0 and later the -w option became -2:

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

If a command copied from an older guide fails with an unrecognised option, this is almost certainly why. Check with sox --version.

Choosing a format

  • sln (signed linear) — uncompressed and native, so Asterisk plays it with no transcoding at all. Largest files, lowest CPU. The right choice for frequently played prompts on a busy system.
  • gsm — compact with acceptable quality, long the conventional choice for prompt libraries.
  • wav — convenient and editable, fine for small numbers of prompts.
  • ulaw / alaw — match G.711 exactly, so no transcoding is needed when the call is already using G.711.

The general principle: store prompts in the format your calls actually use, and you avoid transcoding on every playback. On a system playing an IVR greeting to hundreds of concurrent calls, that difference is measurable.

A practical trick is to keep the same prompt in several formats with the same base name — greeting.gsm, greeting.ulaw, greeting.sln. Asterisk picks whichever needs least conversion for the current call. Note that you reference prompts without the extension in the dialplan:

exten => s,1,Playback(greeting)

Where files go

Custom sounds belong in Asterisk's sounds directory, conventionally /var/lib/asterisk/sounds (with language subdirectories such as en/ on most installs). Keep your own recordings in a custom/ subdirectory so a package upgrade cannot overwrite them, and reference them as custom/greeting.

Check ownership after copying files in — a prompt owned by root that the Asterisk user cannot read fails silently, which is a genuinely annoying way to lose an afternoon:

chown asterisk:asterisk /var/lib/asterisk/sounds/en/custom/*
chmod 644 /var/lib/asterisk/sounds/en/custom/*

Recording well in the first place

Conversion cannot add quality that was never captured. If you are recording source material, capture at 8 kHz and 16 bits rather than 8 bits — the extra bit depth survives conversion noticeably better.

Better still, record at a high rate in a quiet room and downsample once. Resampling from clean 44.1 kHz source produces a better 8 kHz file than recording directly at 8 kHz on poor equipment. Normalise levels across a prompt set too, so callers do not get a quiet menu followed by a loud announcement.

Music on hold

MoH follows the same rules but has an extra consideration: it plays continuously to every held caller, so transcoding cost multiplies. Convert MoH to a native format and it becomes essentially free to play.

Also, obviously but frequently forgotten — hold music needs a licence. Commercial recordings are not free to play to your callers, and this is a real source of trouble for businesses.

Frequently asked questions

What format does Asterisk need for prompts?

8000 Hz, mono, 16-bit for narrowband. GSM, sln, wav, ulaw and alaw are all usable; sln and ulaw avoid transcoding entirely.

Why does my prompt play too fast or sound garbled?

A sample-rate mismatch — usually a 44.1 kHz file played as if it were 8 kHz. Convert it with -r 8000 -c 1.

Why does sox reject the -w option?

From sox 14.3.0 onward, -w was replaced by -2 for 16-bit output. Older guides still show -w.

Why does my prompt not play at all?

Check file permissions first — the Asterisk user must be able to read it, and a permission failure is silent. Then confirm you referenced the name without its file extension.