Skip to content

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

Keep lyrics per song and prefer word-timed ones from any source

Lelonio9b12a056ab6e
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index 3f5eb96..734a0de 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -66,6 +66,7 @@ class SquareApplication : Application(), ImageLoaderFactory {
// the difference between a page that opens on the right picture and one
// that opens on Spotify's and changes it a second later.
dev.lelonio.square.data.AppleCatalog.attach(this)
+ dev.lelonio.square.backend.lyrics.LyricsLibrary.attach(this)
// Two things feed the offline state, and one thing reads it out to the
// engine. The switch below is the listener's; the watch is the network
diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/LyricsLibrary.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/LyricsLibrary.kt
new file mode 100644
index 0000000..66924be
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/LyricsLibrary.kt
@@ -0,0 +1,217 @@
+package dev.lelonio.square.backend.lyrics
+
+import android.content.Context
+import dev.lelonio.square.data.Catalog
+import dev.lelonio.square.data.Lyrics
+import java.io.File
+import java.security.MessageDigest
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.withContext
+import kotlinx.coroutines.withTimeoutOrNull
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.json.Json
+
+/** Where a song's words can come from; see [LyricsLibrary]. */
+enum class LyricsSource(val label: String) {
+ AMLL("AMLL"),
+ APPLE_MUSIC("Apple Music"),
+ BETTER_LYRICS("Better Lyrics"),
+ SPOTIFY("Spotify"),
+ LRCLIB("LRCLIB"),
+ NETEASE("NetEase"),
+ KUGOU("KuGou"),
+ LYRICS_OVH("Lyrics.ovh"),
+}
+
+/** The song the words are asked for. */
+data class LyricsQuery(val uri: String, val title: String, val artist: String, val durationMs: Long)
+
+/**
+ * Every source of lyrics, in one place, and what they answered before.
+ *
+ * Each backend used to write its own chain by hand, and a source added to one
+ * was missing from the other. Here is the one list: the order a song is asked
+ * in when nobody has chosen, every source at once when the listener wants to
+ * see what else there is, and a copy on disk of what was shown.
+ *
+ * The copy is kept per song with the source it came from. A source the
+ * listener picked stays picked; one the order arrived at is asked again after
+ * a month, since archives fill up and a line-timed answer may by then have a
+ * word-timed one.
+ */
+object LyricsLibrary {
+
+ private val json = Json { ignoreUnknownKeys = true }
+ private var directory: File? = null
+
+ fun attach(context: Context) {
+ directory = File(context.applicationContext.cacheDir, "lyrics").apply { mkdirs() }
+ }
+
+ /** Asked in this order when nobody has chosen; the first answer wins. */
+ fun sourcesFor(uri: String): List<LyricsSource> =
+ if (uri.startsWith("spotify:track:")) {
+ // AMLL is keyed on the Spotify id, and Spotify's own words exist
+ // only for its own tracks.
+ listOf(
+ LyricsSource.AMLL, LyricsSource.APPLE_MUSIC, LyricsSource.BETTER_LYRICS,
+ LyricsSource.SPOTIFY, LyricsSource.LRCLIB, LyricsSource.NETEASE,
+ LyricsSource.KUGOU, LyricsSource.LYRICS_OVH,
+ )
+ } else {
+ listOf(
+ LyricsSource.APPLE_MUSIC, LyricsSource.BETTER_LYRICS, LyricsSource.LRCLIB,
+ LyricsSource.NETEASE, LyricsSource.KUGOU, LyricsSource.LYRICS_OVH,
+ )
+ }
+
+ /** One source's answer, marked with where it came from. */
+ suspend fun fetch(source: LyricsSource, query: LyricsQuery): Lyrics? = runCatching {
+ val (_, title, artist, duration) = query
+ when (source) {
+ LyricsSource.AMLL -> Amll.lyrics(query.uri)
+ LyricsSource.APPLE_MUSIC -> Lossless.lyrics(title, artist, duration)
+ LyricsSource.BETTER_LYRICS -> BetterLyrics.lyrics(title, artist, duration)
+ LyricsSource.SPOTIFY -> Catalog.lyrics(query.uri)
+ LyricsSource.LRCLIB -> LrcLib.lyrics(title, artist, duration)
+ LyricsSource.NETEASE -> NetEase.lyrics(title, artist, duration)
+ LyricsSource.KUGOU -> KuGou.lyrics(title, artist, duration)
+ LyricsSource.LYRICS_OVH -> LyricsOvh.lyrics(title, artist, duration)
+ }
+ }.getOrNull()?.takeIf { it.lines.isNotEmpty() }?.copy(source = source.name)
+
+ /**
+ * Words timed to the word, from any source, are better than words timed
+ * to the line from the first one: the order says who to ask first, this
+ * says what is worth waiting for.
+ */
+ private fun rank(lyrics: Lyrics): Int = when {
+ lyrics.lines.any { it.words.isNotEmpty() } -> 2
+ lyrics.synced -> 1
+ else -> 0
+ }
+
+ private val rechecked = java.util.Collections.synchronizedSet(HashSet<String>())
+
+ private val scope = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.SupervisorJob() + Dispatchers.IO)
+
+ private val _upgrades = kotlinx.coroutines.flow.MutableSharedFlow<Pair<String, Lyrics>>(extraBufferCapacity = 4)
+
+ /**
+ * Better words found for a song after it was shown some: the uri and the
+ * words. The player swaps them in if that song is still the one playing.
+ */
+ val upgrades: kotlinx.coroutines.flow.SharedFlow<Pair<String, Lyrics>> = _upgrades
+
+ /**
+ * The words for a song: what was kept for it, or the best the sources have.
+ *
+ * Asked in the order, and the first answer timed to the word is the one.
+ * An answer timed only to the line is shown at once rather than held back,
+ * and the sources after it are asked in the background for one timed to
+ * the word; when one has it, it is kept and offered through [upgrades].
+ * A song whose word-timed lyrics only one archive held used to be shown
+ * line by line from the first source that answered at all.
+ */
+ suspend fun lyrics(query: LyricsQuery): Lyrics? {
+ val sources = sourcesFor(query.uri)
+ cached(query.uri)?.let { entry ->
+ // Kept from before word timing was looked for everywhere: asked
+ // again in the background, once a session, unless it was chosen.
+ if (!entry.chosen && rank(entry.lyrics) < 2 && rechecked.add(query.uri)) {
+ scope.launch { lookForWordTiming(query, sources.filter { it.name != entry.lyrics.source }) }
+ }
+ return entry.lyrics
+ }
+ var best: Lyrics? = null
+ for ((index, source) in sources.withIndex()) {
+ val found = fetch(source, query) ?: continue
+ if (best == null || rank(found) > rank(best)) best = found
+ if (rank(found) == 2) break
+ // Timed to the line: good enough to show now, and the rest are
+ // asked meanwhile.
+ if (rank(found) == 1) {
+ keep(query.uri, found, chosen = false)
+ val rest = sources.drop(index + 1)
+ scope.launch { lookForWordTiming(query, rest) }
+ return found
+ }
+ }
+ best?.let { keep(query.uri, it, chosen = false) }
+ return best
+ }
+
+ private suspend fun lookForWordTiming(query: LyricsQuery, sources: List<LyricsSource>) {
+ if (sources.isEmpty()) return
+ val answers = coroutineScope {
+ sources.map { source -> async { withTimeoutOrNull(ASK_TIMEOUT_MS) { fetch(source, query) } } }.awaitAll()
+ }
+ val better = answers.firstOrNull { it != null && rank(it) == 2 } ?: return
+ // Not over a choice the listener made in the meantime.
+ if (cached(query.uri)?.chosen == true) return
+ keep(query.uri, better, chosen = false)
+ android.util.Log.i("LyricsLibrary", "word-timed lyrics for ${query.uri} from ${better.source}")
+ _upgrades.tryEmit(query.uri to better)
+ }
+
+ /** Only what is kept, asking nobody: for offline, where the network is not there. */
+ suspend fun cachedLyrics(uri: String): Lyrics? = cached(uri)?.lyrics
+
+ /** What every source has for the song, asked together, in the usual order. */
+ suspend fun everySource(query: LyricsQuery): List<Pair<LyricsSource, Lyrics?>> = coroutineScope {
+ sourcesFor(query.uri).map { source ->
+ async { source to withTimeoutOrNull(ASK_TIMEOUT_MS) { fetch(source, query) } }
+ }.awaitAll()
+ }
+
+ /**
+ * The listener's choice for this song, kept for good: here, and beside a
+ * download, whose own copy is what is read first for a downloaded song.
+ */
+ suspend fun choose(uri: String, lyrics: Lyrics) {
+ keep(uri, lyrics, chosen = true)
+ withContext(Dispatchers.IO) {
+ runCatching {
+ dev.lelonio.square.download.DownloadExtras.rememberLyrics(uri, json.encodeToString(Lyrics.serializer(), lyrics))
+ }
+ }
+ }
+
+ @Serializable
+ private data class Entry(val savedAt: Long, val chosen: Boolean, val lyrics: Lyrics)
+
+ private val memory = android.util.LruCache<String, Entry>(64)
+
+ private suspend fun cached(uri: String): Entry? {
+ memory.get(uri)?.let { return it }
+ val entry = withContext(Dispatchers.IO) {
+ runCatching { json.decodeFromString<Entry>(file(uri)?.readText() ?: return@withContext null) }.getOrNull()
+ } ?: return null
+ if (!entry.chosen && System.currentTimeMillis() - entry.savedAt !in 0..MAX_AGE_MS) return null
+ memory.put(uri, entry)
+ return entry
+ }
+
+ private suspend fun keep(uri: String, lyrics: Lyrics, chosen: Boolean) {
+ val entry = Entry(System.currentTimeMillis(), chosen, lyrics)
+ memory.put(uri, entry)
+ withContext(Dispatchers.IO) {
+ runCatching { file(uri)?.writeText(json.encodeToString(Entry.serializer(), entry)) }
+ }
+ }
+
+ private fun file(uri: String): File? {
+ val dir = directory ?: return null
+ val digest = MessageDigest.getInstance("SHA-1").digest(uri.toByteArray())
+ return File(dir, digest.joinToString("") { "%02x".format(it) } + ".json")
+ }
+
+ /** A source that has not answered by then is shown as having nothing. */
+ private const val ASK_TIMEOUT_MS = 12_000L
+
+ private const val MAX_AGE_MS = 30L * 24 * 60 * 60 * 1000
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/SpotifyLyrics.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/SpotifyLyrics.kt
index 4c19a43..bc57a59 100644
--- a/app/src/main/java/dev/lelonio/square/backend/lyrics/SpotifyLyrics.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/SpotifyLyrics.kt
@@ -27,11 +27,6 @@ object SpotifyLyrics {
private val json = Json { ignoreUnknownKeys = true }
- private class Cached(val lyrics: Lyrics?)
-
- /** Fast session cache so repeat views or replay during playback cost 0ms and 0 network. */
- private val sessionCache = object : android.util.LruCache<String, Cached>(64) {}
-
/**
* @param allowNetwork false offline, where the only lyrics are the kept ones.
*/
@@ -42,23 +37,17 @@ object SpotifyLyrics {
durationMs: Long,
allowNetwork: Boolean = true,
): Lyrics? {
- if (!allowNetwork) return kept(uri)
-
- sessionCache.get(uri)?.let { return it.lyrics }
-
- kept(uri)?.let {
- sessionCache.put(uri, Cached(it))
- return it
- }
+ // Offline: the copy beside a download, or words this phone has shown
+ // before, which LyricsLibrary kept.
+ if (!allowNetwork) return kept(uri) ?: LyricsLibrary.cachedLyrics(uri)
- val found = Amll.lyrics(uri)
- ?: Lossless.lyrics(title, artist, durationMs)
- ?: Catalog.lyrics(uri)
- ?: LrcLib.lyrics(title, artist, durationMs)
- ?: NetEase.lyrics(title, artist, durationMs)
- ?: LyricsOvh.lyrics(title, artist, durationMs)
+ // Repeat views cost nothing: LyricsLibrary keeps its answers in memory
+ // as well as on disk, and a source picked by the listener with them.
+ kept(uri)?.let { return it }
- sessionCache.put(uri, Cached(found))
+ // The order, and every source in it, lives in LyricsLibrary, which
+ // also keeps what it found and the source the listener chose.
+ val found = LyricsLibrary.lyrics(LyricsQuery(uri, title, artist, durationMs))
return withContext(Dispatchers.IO) {
// Nothing found is an answer worth keeping too, so a downloaded
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.kt
index 705d1d9..7a741e8 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
@@ -664,9 +664,9 @@ class YouTubeBackend(private val account: YouTubeAccount) : MusicBackend {
runCatching { lyricsJson.decodeFromString<Lyrics>(raw) }.getOrNull()?.let { return it }
}
- val found = Lossless.lyrics(title, artist, durationMs)
- ?: LrcLib.lyrics(title, artist, durationMs)
- ?: LyricsOvh.lyrics(title, artist, durationMs)
+ val found = dev.lelonio.square.backend.lyrics.LyricsLibrary.lyrics(
+ dev.lelonio.square.backend.lyrics.LyricsQuery(uri, title, artist, durationMs),
+ )
if (found != null) {
runCatching { DownloadExtras.rememberLyrics(uri, lyricsJson.encodeToString(found)) }
diff --git a/app/src/main/java/dev/lelonio/square/data/Catalog.kt b/app/src/main/java/dev/lelonio/square/data/Catalog.kt
index 9b77182..91d7588 100644
--- a/app/src/main/java/dev/lelonio/square/data/Catalog.kt
+++ b/app/src/main/java/dev/lelonio/square/data/Catalog.kt
@@ -106,7 +106,12 @@ data class LyricLine(
data class LyricWord(val startMs: Long, val endMs: Long, val text: String)
@Serializable
-data class Lyrics(val lines: List<LyricLine>, val synced: Boolean)
+data class Lyrics(
+ val lines: List<LyricLine>,
+ val synced: Boolean,
+ /** The LyricsSource it came from, by name; null for words kept before this was recorded. */
+ val source: String? = null,
+)
/**
* A track's Canvas.
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index 49e8618..0d3f0d7 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -1119,6 +1119,18 @@ fun SquareApp(
}
}
+ // Better words found after a song was shown some: word-timed lyrics from a
+ // source further down the order. Swapped in if that song is still on.
+ val playingUri by rememberUpdatedState(playback.mediaId)
+ LaunchedEffect(Unit) {
+ dev.lelonio.square.backend.lyrics.LyricsLibrary.upgrades.collect { (uri, better) ->
+ if (uri == playingUri) {
+ lyrics = better
+ lyricsFor = uri
+ }
+ }
+ }
+
// The language the app is read in. Changing it re-creates the activity,
// which is the only way a Compose tree already built out of one set of
// resources can be rebuilt out of another.