catwav: concatenating two .wav files under Unix

What is this about?

Recently I had a need to concatenate (append) two .wav files in order to create a third. The MP3 encoder I use does not support multiple .wav files as input, and the sox utilities, while otherwise amazing, provide no simple way to concatenate two .wav files. And the audio editors I tried, xwave and snd, were also frustrating. xwave didn't seem to support what I needed to do at all, and while snd is apparently very powerful, I couldn't find the features I needed. Also, my attempts to paste one wav file into another did not correctly account for sampling rate differences.

I assumed I was missing something, and I searched Google. As it turns out, I wasn't missing anything -- it's a pain to do! Most responses recommended that the user roll his own shell script to convert the two .wav files into raw audio files via sox, append them together, and finally convert back to .wav format. But no one presented a finished version of the script. In order to save other users much pain and suffering, I am providing a copy here.

Directions for Use

The correct syntax is:

catwav file1.wav file2.wav outputfile.wav

Bugs

Make sure you put the output file name last! It will be overwritten, with no warning.

The output file will always be 44.1 khz stereo with 16-bit signed samples. (The input files don't have to be.) This is a good thing if your next step is making an MP3. If you want something more compact, you can change the script.

The catwav shell script source code

Directions: copy and paste this script into a file called catwav in /usr/local/bin. Make it executable with the command chmod 755 /usr/local/bin/catwav. Of course, you don't have to put it in a shared directory if you don't want to; it's just a simple shell script.

See also silence.

License Terms

Public domain. Do as you see fit with this script.
#!/bin/sh
sox $1 -r 44100 -c 2 -s -w /tmp/$$-1.raw
sox $2 -r 44100 -c 2 -s -w /tmp/$$-2.raw
cat /tmp/$$-1.raw /tmp/$$-2.raw > /tmp/$$.raw
sox -r 44100 -c 2 -s -w /tmp/$$.raw $3
rm /tmp/$$*.raw

Follow us on Twitter | Contact Us

Copyright 1994-2012 Boutell.Com, Inc. All Rights Reserved.