Skip to content

Commit

Permalink
music: add opus base gain support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
OxygenCobalt committed Jan 7, 2024
1 parent cdd08e7 commit 5c85001
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/org/oxycblt/auxio/music/metadata/TagWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
}
Expand Down

0 comments on commit 5c85001

Please sign in to comment.