This article focuses on using ReplayGain in Linux. If you're not familiar with ReplayGain (originally written Replay Gain with a space), or if you're a Windows user, take a look at my previous article about ReplayGain which explains why it's a great idea, and how you can make use of it with your digital music collection.
Before you can play your music albums with the volume automatically adjusted, you need to tag your audio files with ReplayGain data. In Linux you have several ways of doing this, with a choice of graphical and command line tools on offer.
I didn't realise until recently, but Linux does offer a graphical tool that can tag audio files with ReplayGain values. soundKonverter is a KDE app but it runs fine in GNOME. For people unfamiliar with the command line, soundKonverter nicely packages up the necessary command line tools (which are described further down this page) into a graphical user interface.
To tag a set of audio files using soundKonverter is pretty simple, and the instructions below ought to get you started. (These instructions are for soundKonverter version 0.3.6.)
While soundKonverter is easy to use, it's still a fairly early version, and it's a little rough around the edges. I found that it seems to read and calculate tags for FLAC and Ogg Vorbis files correctly. However, MP3 files seemed to cause a problem, as it calculated the wrong values for an album in MP3 format. (See also the notes about mp3gain below, as the metadata format it uses to tag MP3 files is not recognised by most audio players in Linux.)
As a ReplayGain calculation interface, soundKonverter is a very promising tool. Hopefully the rough edges will be smoothed out over time.
If you're using Windows, foobar2000 does a great job of calculating ReplayGain data for audio files. Unfortunately, there isn't a version of foobar2000 for Linux. However, using the very impressive Wine, it is possible to install and run foobar2000 in Linux so you can use it to scan audio files and tag them with ReplayGain data just the same way as in Windows.
The Wine site has instructions on installing and using Wine, but if you're using Ubuntu 8.04 or similar, the following steps should be all it takes to get foobar2000 running on Wine.
Thanks to Wine, you can run foobar2000 almost exactly as you would in Windows, but make sure to see the Wine site for a list of known problems that foobar2000 has under Wine. You'll probably see some graphical glitches too. But as a ReplayGain scanner, foobar2000 under Linux is a very nice option.
My music collection is made up of audio files in the FLAC format. Even though soundKonverter and foobar2000 can calculate and write ReplayGain data for FLAC audio, I wanted to find a command-line-specific way of calculating ReplayGain data and writing it to my FLAC files. I eventually realised that flac and metaflac do just that. (To use these commands in Ubuntu, you need to install the flac package using Synaptic Package Manager.)
The flac command, using the --replay-gain option, can calculate gain and peak values when creating flac files. However, I couldn't work out how to get it to calculate album gain and peak values when ripping from CD, because KAudioCreator seems to call the flac command one file at a time, probably because it can only rip from CD one track at a time. Instead of calculating ReplayGain values at the time of ripping a CD, it seems easier to use metaflac to do the job on a set of files after the whole album has been ripped and encoded to FLAC files.
If you have a music album as FLAC files in one directory, for example "/media/music/flac/Machine Head/The Blackening" with no other FLAC files in that directory, then the following command will tell metaflac to calculate the ReplayGain values for each individual track, and one value for the album as a whole, and then write the data as metatags into the FLAC files:
metaflac --add-replay-gain "/media/music/flac/Machine Head/The Blackening"/*.flac
To see just the ReplayGain tags that metaflac has added, use the --list option, and then use grep to ignore everything that doesn't contain REPLAYGAIN, like this:
metaflac --list "/media/music/flac/Machine Head/The Blackening"/*.flac | grep REPLAYGAIN
You should see the track gain and peak for each track, and see that the album gain and peak values are the same for all of the files in the directory.
So you can use metaflac's --add-replay-gain option to tag each of your FLAC albums with ReplayGain data, one directory at a time. Or, if you're inherently lazy like I am, you can use the script I wrote to get metaflac to tag every FLAC file in an entire directory tree, one directory after another. See the section below, automating metaflac using a BASH script.
If the audio files you want to tag with ReplayGain are in the Ogg Vorbis format, then things are made easier thanks to vorbisgain (which you can install from Synaptic Package Manager in Ubuntu). This command has the ability to take a directory path and then recursively tag all of the Ogg Vorbis files in the directory tree automatically (using the -r option). It can even skip the Ogg Vorbis files that are already tagged with ReplayGain data (using the -f option). The -a option is necessary to get vorbisgain to consider the album peak and gain for each directory, otherwise it only calculates per-track values.
The only thing I can't get to work with vorbisgain is the filename globbing. According to the man page for vorbisgain, you should be able to add *.ogg to the end of the directory path (making sure to surround the whole path in double-quotes) so that only files with a .ogg extension are affected. However, this generated an error for me every time. (According to the manpage, the Debian, and therefore Ubuntu, version of vorbisgain does not process wildcards in filenames.) Dropping the *.ogg and using just the directory path did work, though:
vorbisgain -a -f -r /media/music/ogg/Slayer/
In my case, the directory tree only contained Ogg Vorbis audio files, so it worked fine. But you can use the -s flag to tell vorbisgain to skip non-Vorbis files if the tree does contain files of other types. (Default behaviour is for vorbisgain to stop when it encounters non-Vorbis files.)
Assuming that's not a problem, though, vorbisgain is very handy. It even skips Ogg Vorbis files that are already tagged with ReplayGain values, making it much quicker if you need to calculate values for a partially-tagged music collection.
The only other problem with vorbisgain is that the calculated gain values differ slightly to those calculated by metaflac. The difference was small enough that I doubt you'd notice even if you do mix Ogg Vorbis and FLAC files in your audio player, but I do wonder why there's a difference at all.
If you're still using MP3 audio files, you can install mp3gain (Ubuntu users can find it in Synaptic Package Manager) and then use a command like this:
mp3gain "/media/music/mp3/Slayer/Reign in Blood (Expanded Edition)"/*.mp3
This should tag all of the MP3 files in that folder with ReplayGain values. The calculated gain values seem to agree with metaflac. Just be sure to read the man page carefully, though, as some of the options modify the audio of the track itself rather than simply adding a ReplayGain metatag to the file.
One potential snag with mp3gain is that it writes ReplayGain values into APE tags, and these aren't read by many of the audio players on offer in Linux, which prefer ID3v1 or ID3v2 tags instead. If you need to tag MP3 files in Linux, it might be more useful to use foobar2000 with Wine.
If you do use mp3gain, though, note that it doesn't offer a way of recursively tagging all the files in a directory tree. However, you could modify the scripts I wrote for FLAC tagging (see below) and replace the call to the metaflac command with a call to mp3gain instead.
If you've got a large number of FLAC albums that you want to tag with ReplayGain values, then the following scripts (which you can download below) will call the metaflac command on one directory after another, only running if FLAC files are found in that directory.
Download the scripts (from the section below) and then unzip them into your home directory. (If you want to store them somewhere else, just edit tfwrg.sh and change ~/ to /some/other/path/ so that it knows where to find tag-flac-with-rg.sh in its alternative location.)
Make the scripts executable by opening a command prompt, changing to the directory where you've stored the files, and typing the following:
chmod +x tfwrg.sh chmod +x tag-flac-with-rg.sh
You may need to use sudo to do this, and you may also need to change the owner and group of the script files (using chown).
Now, from the command prompt, just type:
replacing /path/to/music/directory/root with the path to a directory tree of FLAC albums on your machine that you wish to tag with ReplayGain values. Each album should be stored in a directory of its own, not sharing a directory with another album (or any other FLAC files).
For instance, if you've got a neatly organised digital music collection of FLAC files all stored (one album per directory) inside /media/music/flac and none of them are tagged with ReplayGain values, you could just type:
and the scripts will run, telling you which directory is currently being scanned. Then, after a fair while, every album of FLAC files within /media/music/flac should be tagged with ReplayGain values. From then on, you only need to call tfwrg.sh on each new directory (or new artist directory) of FLAC files.
Note: if your directory path contains spaces, you need to put the whole directory path in double-quotes. So /media/music/flac doesn't need quotes, but "/media/music/flac/Machine Head" does need quotes.
Download the gzipped tarball:
and then copy it into your home directory. Open a command prompt (make sure you're operating on your home directory) and then type the following two commands:
gunzip tfwrg.tar.gz tar -xvvf tfwrg.tar
This should have extracted tfwrg.sh and tag-flac-with-rg.sh into your home directory.
To see the code for the scripts, and a description of how they work, see my page about running a script on a directory tree using find and BASH.
Once you've used one of the methods described above to tag your audio files, you then need an audio player that can read the ReplayGain metatags in each file and adjust the volume automatically.
Several audio players for Linux are alleged to work with ReplayGain. I tested the five audio players listed below (all of which can be installed in Ubuntu using the handy Add/Remove feature in the Applications menu of Ubuntu's main toolbar). However, I could only get the volume to increase and decrease according to ReplayGain values by using Quod Libet and Rhythmbox. (Note that I never use MP3; my comments below refer to FLAC and Ogg Vorbis files.)
If I discover any other audio players that make use of ReplayGain data, I'll update the above list. If you know how to get ReplayGain working in any of the players in which I didn't see any effect, let me know.
Of course, if you've used Wine to install foobar2000 as a ReplayGain scanner, you could also use it to playback audio with full control over whether to use album or track gain values. But I found foobar2000 playback a bit glitchy in Wine, so I stick with Quod Libet and Rhythmbox, both of which do a fine job of keeping playback volume at a consistent level when presented with music files tagged with ReplayGain data.
Thanks to chromium for pointing out that mp3gain tags MP3 files using the less-common APE metadata format.
Thanks to shadowking for pointing me in the direction of soundKonverter, and also for giving me the secret recipe which makes Aqualung obey ReplayGain metadata.
Thanks to Chris Craig and Emy for pointing out the -s flag in vorbisgain.
Ian McEwen recommends a player I've not encountered, called mpd, as does Vladimir Zhelezov and another, anonymous contributor. I'll try to test it if I find the time.
If you know how to fix any errors or omissions on this page, let me know.