-
-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement normalization gain for music tracks
- Loading branch information
1 parent
f0554b2
commit 4727056
Showing
4 changed files
with
84 additions
and
11 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
playback/core/src/main/kotlin/mediastream/normalizationGainElement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.jellyfin.playback.core.mediastream | ||
|
||
import org.jellyfin.playback.core.element.ElementKey | ||
import org.jellyfin.playback.core.element.element | ||
import org.jellyfin.playback.core.queue.QueueEntry | ||
|
||
private val normalizationGainKey = ElementKey<Float>("NormalizationGain") | ||
|
||
/** | ||
* Get or set the normalization gain for this [QueueEntry]. A supported backend will use this to | ||
* apply a gain to the audio output. The normalization gain must target a loudness of -23LUFS. | ||
*/ | ||
var QueueEntry.normalizationGain by element(normalizationGainKey) |
37 changes: 37 additions & 0 deletions
37
playback/exoplayer/src/main/kotlin/ExoPlayerAudioPipeline.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.jellyfin.playback.exoplayer | ||
|
||
import android.media.audiofx.LoudnessEnhancer | ||
import timber.log.Timber | ||
|
||
class ExoPlayerAudioPipeline { | ||
private var loudnessEnhancer: LoudnessEnhancer? = null | ||
var normalizationGain: Float? = null | ||
set(value) { | ||
Timber.d("Normalization gain changed to $value") | ||
field = value | ||
applyGain() | ||
} | ||
|
||
fun setAudioSessionId(audioSessionId: Int) { | ||
Timber.d("Audio session id changed to $audioSessionId") | ||
|
||
// Re-creare loudness enhancer for normalization gain | ||
loudnessEnhancer?.release() | ||
loudnessEnhancer = LoudnessEnhancer(audioSessionId) | ||
|
||
// Re-apply current normalization gain | ||
applyGain() | ||
} | ||
|
||
private fun applyGain() { | ||
val targetGain = normalizationGain | ||
// Convert to millibels | ||
?.times(100f) | ||
// Round to integer | ||
?.toInt() | ||
|
||
Timber.d("Applying gain (targetGain=$targetGain)") | ||
loudnessEnhancer?.setEnabled(targetGain != null) | ||
loudnessEnhancer?.setTargetGain(targetGain ?: 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters