Fix R128_*_GAIN values of 0 being incorrectly ignored

Remove nonZeroOrNull() from parseR128Adjustment() to allow raw R128
values of 0 to be properly converted to +5.0 dB adjustment. A value
of 0 in R128_TRACK_GAIN or R128_ALBUM_GAIN means the file is already
at -23 LUFS and should result in a +5 dB adjustment after conversion.

Added tests for R128_TRACK_GAIN and R128_ALBUM_GAIN with zero values.

Co-authored-by: OxygenCobalt <65370175+OxygenCobalt@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-11 01:47:48 +00:00
co-authored by OxygenCobalt
parent ecb5892443
commit 2cab2f7e77
2 changed files with 11 additions and 1 deletions
@@ -267,7 +267,7 @@ internal fun Metadata.replayGainAlbumAdjustment() =
?: id3v2["TXXX:REPLAYGAIN_ALBUM_GAIN"]?.parseReplayGainAdjustment())
private fun List<String>.parseR128Adjustment() =
first().replace(REPLAYGAIN_ADJUSTMENT_FILTER_REGEX, "").toFloatOrNull()?.nonZeroOrNull()?.run {
first().replace(REPLAYGAIN_ADJUSTMENT_FILTER_REGEX, "").toFloatOrNull()?.run {
// Convert to fixed-point and adjust to LUFS 18 to match the ReplayGain scale
this / 256f + 5
}
@@ -281,6 +281,16 @@ class TagFieldsTest {
// R128 is converted to match ReplayGain scale (-2560/256 + 5 = -5.0)
assertEquals(-5.0f, metadata.replayGainTrackAdjustment())
// Test R128 with zero value (should return +5.0 dB, not null)
metadata = createTestMetadata(xiphTags = mapOf("R128_TRACK_GAIN" to listOf("0")))
// R128 is converted to match ReplayGain scale (0/256 + 5 = 5.0)
assertEquals(5.0f, metadata.replayGainTrackAdjustment())
// Test R128 album gain with zero value
metadata = createTestMetadata(xiphTags = mapOf("R128_ALBUM_GAIN" to listOf("0")))
// R128 is converted to match ReplayGain scale (0/256 + 5 = 5.0)
assertEquals(5.0f, metadata.replayGainAlbumAdjustment())
// Test zero value
metadata = createTestMetadata(xiphTags = mapOf("REPLAYGAIN_TRACK_GAIN" to listOf("0.0 dB")))
assertNull(metadata.replayGainTrackAdjustment())