Mobile Games: Working With Multiple Sound Files

So, I have about 150 very short sound clips I’ve created for an iOS game I am working on. One of the issues is that these sound files come from 6 different sources. I have found that I need to adjust the volume on each set of files to get some consistency. Hands down the best tool for batch sound editing is Sox. It’s the “Swiss Army Knife” of sound editing.

Now listen, I’m a UNIX guy, I’ve been using some form of UNIX on my work and home desktop for almost 20 years. I’m very comfortable at the command line. I realize that not everyone will feel this way, but I honestly think it’s the best way to get things done.

I have 3 other parameters as well that need to be adjusted. I’ve done some testing and since these are human voice files I’ve found that I can go down about 32kbs with essentially no quality loss. I need the file to be mono and in the CAF format for the iPhone.

Get the max volume:

sox file.mp3 -n stat -v

Set the volume:

sox -v vol infile.mp3 outfile.mp3

Convert to Core Audio Format supported by iOS:

afconvert -d aac -f 'caff' -b 32768 infile.mp3 outfile.caf

I found that using afconvert will give me a smaller file than I can get with sox. The binary for afconvert comes with OSX so there is no need to install anything additional.

Let’s put this all together now. I’ve chosen to use 80% of the max volume in each file. So here is what I am doing to “normalize” these files.

#!/bin/bash
for i in *.mp3
    FNAME=$(echo $i| awk -F. '{print $1}');
    MVOL=$(sox $i -n stat -v 2>&1);
    NVOL=$(echo ".8 * $MVOL"|bc);
    sox -c 1 -v $NVOL $i $FNAME-tmp.mp3 tempo 1.3;
    afconvert -d aac -f 'caff' -b 32768 $FNAME-tmp.mp3 $FNAME.caf
    rm $FNAME-tmp.mp3
done

By using this script (or something similar) I can automate the modification of many files and thus saving a ton of time and energy. It may not be the prettiest solution but it works.