Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Lossless matching finds slowed, remastered and featured tracks: Tidal's version joins its title, any credited artist counts, the search asks for one artist and the bare title
kota-zo92850bb3cb42
diff --git a/base/app/src/main/java/com/music/spotui/lossless/LosslessSource.kt b/base/app/src/main/java/com/music/spotui/lossless/LosslessSource.ktindex fd8d8e6f..2219a666 100644--- a/base/app/src/main/java/com/music/spotui/lossless/LosslessSource.kt+++ b/base/app/src/main/java/com/music/spotui/lossless/LosslessSource.kt@@ -282,30 +282,55 @@ object LosslessSource {internal data class TidalHit(val id: String, val title: String, val artist: String, val durationSec: Int, val isrc: String,+ /** Every credited artist, when the search lists them (the first alone is [artist]). */+ val artists: List<String> = emptyList(),)/** Spotify id → Tidal track via the Tidal backend's own search, ISRC or title matched. */internal suspend fun tidalHit(info: TrackInfo): TidalHit? {- val q = URLEncoder.encode("${info.title} ${info.artist}", "UTF-8")+ val queries = searchQueries(info.title, info.artist)for (endpoint in LosslessRegistry.tidalEndpoints()) {if (endpoint.isBlank()) continue- val hits = runCatching {- val root = JSONObject(get("$endpoint/search/?s=$q", keyHeader()))- val items: JSONArray = root.optJSONObject("data")?.optJSONArray("items") ?: JSONArray()- (0 until items.length()).mapNotNull { i ->- val o = items.optJSONObject(i) ?: return@mapNotNull null- TidalHit(- id = o.optString("id"), title = o.optString("title"),- artist = o.optJSONObject("artist")?.optString("name").orEmpty(),- durationSec = o.optInt("duration"), isrc = o.optString("isrc"),- )- }- }.getOrElse { Log.w(TAG, "tidal search $endpoint: ${it.message}"); emptyList() }- pickHit(hits, info.isrc, info.title, info.artist, info.durationSec)?.let { return it }+ for (query in queries) {+ val q = URLEncoder.encode(query, "UTF-8")+ val hits = runCatching {+ val root = JSONObject(get("$endpoint/search/?s=$q", keyHeader()))+ val items: JSONArray = root.optJSONObject("data")?.optJSONArray("items") ?: JSONArray()+ (0 until items.length()).mapNotNull { i ->+ val o = items.optJSONObject(i) ?: return@mapNotNull null+ val names = o.optJSONArray("artists")?.let { a -> (0 until a.length()).mapNotNull { a.optJSONObject(it)?.optString("name") } }.orEmpty()+ TidalHit(+ id = o.optString("id"), title = fullTitle(o.optString("title"), o.optString("version")),+ artist = o.optJSONObject("artist")?.optString("name").orEmpty(),+ durationSec = o.optInt("duration"), isrc = o.optString("isrc"),+ artists = names,+ )+ }+ }.getOrElse { Log.w(TAG, "tidal search $endpoint: ${it.message}"); emptyList() }+ pickHit(hits, info.isrc, info.title, info.artist, info.durationSec)?.let { return it }+ }}return null}+ /**+ * What is typed in the search: the title and the main artist ("A, B" is Spotify's list, Tidal's+ * search wants one name), then the bare title when Spotify's carries a version suffix+ * ("Song - 2011 Remaster", "Song - Slowed") that makes the full phrase match nothing.+ */+ internal fun searchQueries(title: String, artist: String): List<String> {+ val main = artist.substringBefore(",").trim()+ val bare = title.substringBefore(" - ").substringBefore(" (").trim()+ return listOf("$title $main".trim(), "$bare $main".trim()).filter { it.isNotBlank() }.distinct()+ }++ /**+ * Tidal keeps the version of a recording apart from its title ("Song" + "2011 Remaster",+ * "Slowed + Reverb"), where Spotify writes it into the title: the two are put back together.+ */+ internal fun fullTitle(title: String, version: String): String =+ if (version.isBlank() || titleKey(title).containsAll(titleKey(version))) title else "$title ($version)"+/*** The hit is the very recording Spotify plays: both sides have an ISRC and it is the* same. A title + artist match (no ISRC, or another one) is not an ISRC match.@@ -320,15 +345,34 @@ object LosslessSource {internal fun pickHit(hits: List<TidalHit>, isrc: String, title: String, artist: String, durationSec: Int,): TidalHit? {- fun key(s: String) = s.lowercase().filter { it.isLetterOrDigit() }hits.firstOrNull { isrc.isNotBlank() && it.isrc.equals(isrc, ignoreCase = true) && it.id.isNotBlank() }?.let { return it }+ val wanted = titleKey(title)+ val artists = artist.split(',').map(::key).filter { it.isNotEmpty() }return hits.firstOrNull {- it.id.isNotBlank() && key(it.title) == key(title) && key(it.artist) == key(artist) &&+ it.id.isNotBlank() && titleKey(it.title) == wanted && sameArtist(it, artists) &&(durationSec <= 0 || kotlin.math.abs(it.durationSec - durationSec) <= 3)}}+ /**+ * A title as the words it is made of, order aside: "Song - 2011 Remaster" and "Song (Remastered+ * 2011)" are one recording, "Song - Slowed" and "Song" are not (the duration tells the rest).+ */+ internal fun titleKey(title: String): List<String> = title.lowercase()+ .split(Regex("[^\\p{L}\\p{N}]+")).filter { it.isNotEmpty() && it != "version" }+ .map { if (it == "remastered") "remaster" else it }.sorted()++ private fun key(s: String) = s.lowercase().filter { it.isLetterOrDigit() }++ /**+ * The hit's main artist, or any of its credited ones, is one of Spotify's: Spotify hands over+ * "A, B", Tidal's search names the first alone, and requiring the whole string made every+ * track with a featured artist unfindable.+ */+ private fun sameArtist(hit: TidalHit, artists: List<String>): Boolean =+ (listOf(hit.artist) + hit.artists).map(::key).any { it.isNotEmpty() && it in artists }+// Qobuz public search API (app_id/secret are the community's published constants).private const val QOBUZ_APP_ID = "712109809"private const val QOBUZ_APP_SECRET = "589be88e4538daea11f509d29e4a23b1"diff --git a/base/app/src/test/java/com/music/spotui/lossless/LosslessMatchTest.kt b/base/app/src/test/java/com/music/spotui/lossless/LosslessMatchTest.ktindex 0a26003d..0a650464 100644--- a/base/app/src/test/java/com/music/spotui/lossless/LosslessMatchTest.kt+++ b/base/app/src/test/java/com/music/spotui/lossless/LosslessMatchTest.kt@@ -47,6 +47,44 @@ class LosslessMatchTest {// The track's own ISRC decides which of the same-named candidates wins.assertEquals("1", LosslessSource.pickHit(hits, "GBUM71705174", "Real Title", "Artist", 200)?.id)}++ @Test+ fun aVersionKeptApartByTidalIsPutBackInTheTitle() {+ assertEquals("Song (2011 Remaster)", LosslessSource.fullTitle("Song", "2011 Remaster"))+ // Already part of the title: not written twice.+ assertEquals("Song (Slowed)", LosslessSource.fullTitle("Song (Slowed)", "Slowed"))+ assertEquals("Song", LosslessSource.fullTitle("Song", ""))+ val hits = listOf(hit("1", LosslessSource.fullTitle("Song", "2011 Remaster"), "Artist", 200))+ assertEquals("1", LosslessSource.pickHit(hits, "", "Song - 2011 Remaster", "Artist", 200)?.id)+ }++ @Test+ fun remasterSpellingsAndSlowedSuffixesMatch() {+ val hits = listOf(+ hit("1", "Song (Remastered 2011)", "Artist", 200),+ hit("2", "Song (Slowed + Reverb)", "Artist", 250),+ )+ assertEquals("1", LosslessSource.pickHit(hits, "", "Song - 2011 Remaster", "Artist", 200)?.id)+ assertEquals("2", LosslessSource.pickHit(hits, "", "Song - Slowed + Reverb", "Artist", 250)?.id)+ // The plain song is not its slowed version, nor the reverse.+ assertNull(LosslessSource.pickHit(hits.take(1), "", "Song - Slowed + Reverb", "Artist", 200))+ assertNull(LosslessSource.pickHit(hits.drop(1), "", "Song", "Artist", 250))+ }++ @Test+ fun spotifyListsEveryArtistTidalNamesOne() {+ val hits = listOf(hit("1", "Real Title", "Artist", 200))+ assertEquals("1", LosslessSource.pickHit(hits, "", "Real Title", "Artist, Guest", 200)?.id)+ val credited = listOf(LosslessSource.TidalHit("2", "Real Title", "Other", 200, "", listOf("Other", "Guest")))+ assertEquals("2", LosslessSource.pickHit(credited, "", "Real Title", "Artist, Guest", 200)?.id)+ assertNull(LosslessSource.pickHit(hits, "", "Real Title", "Someone, Guest", 200))+ }++ @Test+ fun theSearchAsksForOneArtistThenTheBareTitle() {+ assertEquals(listOf("Song - Slowed A", "Song A"), LosslessSource.searchQueries("Song - Slowed", "A, B"))+ assertEquals(listOf("Song A"), LosslessSource.searchQueries("Song", "A"))+ }}/** The FLAC STREAMINFO read that rejects 30 s previews handed out by backends. */