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
Light the words as they are sung
Lelonioa192c13fe717
diff --git a/README.md b/README.mdindex f032772..93883bf 100644--- a/README.md+++ b/README.md@@ -64,8 +64,10 @@ the screen's own frame rate.- **Records what you listen to** *(Spotify)*. Listens are reported to theaccount, filed under the playlist or album they happened in, so Square can beused *instead* of the official client rather than beside it.-- **Lyrics, canvas, artwork.** Synced lyrics, from Spotify where it has them- and from [LrcLib](https://lrclib.net) for the YouTube side; the looping+- **Lyrics, canvas, artwork.** Synced lyrics, from the TTML archive at+ [lossless.wtf](https://lossless.wtf) first — timed to the word, the format+ Apple Music ships — and from Spotify or [LrcLib](https://lrclib.net) for+ everything it does not carry; the loopingSpotify canvas video on the player; and the covers of the generated playlistsin the app's own language.- **Watches the video** *(YouTube Music)*. One tap on the player swaps the audio@@ -214,6 +216,7 @@ is already dead. It is the bug behind the random logouts in most other clients.| [librespot](https://github.com/librespot-org/librespot) | MIT | The Spotify engine. `librespot-core` is vendored with local patches. || [Metrolist](https://github.com/mostafaalagamy/Metrolist) | GPL-3.0 | Its InnerTube client, vendored as [`innertube/`](innertube/), because it is published nowhere else. Powers the signed-in YouTube Music library. || [NewPipeExtractor](https://github.com/TeamNewPipe/NewPipeExtractor) | GPL-3.0 | Anonymous YouTube Music search and stream URLs. |+| [lossless.wtf](https://lossless.wtf) | n/a | Synced TTML lyrics, asked first on both backends. || [LrcLib](https://lrclib.net) | n/a | Synced lyrics for the YouTube Music backend, over its open API. || [Bungee](https://github.com/kupix/bungee) | MPL-2.0 | Time stretching, fetched at build time. || [AndroidLiquidGlass](https://github.com/Kyant0/AndroidLiquidGlass) | Apache-2.0 | The glass material; the catalog components are copied with their notice. |diff --git a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.ktindex 93bbdf8..9d41189 100644--- a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt+++ b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt@@ -216,13 +216,22 @@ class SpotifyBackend(private val container: SquareApplication) : MusicBackend {}}- /** Spotify's own, which the access point serves; see [Catalog.lyrics]. */+ /**+ * lossless.wtf first, Spotify's own behind it; see [Catalog.lyrics].+ *+ * Spotify's are line-synced and only cover what Spotify itself holds. The+ * TTML archive is timed to the word where it has a track, so it is asked+ * first here for the same reason it is on the other backend, and the access+ * point answers for everything it does not have.+ */override suspend fun lyrics(uri: String,title: String,artist: String,durationMs: Long,- ) = Catalog.lyrics(uri)+ ) = dev.lelonio.square.backend.lyrics.Amll.lyrics(uri)+ ?: Lossless.lyrics(title, artist, durationMs)+ ?: Catalog.lyrics(uri)/*** Whether playlists can be made and changed.diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/Amll.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/Amll.ktnew file mode 100644index 0000000..64030cd--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/Amll.kt@@ -0,0 +1,86 @@+package dev.lelonio.square.backend.lyrics++import dev.lelonio.square.data.Lyrics+import dev.lelonio.square.nativecore.NativeBridge+import java.util.concurrent.TimeUnit+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext+import okhttp3.OkHttpClient+import okhttp3.Request+import org.json.JSONObject++/**+ * TTML lyrics addressed by the track itself.+ *+ * A hand-built, public-domain archive whose files are named after the Spotify+ * track they belong to, which is what makes it worth asking before the archives+ * that are searched: every other source is given a title, an artist and a+ * length and has to work out which recording is meant — and works it out wrongly+ * for live takes, remasters and remixes often enough to matter. Here the key is+ * the id already in the queue, so a hit is the right recording by construction.+ *+ * The documents are the same TTML the rest of this package reads, so the+ * parsing, the translations and the word timings all come from [Ttml].+ *+ * Most of the catalogue is not in it. A miss costs one request and falls+ * through to the sources that were always there.+ */+object Amll {++ private val http = OkHttpClient.Builder()+ .callTimeout(5, TimeUnit.SECONDS)+ .build()++ suspend fun lyrics(trackUri: String): Lyrics? = withContext(Dispatchers.IO) {+ val id = trackUri.takeIf { it.startsWith("spotify:track:") }?.substringAfterLast(':')+ ?: return@withContext null++ document(id)?.let { return@withContext it }++ // The same song, under the ids of its other pressings.+ //+ // A recording is on Spotify many times over — the single, the album,+ // the remaster, the copy licensed for one market — and each of those+ // has its own id. An archive addressed by id therefore holds the song+ // and still misses, purely because the listener is playing a different+ // edition of it. Spotify's own metadata lists the copies it relinks+ // between, and reading that list costs one call to the session that is+ // already open.+ for (other in alternatives(trackUri)) {+ val alternative = other.takeIf { it.startsWith("spotify:track:") }+ ?.substringAfterLast(':')+ ?: continue+ if (alternative == id) continue+ document(alternative)?.let { return@withContext it }+ }+ null+ }++ private fun document(id: String): Lyrics? {+ val raw = runCatching {+ val request = Request.Builder().url("$BASE/$id.ttml").build()+ http.newCall(request).execute().use { response ->+ if (response.isSuccessful) response.body?.string() else null+ }+ }.getOrNull() ?: return null++ return Ttml.parse(raw)?.also {+ android.util.Log.i(+ TAG,+ "$id: ${it.lines.size} lines, " ++ "${it.lines.count { line -> line.words.isNotEmpty() }} timed by word",+ )+ }+ }++ private fun alternatives(trackUri: String): List<String> = runCatching {+ val answer = NativeBridge.trackRelatives(trackUri) ?: return emptyList()+ val array = JSONObject(answer).optJSONArray("alternatives") ?: return emptyList()+ (0 until array.length()).mapNotNull { array.optString(it).takeIf(String::isNotEmpty) }+ }.getOrDefault(emptyList())++ private const val BASE =+ "https://raw.githubusercontent.com/amll-dev/amll-ttml-db/main/spotify-lyrics"++ private const val TAG = "SquareAmll"+}diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/Lossless.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/Lossless.ktnew file mode 100644index 0000000..083975a--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/Lossless.kt@@ -0,0 +1,133 @@+package dev.lelonio.square.backend.lyrics++import dev.lelonio.square.data.Lyrics+import java.net.URLEncoder+import kotlin.math.abs+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext+import okhttp3.OkHttpClient+import okhttp3.Request+import org.json.JSONObject++/**+ * Synced TTML lyrics from the archive lossless.wtf reads, asked before anything else.+ *+ * The other two sources each give up something. LrcLib is LRC, so a line is the+ * smallest thing it can time, and its timings are community-entered and often+ * a beat out. Spotify's own are line-synced too, and only exist for a track+ * Spotify itself holds. This archive publishes TTML — the format Apple Music+ * ships, timed down to the word and mastered against the release rather than+ * typed along to it — so where it has a track it is the better answer, and it+ * is asked first for every track on every backend.+ *+ * Two requests: an index is searched by title, artist and length and answers+ * with the releases it holds, each naming the URL of its document; the document+ * itself is then fetched from the storage host.+ *+ * A miss is ordinary: the archive is far smaller than LrcLib's, and most of the+ * catalogue is not in it. Everything here answers null rather than raising, and+ * the caller falls through to the source it used before.+ *+ * @see Ttml for what is done with the document once it arrives.+ */+object Lossless {++ private val http = OkHttpClient()++ /**+ * @param durationMs tells versions apart — a single, an album cut and a live+ * take share a title and an artist, and the length is what separates+ * them. Sent as seconds, and only when it is known.+ */+ suspend fun lyrics(title: String, artist: String, durationMs: Long): Lyrics? =+ withContext(Dispatchers.IO) {+ val cleaned = title.songTitle()+ val results = search(cleaned, artist, durationMs)+ if (results == null) {+ android.util.Log.i(TAG, "no match for $cleaned / $artist (${durationMs / 1000}s)")+ return@withContext null+ }+ val url = pick(results, durationMs) ?: return@withContext null+ val parsed = Ttml.parse(get(url) ?: return@withContext null)+ android.util.Log.i(+ TAG,+ "$url: ${parsed?.lines?.size ?: 0} lines, " ++ "${parsed?.lines?.count { it.words.isNotEmpty() } ?: 0} timed by word, " ++ "${parsed?.lines?.count { !it.translation.isNullOrBlank() } ?: 0} translated",+ )+ parsed+ }++ /**+ * The index's answer, or null when it holds nothing for the track.+ *+ * Matching is on title, artist and length, the same identity the other+ * lyrics sources are searched by — the archive is not keyed on any id+ * Square holds, since a YouTube video id and a Spotify uri each mean+ * nothing to the other.+ */+ private fun search(title: String, artist: String, durationMs: Long): List<JSONObject>? {+ val url = buildString {+ append(SEARCH_URL).append("?track=").append(encode(title))+ append("&artist=").append(encode(artist))+ if (durationMs > 0) append("&duration=").append(durationMs / 1000)+ }+ val body = get(url) ?: return null+ val results = runCatching { JSONObject(body).optJSONArray("results") }.getOrNull()+ ?: return null+ return (0 until results.length())+ .mapNotNull { results.optJSONObject(it) }+ .takeIf { it.isNotEmpty() }+ }++ /**+ * The document to fetch out of everything the index returned.+ *+ * Releases of the same song are timed to different masters, so the one+ * whose length matches what is playing is the one whose timings will land.+ * Word timing wins ties: a release timed to the word is what this source is+ * asked for in the first place, and a line-timed one is no better than what+ * the caller already had.+ */+ private fun pick(results: List<JSONObject>, durationMs: Long): String? = results+ .filter { it.optString("lyricsUrl").startsWith("http") }+ .minByOrNull { result ->+ val byWord = if (result.optString("timing_type") == "word") 0 else 1+ val drift = if (durationMs > 0) {+ abs(result.optLong("duration") * 1000 - durationMs)+ } else {+ 0L+ }+ byWord * 1_000_000L + drift+ }+ ?.optString("lyricsUrl")++ /** Null on anything but a 200, which includes the 404 for "no match". */+ private fun get(url: String): String? = runCatching {+ val request = Request.Builder()+ .url(url)+ .header("Accept", "application/ttml+xml, application/xml, application/json")+ // The archive answers the web player its pages run in, so the+ // request is made to look like one of those pages making it rather+ // than wearing a library's default agent.+ .header("User-Agent", USER_AGENT)+ .header("Referer", "$PLAYER_URL/")+ .header("Origin", PLAYER_URL)+ .build()+ http.newCall(request).execute().use { response ->+ if (!response.isSuccessful) null else response.body?.string()?.takeIf(String::isNotBlank)+ }+ }.getOrNull()++ private fun encode(value: String): String = URLEncoder.encode(value, "UTF-8")++ private const val TAG = "Lossless"++ private const val SEARCH_URL = "https://lyrics-api.binimum.org/"++ private const val PLAYER_URL = "https://lossless.wtf"++ private const val USER_AGENT =+ "Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 (KHTML, like Gecko) " ++ "Chrome/139.0.0.0 Mobile Safari/537.36"+}diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/LrcLib.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/LrcLib.ktsimilarity index 85%rename from app/src/main/java/dev/lelonio/square/backend/youtube/LrcLib.ktrename to app/src/main/java/dev/lelonio/square/backend/lyrics/LrcLib.ktindex df27aca..8ff291a 100644--- a/app/src/main/java/dev/lelonio/square/backend/youtube/LrcLib.kt+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/LrcLib.kt@@ -1,4 +1,4 @@-package dev.lelonio.square.backend.youtube+package dev.lelonio.square.backend.lyricsimport dev.lelonio.square.data.LyricLineimport dev.lelonio.square.data.Lyrics@@ -31,7 +31,7 @@ object LrcLib {*/suspend fun lyrics(title: String, artist: String, durationMs: Long): Lyrics? =withContext(Dispatchers.IO) {- val cleaned = title.cleanedTitle()+ val cleaned = title.songTitle()val body = get(url(cleaned, artist, durationMs))// Retry without the duration before giving up: YouTube's own// duration includes any silence at the end of the upload, so an@@ -77,23 +77,6 @@ object LrcLib {}}.getOrNull()- /**- * Trims what YouTube titles carry and song titles do not.- *- * Uploads are named for a video — "(Official Video)", "[4K Remaster]",- * "(Lyrics)" — and searching a lyrics database for any of that finds- * nothing.- */- private fun String.cleanedTitle(): String =- replace(NOISE, "").replace(WHITESPACE, " ").trim()-- private val NOISE = Regex(- "\\((?:official|lyric|audio|video|visualizer|hd|4k|mv|m/v)[^)]*\\)" +- "|\\[[^\\]]*(?:official|lyric|audio|video|remaster|hd|4k)[^\\]]*\\]",- RegexOption.IGNORE_CASE,- )- private val WHITESPACE = Regex("\\s+")-/*** `[mm:ss.xx] words` per line.*diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/TrackTitle.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/TrackTitle.ktnew file mode 100644index 0000000..84b8107--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/TrackTitle.kt@@ -0,0 +1,19 @@+package dev.lelonio.square.backend.lyrics++/**+ * Trims what upload titles carry and song titles do not.+ *+ * Uploads are named for a video — "(Official Video)", "[4K Remaster]",+ * "(Lyrics)" — and searching a lyrics database for any of that finds nothing.+ * Every source in this package matches on the words of the title, so they all+ * want the same cleaning.+ */+internal fun String.songTitle(): String =+ replace(NOISE, "").replace(WHITESPACE, " ").trim()++private val NOISE = Regex(+ "\\((?:official|lyric|audio|video|visualizer|hd|4k|mv|m/v)[^)]*\\)" ++ "|\\[[^\\]]*(?:official|lyric|audio|video|remaster|hd|4k)[^\\]]*\\]",+ RegexOption.IGNORE_CASE,+)+private val WHITESPACE = Regex("\\s+")diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/Translate.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/Translate.ktnew file mode 100644index 0000000..52139a4--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/Translate.kt@@ -0,0 +1,129 @@+package dev.lelonio.square.backend.lyrics++import java.net.URLEncoder+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext+import okhttp3.OkHttpClient+import okhttp3.Request+import org.json.JSONArray++/**+ * The lyrics in the language the app is read in, for the songs that ship none.+ *+ * A TTML document sometimes carries a subtitle track a label had made, and that+ * is always the better text — a translation of the song rather than of its+ * sentences. Most do not, and most songs are in a language somebody is not+ * reading: the rest are put through Google's own translation endpoint, the one+ * its browser extension talks to, which needs no key and answers a batch of+ * lines at a time.+ *+ * Machine translation of a lyric is what it sounds like, and that is the point+ * of asking for it explicitly: nothing here runs until the toggle over the+ * lyrics is turned on.+ */+object Translate {++ private val http = OkHttpClient()++ /**+ * @param target a two-letter language, as [dev.lelonio.square.data.LanguageStore.language]+ * gives it.+ * @return one translation per line of [texts], in the same order, or null+ * if the endpoint could not be reached at all. A line that came back+ * untranslated is empty rather than a copy of itself.+ */+ suspend fun lines(texts: List<String>, target: String): List<String>? =+ withContext(Dispatchers.IO) {+ // The gaps between verses are lines too, and there is nothing in+ // them to translate. Sent, they would each cost a slot in a batch+ // and come back as themselves.+ val wanted = texts.withIndex().filter { it.value.isNotBlank() }+ if (wanted.isEmpty()) return@withContext null++ val out = arrayOfNulls<String>(texts.size)+ var reached = false++ for (batch in wanted.chunkedByLength()) {+ val answers = translate(batch.map { it.value }, target) ?: continue+ reached = true+ batch.forEachIndexed { at, line ->+ val answer = answers.getOrNull(at).orEmpty()+ // A translation identical to the line is the endpoint+ // saying the song is already in this language, and a second+ // copy of the line under the line is noise.+ if (answer.isNotBlank() && !answer.equals(line.value, ignoreCase = true)) {+ out[line.index] = answer+ }+ }+ }++ if (!reached) null else out.map { it.orEmpty() }+ }++ /**+ * One request: several lines out, the same number back.+ *+ * The lines are sent as repeated `q` parameters rather than joined by+ * newlines, which is the other way this endpoint is used. Joined, a line+ * the translator decides to merge or split takes every line after it out of+ * step with the song, and a lyric sheet one line out is worse than none.+ */+ private fun translate(texts: List<String>, target: String): List<String>? {+ val url = buildString {+ append(BASE_URL)+ append("?client=dict-chrome-ex&sl=auto&tl=").append(encode(target))+ texts.forEach { append("&q=").append(encode(it)) }+ }++ val body = runCatching {+ val request = Request.Builder()+ .url(url)+ .header("User-Agent", USER_AGENT)+ .build()+ http.newCall(request).execute().use { response ->+ if (response.isSuccessful) response.body?.string() else null+ }+ }.getOrNull() ?: return null++ // `[["translated","detected-language"], ...]`, one entry per line sent.+ val array = runCatching { JSONArray(body) }.getOrNull() ?: return null+ return (0 until array.length()).map { at ->+ array.optJSONArray(at)?.optString(0).orEmpty()+ }+ }++ /**+ * Batches, small enough that the whole request fits in a URL.+ *+ * Everything here goes in the query string, and a query string has a length+ * a server will refuse past. A verse or two per request keeps well inside+ * it, and a song is a handful of requests either way.+ */+ private fun List<IndexedValue<String>>.chunkedByLength(): List<List<IndexedValue<String>>> {+ val batches = mutableListOf<List<IndexedValue<String>>>()+ var batch = mutableListOf<IndexedValue<String>>()+ var length = 0++ for (line in this) {+ if (batch.isNotEmpty() && length + line.value.length > BATCH_CHARS) {+ batches += batch+ batch = mutableListOf()+ length = 0+ }+ batch += line+ length += line.value.length+ }+ if (batch.isNotEmpty()) batches += batch+ return batches+ }++ private fun encode(value: String): String = URLEncoder.encode(value, "UTF-8")++ private const val BATCH_CHARS = 1_200++ private const val BASE_URL = "https://clients5.google.com/translate_a/t"++ private const val USER_AGENT =+ "Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 (KHTML, like Gecko) " ++ "Chrome/139.0.0.0 Mobile Safari/537.36"+}diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/Ttml.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/Ttml.ktnew file mode 100644index 0000000..d99903c--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/Ttml.kt@@ -0,0 +1,308 @@+package dev.lelonio.square.backend.lyrics++import android.util.Xml+import dev.lelonio.square.data.LyricLine+import dev.lelonio.square.data.LyricWord+import dev.lelonio.square.data.Lyrics+import java.io.StringReader+import java.util.Locale+import org.xmlpull.v1.XmlPullParser++/**+ * Timed Text Markup Language, the format synced lyrics are published in.+ *+ * TTML is what Apple Music ships its word-by-word lyrics as, and what every+ * timed-lyrics archive has settled on since. A document is a `<body>` of+ * `<div>` sections — verses — each holding `<p>` elements that are the sung+ * lines, and each line carries `begin` and `end` times. Inside a line the words+ * are usually further split into `<span>`s with their own times, which is what+ * makes karaoke highlighting possible.+ *+ * Square's lyrics model is a flat list of lines, each carrying its text, its own+ * `begin`, and the words it was built from with their moments. A line with no+ * `begin` of its own takes the first time inside it, so a document that places+ * only its spans still yields placed lines.+ *+ * Nothing here raises. A document that is not TTML, or is TTML with no sung+ * text, answers null the same way a lyrics lookup that found nothing does.+ */+object Ttml {++ /**+ * @return null when [raw] is not a TTML document, or carries no lines.+ */+ fun parse(raw: String): Lyrics? {+ val document = runCatching { read(raw) }.getOrNull() ?: return null+ val lines = document.lines+ if (lines.isEmpty()) return null++ // Every line must be placed. A document that times some of its lines+ // and not others would leave the view highlighting one line while+ // another is being sung, which reads worse than no timing at all — and+ // this source is only asked because its timing is the better one, so an+ // untimed document is a miss and the caller falls through to LrcLib or+ // Spotify rather than losing the sync it would have had.+ if (lines.any { it.startMs == null }) return null++ return Lyrics(withGaps(lines, chosen(document.translations)), synced = true)+ }++ /** One `<p>`: when it starts, when it stops, and its words, timed and joined. */+ private class Line(+ val startMs: Long?,+ val endMs: Long?,+ val text: String,+ val words: List<LyricWord> = emptyList(),+ /** `itunes:key`, which is how a translation names the line it is for. */+ val key: String? = null,+ )++ private fun read(raw: String): Document {+ val parser = Xml.newPullParser()+ parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)+ parser.setInput(StringReader(raw))++ val lines = mutableListOf<Line>()+ val translations = mutableMapOf<String, MutableMap<String, String>>()+ var inBody = false++ while (parser.next() != XmlPullParser.END_DOCUMENT) {+ when (parser.eventType) {+ XmlPullParser.START_TAG -> when (parser.name) {+ // Times also appear in the head, on styles and regions;+ // only the body holds anything sung.+ "body" -> inBody = true+ "p" -> if (inBody) lines += readLine(parser)+ // Sits in the head, beside the songwriters, and never in+ // the body — so it is read whichever order the two come in.+ "translation" -> readTranslation(parser, translations)+ }++ XmlPullParser.END_TAG -> if (parser.name == "body") inBody = false+ }+ }+ return Document(lines, translations)+ }++ /** A parsed document: the sung lines, and the translations offered for them. */+ private class Document(+ val lines: List<Line>,+ /** Language tag to the line keys it translates. */+ val translations: Map<String, Map<String, String>>,+ )++ /**+ * Reads one `<translation>` block into [into], keyed by its language.+ *+ * A document can carry several — a label ships the subtitle tracks it has —+ * and each is a flat list of `<text for="L4">` naming the line it belongs+ * to by the `itunes:key` that line carries. Leaves the parser on the+ * closing tag.+ */+ private fun readTranslation(+ parser: XmlPullParser,+ into: MutableMap<String, MutableMap<String, String>>,+ ) {+ val language = parser.getAttributeValue(XML_NS, "lang")+ ?: parser.getAttributeValue(null, "lang")+ ?: return+ val texts = into.getOrPut(language) { mutableMapOf() }++ var depth = 1+ while (depth > 0) {+ when (parser.next()) {+ XmlPullParser.START_TAG -> {+ depth+++ if (parser.name == "text") {+ val key = parser.getAttributeValue(null, "for")+ val text = runCatching { parser.nextText() }.getOrNull()+ // nextText leaves the parser past the closing tag it+ // read, so that tag is no longer coming.+ if (text != null) depth--+ if (key != null && !text.isNullOrBlank()) texts[key] = text.trim()+ }+ }++ XmlPullParser.END_TAG -> depth--++ XmlPullParser.END_DOCUMENT -> depth = 0+ }+ }+ }++ /**+ * The translation to show, out of everything the document offers.+ *+ * Only the listener's own language. A document that carries English+ * subtitles for somebody reading Italian is offering the wrong thing, and+ * the caller can translate the song itself: better a machine's Italian than+ * a label's English underneath a Spanish line.+ */+ private fun chosen(translations: Map<String, Map<String, String>>): Map<String, String> {+ if (translations.isEmpty()) return emptyMap()+ val wanted = Locale.getDefault().language+ return translations.entries+ .firstOrNull { it.key.substringBefore('-').equals(wanted, ignoreCase = true) }+ ?.value+ .orEmpty()+ }++ /**+ * Reads one line, with the parser sitting on its `<p>`.+ *+ * The spans inside are the words, each with its own moment, and they are+ * kept as well as joined: the view sweeps a highlight across a line word by+ * word where a source has actually timed them, and falls back to lighting+ * the whole line where it has not. `<br/>` is a line break the model has no+ * room for, so it comes back as a space. Leaves the parser on the closing+ * `</p>`.+ */+ private fun readLine(parser: XmlPullParser): Line {+ val startMs = time(parser.getAttributeValue(null, "begin"))+ val endMs = time(parser.getAttributeValue(null, "end"))+ val key = parser.getAttributeValue(null, "key")++ val text = StringBuilder()+ val words = mutableListOf<LyricWord>()+ // The first time found inside the line, for documents that place only+ // their words and leave the line itself unplaced.+ var firstSpanMs: Long? = null+ // The span the text being read belongs to, if it is a timed one.+ var wordStart: Long? = null+ var wordEnd: Long? = null+ var depth = 1++ while (depth > 0) {+ when (parser.next()) {+ XmlPullParser.START_TAG -> {+ depth+++ val begin = time(parser.getAttributeValue(null, "begin"))+ if (firstSpanMs == null) firstSpanMs = begin+ if (parser.name == "span") {+ wordStart = begin+ wordEnd = time(parser.getAttributeValue(null, "end"))+ }+ // A break is a space, not a join: without this the last word+ // of one half runs into the first of the next.+ if (parser.name == "br") text.append(' ')+ }++ XmlPullParser.END_TAG -> {+ depth--+ if (parser.name == "span") {+ wordStart = null+ wordEnd = null+ }+ }++ XmlPullParser.TEXT -> {+ val piece = parser.text.orEmpty()+ text.append(piece)+ val start = wordStart+ // The whitespace between two spans is a span's text as far+ // as the parser is concerned, and it is not a word.+ if (start != null && piece.isNotBlank()) {+ // A span that says when it starts and not when it stops+ // is rare; a short guess beats dropping the word, since+ // the next word's start ends the highlight anyway.+ words += LyricWord(start, wordEnd ?: (start + WORD_MS), piece.trim())+ }+ }++ XmlPullParser.END_DOCUMENT -> depth = 0+ }+ }++ return Line(+ startMs = startMs ?: firstSpanMs,+ endMs = endMs,+ // TTML collapses whitespace unless a document asks otherwise, and+ // the indentation between spans is whitespace: left alone, every+ // line arrives padded with the newlines of the source file.+ text = WHITESPACE.replace(text, " ").trim(),+ words = words,+ key = key,+ )+ }++ /**+ * Keeps the view honest through the instrumental stretches.+ *+ * A line holds highlighted until the next one starts, so a solo between two+ * verses would leave the last words sung sitting lit for half a minute.+ * TTML says when a line ends, which is exactly what is needed to close it:+ * an empty line at that moment, whenever the silence that follows is long+ * enough to notice. This is the same shape LRC files are written in by+ * hand, and the view already draws those blanks as a gap.+ */+ private fun withGaps(lines: List<Line>, translations: Map<String, String>): List<LyricLine> {+ val out = mutableListOf<LyricLine>()+ lines.forEachIndexed { index, line ->+ out += LyricLine(+ startTimeMs = line.startMs,+ text = line.text,+ words = line.words,+ translation = line.key?.let(translations::get),+ )++ val endMs = line.endMs ?: return@forEachIndexed+ val nextMs = lines.getOrNull(index + 1)?.startMs+ // Nothing after the last line, so close it either way.+ if (nextMs == null || nextMs - endMs >= GAP_MS) {+ out += LyricLine(startTimeMs = endMs, text = "")+ }+ }+ return out+ }++ /** How long a word is assumed to last when its span does not say. */+ private const val WORD_MS = 300L++ /** Silence shorter than this is the breath between two lines, not a gap. */+ private const val GAP_MS = 3_000L++ /**+ * A TTML time expression in milliseconds, or null if it is not one.+ *+ * Two forms are in use: a clock time, `[hh:]mm:ss[.fff]`, and an offset,+ * a number with a unit — `12.5s`, `1200ms` — whose unit Apple leaves off,+ * writing plain seconds. The frame-based clock form+ * (`hh:mm:ss:ff`) needs a frame rate off the document to mean anything and+ * no lyrics publisher writes it, so it is left unread.+ */+ private fun time(value: String?): Long? {+ val text = value?.trim().orEmpty()+ if (text.isEmpty()) return null++ CLOCK.matchEntire(text)?.let { match ->+ val (hours, minutes, seconds) = match.destructured+ return (hours.ifEmpty { "0" }.toLong() * 3_600_000) ++ (minutes.toLong() * 60_000) ++ Math.round(seconds.toDouble() * 1_000)+ }++ OFFSET.matchEntire(text)?.let { match ->+ val (amount, unit) = match.destructured+ val millis = when (unit) {+ "h" -> 3_600_000.0+ "m" -> 60_000.0+ // Apple writes its times as bare seconds — "37.700", no unit —+ // and the documents this reads are Apple's, so a number on its+ // own is seconds rather than something to refuse.+ "s", "" -> 1_000.0+ "ms" -> 1.0+ else -> return null+ }+ return Math.round(amount.toDouble() * millis)+ }++ return null+ }++ private val CLOCK = Regex("(?:(\\d+):)?(\\d{1,2}):(\\d{1,2}(?:\\.\\d+)?)")+ private val OFFSET = Regex("(\\d+(?:\\.\\d+)?)(ms|h|m|s)?")+ private val WHITESPACE = Regex("\\s+")++ /** Where `xml:lang` lives, which is the one namespace never declared. */+ private const val XML_NS = "http://www.w3.org/XML/1998/namespace"+}diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.ktindex bc3692a..4227dda 100644--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubeBackend.kt@@ -7,6 +7,8 @@ import dev.lelonio.square.backend.BackendIdimport dev.lelonio.square.backend.MusicBackendimport dev.lelonio.square.backend.PlaybackHostimport dev.lelonio.square.backend.SearchLabels+import dev.lelonio.square.backend.lyrics.LrcLib+import dev.lelonio.square.backend.lyrics.Losslessimport dev.lelonio.square.data.CatalogPlaylistimport dev.lelonio.square.data.CatalogTrackimport dev.lelonio.square.data.SearchItem@@ -218,12 +220,20 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {}}+ /**+ * lossless.wtf first, LrcLib for everything it does not have.+ *+ * The TTML archive is the better answer where it has the track and much the+ * smaller of the two, so it is asked first and LrcLib carries the rest of+ * the catalogue as it did before.+ */override suspend fun lyrics(uri: String,title: String,artist: String,durationMs: Long,- ) = LrcLib.lyrics(title, artist, durationMs)+ ) = Lossless.lyrics(title, artist, durationMs)+ ?: LrcLib.lyrics(title, artist, durationMs)override val canEditPlaylists: Booleanget() = account.isSignedIndiff --git a/app/src/main/java/dev/lelonio/square/data/Catalog.kt b/app/src/main/java/dev/lelonio/square/data/Catalog.ktindex 847677f..9aa9cd1 100644--- a/app/src/main/java/dev/lelonio/square/data/Catalog.kt+++ b/app/src/main/java/dev/lelonio/square/data/Catalog.kt@@ -52,7 +52,30 @@ data class CatalogTrack(* @param startTimeMs when the line begins, or null for unsynced lyrics — the* distinction decides whether the view can highlight along with playback.*/-data class LyricLine(val startTimeMs: Long?, val text: String)+data class LyricLine(+ val startTimeMs: Long?,+ val text: String,+ /**+ * When each word of this line is sung, where the source says so.+ *+ * Empty for everything that only times whole lines, which is most of what+ * exists: Spotify's own lyrics and LrcLib both stop at the line. See+ * Ttml for the one that does not.+ */+ val words: List<LyricWord> = emptyList(),+ /**+ * The same line in the listener's language, where the source carries it.+ *+ * Only TTML has this, and only for a song worth translating: an Apple+ * document ships the subtitle track a label had made, so it is a real+ * translation rather than a machine's guess, and it is missing far more+ * often than not. Null means the view has nothing to show under the line.+ */+ val translation: String? = null,+)++/** One word, and the moment it belongs to. */+data class LyricWord(val startMs: Long, val endMs: Long, val text: String)data class Lyrics(val lines: List<LyricLine>, val synced: Boolean)diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.ktindex b3f7e48..1df57c5 100644--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt@@ -377,6 +377,16 @@ object NativeBridge {/** Lyrics JSON for a track, or the string `null` when Spotify has none. */fun lyrics(trackUri: String): String = nativeLyrics(trackUri)+ /**+ * The other Spotify ids for the same recording, and its ISRC.+ *+ * `{"isrc":"…","alternatives":["spotify:track:…"]}`. Used to find a track in+ * a database that is addressed by id when the listener is playing a+ * different pressing of the same song; see the Amll lyrics source.+ */+ fun trackRelatives(trackUri: String): String? =+ runCatching { nativeTrackRelatives(trackUri) }.getOrNull()+/*** Canvas JSON for a track — the short looping clip shown behind the player —* or the string `null` when the track has none, which is the common case.@@ -484,5 +494,6 @@ object NativeBridge {private external fun nativeContextTracks(contextUri: String): Stringprivate external fun nativeTracksMetadata(urisJson: String): Stringprivate external fun nativeLyrics(trackUri: String): String+ private external fun nativeTrackRelatives(trackUri: String): Stringprivate external fun nativeCanvas(trackUri: String): String}diff --git a/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt b/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.ktindex 9d41df7..2680356 100644--- a/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt+++ b/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt@@ -3,6 +3,7 @@ package dev.lelonio.square.ui.playerimport androidx.compose.animation.core.Springimport androidx.compose.animation.core.animateFloatAsStateimport androidx.compose.animation.core.spring+import androidx.compose.animation.core.tweenimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSize@@ -53,6 +54,8 @@ fun LyricsView(positionMs: State<Long>,isPlaying: Boolean,modifier: Modifier = Modifier,+ /** Whether to read each line in the listener's language underneath it. */+ showTranslation: Boolean = false,onSeek: (Long) -> Unit,) {// Keyed on the lyrics, so a new track starts at its first line. Kept across@@ -99,16 +102,15 @@ fun LyricsView(val isActive = index == activeLineLyricRow(text = line.text,+ // Real timings where the source has them; see the lyrics package.+ words = line.words,+ translation = line.translation.takeIf { showTranslation },+ positionMs = position,distance = if (activeLine < 0) 0 else abs(index - activeLine),isActive = isActive,unsynced = !lyrics.synced,// Only the line being sung pays for this; the rest are drawn// plain.- sungFraction = if (isActive) {- lineProgress(lyrics, index, position)- } else {- 0f- },onClick = { line.startTimeMs?.let(onSeek) },)}@@ -158,43 +160,18 @@ private fun lineProgress(lyrics: Lyrics, index: Int, positionMs: Long): Float {return ((positionMs - start).toFloat() / span).coerceIn(0f, 1f)}-/**- * Splits a line into words with a share of its duration each.- *- * Spotify times lyrics by line, never by word, so Apple Music's sung-along- * highlight cannot be reproduced from the data — this approximates it by giving- * each word a share of the line proportional to its length. Longer words take- * longer to sing, which is true often enough to read as right; it will drift on- * a line that holds one syllable for a bar, and nothing here can know that.- *- * Trailing space is counted with the word so the highlight sweeps continuously- * instead of stepping across gaps.- */-private fun wordStops(text: String): List<Pair<IntRange, Float>> {- val total = text.count { !it.isWhitespace() }.coerceAtLeast(1)- val stops = mutableListOf<Pair<IntRange, Float>>()- var consumed = 0- var index = 0- while (index < text.length) {- while (index < text.length && text[index].isWhitespace()) index++- if (index >= text.length) break- val start = index- while (index < text.length && !text[index].isWhitespace()) index++- val wordEnd = index- while (index < text.length && text[index].isWhitespace()) index++- consumed += wordEnd - start- stops += (start until index) to consumed.toFloat() / total- }- return stops-}@Composableprivate fun LyricRow(text: String,+ /** When each word is sung, when that is known. */+ words: List<dev.lelonio.square.data.LyricWord>,+ /** The line in the listener's language, if there is one and it is wanted. */+ translation: String?,+ positionMs: Long,distance: Int,isActive: Boolean,unsynced: Boolean,- sungFraction: Float,onClick: () -> Unit,) {val springSpec = spring<Float>(@@ -234,63 +211,180 @@ private fun LyricRow(.padding(horizontal = 22.dp, vertical = 9.dp),contentAlignment = Alignment.CenterStart,) {- // The sung part of the line is fully lit, the rest of it is not.- //- // Word by word rather than a sweep across the whole line: a gradient- // spans the text block, so on a line that wraps it would light the- // second row from the left while the first is still being sung.- val rendered = remember(text, isActive, sungFraction) {- if (!isActive) {- AnnotatedString(text)- } else {- buildAnnotatedString {- append(text)- var previousStop = 0f- wordStops(text).forEach { (range, stop) ->- // Each word lights over its own span rather than- // switching at the moment it starts. A hard switch is- // what made this read as stepping from word to word;- // Apple's version has each one come up as it is reached.- val span = (stop - previousStop).coerceAtLeast(0.0001f)- val lit = ((sungFraction - previousStop) / span).coerceIn(0f, 1f)- previousStop = stop+ val style = MaterialTheme.typography.titleLarge.copy(+ fontSize = 27.sp,+ lineHeight = 33.sp,+ )++ androidx.compose.foundation.layout.Column {++ // One composable per word, so the one being sung can move on its own.+ //+ // Taken from Convx, whose lyrics lift each word as it is reached: it+ // rises a little, grows a little and carries a soft light, then settles+ // as the next takes over. Doing it to the whole line — which is what+ // this did first — makes the whole sentence breathe, which is not the+ // same thing and reads as a wobble.+ //+ // Wrapped by the layout rather than by the text engine, since a lifted+ // word has to be its own thing to lift. The trailing space is part of+ // each word, so the spacing is the font's own rather than a guess.+ androidx.compose.foundation.layout.FlowRow(+ modifier = Modifier+ .then(if (blurRadius > 0.dp) Modifier.blur(blurRadius) else Modifier)+ .graphicsLayer {+ // Scale from the left edge so the text grows into the line+ // instead of drifting sideways.+ transformOrigin = androidx.compose.ui.graphics.TransformOrigin(0f, 0.5f)+ scaleX = scale+ scaleY = scale+ this.alpha = alpha+ },+ ) {+ // Word by word only where the words really are timed.+ //+ // A line-timed source knows when the line begins and nothing else,+ // and dividing it by how much each word has to say is a guess: on a+ // held note it lights three words while one is still being sung.+ // Better to say what is known — this line, now — and light the+ // whole of it. The words move only when a source has actually timed+ // them; see the lyrics package.+ if (words.isEmpty()) {+ // The whole line does what a word does when a word is timed:+ // rises, grows and carries a light while it is the one being+ // sung. What is not known is where inside the line the singing+ // has reached, so the line is treated as the unit it is.+ val lineBump by animateFloatAsState(+ targetValue = if (isActive) 1f else 0f,+ animationSpec = spring(+ dampingRatio = Spring.DampingRatioLowBouncy,+ stiffness = Spring.StiffnessLow,+ ),+ label = "lineBump",+ )++ Text(+ text = text,+ style = style.copy(+ shadow = if (lineBump > 0.05f) {+ androidx.compose.ui.graphics.Shadow(+ color = Color.White.copy(alpha = 0.4f * lineBump),+ offset = androidx.compose.ui.geometry.Offset.Zero,+ blurRadius = 16f * lineBump,+ )+ } else {+ null+ },+ ),+ fontWeight = FontWeight.Bold,+ color = Color.White,+ textAlign = TextAlign.Start,+ modifier = Modifier.graphicsLayer {+ translationY = -4.dp.toPx() * lineBump+ scaleX = 1f + 0.02f * lineBump+ scaleY = 1f + 0.02f * lineBump+ },+ )+ } else {+ // Each word carries the space that follows it in the line.+ //+ // Taken out, the words sat against each other: what is drawn+ // here is a row of separate pieces of text, and a row has no+ // idea that written language puts gaps between words.+ val pieces = remember(text, words) {+ var at = 0+ words.map { word ->+ val start = text.indexOf(word.text, at).takeIf { it >= 0 } ?: at+ var stop = start + word.text.length+ while (stop < text.length && text[stop].isWhitespace()) stop+++ at = stop+ text.substring(start, stop)+ }+ }++ pieces.forEachIndexed { index, piece ->+ val word = words[index]+ val span = (word.endMs - word.startMs).coerceAtLeast(1L)+ val lit = if (isActive) {+ ((positionMs - word.startMs).toFloat() / span).coerceIn(0f, 1f)+ } else {+ 0f+ }+// Eased at both ends, so a word neither snaps on nor// finishes brightening a beat before the next begins.val eased = lit * lit * (3f - 2f * lit)- addStyle(- SpanStyle(- color = Color.White.copy(- alpha = DIM_WORD_ALPHA + (1f - DIM_WORD_ALPHA) * eased,- ),+ // And a half turn of a sine across the word: strongest in+ // the middle of it, gone by the time the next one starts.+ val bump = if (isActive && lit > 0f && lit < 1f) {+ kotlin.math.sin(lit * kotlin.math.PI.toFloat())+ } else {+ 0f+ }++ Text(+ text = piece,+ style = style.copy(+ shadow = if (bump > 0.05f) {+ androidx.compose.ui.graphics.Shadow(+ color = Color.White.copy(alpha = 0.4f * bump),+ offset = androidx.compose.ui.geometry.Offset.Zero,+ blurRadius = 16f * bump,+ )+ } else {+ null+ },+ ),+ fontWeight = FontWeight.Bold,+ // White rather than the artwork accent. These sit over+ // a Canvas now, and an accent pulled from the cover can+ // land anywhere — including on the clip's own colours.+ color = Color.White.copy(+ alpha = if (isActive) {+ DIM_WORD_ALPHA + (1f - DIM_WORD_ALPHA) * eased+ } else {+ 1f+ },),- range.first,- range.last + 1,+ textAlign = TextAlign.Start,+ modifier = Modifier.graphicsLayer {+ translationY = -4.dp.toPx() * bump+ scaleX = 1f + 0.02f * bump+ scaleY = 1f + 0.02f * bump+ },)}}}- }- Text(- text = rendered,- style = MaterialTheme.typography.titleLarge.copy(fontSize = 27.sp, lineHeight = 33.sp),- fontWeight = FontWeight.Bold,- // White rather than the artwork accent. These sit over a Canvas now,- // and an accent pulled from the cover can land anywhere — including- // on the clip's own colours.- color = Color.White,- textAlign = TextAlign.Start,- modifier = Modifier- .then(if (blurRadius > 0.dp) Modifier.blur(blurRadius) else Modifier)- .graphicsLayer {- // Scale from the left edge so the text grows into the line- // instead of drifting sideways.- transformOrigin = androidx.compose.ui.graphics.TransformOrigin(0f, 0.5f)- scaleX = scale- scaleY = scale- this.alpha = alpha- },- )+ // Under the line, quieter and smaller: a second voice reading+ // along, not a second line of the song. It fades, blurs and grows+ // with the line above it, being part of the same line.+ if (!translation.isNullOrBlank()) {+ Text(+ text = translation,+ style = MaterialTheme.typography.bodyMedium.copy(+ fontSize = 15.sp,+ lineHeight = 19.sp,+ ),+ color = Color.White.copy(alpha = 0.62f),+ textAlign = TextAlign.Start,+ modifier = Modifier+ .then(if (blurRadius > 0.dp) Modifier.blur(blurRadius) else Modifier)+ .padding(top = 3.dp)+ .graphicsLayer {+ // Anchored left with the words above, so it does+ // not drift out from under its own line as that+ // one grows.+ transformOrigin =+ androidx.compose.ui.graphics.TransformOrigin(0f, 0.5f)+ scaleX = scale+ scaleY = scale+ this.alpha = alpha+ },+ )+ }+ }}}diff --git a/app/src/main/java/dev/lelonio/square/ui/player/TranslationToggle.kt b/app/src/main/java/dev/lelonio/square/ui/player/TranslationToggle.ktnew file mode 100644index 0000000..9ddb383--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/ui/player/TranslationToggle.kt@@ -0,0 +1,145 @@+package dev.lelonio.square.ui.player++import androidx.compose.animation.core.animateFloatAsState+import androidx.compose.animation.core.tween+import androidx.compose.foundation.background+import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.awaitFirstDown+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.size+import androidx.compose.material3.Icon+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.input.pointer.PointerEventPass+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.adamglin.PhosphorIcons+import com.adamglin.phosphoricons.Fill+import com.adamglin.phosphoricons.Regular+import com.adamglin.phosphoricons.fill.Translate+import com.adamglin.phosphoricons.regular.Translate+import dev.lelonio.square.R+import dev.lelonio.square.ui.glass.LocalGlassEffectConfig+import dev.lelonio.square.ui.glass.backdrop.Backdrop+import dev.lelonio.square.ui.glass.liquidGlass+import dev.lelonio.square.ui.glass.shapes.ContinuousCapsule++/**+ * Whether to read the song in your own language as well.+ *+ * The same button the karaoke dial is when it is closed — the glass capsule the+ * width of a thumb, the filled glyph for on and the outline for off — because+ * the two sit beside each other over the lyrics and anything else would read as+ * two different kinds of control.+ *+ * On for a song that ships its own translation and for one that does not: the+ * lines the document has no subtitles for are put through a translator when the+ * switch is turned on, which is the wait the dimmed glyph stands for.+ */+@Composable+fun TranslationToggle(+ on: Boolean,+ /** Turns the glyph into a spinner while the lines are away being translated. */+ busy: Boolean = false,+ onChange: (Boolean) -> Unit,+ backdrop: Backdrop,+ modifier: Modifier = Modifier,+) {+ var pressed by remember { mutableStateOf(false) }+ val change by rememberUpdatedState(onChange)+ val state by rememberUpdatedState(on)++ // Gives under the finger, like every other control in the player.+ val grip by animateFloatAsState(+ targetValue = if (pressed) 0.94f else 1f,+ animationSpec = tween(140),+ label = "translationGrip",+ )+ val lit by animateFloatAsState(+ targetValue = if (pressed) 0.12f else 0f,+ animationSpec = tween(140),+ label = "translationPress",+ )++ Box(+ modifier+ .size(SIZE)+ .graphicsLayer {+ scaleX = grip+ scaleY = grip+ }+ .clip(ContinuousCapsule())+ .liquidGlass(+ config = LocalGlassEffectConfig.current,+ shape = ContinuousCapsule(),+ ownBackdrop = backdrop,+ highlightAlpha = BarHighlightAlpha,+ backdropScale = 0.4f,+ )+ // Claimed in the first pass, for the reason the dial beside it+ // does: the lyrics behind this scroll, and the player above it+ // closes on a drag, so a touch left unconsumed becomes theirs.+ .pointerInput(Unit) {+ awaitEachGesture {+ val first = awaitFirstDown(+ requireUnconsumed = false,+ pass = PointerEventPass.Initial,+ )+ first.consume()+ pressed = true+ var moved = 0f++ while (true) {+ val event = awaitPointerEvent(PointerEventPass.Initial)+ val pointer = event.changes.firstOrNull { it.id == first.id } ?: break+ if (!pointer.pressed) break+ moved += kotlin.math.abs(pointer.position.y - first.position.y)+ pointer.consume()+ }++ pressed = false+ // A finger that travelled was going somewhere else.+ if (moved < viewConfiguration.touchSlop) change(!state)+ }+ },+ contentAlignment = Alignment.Center,+ ) {+ if (lit > 0f) {+ Box(Modifier.fillMaxSize().background(Color.White.copy(alpha = lit)))+ }++ // A turning ring while the lines are away, in place of the glyph: the+ // wait is a request over the network on a song nobody has asked for+ // before, and a button that only dimmed looked like one that had not+ // registered the press.+ if (busy) {+ androidx.compose.material3.CircularProgressIndicator(+ color = Color.White,+ strokeWidth = 2.dp,+ modifier = Modifier.size(18.dp),+ )+ } else {+ Icon(+ if (on) PhosphorIcons.Fill.Translate else PhosphorIcons.Regular.Translate,+ contentDescription = stringResource(R.string.translation),+ // White throughout, as the dial is: the filled glyph is what+ // says it is on, and a coloured one competes with the artwork.+ tint = Color.White,+ modifier = Modifier.size(20.dp),+ )+ }+ }+}++private val SIZE = 44.dpdiff --git a/native/src/catalog.rs b/native/src/catalog.rsindex bbdd3b0..28c3f17 100644--- a/native/src/catalog.rs+++ b/native/src/catalog.rs@@ -570,6 +570,46 @@ pub fn access_token() -> EngineResult<String> {/// BatchedEntityRequest { repeated EntityRequest entity_request = 2 }/// EntityRequest { string entity_uri = 1, repeated ExtensionQuery query = 2 }/// ExtensionQuery { ExtensionKind extension_kind = 1 }+/// The other Spotify ids for the same recording, and its ISRC.+///+/// A song is on Spotify many times over — the single, the album, the remaster,+/// the market-specific pressing — and each of those is its own id. Anything+/// addressed by id therefore misses whenever the listener happens to be playing+/// a different edition of the same recording, which is what the word-by-word+/// lyric database does; see AmllLyrics.+///+/// Both answers come from metadata the account already serves: `alternatives`+/// is Spotify's own list of the copies it relinks between, and the ISRC is the+/// recording's number in the industry's register, which is the same on every+/// pressing of it.+pub fn track_relatives(track_uri: &str) -> EngineResult<String> {+ let uri = SpotifyUri::from_uri(track_uri).map_err(|e| format!("bad track uri: {e}"))?;+ let session = with_session(|s| s.clone())?;++ block_on(async move {+ let track = Track::get(&session, &uri)+ .await+ .map_err(|e| format!("track lookup failed: {e}"))?;++ let alternatives: Vec<String> = track+ .alternatives+ .0+ .iter()+ .filter_map(|id| id.to_uri().ok())+ .collect();++ let isrc = track+ .external_ids+ .0+ .iter()+ .find(|id| id.external_type.eq_ignore_ascii_case("isrc"))+ .map(|id| id.id.clone())+ .unwrap_or_default();++ Ok(json!({ "isrc": isrc, "alternatives": alternatives }).to_string())+ })+}+pub fn track_video(track_uri: &str) -> EngineResult<String> {let session = with_session(|s| s.clone())?;let uri = track_uri.to_string();diff --git a/native/src/ffi.rs b/native/src/ffi.rsindex 55f0895..cd68c7b 100644--- a/native/src/ffi.rs+++ b/native/src/ffi.rs@@ -460,6 +460,23 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeLyr}+#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeTrackRelatives(+ mut env: JNIEnv,+ _class: JClass,+ track_uri: JString,+) -> jstring {+ let uri = match read_string(&mut env, &track_uri) {+ Ok(value) => value,+ Err(message) => {+ let _ = env.throw_new(EXCEPTION, message);+ return JObject::null().into_raw() as jstring;+ }+ };+ guard_string(&mut env, "TrackRelatives", || catalog::track_relatives(&uri))++}+#[no_mangle]pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeTracksMetadata(mut env: JNIEnv,