Skip to content

Opus: replaygain_preamp behaves weird when playback gain is non-zero #1200

Open

Description

@Dareka826

Opus files which have a non-zero 'Playback gain' value in the header and have R128 tags don't get affected by replaygain. If the 'Playback gain' is 0dB, the replaygain is applied fine.

This opusinfo output:

User comments section follows...
	R128_TRACK_GAIN=-1280
Opus stream 1:
	Playback gain: -15.5391 dB

should have the same result as:

User comments section follows...
	R128_TRACK_GAIN=-5258
Opus stream 1:
	Playback gain: 0 dB

but only the 2nd track is affected by the replaygain and replaygain_preamp settings. (With replaygain_preamp set to 0, they should have the same volume since cmus adds 5dB to R128 tags)

  1. uname -a:
Linux RinLT 5.17.9-zen1-1-zen #1 ZEN SMP PREEMPT Fri, 20 May 2022 06:39:22 +0000 x86_64 GNU/Linux
  1. cmus --version:
cmus v2.9.1-25-g54d7213
  1. cmus --plugins:
Input Plugins: /usr/lib/cmus/ip
  mad:
    Priority: 55
    File Types: mp3 mp2
    MIME Types: audio/mpeg audio/x-mp3 audio/x-mpeg
  aac:
    Priority: 50
    File Types: aac
    MIME Types: audio/aac audio/aacp
  cdio:
    Priority: 50
    File Types:
    MIME Types: x-content/audio-cdda
  cue:
    Priority: 50
    File Types:
    MIME Types: application/x-cue
  flac:
    Priority: 50
    File Types: flac fla
    MIME Types:
  modplug:
    Priority: 50
    File Types: mod s3m xm it 669 amf ams dbm dmf dsm far mdl med mtm okt ptm stm ult umx mt2 psm
    MIME Types:
  mpc:
    Priority: 50
    File Types: mpc mpp mp+
    MIME Types: audio/x-musepack
  opus:
    Priority: 50
    File Types: opus
    MIME Types:
  vorbis:
    Priority: 50
    File Types: ogg oga ogx
    MIME Types: application/ogg audio/x-ogg
  wav:
    Priority: 50
    File Types: wav
    MIME Types:
  wavpack:
    Priority: 50
    File Types: wv
    MIME Types: audio/x-wavpack
  ffmpeg:
    Priority: 30
    File Types: aa aac ac3 aif aifc aiff ape au fla flac m4a m4b mka mkv mp+ mp2 mp3 mp4 mpc mpp ogg opus shn tak tta wav webm wma wv
    MIME Types:

Output Plugins: /usr/lib/cmus/op
  pulse
  alsa
  oss
  jack
  sndio
  ao

Activity

  1. Dareka826 commented on May 25, 2022

    @Dareka826
    ContributorAuthor

    It seems that replaygain is being applied ok, except the replaygain_preamp setting only works on opus files with 'Playback gain' set if it's negative.

  2. changed the title [-]Opus: replaygain ignored when playback gain is non-zero[/-] [+]Opus: replaygain_preamp behaves weird when playback gain is non-zero[/+] on May 25, 2022
  3. Dareka826 commented on May 25, 2022

    @Dareka826
    ContributorAuthor

    I don't know if I'm describing this problem well, so here's an example:

    sine.zip contains two copies of a generated sine wave:

    • one has the R128 replaygain tag at -4438 (-17.3359 dB) and 0 dB output gain,
    • the other has the R128 tag at -1280 (-5 dB) and the output gain at -12.3359 dB.

    This way players that don't support R128 tags play the 2nd file at -18 LUFS and players that do at -23 (with cmus adding 5 dB to match replaygain 1.0).

    These files sound exactly the same when replaygain is enabled in cmus.

    Now, the problem:

    • When replaygain_preamp is set to a value below 0, e.g. -10, both files sound quieter
    • When it's a positive value, only the file with 0 dB output gain is louder
  4. gavtroy commented on May 26, 2022

    @gavtroy
    Collaborator

    With replaygain we typically have REPLAYGAIN_TRACK_PEAK tags saying how much the volume can be raised without introducing clipping (or how much it needs to be lowered to remove it). With the default setting of replaygain_limit=true, cmus will lower the final gain if the adjusted peak would exceed 1.0.

    RFC 7845 wasn't a fan:

    Peak normalizations are difficult to calculate reliably for lossy codecs because of variation in excursion heights due to decoder differences. In the authors' investigations, they were not applied consistently or broadly enough to merit inclusion here.

    I disagree with their conclusion, but in any case we don't get the peak so we have to make do without. Today we just assume the peak is 1.0 meaning the volume will never be raised, but there's a case to be made for assuming ample headroom instead. The risk of some clipping is probably preferable to some songs playing much too quietly.

  5. Dareka826 commented on May 26, 2022

    @Dareka826
    ContributorAuthor

    But if cmus doesn't raise volume is peak is at 1.0, then why does the file without output gain still get louder?

    Both of these files have their peak assumed at 1.0:

    file_changed: file: /home/rin/repos/cmus/pfx/home/sine_with_playback_gain.opus
    update_rg_scale: peak not available, defaulting to 1
    update_rg_scale: gain = 0.000000, peak = 1.000000, db = 0.000000, scale = 1.000000, limit = 1.000000, replaygain_scale = 1.000000
    ip_setup: pcm convert: scale=1 convert=0 convert_in_place=0
    
    file_changed: file: /home/rin/repos/cmus/pfx/home/sine_no_playback_gain.opus
    update_rg_scale: peak not available, defaulting to 1
    update_rg_scale: gain = -12.335938, peak = 1.000000, db = -12.335938, scale = 0.241659, limit = 1.000000, replaygain_scale = 0.241659
    ip_setup: pcm convert: scale=1 convert=0 convert_in_place=0
    
  6. Dareka826 commented on May 26, 2022

    @Dareka826
    ContributorAuthor

    Nevermind, I misunderstood what you wrote. Now I see that in that case cmus sees that it has headroom to work with thanks to the gain information.

    Wouldn't a solution then be to somehow take the opus output gain into account in update_rg_scale()?

  7. Dareka826 commented on May 26, 2022

    @Dareka826
    ContributorAuthor

    I think the track_info struct needs an additional output_gain member along the album and track gain, which would store the codec gain value for checking the replaygain loudness. This would also be useful if any more future codecs implement this kind of functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions