Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Duck the reverb with the song

Leloniob1348ecafc07
diff --git a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
index 94cf328..93bbdf8 100644
--- a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
@@ -283,6 +283,7 @@ class SpotifyBackend(private val container: SquareApplication) : MusicBackend {
audioOutput::fadeOutThen,
audioOutput::fadeIn,
audioOutput::setPlaybackActive,
+ audioOutput::duckReverb,
)
}
diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
index 279c906..9f9eaa1 100644
--- a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
@@ -154,6 +154,41 @@ class AudioOutput {
* — a notification, another app — arrives into a room it was never meant to
* be in. So the effect is only alive while this player is actually playing.
*/
+ /**
+ * How far the room is held down while something else is talking.
+ *
+ * 1 at rest. The reverb is an auxiliary effect on the output mix and its
+ * send does not pass through the track's volume — which is the whole reason
+ * it can be attached at all — so a duck that quietens the music leaves the
+ * room at full level, and a song ducked under a voice note comes back as
+ * mostly reverb. See [duckReverb].
+ */
+ private var reverbDuck = 1f
+
+ /**
+ * Whether the room is inside this track's session rather than on the mix.
+ *
+ * A session effect needs no send level and no attachment: it is part of the
+ * track. See [applyReverb] for why that is worth preferring.
+ */
+ private var reverbOnSession = false
+
+ /**
+ * Holds the room down by the same amount as everything else.
+ *
+ * Called from the player's own ducking, since that is where the decision is
+ * made; the level is re-applied rather than remembered separately, so the
+ * listener moving the reverb slider mid-duck still lands in the right place.
+ */
+ fun duckReverb(factor: Float) {
+ synchronized(this) {
+ val wanted = factor.coerceIn(0f, 1f)
+ if (wanted == reverbDuck) return
+ reverbDuck = wanted
+ if (reverbAmount > 0f) applyReverb()
+ }
+ }
+
private fun applyReverb() {
val output = track
val amount = reverbAmount
@@ -172,12 +207,40 @@ class AudioOutput {
}
val fresh = reverb == null
- val effect = reverb ?: runCatching {
- // Priority 0: no reason to outbid anything else on the mix.
- EnvironmentalReverb(0, 0)
- }.onFailure {
- android.util.Log.w(TAG, "reverb unavailable: ${it.message}")
- }.getOrNull() ?: return
+ // On this track's own session where the device allows it, on the output
+ // mix where it does not.
+ //
+ // An effect on the output mix is fed *before* the level of the track
+ // that feeds it, which is what lets it be attached at all — and what
+ // made a ducked song come back as a whisper in a cathedral: from
+ // Android 8 the system ducks a track by itself, without telling the
+ // app, so the music dropped and the room did not. An effect on the
+ // session is processed inside the track, so whatever attenuates the
+ // track afterwards attenuates the room with it, whoever does the
+ // attenuating.
+ val effect = reverb ?: run {
+ val session = runCatching { output.audioSessionId }.getOrDefault(0)
+ val made = if (session != 0) {
+ runCatching { EnvironmentalReverb(0, session) }
+ .onSuccess {
+ reverbOnSession = true
+ android.util.Log.i(TAG, "reverb inside this session ($session)")
+ }
+ .onFailure {
+ android.util.Log.w(TAG, "no reverb on the session: ${it.message}")
+ }
+ .getOrNull()
+ } else {
+ null
+ }
+ made ?: runCatching {
+ // Priority 0: no reason to outbid anything else on the mix.
+ reverbOnSession = false
+ EnvironmentalReverb(0, 0)
+ }.onFailure {
+ android.util.Log.w(TAG, "reverb unavailable: ${it.message}")
+ }.getOrNull()
+ } ?: return
reverb = effect
runCatching {
@@ -186,8 +249,12 @@ class AudioOutput {
ReverbTuning.tune(effect, amount)
effect.enabled = true
- output.attachAuxEffect(effect.id)
- output.setAuxEffectSendLevel(ReverbTuning.sendLevel(amount))
+ // A session effect is already in the path; only an auxiliary one
+ // has to be sent to.
+ if (!reverbOnSession) {
+ output.attachAuxEffect(effect.id)
+ output.setAuxEffectSendLevel(ReverbTuning.sendLevel(amount) * reverbDuck)
+ }
if (fresh) android.util.Log.i(TAG, "reverb on at $amount, id ${effect.id}")
}.onFailure { android.util.Log.w(TAG, "reverb not applied: ${it.message}") }
}
@@ -272,10 +339,15 @@ class AudioOutput {
}
private fun suspendReverb() {
- track?.runCatching {
- setAuxEffectSendLevel(0f)
- attachAuxEffect(0)
+ // Only an auxiliary effect has a send to close; a session one goes with
+ // the track it lives in.
+ if (!reverbOnSession) {
+ track?.runCatching {
+ setAuxEffectSendLevel(0f)
+ attachAuxEffect(0)
+ }
}
+ reverbOnSession = false
dropReverb()
}
diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
index 073d346..e48f542 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -53,6 +53,14 @@ class LibrespotPlayer(
* AudioOutput.setPlaybackActive.
*/
private val onPlaybackActive: (Boolean) -> Unit,
+ /**
+ * Holds the reverb down while the music is ducked.
+ *
+ * Separate from the volume below because it has to be: the room is an
+ * effect on the output mix, and its send is fed before the level this
+ * player sets. See AudioOutput.duckReverb.
+ */
+ private val onDuckReverb: (Float) -> Unit,
) : SimpleBasePlayer(looper), NativeEvents {
/**
@@ -700,6 +708,7 @@ class LibrespotPlayer(
* verbatim and restored rather than recomputed.
*/
private fun applyDuck(ducked: Boolean) {
+ onDuckReverb(if (ducked) DUCK_FACTOR.toFloat() else 1f)
if (ducked) {
if (volumeBeforeDuck != null) return
val current = runCatching { NativeBridge.volume }.getOrNull() ?: return
diff --git a/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.kt b/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.kt
index fd2a518..f78c068 100644
--- a/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.kt
@@ -20,6 +20,34 @@ class PlayerReverb {
private var effect: EnvironmentalReverb? = null
+ /** What the room is set to, so a change of level can re-apply it. */
+ private var amount = 0f
+
+ /**
+ * Follows the player's own level.
+ *
+ * ExoPlayer ducks by turning itself down, and the send that feeds this
+ * room is not part of that: a song ducked under a voice note would come
+ * back as music at a third and reverb at full, which reads as the room
+ * suddenly swallowing the track. The send is held down by the same amount.
+ */
+ private var watched: ExoPlayer? = null
+ private val level = object : androidx.media3.common.Player.Listener {
+ override fun onVolumeChanged(volume: Float) {
+ val player = watched ?: return
+ val room = amount
+ if (room <= 0f) return
+ runCatching {
+ player.setAuxEffectInfo(
+ AuxEffectInfo(
+ effect?.id ?: return,
+ ReverbTuning.sendLevel(room) * volume.coerceIn(0f, 1f),
+ ),
+ )
+ }
+ }
+ }
+
/**
* Puts [amount] of room on [player], or takes it away at zero.
*
@@ -29,11 +57,18 @@ class PlayerReverb {
*/
fun apply(player: ExoPlayer, amount: Float) {
val level = amount.coerceIn(0f, 1f)
+ this.amount = level
if (level <= 0f) {
detach(player)
return
}
+ if (watched !== player) {
+ watched?.runCatching { removeListener([email protected]) }
+ watched = player
+ runCatching { player.addListener(this.level) }
+ }
+
val existing = effect
val reverb = existing ?: runCatching {
// Priority 0 and the output mix, as on the other path.
@@ -47,7 +82,12 @@ class PlayerReverb {
runCatching {
ReverbTuning.tune(reverb, level)
reverb.enabled = true
- player.setAuxEffectInfo(AuxEffectInfo(reverb.id, ReverbTuning.sendLevel(level)))
+ player.setAuxEffectInfo(
+ AuxEffectInfo(
+ reverb.id,
+ ReverbTuning.sendLevel(level) * player.volume.coerceIn(0f, 1f),
+ ),
+ )
if (existing == null) android.util.Log.i(TAG, "reverb on at $level, id ${reverb.id}")
}.onFailure { android.util.Log.w(TAG, "reverb not applied: ${it.message}") }
}
@@ -63,6 +103,10 @@ class PlayerReverb {
}
private fun detach(player: ExoPlayer) {
+ if (watched === player) {
+ runCatching { player.removeListener(level) }
+ watched = null
+ }
runCatching {
player.setAuxEffectInfo(AuxEffectInfo(AuxEffectInfo.NO_AUX_EFFECT_ID, 0f))
}