ReplayGain in Linux

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.

Tagging audio files with ReplayGain data in Linux

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.

Using soundKonverter to calculate ReplayGain values

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.)

  1. Install the soundKonverter package (you can do this using Synaptic Package Manager if you're running Ubuntu 8.04 or similar).
  2. You will also need to install the ReplayGain calculation tool for each of the formats you are interested in. FLAC requires that you have the flac package installed on your system; Ogg Vorbis requires the vorbisgain package; MP3 requires the mp3gain package. (These three can be found in Synaptic Package Manager in Ubuntu.) soundKonverter can also use tools for other formats: see the Settings menu, then "Configure soundKonverter" and then look at the Backends page.
  3. Open soundKonverter (which should appear in the Applications -> "Sound & Video" menu in Ubuntu).
  4. In soundKonverter's Tools menu, select "Replay Gain Tool" and a new window will open.
  5. Click on the "Add Folder" button in the top left of the ReplayGain Tool window, then navigate to the directory that contains the audio files of interest. Note that all audio files within the selected directory and all of its subdirectories will be loaded. Click Ok once you've chosen the desired directory.
  6. If a dialog appears asking you to choose from a choice of file extensions, make sure that only the file extensions of interest are highlighted, and then click on Open.
  7. The ReplayGain Tool window should now show a list of the audio files in the directory path you selected. If these audio files are already tagged with ReplayGain data in a metadata format recognised by the tools that soundKonverter uses, these ReplayGain values should also be shown to the right of each audio file in the list. If not, "Unknown" will appear instead.
  8. If you want to calculate ReplayGain data for all of the audio files in the list that don't already have ReplayGain data in a recognised format, then click the "Tag untagged" button in the bottom left.
  9. If you want to remove the displayed ReplayGain data from all of the audio files in the list, click the "Untag tagged" button.
  10. Once you're finished, click on the Close button in the bottom right of the window, then exit the soundKonverter main window.

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.

Using foobar2000 in Linux through Wine

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.

  1. Install the wine package using Synaptic Package Manager.
  2. Run the winecfg application, which can be found in Ubuntu's Applications menu under the Other or Wine menu. In the Audio tab, select a suitable sound driver and then click "Test Sound" to make sure that Wine is able to produce sound. Then click OK to exit the winecfg window.
  3. In either the Other or Wine menu, click on "Browse C:\ Drive" and a window of your virtual C: drive should appear. Download the foobar2000 installer from the foobar2000 site, and place a copy of the file into your virtual C: drive.
  4. Double-click on the executable file, and the foobar2000 installer should run. Install foobar2000 to the virtual C: drive, with any options you want (but make sure that the ReplayGain scanner is selected to install). Once the installation has finished, foobar2000 should run.
  5. To run foobar2000 in future, you can click on it in the Other or Wine menu. (When I first installed Wine, it appeared in the Applications/Other menu, but after a reboot, an Applications/Wine menu appeared.)

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.

ReplayGain using flac and metaflac

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.

ReplayGain for Ogg Vorbis using vorbisgain

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.

ReplayGain for MP3 using mp3gain

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.

Automating metaflac using a BASH script

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.

Using the tfwrg.sh and tag-flac-with-rg.sh scripts

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:

~/tfwrg.sh /path/to/music/directory/root

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:

~/tfwrg.sh /media/music/flac

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 tfwrg.sh and tag-flac-with-rg.sh scripts

Download the gzipped tarball:

Tarball: tfwrg.tar.gz

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.

How the scripts work

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.

ReplayGain audio players in Linux

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.)

Quod Libet
To enable ReplayGain adjustment in Quod Libet, go to the Music menu, then select Preferences. In the preferences window that appears, open the Player tab. Then check the box labeled "Replay Gain volume adjustment". In my test, Quod Libet was reducing volume on FLAC and Ogg Vorbis tracks with a negative gain, but it wasn't playing tracks with a positive gain any louder than tracks which weren't tagged (possibly it was doing this to prevent clipping). Quod Libet appears to use album gain values when the selected playback order mode is "In Order", and track gain values when the selected playback mode is "Shuffle", "Weighted" or "One Song", which I think is sensible behaviour.
Rhythmbox
For some reason, ReplayGain adjustment is not enabled by default for Rhythmbox (at least not in Ubuntu). To enable ReplayGain volume adjustment, open a command line and run gconf-editor (which you may need to install first). Once the Configuration Editor has loaded, go to the Edit menu and then select Find, then enter replaygain as the search term and check the box labeled "Search also in key names", then click the Find button. Click on the key name (near the bottom of the window) called /apps/rhythmbox/use_replaygain and the key values should appear in the top-right of the window. Look for a box labeled use_replaygain and make sure it's checked. Then close the Configuration Editor. Rythmbox does correctly decrease and increase volume on FLAC and Ogg Vorbis files with ReplayGain values. Except for my test file with a gain of exactly zero which was playing with the same volume adjustment as whatever track was last played. Which seems odd, though it might be my test files (the same track several times, but with different ReplayGain values). I don't know whether Rhythmbox uses album or track gain values to adjust volume.
Aqualung
This player seems quite sophisticated, and does have a ReplayGain option, but you have to do a bit of work to enable it. First, right-click in a blank part of the user interface and then click Settings in the menu that appears. Click on the "Playback RVA" tab, and below the graph that appears, change "Reference volume" to -16.5 and change "Steepness" to 0.0 and then click OK. Next, select all of the music files by clicking on one and then hit Ctrl+A, and then right-click somewhere and select "Reread file metadata" in the menu. Once I did all that, Aqualung obeyed the ReplayGain values in my FLAC and Ogg Vorbis test files.
Audacious
This player has a ReplayGain tab in Preferences (right-click the blank area next to the playback controls), but it had no effect on FLAC or Ogg Vorbis files that I tested.
Exaile
Has a ReplayGain page in Preferences, but I couldn't get it to have any effect on FLAC or Ogg Vorbis files tagged with ReplayGain values. Again, it's probably something I'm doing wrong.

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.

Feedback

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.

FeedBack

Post a comment about ReplayGain in Linux.

See visitor comments about ReplayGain in Linux.