From 92ec9cdf63618cf9e6a541ce615967172cae3cca Mon Sep 17 00:00:00 2001 From: Pauljd Date: Mon, 9 Nov 2026 17:41:21 +0000 Subject: [PATCH] Even volume: a gentle leveller on programme sound, off / overnight / always (off by default) Android's dynamics processor on the player's own sound session: quiet parts lifted a little, loud parts held back, a limiter on top. Overnight is 23:00-07:00 and starts and stops by itself. Off until Paul has listened to it; set from adb (orbit_even_volume off|overnight|always). Co-Authored-By: Claude Opus 5.5 --- .../tv/orbit/classic/EmbyPlaybackDialog.kt | 12 +++++++ .../java/tv/orbit/classic/MainActivity.kt | 1 + .../java/tv/orbit/classic/PlayerSwitches.kt | 32 ++++++++++++++++++- .../tv/orbit/classic/PlayerSwitchesTest.kt | 9 ++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt index 798b4998..b88526d6 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt @@ -322,6 +322,18 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl } } DisposableEffect(player) { onDispose { watchAudioClock(false) } } + // Even volume (9 November 2026; off unless switched on): checked each minute, so "overnight" starts and stops by itself. + LaunchedEffect(player) { + var leveller: android.media.audiofx.DynamicsProcessing? = null + try { + while (true) { + val want = EvenVolume.activeNow() + if (want && leveller == null && player.audioSessionId != C.AUDIO_SESSION_ID_UNSET) leveller = EvenVolume.attach(player.audioSessionId)?.also { it.enabled = true } + leveller?.enabled = want + delay(60_000) + } + } finally { runCatching { leveller?.release() } } + } // Frame-rate matching (9 November 2026; off unless switched on): the TV's refresh follows the programme's, and goes // back to the TV's own choice when the programme closes. Full screen is a dialog window over the activity, so the // preference is set on both: Android takes it from the window on top. diff --git a/android/app/src/main/java/tv/orbit/classic/MainActivity.kt b/android/app/src/main/java/tv/orbit/classic/MainActivity.kt index 39a6aa3f..54a3027e 100644 --- a/android/app/src/main/java/tv/orbit/classic/MainActivity.kt +++ b/android/app/src/main/java/tv/orbit/classic/MainActivity.kt @@ -72,6 +72,7 @@ class MainActivity : ComponentActivity() { when (intent?.getStringExtra("orbit_provider_catalogue")) { "on" -> ProviderSwitches.setCatalogue(this, true); "off" -> ProviderSwitches.setCatalogue(this, false) } // Player improvements (9 November 2026): frame-rate matching and AVI, each off until switched on. when (intent?.getStringExtra("orbit_frame_rate")) { "on" -> PlayerSwitches.setFrameRate(this, true); "off" -> PlayerSwitches.setFrameRate(this, false) } + intent?.getStringExtra("orbit_even_volume")?.let { PlayerSwitches.setEvenVolume(this, it) } when (intent?.getStringExtra("orbit_avi")) { "on" -> PlayerSwitches.setAvi(this, true); "off" -> PlayerSwitches.setAvi(this, false) } if (intent?.getBooleanExtra("orbit_shadow_catalogue_stop", false) == true) CatalogueShadow.cancel = true if (intent?.hasExtra("orbit_shadow_from") == true) CatalogueShadow.from = intent.getLongExtra("orbit_shadow_from", 0L).coerceAtLeast(0L) diff --git a/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt b/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt index f8603eda..37bb289a 100644 --- a/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt +++ b/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt @@ -16,10 +16,17 @@ import kotlin.math.roundToInt internal object PlayerSwitches { @Volatile var frameRate = false @Volatile var avi = false + /** Even volume (9 November 2026): "off", "overnight" (23:00 to 07:00) or "always". Off until Paul has listened to it. */ + @Volatile var evenVolume = "off" private const val FRAME_RATE = "player_frame_rate" private const val AVI = "player_avi" + private const val EVEN_VOLUME = "player_even_volume" private fun prefs(context: Context) = context.getSharedPreferences("orbit_settings", Context.MODE_PRIVATE) - fun load(context: Context) { prefs(context).let { frameRate = it.getBoolean(FRAME_RATE, false); avi = it.getBoolean(AVI, false) } } + fun load(context: Context) { prefs(context).let { frameRate = it.getBoolean(FRAME_RATE, false); avi = it.getBoolean(AVI, false); evenVolume = it.getString(EVEN_VOLUME, "off") ?: "off" } } + fun setEvenVolume(context: Context, mode: String) { + if (mode !in setOf("off", "overnight", "always")) return + evenVolume = mode; prefs(context).edit().putString(EVEN_VOLUME, mode).apply(); android.util.Log.i("OrbitPlayer", "evenVolume switch=$mode") + } fun setFrameRate(context: Context, on: Boolean) { frameRate = on; prefs(context).edit().putBoolean(FRAME_RATE, on).apply(); log("frameRate", on) } fun setAvi(context: Context, on: Boolean) { avi = on; prefs(context).edit().putBoolean(AVI, on).apply(); log("avi", on) } private fun log(name: String, on: Boolean) = android.util.Log.i("OrbitPlayer", "$name switch=${if (on) "on" else "off"}") @@ -96,3 +103,26 @@ internal class FrameRateProbe(private val onRate: (Float) -> Unit) : androidx.me } } } + +/** + * Even volume (9 November 2026): a gentle leveller on the programme's sound, so a loud film after a quiet cartoon, or + * explosions after whispers, don't make you reach for the remote. Android's own dynamics processor on the player's + * sound: quiet parts lifted a little, loud parts held back, and a limiter at the top. Stereo and decoded sound only; + * sound sent straight to a soundbar is left alone. Nothing changes when it's off. + */ +internal object EvenVolume { + fun activeAt(mode: String, hour: Int) = mode == "always" || (mode == "overnight" && (hour >= 23 || hour < 7)) + fun activeNow(mode: String = PlayerSwitches.evenVolume) = activeAt(mode, java.util.Calendar.getInstance().get(java.util.Calendar.HOUR_OF_DAY)) + + /** The leveller for a player's sound session, or null where the TV doesn't offer it. */ + fun attach(audioSessionId: Int): android.media.audiofx.DynamicsProcessing? = if (android.os.Build.VERSION.SDK_INT < 28) null else runCatching { + val config = android.media.audiofx.DynamicsProcessing.Config.Builder(android.media.audiofx.DynamicsProcessing.VARIANT_FAVOR_FREQUENCY_RESOLUTION, + 2, false, 0, true, 1, false, 0, true).build() + for (ch in 0 until 2) { + config.setMbcBandByChannelIndex(ch, 0, android.media.audiofx.DynamicsProcessing.MbcBand(true, 20_000f, + 10f, 300f, 3f, -30f, 0f, -90f, 1f, 0f, 6f)) + config.setLimiterByChannelIndex(ch, android.media.audiofx.DynamicsProcessing.Limiter(true, true, 0, 1f, 60f, 10f, -2f, 0f)) + } + android.media.audiofx.DynamicsProcessing(0, audioSessionId, config) + }.getOrNull() +} diff --git a/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt b/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt index d03195f5..3390269b 100644 --- a/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt @@ -3,6 +3,8 @@ package tv.orbit.classic import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Assert.assertFalse import org.junit.Test /** Frame-rate matching and AVI (9 November 2026). The modes are the downstairs TV's own, read with dumpsys display. */ @@ -77,4 +79,11 @@ class PlayerSwitchesTest { assertNull(streamFrameRate(streams())) assertNull(streamFrameRate(null)) } + + @Test fun evenVolumeHours() { + assertFalse(EvenVolume.activeAt("off", 23)); assertTrue(EvenVolume.activeAt("always", 14)) + assertTrue(EvenVolume.activeAt("overnight", 23)); assertTrue(EvenVolume.activeAt("overnight", 3)) + assertFalse(EvenVolume.activeAt("overnight", 7)); assertFalse(EvenVolume.activeAt("overnight", 22)) + assertEquals("off", PlayerSwitches.evenVolume) // off until Paul has listened to it + } }