All commands assume bash shell
Collect all MP3 files in one directory.
If any filenames contain spaces, first convert them to underscores:
for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done
This first step is important because, even if unix itself allows spaces in filenames, most programs get confused by them.
If your MP3 files came from DOS/Windows, they may have uppercase extensions. You can convert whole names to lowercase or just extensions. For everything lowercase do:
for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done
to convert just extensions:
for i in *.MP3; do mv "$i" "`basename "$i" .MP3`.mp3"; done
The CD Audio specification requires wave files that are 16 bit, stereo, 44.1 kHz. MP3 files often have different parameters, but we need to convert them to WAV first, anyway.
We have several choices for the conversion process.
Originally, there was only mpg123. However, it uses a proprietary licensing, and now there's an open source replacement - mpg321. Both commands use the same syntax:
for i in *.mp3; do mpg321 -w `basename $i .mp3`.wav $i; done
When decoding 22khz MP3 files the output of mpg123 may be distorted. I don't know how well mpg321 deals with this problem. If you're converting with mpg123, use:
for i in *.mp3; do mpg123 --rate 44100 --stereo --buffer 3072 --resync -w `basename $i .mp3`.wav $i; done
Mpg123 can be obtained from http://www.mpg123.de/.
Mpg321 is available from http://mpg321.sourceforge.net/.
NOTE I noticed that with some MP3 files mpg123 output was distorted. At first I thought that MP3's were bad, but then I checked with another player and they sounded OK. So mpg123 is not my converter of choice.
Another MP3 player/decoder, and the one I prefer, is madplay. It's available from http://www.mars.org/home/rob/proj/mpeg/. With madplayer, the command line is:
for i in *.mp3; do madplay -o `basename $i .mp3`.wav $i; done
Unfortunately, madplay also had problems with some of MP3 files I had. I don't think there's a problem with the decoder, but rather with it handling broken MP3 files.
The lame encoder, which has a decoding mode, seems to handle difficult cases very well (lame can be found at http://www.mp3dev.org/mp3/) :
for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; done
Well, this HOWTO is about MP3 files, but OGG standard is an alternative which many people prefer over MP3 because of the licensing. To deal wit OGG files, you need to use:
for i in *.ogg ; do ogg123 -d wav -f `basename $i .ogg`.wav $i; done
Nico Genimakis sent me an email about using mplayer to covert audio in many different formats to WAV with automatic resampling to 44100Hz. Mplayer is known to be able to read almost anything, and it can convert your .ogg, .mp3, .flac, .wma etc.
usage (in this example converting WMA files):
for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm -waveheader $i; done
NOTE: The `basename $i .mp3`.wav command replaces MP3 extensions with WAV. There are 101 ways to do that, here's the alternative: `echo "$1" | sed 's/\.mp3$/.wav/'`
After the conversion, run "file *.wav" and check the output for any files that differ from 16 bit, stereo 44100 Hz.
If there are files with different characteristics, convert them to the above specs. For example, to convert file track01.wav to obtain sample rate 44.1 kHz, you could use:
sox track01.wav -r 44100 track01-new.wav resample
or, if the above introduces static when converting mono files:
sox track01.wav -r 44100 -c 2 track01-new.wav
Sox is so popular, that it's probably installed by default with any Linux distribution, and can be obtained from http://www.spies.com/Sox/. However, the command-line options are somewhat cryptic for the casual user (me). Look at http://www.spies.com/Sox/sox.tips.html for some tips on usage.
Normalisation is a process during which all the sound files are brought to the same relative loudness level. I use a program by Chris Vaill (<email@example.com>), called normalize - it can be obtained from http://www.cs.columbia.edu/~cvaill/normalize/
I use the following syntax (-m is for mix mode, where all files should be as loud as possible):
normalize -m *.wav