Shell Scripting

Batch WAV to U-Law Converter with Shell Script

March 2014

So I've been having to do a few conversions of WAV files for old phone systems that require either u-law or a-law encoded audio a bit lately. I've been using sox in terminal but decided I need to speed things up a little, so I created a shell script to process an entire folder of audio files in one go.

Setup

On a Mac with this simple script all you need to do is download SoX and put the sox folder at the base level of your filesystem at /sox. You can change this in the script by altering the soxloc="/sox/sox"; line.

Then all you need to do is open Terminal and first drag the script onto the window. Then drag the folder that contains all the audio files and hit enter. The script will start looping through the files and converting each one.

The Script

#! /bin/sh

soxloc="/sox/sox";
tempfile="";
echo "WAV to u-law converter script by Sandy Milne V1"

shopt -s nullglob
for f in "$1"/*.wav
do
    echo "Processing - $f"

    "$soxloc" "$f" -r 8000 -c 1 -e u-law "${f%.*}"-ulaw.wav

done

Customising

You can change any of the parameters like sample rate or encoding just by altering the -r 8000 -c 1 -e u-law part of the script:

  • -r 8000 — sample rate (8000 Hz is standard for telephony)
  • -c 1 — mono channel
  • -e u-law — encoding type (change to a-law if needed)

The output files are written to the same directory as the source files with -ulaw appended to the filename, so your originals are kept safe.