Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
AutoMix: arm a segue's outgoing lanes by the audio chain's position
kota-zo425ff335a66f
diff --git a/base/app/src/main/java/com/music/spotui/automix/MixPreparation.kt b/base/app/src/main/java/com/music/spotui/automix/MixPreparation.ktindex b2b923ce..d24fa9a8 100644--- a/base/app/src/main/java/com/music/spotui/automix/MixPreparation.kt+++ b/base/app/src/main/java/com/music/spotui/automix/MixPreparation.kt@@ -13,12 +13,19 @@ internal object MixPreparation {playing && bufferedMs >= minOf(BUFFER_MS, remainingMs.coerceAtLeast(0)) &&(!synced || phaseMs != null && kotlin.math.abs(phaseMs) <= 80.0)- /** A plan's outgoing lanes are armed this long before its cue at most, and at least: past the player's buffer, or they would come late. */+ /**+ * A plan's outgoing lanes are armed this long before the audio chain reaches the cue, at most and+ * at least. It is the chain's position that counts: the chain runs ahead of what is heard by the+ * player's whole buffer (seconds on some devices), and what it has passed can no longer change.+ */const val SEGUE_ARM_LEAD_MS = 1_500L- const val SEGUE_ARM_MIN_MS = 1_000L+ const val SEGUE_ARM_MIN_MS = 500L/** How far ahead of what is heard the audio chain may be working. */private const val SEGUE_PLACE_SLACK_MS = 5_000L+ /** How long, in ms of the song, before the chain reaches the cue. */+ fun chainLeadMs(cueUs: Long, chainPositionUs: Long): Long = (cueUs - chainPositionUs) / 1000+/** The audio chain's idea of the song position agrees with the player's: its automation will land where the plan says. */fun placed(chainPositionUs: Long, playerPositionMs: Long): Boolean =kotlin.math.abs(chainPositionUs / 1000 - playerPositionMs) <= SEGUE_PLACE_SLACK_MSdiff --git a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.ktindex 2d40e604..51d6a98c 100644--- a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt+++ b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt@@ -724,21 +724,25 @@ object LocalPlayer {if (!com.music.spotui.automix.AutoMix.segueActive) return com.music.spotui.automix.AutoMix.planAfter(ra, rb, earliest)val album = a.album.trim()val sameAlbum = com.music.spotui.automix.AutoMix.albumOrder && album.isNotEmpty() && !album.equals("<unknown>", true) && album.equals(b.album.trim(), true)- val d = com.music.spotui.automix.AutoMix.decide(ra, rb, earliest, sameAlbum, rate())+ // Its lanes must be armed before the audio chain, ahead of the player, reaches the cue.+ val chainAheadMs = exo?.let { p -> segueStages[p]?.takeIf { it.marked }?.let { (it.positionUs / 1000 - p.currentPosition).coerceAtLeast(0) } } ?: 0L+ val d = com.music.spotui.automix.AutoMix.decide(ra, rb, earliest + (chainAheadMs + MixPreparation.SEGUE_ARM_LEAD_MS) / 1000.0, sameAlbum, rate())com.music.spotui.automix.AutoMix.journal("segue ${d.level} ${d.fingerprint?.take(12) ?: ""}${if (d.attempts.isEmpty()) "" else ": ${d.why}"}")return app.myspoti.automix.segue.SeguePolicy.timing(d)}/*** A data plan's last check ahead of its cue: both songs placed and buffered. The outgoing lanes are- * armed early enough for what is still ahead of the player's buffer. Refused, the deck goes while- * the song is still whole, and a late failure falls back on the plain fade as any other deck.+ * armed before the audio chain reaches the cue. Refused, the deck goes while the song is still+ * whole, and a late failure falls back on the plain fade as any other deck.*/private fun segueGo(run: MixRun, p: ExoPlayer, pos: Long, exitMs: Long) {val plan = run.plan.segue ?: return- if (run.go || exitMs - pos > MixPreparation.SEGUE_ARM_LEAD_MS) return+ if (run.go) returnval d = run.deckval stageA = segueStages[p]; val stageB = segueStages[d]+ val lead = if (stageA?.marked == true) MixPreparation.chainLeadMs(plan.outgoing.anchorUs, stageA.positionUs) else exitMs - pos+ if (lead > MixPreparation.SEGUE_ARM_LEAD_MS) returnval remaining = (d.duration.takeIf { it > 0 } ?: Long.MAX_VALUE) - d.currentPositionval ready = stageA != null && stageB != null && stageA.marked && stageB.marked &&MixPreparation.segueReady(d.playbackState == Player.STATE_READY, d.totalBufferedDuration, remaining,@@ -748,9 +752,9 @@ object LocalPlayer {stageA!!.arm(plan, app.myspoti.automix.segue.LaneSide.OUTGOING)d.volume = 1frun.go = true- com.music.spotui.automix.AutoMix.journal("segue armed ${exitMs - pos} ms ahead, chain at ${stageA.positionUs / 1000} ms, player at $pos ms")- } else if (exitMs - pos < MixPreparation.SEGUE_ARM_MIN_MS) {- cancelMix("segue not ready ${exitMs - pos} ms ahead (chain ${stageA?.marked}/${stageB?.marked}, buffered ${d.totalBufferedDuration} ms); replanning")+ com.music.spotui.automix.AutoMix.journal("segue armed: the chain is $lead ms before the cue and ${stageA.positionUs / 1000 - pos} ms ahead of the player, the cue ${exitMs - pos} ms away")+ } else if (lead < MixPreparation.SEGUE_ARM_MIN_MS) {+ cancelMix("segue not ready $lead ms before the cue (chain ${stageA?.marked}/${stageB?.marked}, buffered ${d.totalBufferedDuration} ms); replanning")}}diff --git a/base/app/src/test/java/com/music/spotui/automix/SegueProcessorTest.kt b/base/app/src/test/java/com/music/spotui/automix/SegueProcessorTest.ktindex 2bff7423..6c19d709 100644--- a/base/app/src/test/java/com/music/spotui/automix/SegueProcessorTest.kt+++ b/base/app/src/test/java/com/music/spotui/automix/SegueProcessorTest.kt@@ -99,6 +99,12 @@ class SegueProcessorTest {assertEquals(AudioProcessor.AudioFormat.NOT_SET, SegueProcessor().configure(AudioProcessor.AudioFormat(rate, 2, C.ENCODING_AC3)))}+ @Test fun theLeadIsCountedFromTheChainNotThePlayer() {+ // The player at 103.7 s, its chain 3 s ahead at 106.8 s: a cue at 105.2 s has already gone by.+ assertTrue(MixPreparation.chainLeadMs(105_220_000, 106_811_000) < MixPreparation.SEGUE_ARM_MIN_MS)+ assertEquals(1_500, MixPreparation.chainLeadMs(105_000_000, 103_500_000))+ }+@Test fun theChainAndThePlayerMustAgreeOnThePlace() {assertTrue(MixPreparation.placed(61_000_000, 60_000))assertTrue(MixPreparation.placed(60_000_000, 60_000))diff --git a/docs/automix/README.md b/docs/automix/README.mdindex 63dac413..4daff57d 100644--- a/docs/automix/README.md+++ b/docs/automix/README.md@@ -46,7 +46,7 @@ mobile data" is on, never under the data saver), to mid and side at 22.05 kHz.structural boundaries and an 8-bar phrase fallback; key (Krumhansl–Kessler on the chroma),loudness, where the sound starts and ends. Voice-like activity uses existing spectral features,including absolute centre and mono uncertainty: it is a heuristic, not a vocal separation model.-- The result is cached (`cache/automix4/`, format 4_08), about 10 KB a song. Source metadata/path+- The result is cached (`cache/automix4/`, format 4_09), about 10 KB a song. Source metadata/pathand local file size/modification time identify the recording; expiring access tokens do not.- Speed: on the S24, 5 to 11 s a song (decoding 3–6 s, analysis 3–5 s). The decoder never waits onthe codec while it has work; the onset features (in slices) and Beat This (its 15-s chunks side bydiff --git a/docs/automix/segue-design.md b/docs/automix/segue-design.mdindex acc84842..7c22dd9d 100644--- a/docs/automix/segue-design.md+++ b/docs/automix/segue-design.md@@ -124,9 +124,11 @@ the lanes stay on the song's musical time. Unarmed, the stage copies its input ein the Mix Lab build; other builds keep their chain as it was.- The incoming deck is armed when prepared: before its window its level lane is null, so its run-up is silent.-- The outgoing side is armed `SEGUE_ARM_LEAD_MS` before the cue, once both songs are placed and- buffered, ahead of what the player has already buffered. Past `SEGUE_ARM_MIN_MS` without that, the- deck is dropped while the song is still whole, and the plan is computed again.+- The outgoing side is armed `SEGUE_ARM_LEAD_MS` before its audio chain reaches the cue, once both+ songs are placed and buffered. The chain runs ahead of the player by its whole buffer, from 0.5 to 3 s+ on the emulator, and what it has passed can no longer change, so this is counted from the chain's position.+ Under `SEGUE_ARM_MIN_MS` without that, the deck is dropped while the song is still whole, and the+ plan is computed again.- If the incoming song stalls during the window, the outgoing one returns to its plain sound(`holdRampUs`), then both re-enter through the existing gentle fade.- After the window the incoming tempo returns to its own as with other plans (`restoreRatePerBeat`,@@ -207,8 +209,9 @@ covers the objective part, in the protocol's order:silent holes), then compares the phase measured on the render with the plan's `residualUs`.- `matrix` reports each pair's level and the causes of the levels left behind, and that a second rungives the same fingerprint.-- `stability` varies each parameter by ±20 % `[I]` and lists those that change nothing: candidates- for removal.+- `stability` varies each parameter by ±20 % `[I]` and lists those that change no plan. Among the+ policy's parameters these are candidates for removal; the sound's own (filters, sends) never change a+ plan and are judged by ear.Still to do, as they need people or annotations: fixing thresholds by controlled degradations in blindlistening; learning the weights from pairwise comparisons with a simple non-negative model,