Convert Audio Files for Asterisk
Convert Audio Files for Asterisk
For traditional narrowband telephony, Asterisk most commonly uses 8 kHz, 16-bit, mono PCM (.wav or .sln). It also supports wideband and other codecs: 16 kHz signed-linear (.sln16), G.722 (.g722), GSM (.gsm), mu-law/A-law (.ulaw/.alaw), and more. Convert to the format that matches what your endpoints actually use.
Single file with sox
# WAV to Asterisk-native WAV (8kHz mono)
sox input.wav -r 8000 -c 1 -b 16 output.wav
# WAV to GSM (smaller files, lower quality)
sox input.wav -r 8000 -c 1 output.gsm
# MP3 to WAV
sox input.mp3 -r 8000 -c 1 -b 16 output.wav
If sox complains about MP3 support, install the libsox-fmt-mp3 package (Debian/Ubuntu) or use ffmpeg instead.
Single file with ffmpeg
# Any format to Asterisk WAV (narrowband, 8kHz)
ffmpeg -i input.mp3 -ar 8000 -ac 1 -sample_fmt s16 output.wav
# 16 kHz signed-linear (SLN16) — wideband PCM
ffmpeg -i input.mp3 -ar 16000 -ac 1 -sample_fmt s16 output.sln16
# Actual G.722 (ADPCM) — use .g722 extension
ffmpeg -i input.mp3 -ar 16000 -ac 1 -c:a g722 output.g722
Batch convert a directory
#!/bin/bash
# convert-sounds.sh
# Convert all audio files in a directory to Asterisk-native format.
#
# Usage: ./convert-sounds.sh /path/to/source /path/to/dest
SRC="${1:-.}"
DEST="${2:-./converted}"
mkdir -p "$DEST"
for f in "$SRC"/*.{wav,mp3,ogg,flac} 2>/dev/null; do
[ -f "$f" ] || continue
base=$(basename "${f%.*}")
echo "Converting: $base"
sox "$f" -r 8000 -c 1 -b 16 "$DEST/$base.wav" 2>/dev/null ||
ffmpeg -y -i "$f" -ar 8000 -ac 1 -sample_fmt s16 "$DEST/$base.wav" 2>/dev/null
done
echo "Done. Files in $DEST/"
The script tries sox first, falls back to ffmpeg. Handles WAV, MP3, OGG, and FLAC input.
Batch convert MOH to multiple formats
Some MOH configurations benefit from having files in several formats so Asterisk can pick the best match without transcoding:
#!/bin/bash
# convert-moh.sh
# Convert MP3 files to WAV, GSM, and ULAW for music on hold.
#
# Usage: ./convert-moh.sh /path/to/mp3s /var/lib/asterisk/moh
SRC="${1:-.}"
DEST="${2:-/var/lib/asterisk/moh}"
mkdir -p "$DEST"
for f in "$SRC"/*.mp3; do
[ -f "$f" ] || continue
base=$(basename "${f%.*}")
echo "Converting: $base"
# WAV (best quality for ulaw/alaw endpoints)
sox "$f" -r 8000 -c 1 -b 16 "$DEST/$base.wav"
# GSM (smallest file size)
sox "$f" -r 8000 -c 1 "$DEST/$base.gsm"
# ULAW (raw G.711)
sox "$f" -r 8000 -c 1 -t ul "$DEST/$base.ulaw"
done
echo "Done. Converted $(ls "$DEST"/*.wav 2>/dev/null | wc -l) files."
Tips
- Convert to the format that matches your endpointIn PJSIP configuration, the SIP entity Asterisk communicates with: a phone, trunk, or other user agent. Defined by an endpoint section in pjsip.conf. codecs. 8 kHz mono is the safe default for narrowband G.711/GSM calls; use 16 kHz (
.sln16or.g722) for wideband endpoints. - GSM files are about 1/10 the size of PCM WAV. Good for systems with many prompts.
- For wideband calls, provide 16 kHz signed-linear (
.sln16) or actual G.722 (.g722) files if your endpoints support those codecs. Sox does not write G.722 natively; use ffmpeg for that. - Test playback from the CLI:
asterisk -rx "channel originate Local/s@test application Playback output-file"(requires atestcontextA named section of the dialplan that groups extensions. Calls enter a specific context and can only reach extensions visible from it, making contexts the basic unit of call routing and security. with extensionA dialplan entry that matches a dialed number or pattern within a context and triggers a sequence of prioritized steps.sin your dialplanThe core call-routing configuration of Asterisk, written mostly in extensions.conf as contexts, extensions, and priorities that decide how every call is handled. Full definition →).
User Notes
Know a tip or gotcha for this topic? Share it below and help others.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.