Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Play Spotify playlists as a Mix
Lelonio1fe0d94445f2
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.ktindex 46a9bcf..00069de 100644--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt@@ -465,6 +465,25 @@ object NativeBridge {*/fun playlistCover(uri: String): String = nativePlaylistCover(uri)+ /** A playlist's Spotify Mix, as `{mixed, transitions}`, or null; see catalog.rs. */+ fun playlistMix(uri: String): String? =+ runCatching { nativePlaylistMix(uri) }.getOrNull()?.takeIf { it != "null" }++ /** Tempo and key of many tracks, as a JSON object keyed by URI; see catalog.rs. */+ fun tracksAudioAttributes(urisJson: String): String? =+ runCatching { nativeTracksAudioAttributes(urisJson) }+ .onFailure { android.util.Log.w("SquareMix", "tempo and key unavailable: ${it.message}") }+ .getOrNull()++ /** What the automatic transitions of a few tracks are worked out from, keyed by URI; see catalog.rs. */+ fun tracksMixAnalysis(urisJson: String): String? =+ runCatching { nativeTracksMixAnalysis(urisJson) }+ .onFailure { android.util.Log.w("SquareMix", "mix analysis unavailable: ${it.message}") }+ .getOrNull()++ /** The transition into the next song, `{from, outMs, fadeMs, to, inMs}`, or "" for none. */+ fun setMixPlan(json: String) = runCatching { nativeSetMixPlan(json) }+/*** A playlist's title, for one arriving by link rather than from the account.*/@@ -477,6 +496,18 @@ object NativeBridge {private external fun nativeFriendActivity(): Stringprivate external fun nativePlaylistCover(uri: String): String++ private external fun nativePlaylistMix(uri: String): String+++ private external fun nativeTracksAudioAttributes(uris: String): String++++ private external fun nativeTracksMixAnalysis(uris: String): String+++ private external fun nativeSetMixPlan(json: String)private external fun nativePlaylistName(uri: String): Stringprivate external fun nativePlaylistMine(uri: String): Stringprivate external fun nativeInitContext(context: android.content.Context)diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.ktindex c8977e6..5a5d4c3 100644--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt@@ -284,6 +284,15 @@ class LibrespotPlayer(/** The last one handed over, so a run of events asks for it once. */private var unknownAsked: String? = null+ /** The pair the engine was last given a mix transition for, "" for none. */+ private var mixPlanFor: String? = null++ init {+ // A mix switched on, off, or read late: the plan is worked out again+ // on this player's own thread.+ Mixes.onChange = { handler.post { if (!released) followMix() } }+ }+fun showRemote(playback: dev.lelonio.square.data.RemotePlayback?) {if (released) returnval was = remote@@ -1255,6 +1264,9 @@ class LibrespotPlayer(// whose listens are filed under nothing and whose player has no source// to show.queue.restoreContext(contextUri, contextIsOrdered, contextLabel)+ // A queue put back after a restart says what it plays before any+ // event from the engine does; see Mixes.playing.+ Mixes.follow(queue.contextUri)queue.replace(tracks, 0)engineIndex = -1@@ -1629,6 +1641,31 @@ class LibrespotPlayer(requestSkipTo(next, 0L)}+ /**+ * Gives the engine the transition into the song after this one, when the+ * playlist playing is mixed and has one for exactly this pair; takes it+ * away otherwise. See Mixes and librespot-playback's MixPlan.+ *+ * Asked on every event, since the pair changes with the song, a skip, a+ * reorder or the mix being switched off, and the answer is a lookup;+ * the engine is only told when it changes.+ */+ private fun followMix() {+ Mixes.follow(queue.contextUri)+ val current = queue.items.getOrNull(queue.currentIndex)?.uri+ val next = queue.items.getOrNull(queue.currentIndex + 1)?.uri+ val transition = if (current != null && next != null && repeatMode != Player.REPEAT_MODE_ONE) {+ Mixes.transition(queue.contextUri, current, next)+ } else {+ null+ }+ val key = transition?.let { "${it.from}>${it.to}" }.orEmpty()+ if (key == mixPlanFor) return+ mixPlanFor = key+ android.util.Log.i("SquarePlayer", "mix plan: ${transition?.planJson() ?: "none"} (context ${queue.contextUri})")+ NativeBridge.setMixPlan(transition?.planJson().orEmpty())+ }+private fun applyEvent(type: String, uri: String, eventPositionMs: Long) {if (released) return@@ -1711,6 +1748,8 @@ class LibrespotPlayer(}}+ if (uri.isNotEmpty()) followMix()+when (type) {"loading" -> {engineStopped = falsediff --git a/app/src/main/java/dev/lelonio/square/playback/Mixes.kt b/app/src/main/java/dev/lelonio/square/playback/Mixes.ktnew file mode 100644index 0000000..003d608--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/playback/Mixes.kt@@ -0,0 +1,347 @@+package dev.lelonio.square.playback++import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.flow.asStateFlow+import dev.lelonio.square.nativecore.NativeBridge+import org.json.JSONObject+import java.util.concurrent.ConcurrentHashMap++/**+ * Spotify's Mix, as Square reads and plays it.+ *+ * A playlist mixed in Spotify's own app carries its transitions: on the row+ * a transition starts from, where the song hands over, where the next comes+ * in and how long the two overlap. They are read through the playlist's `mix`+ * lens (see catalog.rs, playlist_mix) and kept here, for every playlist whose+ * mix is on, by the playlist's URI. The player asks for the pair it is about+ * to play and hands the engine that one transition; see LibrespotPlayer.+ */+object Mixes {++ /** One transition, from [from] into [to]. Times in milliseconds. */+ data class Transition(+ /** The row of the playlist it starts from. */+ val index: Int,+ val from: String,+ val to: String,+ /** Where [from] starts handing over. */+ val outMs: Long,+ /** Where [to] comes in. */+ val inMs: Long,+ /** How long the two play together. */+ val fadeMs: Long,+ /** The tempo both are brought to, when Spotify matched them; 0 when it did not. */+ val bpm: Float,+ /** Spotify's number for the transition's style. Its names are not known. */+ val preset: Int?,+ /** How fast [to] plays during the overlap, to match [from]'s beats. */+ val speedIn: Float = 1f,+ ) {+ /** What the engine is told; see librespot-playback's MixPlan. */+ fun planJson(): String = JSONObject()+ .put("from", from)+ .put("to", to)+ .put("outMs", outMs)+ .put("inMs", inMs)+ .put("fadeMs", fadeMs)+ .put("speedIn", speedIn.toDouble())+ .toString()+ }++ /** A playlist's mix: whether Spotify has it mixed, and the transitions saved with it. */+ data class PlaylistMix(+ val mixed: Boolean,+ val transitions: List<Transition>,+ /** The songs' lengths, which the automatic transitions fit inside. */+ val durations: Map<String, Long> = emptyMap(),+ )++ /**+ * What a song's automatic transitions are worked out from; see catalog.rs,+ * tracks_mix_analysis. Times in milliseconds.+ */+ class Analysis(+ val lengthMs: Long,+ /** Where each bar starts. */+ val downbeats: LongArray,+ /** Where the song is first and last really sounding. */+ val fadeInMs: Long,+ val fadeOutMs: Long,+ /** The singing, 0 to 100, every tenth of a second. */+ val vocals: IntArray,+ /** Spotify's suggested points, by kind: 1 and 2 to leave, 3 and 4 to come in. */+ val points: Map<Int, LongArray>,+ /** Where each part of the song starts. */+ val sections: LongArray,+ ) {+ /** How much singing there is between [from] and [to], 0 to 1. */+ fun singing(from: Long, to: Long): Double {+ val first = (from / 100).toInt().coerceAtLeast(0)+ val last = (to / 100).toInt().coerceAtMost(vocals.size)+ if (last <= first) return 0.0+ var sum = 0+ for (i in first until last) sum += vocals[i]+ return sum / (100.0 * (last - first))+ }+ }++ /** A track's tempo and key, as Spotify's Mix shows them. */+ data class Attributes(val bpm: Float, val camelot: String)++ fun parseMix(json: String?): PlaylistMix? = runCatching {+ val root = JSONObject(json ?: return null)+ val list = root.optJSONArray("transitions")+ val transitions = (0 until (list?.length() ?: 0)).mapNotNull { i ->+ val t = list!!.getJSONObject(i)+ Transition(+ index = t.optInt("index", -1),+ from = t.getString("from"),+ to = t.getString("to"),+ outMs = t.getLong("outMs"),+ inMs = t.optLong("inMs", 0),+ fadeMs = t.getLong("fadeMs"),+ bpm = t.optDouble("bpmB", 0.0).toFloat()+ .takeIf { t.optInt("kind", 0) != 0 } ?: 0f,+ preset = t.optInt("preset", -1).takeIf { it >= 0 },+ // Only a beatmatched transition moves the tempo: kind 0 is a+ // plain fade, whatever speed the recipe records.+ speedIn = if (t.optInt("kind", 0) != 0) {+ (t.optDouble("speedB", 1.0) / t.optDouble("speedA", 1.0)).toFloat()+ } else {+ 1f+ },+ ).takeIf { it.fadeMs > 0 && it.outMs > 0 }+ }+ PlaylistMix(root.optBoolean("mixed", false), transitions)+ }.getOrNull()++ fun parseAnalyses(json: String?): Map<String, Analysis> = runCatching {+ val root = JSONObject(json ?: return emptyMap())+ root.keys().asSequence().map { uri ->+ val a = root.getJSONObject(uri)+ fun longs(name: String) = a.optJSONArray(name).let { list ->+ LongArray(list?.length() ?: 0) { list!!.getLong(it) }+ }+ val points = a.optJSONArray("points")+ val byKind = (0 until (points?.length() ?: 0))+ .map { points!!.getJSONArray(it) }+ .groupBy({ it.getInt(0) }, { it.getLong(1) })+ .mapValues { (_, list) -> list.toLongArray() }+ val vocals = a.optJSONArray("vocals")+ uri to Analysis(+ lengthMs = a.optLong("lengthMs"),+ downbeats = longs("downbeats"),+ fadeInMs = a.optLong("fadeInMs"),+ fadeOutMs = a.optLong("fadeOutMs"),+ vocals = IntArray(vocals?.length() ?: 0) { vocals!!.getInt(it) },+ points = byKind,+ sections = longs("sections"),+ )+ }.toMap()+ }.getOrDefault(emptyMap())++ fun parseAttributes(json: String?): Map<String, Attributes> = runCatching {+ val root = JSONObject(json ?: return emptyMap())+ root.keys().asSequence().mapNotNull { uri ->+ val a = root.getJSONObject(uri)+ val bpm = a.optDouble("bpm", 0.0).toFloat()+ if (bpm <= 0f) null else uri to Attributes(bpm, a.optString("camelot"))+ }.toMap()+ }.getOrDefault(emptyMap())++ /** The mixes that are on, by playlist URI. */+ private val playingMixes = ConcurrentHashMap<String, PlaylistMix>()++ private val _playing = MutableStateFlow(false)++ /**+ * Whether what is playing is a playlist with its mix on, for the player's+ * Play button, which wears the mix's ring while it is. Kept by the player.+ */+ val playing: StateFlow<Boolean> = _playing.asStateFlow()++ /** Called by the player with the context it is playing; null when none. */+ fun follow(contextUri: String?) {+ context = contextUri+ _playing.value = contextUri != null && playingMixes.containsKey(contextUri)+ }++ /** The context the player last said it was playing; see [follow]. */+ @Volatile private var context: String? = null++ /** Puts [mix] on for the playlist at [uri], or takes it off with null. */+ fun set(uri: String, mix: PlaylistMix?) {+ if (mix == null) playingMixes.remove(uri) else playingMixes[uri] = mix+ // The mix of what is already playing, switched on or off from its+ // page, or read only after the queue was put back: the player works+ // its plan out again rather than waiting for the next song.+ follow(context)+ onChange?.invoke()+ }++ /** Set by the player, to hear about [set]; see LibrespotPlayer.followMix. */+ @Volatile var onChange: (() -> Unit)? = null++ /**+ * The transition from [from] into [to] while [contextUri] plays, when that+ * playlist's mix is on and has one for exactly this pair.+ */+ fun transition(contextUri: String?, from: String, to: String): Transition? {+ val mix = playingMixes[contextUri ?: return null] ?: return null+ return mix.transitions.firstOrNull { it.from == from && it.to == to } ?: auto(mix, from, to)+ }++ /** The songs' analyses, by URI, as they arrive; see [prepare]. */+ private val analyses = ConcurrentHashMap<String, Analysis>()++ /** The songs whose analysis has been asked for. */+ private val asked = ConcurrentHashMap.newKeySet<String>()++ private val fetcher = java.util.concurrent.Executors.newSingleThreadExecutor()++ /**+ * Asks for the analyses of [uris] not already asked for, and has the+ * player work its plan out again once they are in. A failed request is+ * asked again the next time the pair comes up.+ */+ private fun prepare(uris: List<String>) {+ val missing = uris.filter { it.startsWith("spotify:track:") && asked.add(it) }+ if (missing.isEmpty()) return+ fetcher.execute {+ val found = parseAnalyses(NativeBridge.tracksMixAnalysis(org.json.JSONArray(missing).toString()))+ analyses.putAll(found)+ (missing - found.keys).forEach { asked.remove(it) }+ android.util.Log.i("SquareMix", "analysis for ${found.size} of ${missing.size} songs")+ if (found.isNotEmpty()) onChange?.invoke()+ }+ }++ /**+ * The automatic transition, where nobody saved one, worked out the way+ * Spotify's Automix does, from the same traits; see catalog.rs.+ *+ * Its app keeps its own result to itself, and doing exactly what it does+ * was not possible: what it chooses among the beat-matched transitions+ * depends on more than these traits tell. What it does was measured on+ * some 200 pairs of songs, and this follows the same rules:+ *+ * - The two are beat-matched when, somewhere near the end of the first and+ * the start of the second, a bar of one is within 10% of a bar of the+ * other: every pair it matched and none it did not, but for a few. Then+ * the second is brought to the first one's tempo over two, four or eight+ * bars, starting on a downbeat in both; see [beatmatched].+ * - Otherwise it is a plain five-second crossfade, centred where the first+ * song stops really sounding, with the second coming in two and a half+ * seconds before it starts to; see [crossfade].+ */+ private fun auto(mix: PlaylistMix, from: String, to: String): Transition? {+ val a = analyses[from]+ val b = analyses[to]+ if (a == null || b == null) {+ prepare(listOf(from, to))+ return null+ }+ val length = mix.durations[from]?.takeIf { it > 0 } ?: a.lengthMs+ return beatmatched(from, to, a, b) ?: crossfade(from, to, a, b, length)+ }++ /** See [auto]. Within half of it each side of the first song's last sound. */+ private fun crossfade(from: String, to: String, a: Analysis, b: Analysis, length: Long): Transition? {+ val centre = minOf(a.fadeOutMs, length - 10)+ val out = (centre - CROSSFADE_MS / 2).coerceAtLeast(0)+ val end = minOf(length, centre + CROSSFADE_MS / 2)+ if (end <= out) return null+ return Transition(+ index = -1,+ from = from,+ to = to,+ outMs = out,+ inMs = (b.fadeInMs - CROSSFADE_MS / 2).coerceAtLeast(0),+ fadeMs = end - out,+ bpm = 0f,+ preset = null,+ )+ }++ /**+ * See [auto]. Every way of laying [BEATMATCH_BARS] bars of the first song's+ * last forty over as many of the second one's first thirty-two, at a+ * tempo within 10%, scored on what Spotify's choices were seen to favour:+ *+ * - ending near where the first stops really sounding, and starting near+ * where the second starts to;+ * - two bars rather than four, four rather than eight;+ * - nobody singing in both at once, no singing of the second skipped and+ * none of the first cut off;+ * - Spotify's own suggested points, and as little change of tempo as can be.+ *+ * The weights were fitted to its choices. Null when no bars match.+ */+ private fun beatmatched(from: String, to: String, a: Analysis, b: Analysis): Transition? {+ val da = a.downbeats+ val db = b.downbeats+ if (da.size < 10 || db.size < 10) return null+ val barA = (da.last() - da[da.size - 9]) / 8.0+ val barB = (db[8] - db[0]) / 8.0+ val leave = a.fadeOutMs+ val arrive = b.fadeInMs+ val outStarts = a.points[1] ?: LongArray(0)+ val outEnds = a.points[2] ?: LongArray(0)+ val inStarts = b.points[3] ?: LongArray(0)+ val inEnds = b.points[4] ?: LongArray(0)++ var best: Transition? = null+ var bestScore = Double.NEGATIVE_INFINITY+ for (bars in BEATMATCH_BARS) {+ for (end in da.lastIndex downTo maxOf(bars, da.size - 40) + 1) {+ if (da[end] > leave + 2 * barA) continue+ val start = end - bars+ val overlap = (da[end] - da[start]).toDouble()+ for (j in 0 until minOf(32, db.size - 1 - bars)) {+ val speed = (db[j + bars] - db[j]) / overlap+ if (speed < 0.9 || speed > 1.1) continue+ val early = (leave - da[end]) / barA+ val late = (db[j] - arrive) / barB+ val singingA = a.singing(da[start], da[end])+ val singingB = b.singing(db[j], db[j + bars])+ val score = -0.083 * early - 0.021 * early * early / 10 -+ 0.018 * late - 0.034 * late * late / 10 -+ (if (bars == 4) 2.223 else 0.0) - (if (bars == 8) 3.907 else 0.0) ++ 0.342 * singingA - 0.705 * singingB - 5.813 * singingA * singingB -+ 2.738 * (if (db[j] > arrive) b.singing(arrive, db[j]) else 0.0) -+ 3.637 * (if (leave > da[end]) a.singing(da[end], leave) else 0.0) -+ 7.13 * kotlin.math.abs(speed - 1) ++ (if (outStarts.near(da[start])) 1.501 else 0.0) ++ (if (outEnds.near(da[end])) 1.138 else 0.0) ++ (if (inStarts.near(db[j])) 0.827 else 0.0) ++ (if (inEnds.near(db[j + bars])) 0.686 else 0.0) -+ (if (b.sections.near(db[j])) 0.582 else 0.0) ++ (if (a.sections.near(da[end])) 0.553 else 0.0)+ if (score > bestScore) {+ bestScore = score+ best = Transition(+ index = -1,+ from = from,+ to = to,+ outMs = da[start],+ inMs = db[j],+ fadeMs = overlap.toLong(),+ bpm = (bars * 240_000 / overlap).toFloat(),+ preset = null,+ speedIn = speed.toFloat(),+ )+ }+ }+ }+ }+ return best+ }++ /** Whether any of these times is within a few milliseconds of [ms]. */+ private fun LongArray.near(ms: Long) = any { kotlin.math.abs(it - ms) <= 60 }++ private const val CROSSFADE_MS = 5_000L++ private val BEATMATCH_BARS = intArrayOf(2, 4, 8)+}diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.ktindex 0d19135..c3c550c 100644--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt@@ -267,6 +267,12 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {val shelves: List<dev.lelonio.square.data.ArtistShelf> = emptyList(),/** Where the top songs' "see all" leads, when the source has a list for it. */val topSongsMoreUri: String? = null,+ /** The playlist's Spotify Mix, when it was mixed in Spotify's app; see Mixes. */+ val mix: dev.lelonio.square.playback.Mixes.PlaylistMix? = null,+ /** Whether the mix is on here: it plays, and the page shows it. */+ val mixOn: Boolean = false,+ /** Tempo and key of the songs, by URI, once a mix is on; see Mixes. */+ val audioAttributes: Map<String, dev.lelonio.square.playback.Mixes.Attributes> = emptyMap(),)private val container get() = getApplication<SquareApplication>()@@ -2915,6 +2921,13 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {// flag's default and would clear the wait a batch of tracks// early, which is the swap this exists to prevent.heroPending = current.heroPending,+ // The mix, read once like the photograph: a batch of tracks+ // landing after it took the tempo and key back off the rows.+ // What is on screen wins, being the newest — switched on or+ // off since, or given its songs' lengths since.+ mix = current.mix ?: state.mix,+ mixOn = if (current.mix != null) current.mixOn else state.mixOn,+ audioAttributes = current.audioAttributes.ifEmpty { state.audioAttributes },)}}@@ -4099,6 +4112,81 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {}+ private fun mixPrefs() =+ getApplication<SquareApplication>().getSharedPreferences(MIX_PREFS, android.content.Context.MODE_PRIVATE)++ /**+ * Whether a playlist plays as a mix here: as Spotify has it, unless the+ * listener said otherwise on this phone — off for one that is mixed, on+ * for one that is not.+ */+ private fun mixOn(uri: String, mixed: Boolean): Boolean {+ val prefs = mixPrefs()+ return if (mixed) {+ uri !in prefs.getStringSet(MIX_OFF, emptySet()).orEmpty()+ } else {+ uri in prefs.getStringSet(MIX_ON, emptySet()).orEmpty()+ }+ }++ /**+ * Turns the page's Spotify Mix on or off, here only.+ *+ * Kept per playlist on this phone, and nothing is written to Spotify: off,+ * the songs play one after another as anywhere else, and the playlist is+ * still mixed for every other device and for Spotify's own app.+ */+ fun toggleMix() {+ val page = _playlist.value+ val uri = page.uri ?: return+ val mix = page.mix ?: return+ val on = !page.mixOn+ val prefs = mixPrefs()+ // Kept as a difference from Spotify's own state, so a playlist mixed+ // or unmixed in Spotify's app later follows it unless told otherwise.+ val key = if (mix.mixed) MIX_OFF else MIX_ON+ val listed = prefs.getStringSet(key, emptySet()).orEmpty().toMutableSet()+ if (on == mix.mixed) listed.remove(uri) else listed.add(uri)+ prefs.edit().putStringSet(key, listed).apply()+ dev.lelonio.square.playback.Mixes.set(uri, mix.takeIf { on })+ _playlist.value = page.copy(mixOn = on)+ if (on) viewModelScope.launch { loadMixData(uri) }+ }++ /**+ * What a mixed page needs beyond its transitions: the tempo and key the+ * rows show, and the songs' lengths, which the automatic transitions fit+ * inside. What those are worked out from is asked for by the player, for+ * the pair about to play; see Mixes.+ *+ * Waits for the songs, which arrive on their own clock, and asks once for+ * all of them; see catalog.rs. Returns what it found, for a caller keeping+ * its own copy of the page, and null when the page was left meanwhile.+ */+ private suspend fun loadMixData(+ uri: String,+ ): Pair<Map<String, dev.lelonio.square.playback.Mixes.Attributes>, dev.lelonio.square.playback.Mixes.PlaylistMix>? {+ val tracks = withTimeoutOrNull(ARTIST_WAIT_MS) {+ while (_playlist.value.uri == uri && _playlist.value.tracks.isEmpty()) delay(POLL_INTERVAL_MS)+ _playlist.value.tracks+ }.orEmpty()+ val uris = tracks.map { it.uri }.filter { it.startsWith("spotify:track:") }.distinct()+ if (uris.isEmpty()) return null+ val json = org.json.JSONArray(uris).toString()+ val attributes = withContext(Dispatchers.IO) {+ dev.lelonio.square.playback.Mixes.parseAttributes(NativeBridge.tracksAudioAttributes(json))+ }+ android.util.Log.i(TAG, "mix of $uri: tempo and key for ${attributes.size} of ${uris.size} songs")+ val page = _playlist.value+ if (page.uri != uri) return null+ val mix = (page.mix ?: return null).copy(+ durations = page.tracks.filter { it.durationMs > 0 }.associate { it.uri to it.durationMs },+ )+ _playlist.value = page.copy(audioAttributes = attributes, mix = mix)+ if (page.mixOn) dev.lelonio.square.playback.Mixes.set(uri, mix)+ return attributes to mix+ }+fun openPlaylist(playlist: CatalogPlaylist) {// Read again on every page: a file copied to the phone since the last// one should be found the next time its playlist is opened.@@ -4247,6 +4335,31 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {}}+ // Its Spotify Mix: the transitions it was saved with in Spotify's+ // own app, if any, and whether it plays as a mix here. Any Spotify+ // playlist can: one that was never mixed plays with the automatic+ // transitions alone. On by default only where Spotify has it mixed.+ if (playlist.uri.startsWith("spotify:playlist:") &&+ container.activeBackend.id == BackendId.SPOTIFY &&+ !dev.lelonio.square.playback.OfflineMode.active.value+ ) launch {+ if (!awaitEngine()) return@launch+ val mix = withContext(Dispatchers.IO) {+ dev.lelonio.square.playback.Mixes.parseMix(NativeBridge.playlistMix(playlist.uri))+ } ?: return@launch+ val on = mixOn(playlist.uri, mix.mixed)+ android.util.Log.i(TAG, "mix of ${playlist.uri}: mixed=${mix.mixed}, ${mix.transitions.size} saved, on=$on")+ base = base.copy(mix = mix, mixOn = on)+ if (!isActive || _playlist.value.uri != playlist.uri) return@launch+ _playlist.value = _playlist.value.copy(mix = mix, mixOn = on)+ if (on) {+ dev.lelonio.square.playback.Mixes.set(playlist.uri, mix)+ loadMixData(playlist.uri)?.let { (attributes, withLengths) ->+ base = base.copy(audioAttributes = attributes, mix = withLengths)+ }+ }+ }+// Whether it is already kept, for the button that keeps it.if (kind != DetailKind.ARTIST) launch {val kept = savedState(playlist.uri) ?: return@launch@@ -6696,6 +6809,13 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {/** How long the album lookup waits for a track to name the artist. */const val ARTIST_WAIT_MS = 8_000L+ /** Where the playlists' mix choices are kept; see mixOn and toggleMix. */+ const val MIX_PREFS = "square_mix"+ /** Mixed in Spotify's app, and turned off here. */+ const val MIX_OFF = "off"+ /** Not mixed in Spotify's app, and turned on here. */+ const val MIX_ON = "on"+const val ENGINE_TIMEOUT_MS = 30_000L/** How long the library is waited for once there is a session. */diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.ktindex a0715bf..62d63d1 100644--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt@@ -2174,6 +2174,7 @@ fun SquareApp(},storedSort = (trackOrders[page.uri] ?: viewModel.defaultTrackOrder).sort,onSortChange = { viewModel.setTrackSort(page.uri, it) },+ onToggleMix = viewModel::toggleMix,storedSortDescending = (trackOrders[page.uri] ?: viewModel.defaultTrackOrder).descending,onSortDescendingChange = { viewModel.setTrackSortDescending(page.uri, it) },onOpenItem = { item ->diff --git a/app/src/main/java/dev/lelonio/square/ui/components/MixRing.kt b/app/src/main/java/dev/lelonio/square/ui/components/MixRing.ktnew file mode 100644index 0000000..08cb97e--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/ui/components/MixRing.kt@@ -0,0 +1,76 @@+package dev.lelonio.square.ui.components++import androidx.compose.animation.core.LinearEasing+import androidx.compose.animation.core.animateFloat+import androidx.compose.animation.core.infiniteRepeatable+import androidx.compose.animation.core.rememberInfiniteTransition+import androidx.compose.animation.core.tween+import androidx.compose.material3.MaterialTheme+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.drawBehind+import androidx.compose.ui.geometry.CornerRadius+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.geometry.Size+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.drawscope.Stroke+import androidx.compose.ui.graphics.toArgb+import androidx.compose.ui.unit.dp++/** How far the mix ring and its glow reach outside the button they go round. */+val MIX_RING_ROOM = 8.dp++/**+ * The lit ring Spotify's Mix puts round its Play button: a sweep of the+ * accent turning slowly round the edge. Nothing at all when [on] is false.+ */+@Composable+fun Modifier.mixRing(+ on: Boolean,+ /**+ * The ring's light: the accent of whatever it is on, which is the+ * playlist's on its page and the song's in the player, rather than+ * Spotify's own green, which belongs to neither.+ */+ color: Color = MaterialTheme.colorScheme.primary,+): Modifier {+ if (!on) return this+ val accent = color+ val turn = rememberInfiniteTransition(label = "mixRing")+ val angle by turn.animateFloat(+ initialValue = 0f,+ targetValue = 360f,+ animationSpec = infiniteRepeatable(tween(4_000, easing = LinearEasing)),+ label = "mixRingAngle",+ )+ return this+ .drawBehind {+ val stroke = 3.dp.toPx()+ val inset = -stroke * 1.4f+ // The light turns, the shape does not: the gradient is rotated+ // through the shader's own matrix, round the button's centre.+ val cx = size.width / 2f+ val cy = size.height / 2f+ val dim = accent.copy(alpha = 0.15f).toArgb()+ val shader = android.graphics.SweepGradient(+ cx, cy, intArrayOf(dim, accent.toArgb(), dim), null,+ ).apply { setLocalMatrix(android.graphics.Matrix().apply { setRotate(angle, cx, cy) }) }+ drawRoundRect(+ brush = androidx.compose.ui.graphics.ShaderBrush(shader),+ topLeft = Offset(inset, inset),+ size = Size(size.width - 2 * inset, size.height - 2 * inset),+ cornerRadius = CornerRadius(size.height),+ style = Stroke(stroke),+ )+ }+ .drawBehind {+ // A soft glow under the ring, so it reads as light and not a line.+ drawRoundRect(+ color = accent.copy(alpha = 0.22f),+ topLeft = Offset(-6.dp.toPx(), -6.dp.toPx()),+ size = Size(size.width + 12.dp.toPx(), size.height + 12.dp.toPx()),+ cornerRadius = CornerRadius(size.height),+ )+ }+}diff --git a/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt b/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.ktindex b3e52c1..0de788f 100644--- a/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt+++ b/app/src/main/java/dev/lelonio/square/ui/library/PlaylistScreen.kt@@ -8,6 +8,21 @@ import androidx.compose.animation.core.Springimport androidx.compose.animation.core.animateFloatAsStateimport androidx.compose.animation.core.springimport androidx.compose.animation.core.tween+import androidx.compose.animation.core.LinearEasing+import androidx.compose.animation.core.animateFloat+import androidx.compose.animation.core.infiniteRepeatable+import androidx.compose.animation.core.rememberInfiniteTransition+import androidx.compose.ui.geometry.CornerRadius+import androidx.compose.ui.geometry.Size+import androidx.compose.ui.graphics.drawscope.Stroke+import androidx.compose.ui.semantics.role+import androidx.compose.ui.semantics.semantics+import androidx.compose.ui.semantics.stateDescription+import com.adamglin.phosphoricons.regular.SlidersHorizontal+import com.adamglin.phosphoricons.regular.Sparkle+import com.adamglin.phosphoricons.regular.Waveform+import dev.lelonio.square.ui.components.MIX_RING_ROOM+import dev.lelonio.square.ui.components.mixRingimport androidx.compose.foundation.layout.RowScopeimport dev.lelonio.square.ui.glass.liquidGlassimport dev.lelonio.square.ui.player.GlassFilm@@ -264,6 +279,8 @@ fun PlaylistScreen(/** The remembered track order, and where a change to it is stored. */storedSort: String? = null,onSortChange: (String) -> Unit = {},+ /** Turns the page's Spotify Mix on or off; see MainViewModel.toggleMix. */+ onToggleMix: () -> Unit = {},/** And which way round it runs; see the same menu. */storedSortDescending: Boolean = false,onSortDescendingChange: (Boolean) -> Unit = {},@@ -341,7 +358,14 @@ fun PlaylistScreen(// or searching makes them a selection out of it, and playing that as the// context would have Spotify play the playlist's own order instead of the// one on screen.- val asContext = sort == TrackSort.ORIGINAL && !descending && query.isBlank()+ // A mixed playlist plays in its own order: its transitions are between+ // neighbours in that order, and the same songs sorted otherwise have none.+ // The chosen sort is kept, not cleared, and comes back with the mix off.+ val mixed = state.mixOn && state.mix != null+ val shownSort = if (mixed) TrackSort.ORIGINAL else sort+ val shownDescending = descending && !mixed++ val asContext = shownSort == TrackSort.ORIGINAL && !shownDescending && query.isBlank()// Whether the top songs are shown in full.//@@ -353,14 +377,14 @@ fun PlaylistScreen(// Derived, not stored: keeping a second list in state would leave the two// able to disagree after a reload.- val visible = remember(state.tracks, query, sort, descending) {+ val visible = remember(state.tracks, query, shownSort, shownDescending) {state.tracks.filter { it.matches(query) }- .sortedWith(sort.comparator())+ .sortedWith(shownSort.comparator())// Reversed after sorting rather than by a second comparator: the// playlist's own order has no comparator to invert, and turning it// upside down is exactly what "descending" means there too.- .let { if (descending) it.reversed() else it }+ .let { if (shownDescending) it.reversed() else it }}// Taken from *this* screen's cover, not from whatever is playing. The two@@ -726,6 +750,7 @@ fun PlaylistScreen(collapsedHeight = collapsedHeight,topPadding = topPadding,onBack = onBack,+ mixOn = mixed,onPlay = {visible.takeIf { it.isNotEmpty() }?.let { onPlayInOrder(it, asContext) }},@@ -842,6 +867,8 @@ fun PlaylistScreen(// several, not the whole of what is below the fold.large = isArtist,sort = sort,+ mix = state.mixOn.takeIf { state.mix != null },+ onToggleMix = onToggleMix,onSortOpen = { sortOpen = it },onAnchor = { sortAnchor = it },// Only where there is more than what is shown.@@ -950,7 +977,8 @@ fun PlaylistScreen(contentType = { _, _ -> "track" },) { index, track ->// Faded in as the list arrives; nothing moves after that.- Box(Modifier.animateItem(fadeInSpec = tween(260), placementSpec = null, fadeOutSpec = null)) {+ // A column, so a mix's transition can sit under the row.+ Column(Modifier.animateItem(fadeInSpec = tween(260), placementSpec = null, fadeOutSpec = null)) {SwipeToQueue(onQueue = { onEnqueue(track) }) {if (track.isEpisode) {val current = track.uri == nowPlayingUri@@ -1007,9 +1035,24 @@ fun PlaylistScreen(!track.uri.startsWith("local:") &&trackDownload(track) !=dev.lelonio.square.data.DownloadState.Done,+ mixAttributes = if (mixed) state.audioAttributes[track.uri] else null,)}}+ // Between this song and the next, what Spotify's Mix does+ // there: the transition saved with the playlist, or its+ // automatic one where none was. An item of its own, so it+ // sits under the row rather than over it, and is not part+ // of the row's swipe.+ if (mixed && query.isBlank() && index < visible.lastIndex) {+ // By the pair, not the row number: the page's list+ // and the playlist the mix was read from need not+ // number their rows the same.+ val next = visible[index + 1].uri+ val transition = state.mix?.transitions+ ?.firstOrNull { it.from == track.uri && it.to == next }+ TransitionTag(transition)+ }}}@@ -1568,6 +1611,8 @@ private fun DetailHeader(onToggleSearch: () -> Unit,/** What the header opens at, which the artist page makes taller. */heroHeight: Dp = HERO_HEIGHT,+ /** The page plays as a Spotify Mix; the Play button wears its ring. */+ mixOn: Boolean = false,) {val density = LocalDensity.currentval heroPx = with(density) { heroHeight.roundToPx() }@@ -1710,6 +1755,10 @@ private fun DetailHeader(val lightPage = pageColor.luminance() > LIGHT_PAGE_LUMINANCEval playFill = if (lightPage) Color.Black else Color.Whiteval playInk = if (lightPage) Color.White else pageColor+ // A mixed page plays as a mix, and its button says so: the+ // page's glass instead of the solid slab, lit by the ring the+ // official app turns round its own.+ if (mixOn) MixPlayButton(onPlay = onPlay, backdrop = backdrop, ink = ink) elseRow(Modifier.softShadow(ContinuousCapsule, elevation = 20.dp, spot = 0.32f)@@ -2723,6 +2772,9 @@ private fun SectionHeader(expandable: Boolean = false,expanded: Boolean = false,onToggle: () -> Unit = {},+ /** The page's Spotify Mix, on or off; null where there is none. */+ mix: Boolean? = null,+ onToggleMix: () -> Unit = {},) {Row(Modifier@@ -2779,7 +2831,11 @@ private fun SectionHeader(}}- IconButton(+ if (mix != null) MixPill(on = mix, onClick = onToggleMix)++ // A mixed playlist plays in its own order, so there is nothing to sort+ // while the mix is on; see `mixed` where the list is built.+ if (mix != true) IconButton(onClick = { onSortOpen(true) },modifier = Modifier.onGloballyPositioned {val root = it.boundsInRoot()@@ -2953,6 +3009,8 @@ private fun TrackRow(* missing ones are exactly what they might want to download later.*/unavailable: Boolean = false,+ /** Tempo and key, on a page playing as a Spotify Mix; see Mixes. */+ mixAttributes: dev.lelonio.square.playback.Mixes.Attributes? = null,) {val shape = RoundedCornerShape(14.dp)// Everything in the row fades together, artwork included, so it reads as@@ -3135,6 +3193,8 @@ private fun TrackRow(}}+ if (mixAttributes != null) MixTag(mixAttributes)+IconButton(onClick = onMenu, modifier = Modifier.size(32.dp)) {Icon(PhosphorIcons.Bold.DotsThree,@@ -3146,6 +3206,162 @@ private fun TrackRow(}}+/**+ * The switch for a playlist's Spotify Mix, as the official app draws it: a+ * pill with the mixer's sliders and the word, its rim lit while it is on.+ */+@Composable+private fun MixPill(on: Boolean, onClick: () -> Unit) {+ val accent = MaterialTheme.colorScheme.primary+ val ink = if (on) accent else MaterialTheme.colorScheme.onSurfaceVariant+ Row(+ Modifier+ .padding(end = 4.dp)+ .clip(ContinuousCapsule)+ .background(if (on) accent.copy(alpha = 0.16f) else Color.Transparent)+ .border(1.dp, ink.copy(alpha = if (on) 0.9f else 0.4f), ContinuousCapsule)+ .pressable(onClick, shape = ContinuousCapsule, pressedScale = 0.95f)+ .semantics { role = androidx.compose.ui.semantics.Role.Switch; stateDescription = if (on) "on" else "off" }+ .padding(horizontal = 12.dp, vertical = 6.dp),+ verticalAlignment = Alignment.CenterVertically,+ horizontalArrangement = Arrangement.spacedBy(6.dp),+ ) {+ Icon(+ PhosphorIcons.Regular.SlidersHorizontal,+ contentDescription = null,+ tint = ink,+ modifier = Modifier.size(15.dp),+ )+ Text(+ stringResource(R.string.mix),+ style = MaterialTheme.typography.labelLarge,+ fontWeight = FontWeight.SemiBold,+ color = ink,+ )+ }+}++/**+ * A song's tempo and key, at the end of its row on a mixed page: the number+ * over the Camelot key, which is drawn in the colour of its place on the wheel,+ * as the official app does.+ */+@Composable+private fun MixTag(attributes: dev.lelonio.square.playback.Mixes.Attributes) {+ Column(+ Modifier.padding(start = 8.dp, end = 2.dp),+ horizontalAlignment = Alignment.End,+ verticalArrangement = Arrangement.spacedBy(3.dp),+ ) {+ Text(+ stringResource(R.string.mix_bpm, attributes.bpm.roundToInt()),+ style = MaterialTheme.typography.labelSmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ if (attributes.camelot.isNotEmpty()) {+ val color = camelotColor(attributes.camelot)+ Text(+ attributes.camelot,+ style = MaterialTheme.typography.labelSmall,+ fontWeight = FontWeight.Bold,+ color = if (color.luminance() > 0.5f) Color.Black else Color.White,+ modifier = Modifier+ .clip(RoundedCornerShape(4.dp))+ .background(color)+ .padding(horizontal = 5.dp, vertical = 1.dp),+ )+ }+ }+}++/**+ * The Camelot wheel's colour for a key: twelve hues round the wheel, the+ * minor keys (A) a shade deeper than their major (B) neighbours.+ */+private fun camelotColor(camelot: String): Color {+ val number = camelot.dropLast(1).toIntOrNull()?.coerceIn(1, 12) ?: return Color.Gray+ val minor = camelot.endsWith("A", ignoreCase = true)+ val hue = ((number - 1) * 30f + 60f) % 360f+ return Color.hsv(hue, if (minor) 0.62f else 0.48f, if (minor) 0.86f else 0.95f)+}++/**+ * What happens between two songs on a mixed page, under the first of them:+ * the transition saved with the playlist, as how long the two overlap, or+ * "Auto" where Spotify's own automatic transition applies.+ */+@Composable+private fun TransitionTag(transition: dev.lelonio.square.playback.Mixes.Transition?) {+ val ink = MaterialTheme.colorScheme.onSurfaceVariant+ Row(+ Modifier+ .padding(start = ROW_TEXT_INDENT, top = 0.dp, bottom = 4.dp)+ .clip(ContinuousCapsule)+ .background(ink.copy(alpha = 0.10f))+ .padding(horizontal = 10.dp, vertical = 4.dp),+ verticalAlignment = Alignment.CenterVertically,+ horizontalArrangement = Arrangement.spacedBy(5.dp),+ ) {+ Icon(+ if (transition != null) PhosphorIcons.Regular.Waveform else PhosphorIcons.Regular.Sparkle,+ contentDescription = null,+ tint = if (transition != null) ink else MaterialTheme.colorScheme.primary,+ modifier = Modifier.size(13.dp),+ )+ Text(+ if (transition != null) {+ stringResource(R.string.mix_overlap, ((transition.fadeMs + 500) / 1000).toInt().coerceAtLeast(1))+ } else {+ stringResource(R.string.mix_auto)+ },+ style = MaterialTheme.typography.labelMedium,+ color = ink,+ )+ }+}+++++/** Where a transition sits under its row: in line with the row's text. */+private val ROW_TEXT_INDENT = 90.dp++/**+ * The Play button of a page playing as a mix: the same glass as the round+ * controls either side of it, with the mix's ring turning round its rim.+ */+@Composable+private fun MixPlayButton(onPlay: () -> Unit, backdrop: Backdrop, ink: Color) {+ // Room for the ring and its glow, which are drawn outside the button:+ // without it the row around the button cut them off at its edges.+ Box(Modifier.padding(MIX_RING_ROOM).mixRing(true)) {+ LiquidButton(+ onClick = onPlay,+ backdrop = backdrop,+ modifier = Modifier+ .background(Color.White.copy(alpha = 0.17f), ContinuousCapsule)+ .border(0.6.dp, Color.White.copy(alpha = 0.26f), ContinuousCapsule),+ contentHeight = 50.dp,+ contentPadding = 28.dp,+ ) {+ Icon(+ PhosphorIcons.Fill.Play,+ contentDescription = null,+ tint = ink,+ modifier = Modifier.size(20.dp),+ )+ Text(+ stringResource(R.string.play),+ style = MaterialTheme.typography.titleMedium,+ fontWeight = FontWeight.SemiBold,+ color = ink,+ modifier = Modifier.padding(start = 8.dp),+ )+ }+ }+}++/*** What can be done with one track, without leaving the list.*diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.ktindex 4c65e60..134a3e2 100644--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt@@ -55,6 +55,7 @@ import androidx.compose.material3.LocalContentColorimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Textimport androidx.compose.runtime.Composable+import dev.lelonio.square.ui.components.mixRingimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.ui.platform.LocalLayoutDirectionimport androidx.compose.animation.animateColorAsState@@ -2565,6 +2566,10 @@ internal fun Controls()}+ // Ringed while a playlist plays as a Spotify Mix, as the official+ // player's button is; see Mixes.playing.+ val mixPlaying by dev.lelonio.square.playback.Mixes.playing.collectAsStateWithLifecycle()+ Box(Modifier.mixRing(mixPlaying)) {RoundGlassButton(backdrop = backdrop, size = 76.dp, onClick = onTogglePlay) {// A ring around the icon while the track is still being fetched.//@@ -2596,6 +2601,7 @@ internal fun Controls(}}}+ }RoundGlassButton(backdrop = backdrop,diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xmlindex c3d16e4..921cb0b 100644--- a/app/src/main/res/values-de/strings.xml+++ b/app/src/main/res/values-de/strings.xml@@ -111,6 +111,10 @@<string name="add_to_playlist">Zu einer Playlist hinzufügen</string><string name="go_to_artist">Zum Künstler</string><string name="go_to_album">Zum Album</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">Auto</string>+ <string name="mix_overlap">%1$d s</string><string name="copy_link">Link kopieren</string><string name="remove_from_playlist">Aus der Playlist entfernen</string><string name="queued">In der Warteschlange</string>diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xmlindex 579c75c..b000232 100644--- a/app/src/main/res/values-es/strings.xml+++ b/app/src/main/res/values-es/strings.xml@@ -111,6 +111,10 @@<string name="add_to_playlist">Añadir a una playlist</string><string name="go_to_artist">Ir al artista</string><string name="go_to_album">Ir al álbum</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d bpm</string>+ <string name="mix_auto">Auto</string>+ <string name="mix_overlap">%1$d s</string><string name="copy_link">Copiar enlace</string><string name="remove_from_playlist">Quitar de la playlist</string><string name="queued">En cola</string>diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xmlindex 82bf531..feafe72 100644--- a/app/src/main/res/values-fr/strings.xml+++ b/app/src/main/res/values-fr/strings.xml@@ -111,6 +111,10 @@<string name="add_to_playlist">Ajouter à une playlist</string><string name="go_to_artist">Accéder à l\'artiste</string><string name="go_to_album">Accéder à l\'album</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">Auto</string>+ <string name="mix_overlap">%1$d s</string><string name="copy_link">Copier le lien</string><string name="remove_from_playlist">Retirer de la playlist</string><string name="queued">Dans la file</string>diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xmlindex 7e1a5d9..f2f3885 100644--- a/app/src/main/res/values-hi/strings.xml+++ b/app/src/main/res/values-hi/strings.xml@@ -120,6 +120,10 @@<string name="add_to_playlist">प्लेलिस्ट में जोड़ें</string><string name="go_to_artist">कलाकार पर जाएँ</string><string name="go_to_album">एल्बम पर जाएँ</string>+ <string name="mix">मिक्स</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">ऑटो</string>+ <string name="mix_overlap">%1$d से.</string><string name="copy_link">लिंक कॉपी करें</string><string name="remove_from_playlist">प्लेलिस्ट से हटाएँ</string><string name="queued">कतार में</string>diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xmlindex 0d9a265..b4dd292 100644--- a/app/src/main/res/values-it/strings.xml+++ b/app/src/main/res/values-it/strings.xml@@ -112,6 +112,10 @@<string name="add_to_playlist">Aggiungi a una playlist</string><string name="go_to_artist">Vai all\'artista</string><string name="go_to_album">Vai all\'album</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d bpm</string>+ <string name="mix_auto">Auto</string>+ <string name="mix_overlap">%1$d s</string><string name="copy_link">Copia link</string><string name="remove_from_playlist">Rimuovi dalla playlist</string><string name="queued">In coda</string>diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xmlindex cc3b6db..79e04df 100644--- a/app/src/main/res/values-ja/strings.xml+++ b/app/src/main/res/values-ja/strings.xml@@ -121,6 +121,10 @@<string name="add_to_playlist">プレイリストに追加</string><string name="go_to_artist">アーティストを表示</string><string name="go_to_album">アルバムを表示</string>+ <string name="mix">ミックス</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">自動</string>+ <string name="mix_overlap">%1$d秒</string><string name="copy_link">リンクをコピー</string><string name="remove_from_playlist">プレイリストから削除</string><string name="queued">キューに追加しました</string>diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xmlindex 876d26b..f1b6bf7 100644--- a/app/src/main/res/values-ko/strings.xml+++ b/app/src/main/res/values-ko/strings.xml@@ -108,6 +108,10 @@<string name="add_to_playlist">플레이리스트에 추가</string><string name="go_to_artist">아티스트로 이동</string><string name="go_to_album">앨범으로 이동</string>+ <string name="mix">믹스</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">자동</string>+ <string name="mix_overlap">%1$d초</string><string name="copy_link">링크 복사</string><string name="remove_from_playlist">플레이리스트에서 삭제</string><string name="queued">대기열에 추가됨</string>diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xmlindex b040946..0effaae 100644--- a/app/src/main/res/values-pt/strings.xml+++ b/app/src/main/res/values-pt/strings.xml@@ -111,6 +111,10 @@<string name="add_to_playlist">Adicionar a uma playlist</string><string name="go_to_artist">Ir para o artista</string><string name="go_to_album">Ir para o álbum</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d bpm</string>+ <string name="mix_auto">Auto</string>+ <string name="mix_overlap">%1$d s</string><string name="copy_link">Copiar link</string><string name="remove_from_playlist">Remover da playlist</string><string name="queued">Na fila</string>diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xmlindex 8f445d5..f8a5f78 100644--- a/app/src/main/res/values-ru/strings.xml+++ b/app/src/main/res/values-ru/strings.xml@@ -122,6 +122,10 @@<string name="add_to_playlist">Добавить в плейлист</string><string name="go_to_artist">К исполнителю</string><string name="go_to_album">К альбому</string>+ <string name="mix">Микс</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">Авто</string>+ <string name="mix_overlap">%1$d с</string><string name="copy_link">Копировать ссылку</string><string name="remove_from_playlist">Убрать из плейлиста</string><string name="queued">В очереди</string>diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xmlindex 1b281e0..f009536 100644--- a/app/src/main/res/values-tr/strings.xml+++ b/app/src/main/res/values-tr/strings.xml@@ -120,6 +120,10 @@<string name="add_to_playlist">Çalma listesine ekle</string><string name="go_to_artist">Sanatçıya git</string><string name="go_to_album">Albüme git</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d BPM</string>+ <string name="mix_auto">Otomatik</string>+ <string name="mix_overlap">%1$d sn</string><string name="copy_link">Bağlantıyı kopyala</string><string name="remove_from_playlist">Çalma listesinden çıkar</string><string name="queued">Sıraya eklendi</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex 2ec871f..ac44315 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -126,6 +126,10 @@<string name="add_to_playlist">Add to a playlist</string><string name="go_to_artist">Go to artist</string><string name="go_to_album">Go to album</string>+ <string name="mix">Mix</string>+ <string name="mix_bpm">%1$d bpm</string>+ <string name="mix_auto">Auto</string>+ <string name="mix_overlap">%1$d s</string><string name="copy_link">Copy link</string><string name="remove_from_playlist">Remove from playlist</string><string name="queued">Queued</string>diff --git a/native/Cargo.lock b/native/Cargo.lockindex 90178e1..2e21162 100644--- a/native/Cargo.lock+++ b/native/Cargo.lock@@ -2358,6 +2358,7 @@ name = "squarecore"version = "0.1.0"dependencies = ["android_logger",+ "base64","bytes","futures-util","http",diff --git a/native/Cargo.toml b/native/Cargo.tomlindex f6449d4..849816b 100644--- a/native/Cargo.toml+++ b/native/Cargo.toml@@ -37,6 +37,9 @@ http-body-util = "0.1"# The body type librespot's own HTTP client takes.bytes = "1"serde_json = "1"+# The transition recipes of a mixed playlist arrive as base64; see+# catalog::transition_in. The version librespot-core already brings in.+base64 = "0.22"futures-util = "0.3"# Decoding the DJ's lines, which arrive as MP3; see src/dj.rs. The same# version librespot-playback already builds, so nothing new is compiled.diff --git a/native/src/catalog.rs b/native/src/catalog.rsindex 4ae505a..b951c1a 100644--- a/native/src/catalog.rs+++ b/native/src/catalog.rs@@ -382,6 +382,468 @@ pub fn playlist_cover(uri: &str) -> EngineResult<String> {})}+/// A playlist's Spotify Mix, as JSON `{mixed, transitions}`, or null.+///+/// A playlist mixed in Spotify's own app is the same playlist seen through a+/// lens named `mix`, and the lens has to be asked for: the desktop client sends+/// `spotify-apply-lenses: mix,auto`, and without it the answer is the plain+/// list, with nothing about the mix in it. Through the lens, the row a+/// transition starts from carries it as the format attribute+/// `automix.auto_transition_recipe`, base64 of a protobuf; see+/// [transition_in]. `mixed` says whether the `mix` lens was applied at all.+pub fn playlist_mix(uri: &str) -> EngineResult<String> {+ let parsed = SpotifyUri::from_uri(uri).map_err(|e| format!("bad uri: {e}"))?;+ let SpotifyUri::Playlist { id, .. } = parsed else {+ return Ok("null".into());+ };++ let session = with_session(|s| s.clone())?;+ block_on(async move {+ let endpoint = format!("/playlist/v2/playlist/{}", id.to_base62().map_err(|e| e.to_string())?);+ let mut headers = http::HeaderMap::new();+ headers.insert("spotify-apply-lenses", http::HeaderValue::from_static("mix,auto"));+ let bytes = session+ .spclient()+ .request(&Method::GET, &endpoint, Some(headers), None)+ .await+ .map_err(|e| format!("playlist read failed: {e}"))?;+ let list = SelectedListContent::parse_from_bytes(&bytes)+ .map_err(|e| format!("playlist was not a SelectedListContent: {e}"))?;++ // Whether it is mixed is the playlist's MixState, not the lens: asked+ // for, the `mix` lens is applied to any playlist at all, mixed or not.+ let playlist_uri = format!("spotify:playlist:{}", id.to_base62().map_err(|e| e.to_string())?);+ let mixed = match extension(&session, &playlist_uri, MIX_STATE).await {+ Ok(Some(bytes)) => mix_state_in(&bytes) == Some(1),+ _ => false,+ };+ let transitions: Vec<Value> = list+ .contents+ .items+ .iter()+ .enumerate()+ .filter_map(|(index, item)| {+ let recipe = item+ .attributes+ .format_attributes+ .iter()+ .find(|a| a.key() == "automix.auto_transition_recipe")?;+ let mut transition = transition_in(recipe.value())?;+ transition["index"] = json!(index);+ Some(transition)+ })+ .collect();++ Ok(json!({ "mixed": mixed, "transitions": transitions }).to_string())+ })+}++/// `MIX_STATE`, a `spotify.playlistmixing.extensions.mixstate.MixState`:+/// `{ 1: 1 }` on a playlist mixed in Spotify's app, empty on any other.+const MIX_STATE: u64 = 225;++/// The state in a MixState reply: the `Any`'s value, its field 1.+fn mix_state_in(bytes: &[u8]) -> Option<u64> {+ for array in message_fields(bytes, 2) {+ for data in message_fields(array, 3) {+ let any = read_first_message_field(data, 3)?;+ let value = read_first_message_field(any, 2)?;+ let (tag, next) = read_varint(value, 0)?;+ if tag == 1 << 3 {+ return read_varint(value, next).map(|(state, _)| state);+ }+ }+ }+ None+}++/// One transition recipe, as JSON, from its base64.+///+/// What each field is was worked out from transitions made in the official+/// app and read back here, not from a published schema:+///+/// 1 { 3 out_ms, 4 in_ms, 5 fade_ms, 6 speed of A, 7 speed of B, 8 kind,+/// 10 from uri, 11 to uri, 16 bpm of A, 17 bpm of B }+/// 2 { 1 preset id, … the preset's own settings }+///+/// `in_ms` is left out of the message when the next song starts from its+/// beginning, which is what a protobuf does with a zero.+fn transition_in(recipe: &str) -> Option<Value> {+ use base64::Engine as _;+ let bytes = base64::engine::general_purpose::STANDARD.decode(recipe.trim()).ok()?;+ let timing = read_first_message_field(&bytes, 1)?;+ let preset = read_first_message_field(&bytes, 2);++ let mut out_ms = None;+ let mut in_ms = 0u64;+ let mut fade_ms = None;+ let mut speed_a = 1.0f32;+ let mut speed_b = 1.0f32;+ let mut kind = 0u64;+ let mut from = None;+ let mut to = None;+ let mut bpm_a = 0.0f32;+ let mut bpm_b = 0.0f32;+ let mut cursor = 0usize;+ while cursor < timing.len() {+ let (tag, next) = read_varint(timing, cursor)?;+ cursor = next;+ match (tag >> 3, tag & 0x7) {+ (field @ (3 | 4 | 5 | 8), 0) => {+ let (value, next) = read_varint(timing, cursor)?;+ cursor = next;+ match field {+ 3 => out_ms = Some(value),+ 4 => in_ms = value,+ 5 => fade_ms = Some(value),+ _ => kind = value,+ }+ }+ (field @ (6 | 7 | 16 | 17), 5) => {+ let raw = timing.get(cursor..cursor + 4)?;+ let value = f32::from_le_bytes(raw.try_into().ok()?);+ cursor += 4;+ match field {+ 6 => speed_a = value,+ 7 => speed_b = value,+ 16 => bpm_a = value,+ _ => bpm_b = value,+ }+ }+ (field @ (10 | 11), 2) => {+ let (payload, next) = read_bytes(timing, cursor)?;+ cursor = next;+ let uri = std::str::from_utf8(payload).ok()?.to_string();+ if field == 10 { from = Some(uri) } else { to = Some(uri) }+ }+ (_, wire) => cursor = skip_field(timing, cursor, wire)?,+ }+ }++ let preset_id = preset.and_then(|preset| {+ let (tag, next) = read_varint(preset, 0)?;+ (tag == 1 << 3).then(|| read_varint(preset, next).map(|(id, _)| id)).flatten()+ });++ Some(json!({+ "from": from?,+ "to": to?,+ "outMs": out_ms?,+ "inMs": in_ms,+ "fadeMs": fade_ms?,+ "speedA": speed_a,+ "speedB": speed_b,+ "bpmA": bpm_a,+ "bpmB": bpm_b,+ "kind": kind,+ "preset": preset_id,+ }))+}++/// Tempo and key of many tracks at once, as a JSON object keyed by URI, each+/// `{bpm, key, mode, camelot}`. A track Spotify has no numbers for is left out.+pub fn tracks_audio_attributes(uris_json: &str) -> EngineResult<String> {+ batched_extension(uris_json, AUDIO_ATTRIBUTES_V2, |data| audio_attributes_in(data, 0))+}++/// What Square's automatic transitions are worked out from, for a few tracks+/// at once: a JSON object keyed by URI, each+/// `{lengthMs, downbeats, fadeInMs, fadeOutMs, vocals, points, sections}`.+///+/// Spotify's own Automix works its transitions out on the phone, from these+/// same traits, and keeps the result to itself; see Mixes.kt for what Square+/// makes of them. Asked for the pair about to play rather than for a whole+/// playlist: the waveforms alone are some forty kilobytes a song.+///+/// - `downbeats`: where each bar starts, from `MIX_BEATS`.+/// - `fadeInMs`, `fadeOutMs`: where the song is first and last really+/// sounding, read off `MIX_WAVEFORMS` (see [loudness_edges]).+/// - `vocals`: how much singing there is, 0 to 100, every tenth of a second,+/// from `MIX_VOCALS`.+/// - `points`: `[kind, ms]`, Spotify's own suggestions from+/// `TRANSITION_POINTS`; 1 and 2 where a song may start and finish leaving,+/// 3 and 4 where the next may start and be fully in.+/// - `sections`: where each part of the song starts, from `TRACK_SECTIONS`.+///+/// A song missing its beats or waveform is left out, and has no automatic+/// transition.+pub fn tracks_mix_analysis(uris_json: &str) -> EngineResult<String> {+ let uris: Vec<String> = serde_json::from_str(uris_json).map_err(|e| format!("bad uris: {e}"))?;+ let session = with_session(|s| s.clone())?;++ block_on(async move {+ let (beats, waves, vocals, points, sections) = tokio::join!(+ extension_payloads(&session, &uris, MIX_BEATS),+ extension_payloads(&session, &uris, MIX_WAVEFORMS),+ extension_payloads(&session, &uris, MIX_VOCALS),+ extension_payloads(&session, &uris, TRANSITION_POINTS),+ extension_payloads(&session, &uris, TRACK_SECTIONS),+ );+ let (beats, waves) = (beats?, waves?);+ // Without these the transitions are only less careful.+ let (vocals, points, sections) =+ (vocals.unwrap_or_default(), points.unwrap_or_default(), sections.unwrap_or_default());++ let mut found = serde_json::Map::new();+ for uri in &uris {+ let downbeats = beats.get(uri).map(|b| downbeats_in(b)).unwrap_or_default();+ if downbeats.len() < 10 {+ continue;+ }+ let Some((length_ms, fade_in_ms, fade_out_ms)) = waves.get(uri).and_then(|w| loudness_edges(w))+ else {+ continue;+ };+ found.insert(+ uri.clone(),+ json!({+ "lengthMs": length_ms,+ "downbeats": downbeats,+ "fadeInMs": fade_in_ms,+ "fadeOutMs": fade_out_ms,+ "vocals": vocals.get(uri).map(|v| vocal_curve(v)).unwrap_or_default(),+ "points": points.get(uri).map(|p| transition_points_in(p)).unwrap_or_default(),+ "sections": sections.get(uri).map(|s| sections_in(s)).unwrap_or_default(),+ }),+ );+ }+ Ok(Value::Object(found).to_string())+ })+}++/// `spotify.playlistmixing.extensions.mixbeats.Beats`:+/// `{ 1 beats_per_bar; 2 repeated Beat { 1 float seconds; 3 beat_in_bar } }`.+const MIX_BEATS: u64 = 217;+/// `...mixvocalactivity.VocalActivity`:+/// `{ 1 float sample_rate; 4 hop in samples; 5 bytes one value a hop }`.+const MIX_VOCALS: u64 = 218;+/// `...mixthreebandwaveforms.ThreeBandWaveforms`:+/// `{ 1 sample_rate; 2 hop in ms; 3, 4, 5 bytes low, mid and high, one a hop }`.+const MIX_WAVEFORMS: u64 = 237;+/// `...commontransitionpoint.TransitionPoints`:+/// `{ 1 repeated Point { 1 kind; 2 position_ms; 3 double weight } }`.+const TRANSITION_POINTS: u64 = 247;+/// `...tracksections.TrackSections`: `{ 1 repeated Section { 1 start_ms } }`.+const TRACK_SECTIONS: u64 = 261;++/// Where each bar starts, in milliseconds.+fn downbeats_in(bytes: &[u8]) -> Vec<u64> {+ message_fields(bytes, 2)+ .into_iter()+ .filter_map(|beat| {+ let fields = scalar_fields(beat);+ let seconds = fields.iter().find(|(f, _)| *f == 1).map(|(_, raw)| f32::from_bits(*raw as u32))?;+ let in_bar = fields.iter().find(|(f, _)| *f == 3).map(|(_, v)| *v)?;+ (in_bar == 1 && seconds >= 0.0).then(|| (seconds * 1000.0).round() as u64)+ })+ .collect()+}++/// A song's length and where it is first and last really sounding, from its+/// waveform: the first and last half second louder on average than+/// [EDGE_LEVEL] of the song's median loudness.+///+/// Where Spotify's own plain crossfades centre themselves, as near as could be+/// told from what its app chose for some 150 pairs of songs: the next song+/// comes in two and a half seconds before its first such moment, and the+/// overlap is centred on the last one.+fn loudness_edges(bytes: &[u8]) -> Option<(u64, u64, u64)> {+ let hop = scalar_fields(bytes).into_iter().find(|(f, _)| *f == 2).map(|(_, v)| v)?;+ let bands: Vec<&[u8]> = (3..=5).filter_map(|f| read_first_message_field(bytes, f)).collect();+ let n = bands.iter().map(|b| b.len()).min()?;+ if hop == 0 || bands.len() < 3 || n < 2 {+ return None;+ }+ let level: Vec<u64> = (0..n).map(|i| bands.iter().map(|b| b[i] as u64).sum()).collect();+ let mut sorted = level.clone();+ sorted.sort_unstable();+ let threshold = sorted[n / 2] as f64 * EDGE_LEVEL;+ let window = ((500 / hop) as usize).max(1);+ let mut prefix = vec![0u64; n + 1];+ for i in 0..n {+ prefix[i + 1] = prefix[i] + level[i];+ }+ let mean = |i: usize| {+ let end = (i + window).min(n);+ (prefix[end] - prefix[i]) as f64 / (end - i).max(1) as f64+ };+ let first = (0..n).find(|&i| mean(i) >= threshold).unwrap_or(0);+ let last = (1..n).rev().find(|&i| mean(i) >= threshold).unwrap_or(n - 1);+ Some((n as u64 * hop, first as u64 * hop, last as u64 * hop))+}++/// See [loudness_edges].+const EDGE_LEVEL: f64 = 0.15;++/// The singing, 0 to 100, every tenth of a second. The trait's own scale+/// rests at about 14 where nobody sings and tops out at 100.+fn vocal_curve(bytes: &[u8]) -> Vec<u8> {+ let fields = scalar_fields(bytes);+ let rate = fields.iter().find(|(f, _)| *f == 1).map(|(_, raw)| f32::from_bits(*raw as u32) as f64);+ let hop = fields.iter().find(|(f, _)| *f == 4).map(|(_, v)| *v as f64);+ let (Some(rate), Some(hop), Some(values)) = (rate, hop, read_first_message_field(bytes, 5)) else {+ return Vec::new();+ };+ if rate <= 0.0 || hop <= 0.0 {+ return Vec::new();+ }+ let frame_ms = hop * 1000.0 / rate;+ let tenths = (values.len() as f64 * frame_ms / 100.0) as usize;+ (0..tenths)+ .map(|k| {+ let from = ((k as f64 * 100.0) / frame_ms) as usize;+ let to = ((((k + 1) as f64 * 100.0) / frame_ms) as usize).clamp(from + 1, values.len());+ let slice = &values[from.min(values.len() - 1)..to];+ let mean = slice.iter().map(|&v| v as f64).sum::<f64>() / slice.len() as f64;+ (((mean - 14.0) / 86.0).clamp(0.0, 1.0) * 100.0).round() as u8+ })+ .collect()+}++/// `[kind, ms]` for each of Spotify's suggested transition points.+fn transition_points_in(bytes: &[u8]) -> Vec<[u64; 2]> {+ message_fields(bytes, 1)+ .into_iter()+ .filter_map(|point| {+ let fields = scalar_fields(point);+ let kind = fields.iter().find(|(f, _)| *f == 1).map(|(_, v)| *v)?;+ let ms = fields.iter().find(|(f, _)| *f == 2).map(|(_, v)| *v).unwrap_or(0);+ Some([kind, ms])+ })+ .collect()+}++/// Where each section starts; the first, at 0, carries no number at all.+fn sections_in(bytes: &[u8]) -> Vec<u64> {+ message_fields(bytes, 1)+ .into_iter()+ .map(|section| scalar_fields(section).into_iter().find(|(f, _)| *f == 1).map(|(_, v)| v).unwrap_or(0))+ .collect()+}++/// One extended-metadata trait of many tracks, a hundred to a request: the+/// trait's own message for each, out of the `Any` it arrives in, by URI.+async fn extension_payloads(+ session: &Session,+ uris: &[String],+ kind: u64,+) -> Result<std::collections::HashMap<String, Vec<u8>>, String> {+ let mut found = std::collections::HashMap::new();+ for (uri, data) in extension_data(session, uris, kind).await? {+ if let Some(value) = read_first_message_field(&data, 3).and_then(|any| read_first_message_field(any, 2)) {+ found.insert(uri, value.to_vec());+ }+ }+ Ok(found)+}++/// Every `EntityExtensionData` answering one trait of many tracks, by URI.+async fn extension_data(session: &Session, uris: &[String], kind: u64) -> Result<Vec<(String, Vec<u8>)>, String> {+ let mut found = Vec::new();+ for chunk in uris.chunks(100) {+ let mut body = Vec::new();+ for uri in chunk {+ let mut query = Vec::new();+ write_varint(&mut query, 1 << 3);+ write_varint(&mut query, kind);+ let mut entity = Vec::new();+ write_string_field(&mut entity, 1, uri);+ write_len_delimited(&mut entity, 2, &query);+ write_len_delimited(&mut body, 2, &entity);+ }+ let bytes = session+ .spclient()+ .request(&Method::POST, "/extended-metadata/v0/extended-metadata", None, Some(&body))+ .await+ .map_err(|e| format!("extended metadata failed: {e}"))?;+ // BatchedExtensionResponse { 2 EntityExtensionDataArray { 3+ // EntityExtensionData { 2 entity_uri, 3 Any } } }, one per entity.+ for array in message_fields(&bytes, 2) {+ for data in message_fields(array, 3) {+ if let Some(uri) = read_first_message_field_str(data, 2) {+ found.push((uri, data.to_vec()));+ }+ }+ }+ }+ Ok(found)+}++/// One extended-metadata trait of many tracks, each answer read by [read]+/// into the JSON object returned, keyed by URI.+fn batched_extension(+ uris_json: &str,+ kind: u64,+ read: fn(&[u8]) -> Option<Value>,+) -> EngineResult<String> {+ let uris: Vec<String> = serde_json::from_str(uris_json).map_err(|e| format!("bad uris: {e}"))?;+ let session = with_session(|s| s.clone())?;++ block_on(async move {+ let mut found = serde_json::Map::new();+ for (uri, data) in extension_data(&session, &uris, kind).await? {+ if let Some(value) = read(&data) {+ found.insert(uri, value);+ }+ }+ Ok(Value::Object(found).to_string())+ })+}++/// Every scalar field of a message as `(number, raw)`: a varint as read, a+/// fixed32 or fixed64 as its bits, for the caller to read as the float it is.+fn scalar_fields(bytes: &[u8]) -> Vec<(u32, u64)> {+ let mut found = Vec::new();+ let mut cursor = 0usize;+ while cursor < bytes.len() {+ let Some((key, next)) = read_varint(bytes, cursor) else { break };+ cursor = next;+ let field = (key >> 3) as u32;+ match key & 0x7 {+ 0 => {+ let Some((value, next)) = read_varint(bytes, cursor) else { break };+ found.push((field, value));+ cursor = next;+ }+ 5 => {+ let Some(raw) = bytes.get(cursor..cursor + 4) else { break };+ found.push((field, u32::from_le_bytes(raw.try_into().unwrap_or_default()) as u64));+ cursor += 4;+ }+ 1 => {+ let Some(raw) = bytes.get(cursor..cursor + 8) else { break };+ found.push((field, u64::from_le_bytes(raw.try_into().unwrap_or_default())));+ cursor += 8;+ }+ wire => {+ let Some(next) = skip_field(bytes, cursor, wire) else { break };+ cursor = next;+ }+ }+ }+ found+}++/// Every length-delimited field [field] of a message.+fn message_fields(bytes: &[u8], field: u32) -> Vec<&[u8]> {+ let mut found = Vec::new();+ let mut cursor = 0usize;+ while cursor < bytes.len() {+ let Some((key, next)) = read_varint(bytes, cursor) else { break };+ cursor = next;+ let wire = key & 0x7;+ if (key >> 3) as u32 == field && wire == 2 {+ let Some((payload, next)) = read_bytes(bytes, cursor) else { break };+ found.push(payload);+ cursor = next;+ } else {+ let Some(next) = skip_field(bytes, cursor, wire) else { break };+ cursor = next;+ }+ }+ found+}+/// The title of one playlist, or `null`.////// For a playlist arriving as a link there is nowhere else to read it: the@@ -656,6 +1118,99 @@ pub fn track_video(track_uri: &str) -> EngineResult<String> {})}+/// `AUDIO_ATTRIBUTES_V2`: a track's tempo and key, as the official app's Mix+/// reads them. The public audio-features endpoint is shut to new apps, and the+/// old `audio-attributes` one is gone; this is where the numbers went. See+/// [audio_attributes_in] for the message.+const AUDIO_ATTRIBUTES_V2: u64 = 222;++/// The first message in [bytes] shaped like `AudioAttributes`, as JSON.+///+/// AudioAttributes { double bpm = 1; AudioKey key = 2 }+/// AudioKey { string key = 1; int32 mode = 2; Camelot camelot = 3 }+/// Camelot { string value = 1; string color = 2 }+///+/// `mode` is 1 for minor and 2 for major, as the Camelot letter agrees.+/// Searched for rather than walked down to: it sits inside an `Any` inside the+/// batched reply, at a depth that is the reply's business.+fn audio_attributes_in(bytes: &[u8], depth: u32) -> Option<Value> {+ if depth > 8 {+ return None;+ }+ let mut bpm = None;+ let mut key = None;+ let mut nested = Vec::new();+ let mut cursor = 0usize;+ while cursor < bytes.len() {+ let (tag, next) = read_varint(bytes, cursor)?;+ cursor = next;+ let (field, wire) = (tag >> 3, tag & 0x7);+ match wire {+ 1 => {+ let raw = bytes.get(cursor..cursor + 8)?;+ if field == 1 {+ bpm = Some(f64::from_le_bytes(raw.try_into().ok()?));+ }+ cursor += 8;+ }+ 2 => {+ let (payload, next) = read_bytes(bytes, cursor)?;+ if field == 2 {+ key = audio_key_in(payload);+ }+ nested.push(payload);+ cursor = next;+ }+ _ => cursor = skip_field(bytes, cursor, wire)?,+ }+ }+ if let Some(bpm) = bpm.filter(|bpm| (20.0..400.0).contains(bpm)) {+ let (name, mode, camelot) = key.unwrap_or_default();+ return Some(json!({ "bpm": bpm, "key": name, "mode": mode, "camelot": camelot }));+ }+ nested.into_iter().find_map(|payload| audio_attributes_in(payload, depth + 1))+}++/// `AudioKey`'s name, mode and Camelot value, whichever are there.+fn audio_key_in(bytes: &[u8]) -> Option<(String, i64, String)> {+ let mut name = String::new();+ let mut mode = -1i64;+ let mut camelot = String::new();+ let mut cursor = 0usize;+ while cursor < bytes.len() {+ let (tag, next) = read_varint(bytes, cursor)?;+ cursor = next;+ match (tag >> 3, tag & 0x7) {+ (1, 2) => {+ let (payload, next) = read_bytes(bytes, cursor)?;+ name = std::str::from_utf8(payload).ok()?.to_string();+ cursor = next;+ }+ (2, 0) => {+ let (value, next) = read_varint(bytes, cursor)?;+ mode = value as i64;+ cursor = next;+ }+ (3, 2) => {+ let (payload, next) = read_bytes(bytes, cursor)?;+ if let Some(value) = read_first_message_field_str(payload, 1) {+ camelot = value;+ }+ cursor = next;+ }+ (_, wire) => cursor = skip_field(bytes, cursor, wire)?,+ }+ }+ Some((name, mode, camelot))+}++/// The first string field [field] of a message.+fn read_first_message_field_str(bytes: &[u8], field: u32) -> Option<String> {+ read_first_message_field(bytes, field)+ .and_then(|payload| std::str::from_utf8(payload).ok())+ .map(str::to_string)+}+/// `VIDEO_ASSOCIATIONS`, which names a song's video as another track.const VIDEO_ASSOCIATIONS: u64 = 99;diff --git a/native/src/engine.rs b/native/src/engine.rsindex 91d9f87..b0c4215 100644--- a/native/src/engine.rs+++ b/native/src/engine.rs@@ -1062,6 +1062,29 @@ pub fn set_hold_end(enabled: bool) -> EngineResult<()> {Ok(())}+/// The transition into the next song of a mixed playlist, as JSON+/// `{from, outMs, fadeMs, to, inMs}`, or empty for none; see+/// librespot-playback's MixPlan. Set by the app for the pair that is next.+pub fn set_mix_plan(json: &str) -> EngineResult<()> {+ let plan = if json.trim().is_empty() {+ None+ } else {+ let value: serde_json::Value =+ serde_json::from_str(json).map_err(|e| format!("bad mix plan: {e}"))?;+ let text = |key: &str| value[key].as_str().map(str::to_string);+ let number = |key: &str| value[key].as_u64().map(|n| n as u32);+ Some(librespot_playback::player::MixPlan {+ from: text("from").ok_or("mix plan without from")?,+ out_ms: number("outMs").ok_or("mix plan without outMs")?,+ fade_ms: number("fadeMs").ok_or("mix plan without fadeMs")?,+ to: text("to").ok_or("mix plan without to")?,+ in_ms: number("inMs").unwrap_or(0),+ speed_in: value["speedIn"].as_f64().map(|v| v as f32).filter(|v| (0.5..2.0).contains(v)).unwrap_or(1.0),+ })+ };+ with_bundle(|bundle| bundle.player.set_mix_plan(plan))+}+/// What the app's sleep timer is doing, for the state other devices read:/// -1 none, 0 the end of the track, otherwise its end in epoch milliseconds./// Kept by the connect crate, so a rebuilt device still publishes it.diff --git a/native/src/ffi.rs b/native/src/ffi.rsindex a79bda4..0cf3170 100644--- a/native/src/ffi.rs+++ b/native/src/ffi.rs@@ -369,6 +369,38 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeAccguard_string(&mut env, "AccessToken", catalog::access_token)}+#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeTracksMixAnalysis(+ mut env: JNIEnv,+ _class: JClass,+ uris: JString,+) -> jstring {+ let uris = match read_string(&mut env, &uris) {+ Ok(value) => value,+ Err(message) => {+ let _ = env.throw_new(EXCEPTION, message);+ return JObject::null().into_raw() as jstring;+ }+ };+ guard_string(&mut env, "TracksMixAnalysis", || catalog::tracks_mix_analysis(&uris))+}++#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeTracksAudioAttributes(+ mut env: JNIEnv,+ _class: JClass,+ uris: JString,+) -> jstring {+ let uris = match read_string(&mut env, &uris) {+ Ok(value) => value,+ Err(message) => {+ let _ = env.throw_new(EXCEPTION, message);+ return JObject::null().into_raw() as jstring;+ }+ };+ guard_string(&mut env, "TracksAudioAttributes", || catalog::tracks_audio_attributes(&uris))+}+#[no_mangle]pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeTrackVideo(mut env: JNIEnv,@@ -459,6 +491,22 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativePlaguard_string(&mut env, "PlaylistMine", || catalog::playlist_mine(&uri))}+#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativePlaylistMix(+ mut env: JNIEnv,+ _class: JClass,+ uri: JString,+) -> jstring {+ let uri = match read_string(&mut env, &uri) {+ Ok(value) => value,+ Err(message) => {+ let _ = env.throw_new(EXCEPTION, message);+ return JObject::null().into_raw() as jstring;+ }+ };+ guard_string(&mut env, "PlaylistMix", || catalog::playlist_mix(&uri))+}+#[no_mangle]pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativePlaylistCover(mut env: JNIEnv,@@ -625,6 +673,22 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetguard(&mut env, "SetHoldEnd", || engine::set_hold_end(enabled != 0));}+#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetMixPlan(+ mut env: JNIEnv,+ _class: JClass,+ json: JString,+) {+ let json = match read_string(&mut env, &json) {+ Ok(value) => value,+ Err(message) => {+ let _ = env.throw_new(EXCEPTION, message);+ return;+ }+ };+ guard(&mut env, "SetMixPlan", || engine::set_mix_plan(&json));+}+#[no_mangle]pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetLocalFiles(mut env: JNIEnv,diff --git a/native/vendor/README.md b/native/vendor/README.mdindex 3082bd6..2710475 100644--- a/native/vendor/README.md+++ b/native/vendor/README.md@@ -255,6 +255,25 @@ halves go on sharing one clock as before.Zero, the default, leaves every one of these paths as upstream has it.+### The patch: a mixed playlist's transitions++A playlist mixed in Spotify's own app keeps, on the row a transition starts+from, where the song hands over, where the next comes in and how long the two+overlap (read by the app; see `catalog::playlist_mix`). `Player::set_mix_plan`+gives the player the one transition that is next. While the song it names is+playing, its end is announced at the transition's point instead of a+crossfade's length before the end, whatever the crossfade setting, and the+fade lasts as long as the transition says. The song it goes into, loaded from+the start, starts at the transition's point instead.++When the transition is beatmatched, the incoming song plays at the speed the+recipe gives it for the overlap and eases back to its own over eight seconds+(`TempoRamp`). It is resampled, turntable-style, so its key moves with its+tempo, by about a semitone at a typical 6%.++The markers are `LOCAL PATCH` around `MixPlan`, `mix_plan`, `mix_fade_ms` and+`tempo` in `src/player.rs`.+### The patch: the DJ talks between the tracksSpotify's DJ gives some tracks a line to say before them and some a line after,diff --git a/native/vendor/librespot-playback/src/player.rs b/native/vendor/librespot-playback/src/player.rsindex 1e7e5f2..f4a49b6 100644--- a/native/vendor/librespot-playback/src/player.rs+++ b/native/vendor/librespot-playback/src/player.rs@@ -215,6 +215,117 @@ pub enum SinkStatus {pub type SinkEventCallback = Box<dyn Fn(SinkStatus) + Send>;+/// LOCAL PATCH: one transition of a mixed playlist, from the song playing+/// into the one after it.+///+/// A playlist mixed in Spotify's own app keeps, on the row a transition starts+/// from, where the song starts handing over (`out_ms`), where the next one+/// comes in (`in_ms`) and how long the two overlap (`fade_ms`). The player+/// announces the end at `out_ms` instead of a crossfade's length before the+/// end, fades over `fade_ms`, and starts `to` at `in_ms` when it is loaded+/// from the start. Set for the pair that is actually next, and only that one:+/// a shuffled queue or a skip has no use for it, and it does nothing to any+/// other song.+#[derive(Clone, Debug)]+pub struct MixPlan {+ pub from: String,+ pub out_ms: u32,+ pub fade_ms: u32,+ pub to: String,+ pub in_ms: u32,+ /// How fast `to` plays while the two overlap, to put its beats on the+ /// outgoing song's: 1.0 for a transition that is not beatmatched. It+ /// eases back to 1.0 after the overlap; see [TempoRamp].+ pub speed_in: f32,+}++/// LOCAL PATCH: the incoming song of a beatmatched mix transition, played a+/// little faster or slower so its beats land on the outgoing one's.+///+/// Resampled, the way a turntable's pitch fader does it: the key moves with+/// the tempo, by about a semitone at the 6% a typical transition asks for.+/// Held at [MixPlan::speed_in] for the overlap, then eased back to normal+/// over [TEMPO_RETURN_MS], so the song settles into its own tempo rather than+/// jumping to it.+struct TempoRamp {+ speed_in: f64,+ /// Output frames still to come at `speed_in`, then over the return.+ hold: usize,+ ramp: usize,+ done: usize,+ /// Input frames not yet used, interleaved, and where in them the next+ /// output frame is read from.+ carry: Vec<f64>,+ position: f64,+}++/// LOCAL PATCH: how long the incoming song takes to come back to its own+/// tempo after a beatmatched transition.+const TEMPO_RETURN_MS: u32 = 8000;++impl TempoRamp {+ fn new(speed_in: f32, hold_ms: u32) -> Self {+ let frames = |ms: u32| ms as usize * SAMPLE_RATE as usize / 1000;+ Self {+ speed_in: speed_in as f64,+ hold: frames(hold_ms),+ ramp: frames(TEMPO_RETURN_MS),+ done: 0,+ carry: Vec::new(),+ position: 0.0,+ }+ }++ fn speed(&self) -> f64 {+ if self.done < self.hold {+ return self.speed_in;+ }+ let x = ((self.done - self.hold) as f64 / self.ramp.max(1) as f64).min(1.0);+ // Eased, so the tempo leaves the matched speed and arrives at its own+ // without a corner either end.+ let eased = x * x * (3.0 - 2.0 * x);+ self.speed_in + (1.0 - self.speed_in) * eased+ }++ fn finished(&self) -> bool {+ self.done >= self.hold + self.ramp+ }++ /// The packet's samples at the speed of the moment, read between frames.+ fn process(&mut self, data: &[f64]) -> Vec<f64> {+ let channels = NUM_CHANNELS as usize;+ self.carry.extend_from_slice(data);+ let frames = self.carry.len() / channels;+ let mut out = Vec::with_capacity(data.len() + channels * 2);+ while !self.finished() {+ let at = self.position.floor() as usize;+ if at + 1 >= frames {+ break;+ }+ let t = self.position - at as f64;+ for c in 0..channels {+ let a = self.carry[at * channels + c];+ let b = self.carry[(at + 1) * channels + c];+ out.push(a + (b - a) * t);+ }+ self.position += self.speed();+ self.done += 1;+ }+ if self.finished() {+ // Back at its own speed: what is left goes out as it is.+ let at = (self.position.round() as usize).min(frames);+ out.extend_from_slice(&self.carry[at * channels..]);+ self.carry.clear();+ self.position = 0.0;+ } else {+ let used = (self.position.floor() as usize).min(frames);+ self.carry.drain(..used * channels);+ self.position -= used as f64;+ }+ out+ }+}+struct PlayerInternal {session: Session,config: PlayerConfig,@@ -247,6 +358,15 @@ struct PlayerInternal {fade_out: Option<FadeOut>,/// LOCAL PATCH: the play request whose end has already been announced early.early_end: Option<u64>,+ /// LOCAL PATCH: the transition into the next song, when a mixed playlist+ /// names one; see [MixPlan].+ mix_plan: Option<MixPlan>,+ /// LOCAL PATCH: the length of the fade an announced mix transition owes,+ /// in milliseconds, taken by the load that answers the announcement.+ mix_fade_ms: Option<u32>,+ /// LOCAL PATCH: the incoming song of a beatmatched transition, while it+ /// plays off its own tempo; see [TempoRamp].+ tempo: Option<TempoRamp>,/// LOCAL PATCH: the play request that has been heard playing before its own/// fade window, and so may have its end announced early. See the use of it.fade_armed: Option<u64>,@@ -391,6 +511,8 @@ enum PlayerCommand {SetTrimSilence(bool),/// LOCAL PATCH: see PlayerConfig::hold_end.SetHoldEnd(bool),+ /// LOCAL PATCH: see [MixPlan].+ SetMixPlan(Option<MixPlan>),/// LOCAL PATCH: see PlayerConfig::skip_fade_ms.SetSkipFade(u32),EmitSessionDisconnectedEvent {@@ -776,6 +898,9 @@ impl Player {fade_out: None,early_end: None,+ mix_plan: None,+ mix_fade_ms: None,+ tempo: None,fade_armed: None,consecutive_silent_samples: 0,short_end: None,@@ -913,6 +1038,11 @@ impl Player {self.command(PlayerCommand::SetHoldEnd(enabled));}+ /// LOCAL PATCH: see [MixPlan]. None goes back to the ordinary crossfade.+ pub fn set_mix_plan(&self, plan: Option<MixPlan>) {+ self.command(PlayerCommand::SetMixPlan(plan));+ }+/// LOCAL PATCH: see PlayerConfig::skip_fade_ms.pub fn set_skip_fade(&self, ms: u32) {self.command(PlayerCommand::SetSkipFade(ms));@@ -2370,16 +2500,32 @@ impl Future for PlayerInternal {// still reading this track is handed to `fade_out` instead of// being dropped. Nothing here decides what plays next, which is// what keeps the Connect device the only thing that does.- let crossfade_ms = self.config.crossfade_duration_ms as i64;+ // LOCAL PATCH: a mixed playlist's transition, where this+ // track has one, decides both when its end is announced and+ // how long the fade into the next lasts — the point the+ // playlist's author chose rather than a fixed distance from+ // the end, and whatever the crossfade setting says.+ let plan = track_id+ .to_uri()+ .ok()+ .and_then(|uri| self.mix_plan.as_ref().filter(|plan| plan.from == uri).cloned());let position = stream_position_ms as i64;let duration = duration_ms as i64;- if crossfade_ms > 0 && duration > crossfade_ms && !self.config.hold_end && !narration_follows(&track_id) {+ let (crossfade_ms, end_at) = match &plan {+ Some(plan) => (plan.fade_ms as i64, plan.out_ms as i64),+ None => {+ let crossfade_ms = self.config.crossfade_duration_ms as i64;+ (crossfade_ms, duration - crossfade_ms)+ }+ };+ if crossfade_ms > 0 && duration > crossfade_ms && end_at > 0 && !self.config.hold_end && !narration_follows(&track_id) {let silence_threshold_samples = (SAMPLE_RATE as usize * NUM_CHANNELS as usize * 350) / 1000;- let trailing_silence = self.config.trim_silence+ let trailing_silence = plan.is_none()+ && self.config.trim_silence&& self.consecutive_silent_samples >= silence_threshold_samples&& position >= duration - crossfade_ms - 2000;- if position < duration - crossfade_ms && !trailing_silence {+ if position < end_at && !trailing_silence {// LOCAL PATCH: the track has been heard playing outside// its own ending, so its ending means something.//@@ -2399,7 +2545,13 @@ impl Future for PlayerInternal {&& self.fade_armed == Some(play_request_id)&& self.state.is_playing(){- if trailing_silence {+ if let Some(plan) = &plan {+ info!(+ "mix: {position} ms of {duration} ms played, into {} from {} ms over {} ms",+ plan.to, plan.in_ms, plan.fade_ms,+ );+ self.mix_fade_ms = Some(plan.fade_ms);+ } else if trailing_silence {info!("silence trimming: {position} ms of {duration} ms played, trailing silence detected, triggering crossfade early for <{track_id:?}>");@@ -2546,7 +2698,10 @@ impl PlayerInternal {let expected = self.early_end.take();let (total, rising) = match &self.state {PlayerState::Playing { play_request_id, .. } if expected == Some(*play_request_id) => {- (self.crossfade_samples(), None)+ match self.mix_fade_ms.take() {+ Some(ms) => (Self::fade_samples(ms), None),+ None => (self.crossfade_samples(), None),+ }}PlayerState::Playing { .. } => (Self::fade_samples(self.config.skip_fade_ms), Some(0)),// Nothing playing to fade out of. Whatever is already fading, if@@ -2896,6 +3051,15 @@ impl PlayerInternal {Some((_, mut packet)) => {if !packet.is_empty() {if let AudioPacket::Samples(ref mut data) = packet {+ // LOCAL PATCH: a mix's incoming song, at the tempo+ // the transition asked for; see TempoRamp.+ if let Some(tempo) = self.tempo.as_mut() {+ *data = tempo.process(data);+ if tempo.finished() {+ self.tempo = None;+ }+ }+// Get the volume for the packet. In the case of hardware volume control// this will always be 1.0 (no change).let volume = self.volume_getter.attenuation_factor();@@ -3193,6 +3357,23 @@ impl PlayerInternal {let play_request_id =play_request_id_option.unwrap_or(self.play_request_id_generator.get());+ // LOCAL PATCH: the song a mix transition goes into comes in where the+ // transition says, not from its first second; see [MixPlan]. Only a+ // load from the start, which is what moving on to the next song is:+ // a load at a position is a seek or a resume, and keeps it.+ let mut position_ms = position_ms;+ // Whatever song was being brought back to its tempo is gone.+ self.tempo = None;+ if let Some(plan) = self.mix_plan.as_ref() {+ if position_ms == 0 && track_id.to_uri().ok().as_deref() == Some(plan.to.as_str()) {+ info!("mix: {} comes in at {} ms, at {}x", plan.to, plan.in_ms, plan.speed_in);+ position_ms = plan.in_ms;+ if (plan.speed_in - 1.0).abs() > 0.002 {+ self.tempo = Some(TempoRamp::new(plan.speed_in, plan.fade_ms));+ }+ }+ }+// LOCAL PATCH: a line about the track being left goes with it.self.narration = None;@@ -3665,6 +3846,11 @@ impl PlayerInternal {self.config.skip_fade_ms = ms;}+ PlayerCommand::SetMixPlan(plan) => {+ info!("mix plan is now {plan:?}");+ self.mix_plan = plan;+ }+PlayerCommand::SetHoldEnd(enabled) => {info!("hold_end is now {enabled}");self.config.hold_end = enabled;@@ -3882,6 +4068,7 @@ impl fmt::Debug for PlayerCommand {f.debug_tuple("SetTrimSilence").field(enabled).finish()}PlayerCommand::SetSkipFade(ms) => f.debug_tuple("SetSkipFade").field(ms).finish(),+ PlayerCommand::SetMixPlan(plan) => f.debug_tuple("SetMixPlan").field(plan).finish(),PlayerCommand::SetHoldEnd(enabled) => {f.debug_tuple("SetHoldEnd").field(enabled).finish()}