The Gains of Replay Gain

So… no-one responded with anything helpful on my last post.  There was a Dot article about the semantic desktop (and hence Nepomuk), however.

On to more interesting things.  I’ve got fed up recently with changing volumes for different songs.  I actually notice it more on my portable MP3 player, but Amarok 2 has long bugged me by not having any Replay Gain support.  And I’ve been thinking about implementing it for about the same amount of time.  Yesterday, I finally got around to starting it.

The first hurdle I came up against was how to get the information I needed from the files.  If you run mp3gain or aacgain on your MP3 or MP4 files, or if you have MusePack files, there’s no problem since the information about changing the volume is stored in such a way that it is interpreted by the decoder, and so Amarok doesn’t need to do anything.  But with other file formats, and when you use other tools for calculating the gain for MP3 and MP4 files, the information is stored as tags.  Fine, we can read tags.  We can store the information we read in the database, and then access it later.  But we’re left with the question of what tags we’re looking for.

The official Replay Gain standard is incredibly unhelpful in this regard.  It gives you all sorts of details about the mathematics behind calculating how much gain to apply to a track, but almost nothing about how to store or retreive that value.  On top of that, it is woefully out of date, not having been updated since 2001.

OK, so over to the Replay Gain page on the slightly more active Hydrogenaudio Knowledgebase wiki.  However, which tag format (ID3v2 or APE, say) the relevant tags are stored in by various tools and for various formats is about as detailed as it gets.  None of the web pages for the various tools are any more helpful.

The vorbisgain man page is more forthcoming: the relevant tags are REPLAYGAIN_TRACK_GAIN, REPLAYGAIN_TRACK_PEAK, REPLAYGAIN_ALBUM_GAIN and REPLAYGAIN_ALBUM_PEAK. The REPLAYGAIN_*_GAIN values are in decibels and have ” dB” appended to the end. The REPLAYGAIN_*_PEAK values are floats (no further information given).

So I turned to the old Amarok 1.4 Replay Gain script.  This is what I found:

  • Vorbis and FLAC works as described above. To get the peak gain relative to the adjusted gain, you apparently need to take the log of the float, base 10, and multiply the result by 20.
  • MP3s can have the data stored in one of several ways (apart from the method detailed above, which we don’t need to concern ourselves with):
    • In the APEv2 tags in the same manner as with Vorbis (possibly not all uppercase)
    • In the ID3v2 TXXX (user comment) tags in the same manner as Vorbis (possibly not all uppercase) – the TXXX frames have a description field and value field, which are used for the tag name and the tag value. Total of 4 TXXX tags to store all the values. These are written by Foobar2000.
    • In the ID3v2.4 RVA2 tags (only Quod Libet does this AFAIK). Each tag has an identification string, into which Quod Libet writes the values “album” and “track”. The format of the rest of the frame is specified in section 4.11 of the ID3v2.4 frames specification.
  • MP4s are done in the same way as Vorbis.
  • AAC (not in an MP4 container): APEv2, presumeably in the same manner as MP3 APE tags (the ReplayGain script doesn’t support AAC files).
  • MusePack: values are stored in the header in fields designated for the purpose. Should be supported natively by the decoder, according to the Hydrogenaudio Knowledgebase, but the 1.4 ReplayGain script stil reads these values. The peak value apparently needs to be multiplied by 2 to get the peak gain relative to the adjusted gain.
  • WMA: same as Vorbis, stored in the ASF tags (this may well be only files tagged by the ReplayGain script itself).
  • WavPack: stored in APEv2 tags, presumeably in the same manner as MP3 APE tags (the ReplayGain script doesn’t support WavPack files).
  • MOD files: Foobar2000 stores these in APEv2 format, presumeably in the same manner as MP3 APE tags (the ReplayGain script doesn’t support MOD files).

Don’t you feel all enlightened now?

Tags: , , , , , , , , , , ,

