xiph / flac

Free Lossless Audio Codec
https://xiph.org/flac/
GNU Free Documentation License v1.3
1.61k stars 275 forks source link

Update ReplayGain functionality #96

Open mooninite opened 5 years ago

mooninite commented 5 years ago

The metaflac tool allows users to update their flac files with REPLAYGAIN_* tags. The support offered by metaflac is lacking:

  1. No multi-channel support.
  2. All files must have the same characteristics regarding bit, sample rate, and channels.
  3. Loudness algorithm is unknown? In-house?

Over time there have been advances in loudness algorithms and even a well-adopted standard has come about called "ITU BS.1770-4" a.k.a. "EBU R 128" and a few tools have been created that follow this standard.

I'm creating this issue to update metaflac because most users and even some software (Soundkonverter) uses metaflac's replay gain functionality. Updating it here will benefit everyone.

Would this project be willing to adopt an EBU R 128 library and switch metaflac to use it?

lvqcl commented 5 years ago

"R128GAIN is a FFmpeg and SoX based EBU R128 compliant loudness scanner." IMHO it's too heavy for flac / metaflac. Also, abandoned? (last update was in 2014).

libebur128, on the other hand, looks compact enough.

Moonbase59 commented 5 years ago

Since EBU R128/ITU-R BS.1770 are the better (and agreed-upon standard) algorithms for loudness calculation, and even ReplayGain v2 uses these nowadays, I’d propose to add these. Probably the old ReplayGain v1 algorithm (and the --add-replay-gain option) could be kept a while for backwards compatibility.

Since bs1770gain and others re-use the "old ReplayGain tags" BUT add "LU"/"LUFS" as a unit (instead of "dB"), there should be a means to set the desired unit.

We’d need a possibility to set the desired loudness target, too: EBU R128 (Europe) use -23 LUFS and gating, ATSC A/85 (USA) use -24 LUFS and no gating, and ReplayGain v2 uses -18 LUFS (for compatibility reasons, assumed to be about the same as the old 89 dB reference level).

In theory, there could be 3 "modes" like "ebu", "atsc" and "replaygain" using the exact same loudness code and generating tags like these (example from my tests) — or just specify the intended loudness target (like -18.0, -23.0 or -24.0 LUFS).

EBU: REPLAYGAIN_ALBUM_GAIN=-12.4 LU REPLAYGAIN_ALGORITHM=BS.1770 REPLAYGAIN_REFERENCE_LOUDNESS=-23.0 LUFS REPLAYGAIN_TRACK_GAIN=-12.4 LU

ATSC: REPLAYGAIN_ALBUM_GAIN=-13.4 LU REPLAYGAIN_ALGORITHM=BS.1770 REPLAYGAIN_REFERENCE_LOUDNESS=-24.0 LUFS REPLAYGAIN_TRACK_GAIN=-13.4 LU

REPLAYGAIN: REPLAYGAIN_ALBUM_GAIN=-7.4 LU REPLAYGAIN_ALGORITHM=BS.1770 REPLAYGAIN_REFERENCE_LOUDNESS=-18.0 LUFS REPLAYGAIN_TRACK_GAIN=-7.4 LU — and/or (for compatibility) — REPLAYGAIN_ALBUM_GAIN=-7.4 dB REPLAYGAIN_ALGORITHM=BS.1770 REPLAYGAIN_REFERENCE_LOUDNESS=89 dB REPLAYGAIN_TRACK_GAIN=-7.4 dB

If defaults are needed, I’d suggest using EBU 128 (BS.1770-2 or newer, -23 LUFS target, gating), because these turn out to be most widely used. ATSC A/85 was mainly intended for HD TV anyway, if I remember correctly.

There have been some changes in the BS.1770 specs over the years, I’d recommend using at least BS.1770-2 (Gate 10) or BS.1770-3 (some filter added, I forgot which).

Side note: EBU R128/ITU-R BS.1770 are clearly the better algorithms for loudness perception, and also use less computing power. They tend to "punish" modern overcompressed (mostly Pop/Metal/Rock) productions as well as overcompressed commercials. The overall experience is much more pleasing, although some might need to get used to the now "quieter" Pop/Rock/Metal albums.

Eventually, this might end the notorious "loudness wars" (I hope) and stop producers from compressing everything to death just in order to be perceived "louder" than the competition (und thus losing beautiful detail!).

So YES, I agree with the TO and very much hope this can be done. In the broadcasting world, we use it for so many years now, there are tools that support it alreday (bs1770gain, ffmpeg) and I strongly support the notion to also "have it in the original place" (FLAC, Xiph)!

See also: http://bs1770gain.sourceforge.net/

lvqcl commented 5 years ago

In theory, there could be 3 "modes" like "ebu", "atsc" and "replaygain" using the exact same loudness code and generating tags like these (example from my tests) — or just specify the intended loudness target (like -18.0, -23.0 or -24.0 LUFS).

EBU: REPLAYGAIN_ALBUM_GAIN=-12.4 LU REPLAYGAIN_ALGORITHM=BS.1770 REPLAYGAIN_REFERENCE_LOUDNESS=-23.0 LUFS REPLAYGAIN_TRACK_GAIN=-12.4 LU

And all existing players won't be able to recognize these tags. Why break backward compatibility? It's totally unnecessary IMHO.

Moonbase59 commented 5 years ago

That’s actually not true—many are moving with the times. Professional broadcasters and playout systems use this STANDARD for a long time now. Software like Audacious, Foobar2000, ffmpeg and IDJC already support this, just to mention a few. It’s more a question like »Why use fb2k or bs1770gain instead of native tools?«.

I strongly feel there IS a need to adopt newer standards (it can easily be done WITHOUT breaking backwards compatibility). Technological innovation (and worldwide standards) are important—had we been closing our eyes to progess forever, we wouldn’t have Linux, for instance, or even the beautiful FLAC format! :-)

lvqcl commented 5 years ago

My post was not about algorithms, but about tags. For example, foobar2000 uses new algorithm to calculate loudness, but it writes tags that are 100% compatible with the old standard.

Moonbase59 commented 5 years ago

That’s why I suggest having a "replaygainv2" mode that still uses "dB" instead of "LU"/"LUFS". I do agree that it’s still necessary to have a mode that uses the new algorithm but still keeps the old tag format (like, for instance, desbma’s r128gain).

Moonbase59 commented 5 years ago

I made my own "loudgain" in the meantime (uses libebur128), maybe you want to pick up some ideas. It’s far from perfect but perfectly fits our needs. I’d still love to see EBU R128/RG2 functionality in the native tools. :-)

averms commented 5 years ago

I've always felt like having replaygain scanning and tagging is a bit out of the scope of metaflac. If r128gain and loudness-scanner exist, why not use them instead? It is too bad that soundKonverter uses metaflac but I think that is their problem.

jherby2k commented 4 years ago

I support updating the algorithm, or at least adding R128 as an option, but don't see why the tag format should change. http://wiki.hydrogenaud.io/index.php?title=ReplayGain_2.0_specification says to keep the tag format as-is.

DanielTheGreat commented 3 years ago

Why waste time with Metaflac when you have such an amazing tool as Xrecode3, which, apart from converting anything to anything (audio), can analyse and tag (or apply to the audio samples) any output file (1–6 channels) with ReplayGain (album and/or track), EBU-R128, or (what he calls "Loudness Normalization") EBU-R128 with dynamic clip-limiting? [Some (mostly classical) multi-channel SACDs I've ripped, when scaled using EBU-R128 cause clipping, so I now always use the Loudness Normalization method for Mch SACDs.]

[Not associated with Xrecode3; just a huge fan!]

Daniel

audiomuze commented 1 year ago

I agree flac and metaflac should be enhanced to include support for EBU R 128 via --add-replay-gain type parameter.

Perhaps the simplest is to leverage libebur128 and add a parameter --add-r128-replay-gain but write the same tags. It'd be a real pity to see flac and metaflac remain behind current standards in handling loudness.

ktmf01 commented 1 year ago

There is a tool to scan FLAC files here: https://pypi.org/project/r128gain/ and https://github.com/desbma/r128gain

Anyone is welcome to supply a patch to implement this directly into flac and metaflac.