From 5c85001b0c0d6f378992a9fe1bae445718e239b8 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Sat, 6 Jan 2024 18:24:48 -0700 Subject: [PATCH] music: add opus base gain support OPUS has another volume adjustment field on top of the existing R128 adjustments. I was under the impression this was handled by the android system, but apparently not. This commit applies the base gain to files by just adding them onto the existing ReplayGain values. Resolves #521. --- CHANGELOG.md | 3 +++ .../oxycblt/auxio/music/metadata/TagWorker.kt | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49474667d..67e0d8866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## dev +#### What's Improved +- The OPUS base volume adjustment field is now parsed and used in as a ReplayGain adjustment + ## 3.3.0 #### What's New diff --git a/app/src/main/java/org/oxycblt/auxio/music/metadata/TagWorker.kt b/app/src/main/java/org/oxycblt/auxio/music/metadata/TagWorker.kt index 196c7c0dc..5b3d8fa51 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/metadata/TagWorker.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/metadata/TagWorker.kt @@ -20,6 +20,7 @@ package org.oxycblt.auxio.music.metadata import androidx.core.text.isDigitsOnly import androidx.media3.common.MediaItem +import androidx.media3.common.MimeTypes import androidx.media3.exoplayer.MetadataRetriever import androidx.media3.exoplayer.source.MediaSource import androidx.media3.exoplayer.source.TrackGroupArray @@ -97,6 +98,21 @@ private class TagWorkerImpl( val textTags = TextTags(metadata) populateWithId3v2(textTags.id3v2) populateWithVorbis(textTags.vorbis) + + // If this metadata is of a vorbis file, we actually need to extract it's base gain + // and divide it by 256 to get the gain in decibels. + if (format.sampleMimeType == MimeTypes.AUDIO_OPUS + && format.initializationData.isNotEmpty() + && format.initializationData[0].size >= 18) { + val header = format.initializationData[0] + val gain = header[1].toInt() or ((header[0].toInt() shl 8) and 0xFF) + logD("Obtained opus base gain: ${gain / 256f} dB") + rawSong.replayGainTrackAdjustment = + rawSong.replayGainTrackAdjustment?.plus(gain / 256f) + rawSong.replayGainAlbumAdjustment = + rawSong.replayGainAlbumAdjustment?.plus(gain / 256f) + } + } else { logD("No metadata could be extracted for ${rawSong.name}") }