playback: add dynamic replaygain mode [#7]

Add a "Dynamic" replaygain mode inspired by the FooBar2000 plugin. This
will automatically determine whether the playback is in an album or not
and use the album gain or track gain accordingly.
This commit is contained in:
OxygenCobalt
2022-01-13 20:15:22 -07:00
parent 5359c819bd
commit ddf2757cb6
7 changed files with 85 additions and 28 deletions
@@ -27,6 +27,7 @@ import androidx.media.AudioManagerCompat
import com.google.android.exoplayer2.metadata.Metadata
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame
import com.google.android.exoplayer2.metadata.vorbis.VorbisComment
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.playback.state.PlaybackStateManager
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.util.getSystemServiceSafe
@@ -88,30 +89,54 @@ class AudioReactor(
* This is based off Vanilla Music's implementation.
*/
fun applyReplayGain(metadata: Metadata?) {
if (settingsManager.replayGainMode == ReplayGainMode.OFF || metadata == null) {
logD("ReplayGain is disabled or cannot be determined for this track, resetting volume.")
if (metadata == null) {
logD("No metadata.")
volume = 1f
return
}
// ReplayGain is configurable, so determine what to do based off of the mode.
val useAlbumGain: (Gain) -> Boolean = when (settingsManager.replayGainMode) {
ReplayGainMode.OFF -> {
logD("ReplayGain is off.")
volume = 1f
return
}
// User wants track gain to be preferred
ReplayGainMode.TRACK ->
{ gain ->
gain.track == 0f
}
ReplayGainMode.ALBUM ->
{ gain ->
gain.album != 0f
}
ReplayGainMode.DYNAMIC ->
{ _ ->
playbackManager.parent is Album &&
playbackManager.song?.album == playbackManager.parent
}
}
val gain = parseReplayGain(metadata)
// Currently we consider both the album and the track gain.
var adjust = 0f
if (gain != null) {
// Allow the user to configure a preferred mode for ReplayGain.
adjust = if (settingsManager.replayGainMode == ReplayGainMode.TRACK) {
if (gain.track != 0f) gain.track else gain.album
val adjust = if (gain != null) {
if (useAlbumGain(gain)) {
logD("Using album gain.")
gain.album
} else {
if (gain.album != 0f) gain.album else gain.track
logD("Using track gain.")
gain.track
}
} else {
0f
}
// Final adjustment along the volume curve.
// Ensure this is clamped to 0 or 1 so that it can be used as a volume.
volume = MathUtils.clamp((10f.pow((adjust / 20f))), 0f, 1f)
logD("Applied ReplayGain adjustment: $volume")
}
private fun parseReplayGain(metadata: Metadata): Gain? {
@@ -126,14 +151,25 @@ class AudioReactor(
for (i in 0 until metadata.length()) {
val entry = metadata.get(i)
// Sometimes the ReplayGain keys will be lowercase, so make them uppercase.
if (entry is TextInformationFrame && entry.description?.uppercase() in REPLAY_GAIN_TAGS) {
tags.add(GainTag(entry.description!!.uppercase(), parseReplayGainFloat(entry.value)))
continue
val key: String?
val value: String
when (entry) {
is TextInformationFrame -> {
key = entry.description?.uppercase()
value = entry.value
}
is VorbisComment -> {
key = entry.key
value = entry.value
}
else -> continue
}
if (entry is VorbisComment && entry.key.uppercase() in REPLAY_GAIN_TAGS) {
tags.add(GainTag(entry.key.uppercase(), parseReplayGainFloat(entry.value)))
if (key in REPLAY_GAIN_TAGS) {
tags.add(GainTag(key!!, parseReplayGainFloat(value)))
}
}
@@ -3,13 +3,15 @@ package org.oxycblt.auxio.playback.system
enum class ReplayGainMode {
OFF,
TRACK,
ALBUM;
ALBUM,
DYNAMIC;
fun toInt(): Int {
return when (this) {
OFF -> INT_OFF
TRACK -> INT_TRACK
ALBUM -> INT_ALBUM
DYNAMIC -> INT_DYNAMIC
}
}
@@ -17,12 +19,14 @@ enum class ReplayGainMode {
private const val INT_OFF = 0xA110
private const val INT_TRACK = 0xA111
private const val INT_ALBUM = 0xA112
private const val INT_DYNAMIC = 0xA113
fun fromInt(value: Int): ReplayGainMode? {
return when (value) {
INT_OFF -> OFF
INT_TRACK -> TRACK
INT_ALBUM -> ALBUM
INT_DYNAMIC -> DYNAMIC
else -> null
}
}
+3
View File
@@ -36,12 +36,14 @@
<item>@string/set_replay_gain_off</item>
<item>@string/set_replay_gain_track</item>
<item>@string/set_replay_gain_album</item>
<item>@string/set_replay_gain_dynamic</item>
</array>
<string-array name="values_replay_gain">
<item>@integer/replay_gain_off</item>
<item>@integer/replay_gain_track</item>
<item>@integer/replay_gain_album</item>
<item>@integer/replay_gain_dynamic</item>
</string-array>
<integer name="theme_auto">-1</integer>
@@ -56,4 +58,5 @@
<integer name="replay_gain_off">0xA110</integer>
<integer name="replay_gain_track">0xA111</integer>
<integer name="replay_gain_album">0xA112</integer>
<integer name="replay_gain_dynamic">0xA113</integer>
</resources>
+2 -1
View File
@@ -85,10 +85,11 @@
<string name="set_focus_desc">Pause when other audio plays (ex. Calls)</string>
<string name="set_plug_mgt">Headset focus</string>
<string name="set_plug_mgt_desc">Play/Pause when the headset connection changes</string>
<string name="set_replay_gain">ReplayGain (MP3/FLAC Only)</string>
<string name="set_replay_gain">ReplayGain</string>
<string name="set_replay_gain_off">Off</string>
<string name="set_replay_gain_track">Prefer track</string>
<string name="set_replay_gain_album">Prefer album</string>
<string name="set_replay_gain_dynamic">Dynamic</string>
<string name="set_behavior">Behavior</string>
<string name="set_song_mode">When a song is selected</string>