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.
This commit is contained in:
Alexander Capehart 2024-01-06 18:24:48 -07:00
parent cdd08e7f99
commit 5c85001b0c
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 19 additions and 0 deletions

View file

@ -2,6 +2,9 @@
## dev ## dev
#### What's Improved
- The OPUS base volume adjustment field is now parsed and used in as a ReplayGain adjustment
## 3.3.0 ## 3.3.0
#### What's New #### What's New

View file

@ -20,6 +20,7 @@ package org.oxycblt.auxio.music.metadata
import androidx.core.text.isDigitsOnly import androidx.core.text.isDigitsOnly
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.MimeTypes
import androidx.media3.exoplayer.MetadataRetriever import androidx.media3.exoplayer.MetadataRetriever
import androidx.media3.exoplayer.source.MediaSource import androidx.media3.exoplayer.source.MediaSource
import androidx.media3.exoplayer.source.TrackGroupArray import androidx.media3.exoplayer.source.TrackGroupArray
@ -97,6 +98,21 @@ private class TagWorkerImpl(
val textTags = TextTags(metadata) val textTags = TextTags(metadata)
populateWithId3v2(textTags.id3v2) populateWithId3v2(textTags.id3v2)
populateWithVorbis(textTags.vorbis) 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 { } else {
logD("No metadata could be extracted for ${rawSong.name}") logD("No metadata could be extracted for ${rawSong.name}")
} }