Composer, electronic musician, improviser

Batch Normalize and add Fades to Audio Files


A new project I have just started has had me editing a lot of relatively long audio files.  I have to cut them up separating the noise from the spoken word, normalize each file, and add fades to the beginning and end.  Then, I have to export them.  I was using Audacity, which is a great (though ugly) audio editor.  However, there are some problems with batch work in Audacity that ended cost me a lot of time and ultimately making it unsuitable for my purposes.  Finding no app for mac to quickly batch normalize, add fades, and save as a 48k/24-bit file, I decided I would bite the bullet and try to *assemble a bash script to do the tedious work for me.

The actual program that does the audio processing is called SoX.  SoX is a great command-line utility (Mac, Linux) for digital signal processing.  If you are on a gnu-linux machine, chances are your distro has a sox package you can easily install.  If you are on a mac, you can just grab this package and install it wherever you want.

You can download the script from here.

It is based on this script by Ben McDowell.  I have commented it rather heavily so you know (as much as I do) what is going on in the file.  Here’s what it looks like.


#  Script.sh
#  Created by scacinto on 1/31/13.
#  For now, only put audio files in the working directory - working on a fix

# This is the directory to the source soundfiles that need to be
# normalized and faded (the first argument on the command line.)
# This is the directory to write the normalized and faded files to
# (The second path you must supply on the command line.)
#  This is the sox binary directory.  Please set this to your sox path.
#  As it is now, this assumes that the sox binary is in the same directory
#  as the script.

#enable for loops over items with spaces in their name

#  This is the 'for' loop - it will run for each file in your directory.
for original_file in `ls "$src/"`
# Get the base filename of the current wav file
base_filename=`basename "$original_file" .wav`

# We need a temp file name to save the intermediate file as

# And we need the output WAV file

# Convert all spaces to hyphens in the output file name
wav_file=`echo $wav_file | tr -s " " "-"`

#Print a progress message
echo "Processing: \"$original_file\". Saving as \"$wav_file\" ..."

# We need the length of the audio file
original_file_length=`$SOX $src/"$original_file" 2>&1 -n stat | grep Length | cut -d : -f 2 | cut -f 1`

# Use sox to add perform the fade-in and fade-out
# saving the result as our temp_file.  Adjust the 0.1s to your desired fade
# times.
$SOX $src/"$original_file" $src/"$temp_file" fade t 0.1 $original_file_length 0.1

# If files have readable headers, you can skip the above operation to get the
# file length and just use 0 as below.
#$SOX $src/"$original_file" $src/"$temp_file" fade t 0.5 0 0.5

# normalize and write to the output wave file
$SOX $src/"$temp_file" $dest/"$wav_file" norm -0.5

# Delete that temp file
rm $src/$temp_file


To get the script to work on a Mac, either alter the beginning of the file above (in red) to supply the path to your sox installation or simply put it in run it from the sox directory you downloaded.  On GNU Linux just supply your sox path after installing from your distro.  Then, from the command line do:

$ sh norm_fade.sh /path/to/original/audiofiles /path/to/write/to

The ‘sh’ tells bash to run the script, ‘norm_fade.sh’ is the script, the first path should be to the files you want to add fades to and normalize, and the second path should be the output directory where you want the files written to.

I found it necessary to comment out (or remove) the following line from the script)


from the file on my Ubuntu laptop.  Instead of skipping over spaces it just removes all the ‘n’s from the paths and files…   More as I learn it!

Now, a couple caveats.  First, the first path must be to a FOLDER not a file.  If you want to run the script on a single file, put it in a directory and supply the directory path.  Second, the script expects .wav files.   If you throw an .aif in there, SoX will handle it, but the script has it writing out a file with the .wav extension.  The files will play, but your mileage may vary…  Be warned!

If you have questions or a way to make the code better/more efficient, please post to the comments!

Cheers, and happy normalization!




*I didn’t so much write it as assemble it from bits of code I found with a small amount of my own ‘code glue.’

Filed under: Code, Music, Tutorial / How-to, , , , , , , ,

6 Responses

  1. Dale Adams says:

    Thanks for creating this script. I can’t seem to get it to work though.It appears that it’s not creating a tmp file and I’m getting an error:

    norm_fade.sh: line 57: /Applications/sox/WantNotSamples/Who was he after all-temp.wav: No such file or directory

    rm: /Applications/sox/WantNotSamples/Who was he after all-temp.wav: No such file or directory

    Obviously it’s not creating the normalized wav file either. Any ideas what might be causing this?

  2. scacinto says:

    Hi Dale – thanks for reading. I think the problem is that your sox executable is not specified correctly. I just ran through the installation and normalization process, and got a similar error myself. The problem was resolved when I specified the precise path of the executable in the script. This part of the script for me looks like this:


    I ran the executable script from the desktop (after running chmod a+x on it to make it executable – otherwise I get a permissions error) with a folder called ‘test’ with a ‘test.wav’ test file in it and a processed/finished folder on the desktop called ‘dunzo.’

    Here’s my output:

    $ ./norm_fade.sh test/ dunzo/
    Processing: “test.wav”. Saving as “test-nf.wav” …

    Please note also that the sox download I link to is out of date. I’ll note that in an edit later today. Please let me know how it goes.


  3. I have tried sox-14.4.0 -14.4.1. I downloaded 14.4.1. I can get neither to work. I’m still getting the same error except 14.4.1 I end up getting:

    /Applications/sox-14.4.1/sox FAIL formats: can’t open input file /Applications/sox-14.4.1/WantNotSamples/Who was he after all-temp.wav’: No such file or directory

    rm: /Applications/sox-14.4.1/WantNotSamples/Who was he after all-temp.wav: No such file or directory

    • scacinto says:

      Hi Dale – can you contact me at scacinto @ gmail dot com with the following info:
      1: the command line command you are using to get the above output
      2: location of your script at runtime
      3: name and location of your waves folder
      4: name of sound files, sound file format (16bit PCM?)
      5: contents of your script (if you are editing the script.)

      I would think from the output that sox is either 1) not able to read your sound files or 2) not able to find your soundfiles. I’m happy to help your troubleshoot this privately and then post our findings for others later.


  4. So it appears the human error has struck again. I had commented out the two $SOX lines and not notice that it says this is where we create the temp file. Yup, no temp file = no such file or directory. Jeez. How did I miss that?

    Great script. Thanks Scott.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

My Other Awesome Sites [•_•]

Assault! 375 Aural Assaults!
About me! About me!
MySpace! MySpace!
Google+! Google+!
My (soon-to-be) Company! My (soon-to-be) Company!

Enter your email address to subscribe to this awesome blog and receive notifications of new posts by email!

Join 81 other followers


P O S T E R S !







INI new haven

Handmade instruments by Scott Petersen and Brian Kane at Artspace New Haven

%d bloggers like this: