FrancisRussell / zoog

Updates Opus headers and R128 tags to normalize playback volume for non-R128-aware players.
BSD 3-Clause "New" or "Revised" License
13 stars 3 forks source link
audio normalization ogg opus vorbis xiph

Zoog: Zero Opus Output Gain

Zoog crate Zoog documentation

Zoog is a Rust library that consists of functionality that can be used to determine the loudness of an Ogg Opus file and also to rewrite that file with new internal gain information as well as loudness-related comment tags. It also has functionality for purely manipulating comment tags of both Ogg Opus and Ogg Vorbis files.

Zoog currently contains two tools, opusgain and zoogcomment. opusgain can be used to:

It is intended to solve the "Opus plays too quietly" problem.

zoogcomment can be used to list, replace or modify the comment tags of Ogg Opus and Ogg Vorbis files. Its usage is roughly based on that of vorbiscomment though many options have different naming for improved clarity.

Although zoog exposes a library, its API is unstable and this package is released on crates.io primarily to allow access to the command-line tools. The API is documented however, and the reading the source may prove useful to anyone else wishing to work with Ogg Opus files.

opusgain

opusgain adjusts the Opus binary header for playback at a specific volume and will always generate the R128_TRACK_GAIN tag and the R128_ALBUM_GAIN tag (when in album mode) such that files will play at an appropriate volume in players that support these tags, and at a more appropriate volume in players that don't. Existing R128_ALBUM_GAIN tags will be stripped when not in album mode.

opusgain (unlike its predecessor zoog) decodes Opus audio in order to determine its volume so that it's possible to be certain that all generated gain values are correct without making assumptions about their existing values.

The following options are available (run opusgain --help for usage):

If the internal gain and tag values are already correct for the specified files, opusgain will avoid rewriting them.

Sequentially multiplexed or "chained" Ogg Opus streams are not supported.

opusgain supports Unix shell style wildcards under Windows, where wildcards must be handled by the application rather than expanded by the shell.

zoogcomment

zoogcomment can be used to delete, append, replace and list the comments located in an Ogg Opus or Ogg Vorbis file.

The following options are available (run zoogcomment --help for usage):

zoogcomment only has knowledge of UTF-8. Usage on systems where UTF-8 is not the character encoding scheme in use may encounter issues.

Build Instructions

If you do not have Cargo, install it by following the instructions here.

Clone the Git repository:

$ git clone https://github.com/FrancisRussell/zoog.git

Inside the cloned repository:

cargo build

or

cargo build --release

for a release build.

Built binaries can be found in target/debug or target/release.

Installation via cargo

At the command line, simply run

$ cargo install zoog

opusgain and zoogcomment should now be available in the path.

Releases

Zoog binaries for Windows, MacOS and Linux can be found on the releases page. Only the Linux binaries have undergone any testing at present.

About Ogg Opus Volume Normalization

Background

Opus-encoded audio files contain an ‘output gain’ value which describes a gain to be applied when decoding the audio. This value appears to exist in order to ensure that loudness changes to Opus files are always applied, rather than being dependent on decoder support for tags such as REPLAYGAIN_TRACK_GAIN and REPLAYGAIN_ALBUM_GAIN which are used in Ogg Vorbis, but not Opus.

The in-header value was intended to correspond to the album gain with RFC 7845 defining the tag R128_TRACK_GAIN for single-track normalization. It seems the original intent of the output gain was to eliminate the need for an album gain tag, however R128_ALBUM_GAIN was later added for album normalization.

The problem

When encoding an Opus stream using opusenc from a FLAC stream which has embedded ReplayGain tags, the resulting Opus stream will have the output gain field set in the Opus header. The gain value will be chosen using EBU R 128 with a loudness value of -23 LUFS, which is 5 dB quieter than ReplayGain.

The presence of either R128_TRACK_GAIN or R128_ALBUM_GAIN tags will allow players that support these to play tracks at an appropriate volume. However, in audio players that do not support these tags, track will likely sound extremely quiet (unless your entire music collection is normalized to -23 LUFS).

Even more problematically, using opusenc with a FLAC file that does not have embedded ReplayGain tags will produce a file that plays at the original volume of the source audio. This difference in behaviour means that it's not possible for players that do not support R128 tags to assume that different Opus files will play at a similar volume, despite the presence of the internal gain header.

Even if a player does support the R128 tags, this is not enough to correctly play Opus files at the right volume. In the case described above, opusenc will use the internal gain to apply album normalization, meaning that it does not generate a R128_ALBUM_GAIN tag. Without this, it's not possible for a music player to play a track at album volume without again assuming that the internal gain corresponds to an album normalization at -23 LUFS.

Q & A

How is loudness calculated?

Loudness is calculated using ITU-R BS.1770. This is the standard used by EBU R 128 for measuring loudness and the one intended for use when calculating Opus R128 tags.

What's with the name zoog?

zoog stands for "Zero Opus Output Gain" and was the name of the original tool in this project. It served a similar purpose to opusgain but used the existing R128 tags to determine file volume rather than decoding audio directly.

zoog was deprecated because the issues around whether it is possible to assume that a track is album normalized made it possible to break album normalization if it occured via the ouput gain value and not the R128_ALBUM_GAIN tag.

When should I use opusgain versus loudgain

If you only play Opus files in players which support R128 tags, then use loudgain.

You should use opusgain if you play Ogg Opus files in players that do not support R128 tags and would like them to play at either their original volume, or at the volumes suggested by ReplayGain or EBU R 128.

Once you have set the internal gains of a set of Opus files to the desired values, then loudgain is likely preferable for any future tag updates related to normalization.

How can I check if opusgain is working correctly?

Applying opusgain to various test files then reviewing the diagnostic output and R128 tags generated by loudgain when applied to the rewritten files is helpful in this regard.

Disclaimer

Please see LICENSE. Unless you have a source you can easily reconstruct your Ogg Opus and/or Vorbis files from, the author recommends making a backup of any files you intend to modify first, and running opusinfo afterwards on any processed files.

References

  1. RFC 7845 - Ogg Encapsulation for the Opus Audio Codec
  2. Vorbis I Specification
  3. Ogg Vorbis identification header