Skip to content

Native Android client for Spotify, built with Jetpack Compose

Palette off the main thread; a skip's cut waits 2 s at most for a phrase

kota-zo62a10880e330
diff --git a/base/app/src/main/java/com/music/spotui/ui/look/MeshGradient.kt b/base/app/src/main/java/com/music/spotui/ui/look/MeshGradient.kt
index a70b0767..514b1ccf 100644
--- a/base/app/src/main/java/com/music/spotui/ui/look/MeshGradient.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/look/MeshGradient.kt
@@ -44,6 +44,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.graphics.ColorUtils
import androidx.palette.graphics.Palette
+import kotlinx.coroutines.launch
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
@@ -246,12 +247,20 @@ fun rememberArtworkColors(imageUrl: String?): MeshPalette {
// so a song change goes straight from one palette to the next, not through the stock purple.
var palette by remember { mutableStateOf(MeshPalette(FallbackColors)) }
+ val scope = androidx.compose.runtime.rememberCoroutineScope()
DisposableEffect(imageUrl) {
if (imageUrl == null) palette = MeshPalette(FallbackColors)
+ var job: kotlinx.coroutines.Job? = null
val target = if (imageUrl == null) null else loadArtwork(context, imageUrl) { bitmap ->
- palette = MeshPalette(artworkMeshColors(bitmap))
+ // The quantising off the main thread (it held it ~160 ms at each song change), on a
+ // small copy of its own: Glide may recycle its bitmap once the target is cleared.
+ val small = Bitmap.createScaledBitmap(bitmap, PALETTE_PX, PALETTE_PX, true)
+ job = scope.launch(kotlinx.coroutines.Dispatchers.Default) {
+ val colors = artworkMeshColors(small)
+ kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Main) { palette = MeshPalette(colors) }
+ }
}
- onDispose { target?.let { Glide.with(context.applicationContext).clear(it) } }
+ onDispose { job?.cancel(); target?.let { Glide.with(context.applicationContext).clear(it) } }
}
return palette
}
@@ -272,6 +281,9 @@ private fun loadArtwork(context: Context, imageUrl: String, onLoaded: (Bitmap) -
* that the backdrop visibly reacts to a track change, short of a full orbit
* that would land the blobs back where they started.
*/
+/** The side of the copy the colours are taken from (Palette scales to about this anyway). */
+private const val PALETTE_PX = 112
+
private const val DRIFT_RADIANS = (PI * 0.45f).toFloat()
/**
diff --git a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
index d449b8db..6a266134 100644
--- a/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
+++ b/base/automix/src/main/kotlin/app/myspoti/automix/MixPlanner.kt
@@ -97,6 +97,9 @@ object MixPlanner {
* over halfway; tempos too far apart: a cut on A's downbeat onto that entry. Null without both
* analyses and grids there: [planSkip] then.
*/
+ /** The longest a skip's cut waits for A's phrase. */
+ private const val CUT_WAIT_SEC = 2.0
+
fun planSkipFull(a: TrackAnalysis?, b: TrackAnalysis?, now: Double, lead: Double, s: Settings = Settings(), window: Double = 4.0): MixPlan? {
if (a == null || b == null || s.style == MixPlan.Style.FADE) return null
val from = now + lead
@@ -127,6 +130,8 @@ object MixPlanner {
}
val entry = entries.maxBy { it.score }
val speed = if (synced) m.speed else 1.0
+ // A cut is over at once: a phrase is worth a short wait only, after that A's next downbeat.
+ if (exit > from + CUT_WAIT_SEC) { exit = firstDown(ga, downA, from); why = "A's next downbeat" }
return MixPlan(MixPlan.Style.CUT, exit, entry.time, 2 * ga.period, speed, 2, 0.0, 0.5, matchGain(a, b),
if (synced) restore(speed, gb.period) else 0.0, String.format("skip (real mix): cut on %s at %.1f onto %s", why, exit, entry.why))
}