Sound editing libraries?
February 25, 2009 1:41 PM   Subscribe

I have many wav files that I need to modify in the same way. Specifically, I want to fade in from zero dB for the first 5 ms and fade out to zero db for the last 5 ms. What do I use?

I'd prefer a scriptable solution that I can just point at the files. I'm a capable programmer, but I'm not sure what free tools or libraries are available for this. Most ideal would be a python or ruby library, but I'm open. I'm using OSX fwiw.
posted by pealco to Technology (7 answers total) 2 users marked this as a favorite
Also, if somebody knows how to script Audacity, that'd work great, too.
posted by pealco at 1:43 PM on February 25, 2009

sounds like a job for SoX.
posted by dirm at 1:48 PM on February 25, 2009

seconding sox. it has, among tons of other features, a fade effect which you can apply to the beginning and end of sound files.
posted by xbonesgt at 1:53 PM on February 25, 2009

sox is audio as imagemagick is to images. Just throw it in a bash for each file loop, and you're done.
posted by pwnguin at 2:45 PM on February 25, 2009

Do you know the length of the samples? sox will do fade in and out in one command, but you need to know the length to truncate at for the fade out. Easier, but more computationally intense, is to apply a fade in, reverse the sample, apply another fade in, then reverse the sample again:

sox infile.wav fade1.wav fade 0.005
sox fade1.wav fade2.wav reverse
sox fade2.wav fade3.wav fade 0.005
sox fade3.wav outfile.wav reverse
rm fade[123].wav

I might be running an old version, so they may have made this easier.
posted by scruss at 5:22 PM on February 25, 2009

If you want to do this really manually, you can use the wave module from the Python standard library to read a string of bytes corresponding to the raw sample data and access the sample width, sample rate, number of channels, etc. Then interpret the string as raw array data using numpy.fromstring, and you have a numpy array of sample values. Then you can apply whatever operation you want to the numpy array; a linear, logarithmic, or exponential fade should be easy to implement. Finally, use numpy.array.tostring() to get a string of samples, and use the wave module to write a new .wav file.

The easiest way to get this working is to use SoX, but the above should get you started if you really want to use Python or if you want to do something SoX can't do.
posted by musicinmybrain at 7:12 PM on February 25, 2009

SoX is great. Thank you.
posted by pealco at 7:57 AM on February 26, 2009

« Older A good family vacation within three hours (by air)...   |   Unknown bugs bugging. Newer »
This thread is closed to new comments.