15 Responses to “The Gains of Replay Gain”

  1. Matija "hook" Šuklje Says:

    I always use ‘normalize’ before adding tracks to my collection (actually, I do as soon as they get on my harddrive).

    link:
    http://normalize.nongnu.org

  2. TheBlackCat Says:

    “To get the peak gain relative to the adjusted gain, you apparently need to take the log of the float, base 10, and multiply the result by 20.”

    I would double check that. For the normal definition of dB, you take 20*log(spl/splref), where spl is the level of the sound pressure and splref is a reference sound pressure level. If the peak value in the tag is already relative to a reference value then just taking 20*log of the tag is enough, but I would make sure the tag value isn’t divided by a reference value anywhere.

  3. MoD Says:

    First of all, thanks a lot for your enthusiasm in implementing replaygain natively into Amarok. This is actually the only feature which I am still missing in the Amarok 2 release and prevents me from installing it.

    For another replaygain code implementation comparison you might also want to have a look at the rockbox firmware project: http://www.rockbox.org . I never looked at the code myself but I guess it covers all supported formats.

  4. randomguy3 Says:

    @TheBlackCat: there are no divisions, but I think that makes sense. The replay gain spec says “The maximum peak amplitude (a single value) should be stored as a 32-bit floating point number, where 1=digital full scale”. Which suggests that the division has already taken place (so 1 means the peak is same as the reference value – what you would get for a constant tone, for example).

  5. Bakterie Says:

    Any idea when this will show up in an Amarok2 release? This is a must have for me as most of my collection is FLAC. Will there also be an option to apply replay gain on a song from within Amarok, like there is with the replay gain script for Amarok 1.4?

    Thank you for your work on this!

  6. Bruno Says:

    Would it be possible to normalize songs when syncing with a portable mp3 player (who don’t support replay gain)?

  7. randomguy3 Says:

    @Bakterie: I’m aiming for 2.1. I don’t have any plans to put running mp3gain etc. on files into Amarok. I don’t think that belongs in Amarok core, anyway – it should be an addon script.

    @Bruno: that would be quite neat. It should probably be part of a transcoding script (I’m not aware of any transcoding capability in Amarok 2 yet).

  8. Trigger Says:

    Currently I think the IPod cannot use the information written by mp3gain, but uses something similar.
    I will love you if you find a way to convert the mp3gain tag to something the stupid IPod can use while copying files with Amarok to the IPod.

    And even if this won’t/can’t be implemented, I love Amarok anyway 🙂

  9. randomguy3 Says:

    @Trigger: if you use mp3gain with the -r or -a option, it should be written in a form that any MP3 decoder will interpret (not in the metadata but in the file itself). I’ve tested this on my Creative ZEN Stone Plus (which isn’t fancy enough to display the track artist, never mind read replay gain metadata).

  10. . Says:

    @Matija “hook” Šuklje
    normalize adds tags to mp3 files, and reencodes vorbis files (which decreases quality.

  11. gavlee Says:

    I have some code in ~/.asoundrc to use ladspa plugins with a compressor/limiter to keep the volume the same. If there is need for this I could post it somewhere, I don’t want to flood your comments. 🙂

  12. Fri13 Says:

    I like to read about Nepomuk but I just cant find how to use it.
    Many times I have readed how you can find files by tags and rating, but not mention HOW.

    Only kio-slave what I have now found is “nepomuksearch:/hasTag:foobar”
    But it does not show anything on folderview or on Dolphin, it just freeze the filemanagement while nepomuk query is done (about 90 seconds).

    Tags and files can be founded trought krunner but I wanna get use the tag filterin to folderview…

    Because I use KDE 4.1.96 (RC1) it seems it does not work yet. This is bad miss why nepomuk might fall if it is not possible to use easily like metadata on digiKam.

    Today I was using kdenlive 0.7.1 and I noticed that there was nepomuk integration possible to enable in settings. All what it did was giving error… I was so disapointed.
    Then I tried to get audio and video work on it and got so terrible video preview (with uncompressed screenrecord too!) so i turned it off.

    Now I am searching good audio/video conversation application… mayby doing few mockups for such and hope someone would implent if there is not such easy-to-use one.

  13. TheBlackCat Says:

    I think full Nepomuk support is not planned until 4.3 at the earliest. I was under the impression the current Nepomuk search system is still very experimental.

  14. Harold Says:

    The replaygain script in Amarok 1.4 makes the volume control unusable (the script moves it up and down).
    For Amarok 2 can you have it leave a usable volume control. Maybe show the replaygain ajustment in outline or transparently on the volume control.
    Thank you for working on replaygain for Amarok 2.

  15. Ian Monroe Says:

    Feel free to narrow the scope of what formats replaygain will support before committing. 😛 Once its committed people will have incentive and not much work to get support for their sort of file.

    Perhaps more of a challenge is that you need a sort of ‘hidden’ way to change the volume level with Phonon. The Replaygain script is a bit obvious and just changes the user-visible amarok volume, this isn’t how replaygain is supposed to work.

Leave a reply to Bruno Cancel reply