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.

Make the player smooth over a moving cover, and fix four reported issues

Lelonio783823888e0b
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
index d172f6c..2aec430 100644
--- a/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/YouTubePlayerFactory.kt
@@ -90,7 +90,10 @@ object YouTubePlayerFactory {
dev.lelonio.square.R.string.youtube_sign_in_required
causes.any { it is SignedInStreams.Refused } ->
dev.lelonio.square.R.string.youtube_signed_in_unplayable
- else -> return
+ else -> {
+ recover(exo, error)
+ return
+ }
}
android.os.Handler(android.os.Looper.getMainLooper()).post {
android.widget.Toast.makeText(
@@ -150,6 +153,50 @@ object YouTubePlayerFactory {
* address, so nothing reloads: only the name, the picture and the id the
* rest of the app reads (lyrics, credits, likes) change.
*/
+ /** The song last picked up again after an error, and when, and how often. */
+ private var recovering: Triple<String, Long, Int>? = null
+
+ /**
+ * Picks the song up again where it stopped, after an error that was not
+ * about the account.
+ *
+ * Everything else used to be left as it was: a stream address that had
+ * expired, a 403 halfway through, a connection that dropped for a moment,
+ * and the player sat stopped in its error with the song half played until
+ * the listener came back and chose another. Asked again, with the addresses
+ * it had forgotten, almost every one of these plays on. One that fails
+ * [RETRIES] times in a row within [RETRY_WINDOW_MS] is passed over, as a
+ * song that cannot be played would be.
+ */
+ private fun recover(exo: ExoPlayer, error: androidx.media3.common.PlaybackException) {
+ val item = exo.currentMediaItem ?: return
+ val id = item.mediaId
+ if (!exo.playWhenReady) return
+ val now = android.os.SystemClock.elapsedRealtime()
+ val last = recovering?.takeIf { it.first == id && now - it.second < RETRY_WINDOW_MS }
+ val attempt = (last?.third ?: 0) + 1
+ recovering = Triple(id, now, attempt)
+ YouTubeStreams.forget(YouTubeBackend.videoIdOfUri(id))
+ val at = exo.currentPosition
+ val index = exo.currentMediaItemIndex
+ android.util.Log.w("SquareYT", "playback failed (${error.errorCodeName}), try $attempt at ${at}ms")
+ android.os.Handler(exo.applicationLooper).postDelayed({
+ // Moved on in the meantime: nothing of this to pick up.
+ if (exo.currentMediaItem?.mediaId != id || exo.playerError == null) return@postDelayed
+ if (attempt > RETRIES) {
+ if (exo.hasNextMediaItem()) exo.seekToNextMediaItem() else return@postDelayed
+ } else {
+ exo.seekTo(index, at)
+ }
+ exo.prepare()
+ exo.play()
+ }, RETRY_DELAY_MS * attempt)
+ }
+
+ private const val RETRIES = 3
+ private const val RETRY_DELAY_MS = 1_500L
+ private const val RETRY_WINDOW_MS = 60_000L
+
private fun retitle(player: ExoPlayer, video: String, song: com.metrolist.innertube.models.SongItem) {
val listed = "${YouTubeBackend.TRACK_PREFIX}$video"
for (index in 0 until player.mediaItemCount) {
@@ -569,6 +616,12 @@ object YouTubeStreams {
}
}
+ /** Drops what is kept about [videoId], so its addresses are asked for again. */
+ fun forget(videoId: String) {
+ infos.remove(videoId)
+ watched.remove(videoId)
+ }
+
private const val INFO_TTL_MS = 60 * 60 * 1000L
@Volatile
diff --git a/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.kt b/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.kt
index 58a7530..30769d8 100644
--- a/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.kt
@@ -52,6 +52,12 @@ class CentreExtractor {
private val rightReal = FloatArray(WINDOW)
private val rightImaginary = FloatArray(WINDOW)
+ /** Each bin's gain as last applied, so the next frame moves from it. */
+ private val smoothed = FloatArray(BINS) { 1f }
+
+ /** This frame's gains before smoothing. */
+ private val wanted = FloatArray(BINS)
+
private val window = FloatArray(WINDOW) { i ->
// Hann, and at three-quarters overlap it sums to a constant, so the
// frames can be added back together without the level breathing.
@@ -68,6 +74,7 @@ class CentreExtractor {
pending = 0
ready = 0
rate = 0
+ smoothed.fill(1f)
}
/**
@@ -174,8 +181,30 @@ class CentreExtractor {
fft(leftReal, leftImaginary, false)
fft(rightReal, rightImaginary, false)
- val lowBin = (KEEP_BELOW_HZ * WINDOW / rate).toInt()
- val highBin = (KEEP_ABOVE_HZ * WINDOW / rate).toInt()
+ val binHz = rate.toFloat() / WINDOW
+ for (bin in 0 until BINS) {
+ val reach = band(bin * binHz)
+ wanted[bin] = if (reach <= 0f) {
+ 1f
+ } else {
+ 1f - reach * (1f - centreGain(leftReal[bin], leftImaginary[bin], rightReal[bin], rightImaginary[bin], amount))
+ }
+ }
+
+ // Smoothed across neighbouring bins and from one frame to the next.
+ //
+ // Taken raw, the mask jumped between keep and drop from cell to cell as
+ // the music moved, and those jumps are what was heard at the top of the
+ // slider: a thin, metallic warble over the band ("musical noise"), more
+ // than any voice left behind. Turning down is quicker than letting back
+ // up, so a syllable is caught at its start and its tail does not flutter.
+ for (bin in 0 until BINS) {
+ val below = wanted[if (bin > 0) bin - 1 else bin]
+ val above = wanted[if (bin < BINS - 1) bin + 1 else bin]
+ val target = 0.25f * below + 0.5f * wanted[bin] + 0.25f * above
+ val previous = smoothed[bin]
+ smoothed[bin] = previous + (target - previous) * if (target < previous) ATTACK else RELEASE
+ }
for (bin in 0 until BINS) {
val lr = leftReal[bin]
@@ -183,11 +212,7 @@ class CentreExtractor {
val rr = rightReal[bin]
val ri = rightImaginary[bin]
- val gain = if (bin < lowBin || bin > highBin) {
- 1f
- } else {
- centreGain(lr, li, rr, ri, amount)
- }
+ val gain = smoothed[bin]
leftReal[bin] = lr * gain
leftImaginary[bin] = li * gain
@@ -240,7 +265,22 @@ class CentreExtractor {
-(level * level) / (LEVEL_WIDTH * LEVEL_WIDTH) -
(phase * phase) / (PHASE_WIDTH * PHASE_WIDTH),
)
- return 1f - amount * centred
+ // Never all the way down. A cell taken to silence is the hole the
+ // warble is heard through; a voice twenty decibels under the music is
+ // already gone to anyone singing over it.
+ return 1f - amount * DEPTH * centred
+ }
+
+ /**
+ * How much of the slider reaches [hz]: none below the bass and above the
+ * air, all of it across the voice, and a slope between rather than an edge,
+ * which was a step in the spectrum heard as a filter.
+ */
+ private fun band(hz: Float): Float = when {
+ hz <= KEEP_BELOW_HZ || hz >= KEEP_ABOVE_HZ -> 0f
+ hz < VOICE_FROM_HZ -> (hz - KEEP_BELOW_HZ) / (VOICE_FROM_HZ - KEEP_BELOW_HZ)
+ hz > VOICE_TO_HZ -> (KEEP_ABOVE_HZ - hz) / (KEEP_ABOVE_HZ - VOICE_TO_HZ)
+ else -> 1f
}
/**
@@ -330,9 +370,20 @@ class CentreExtractor {
/** Below this the record keeps its bass and its kick, centred or not. */
const val KEEP_BELOW_HZ = 120f
+ /** Where the slider applies in full: the body of a singing voice. */
+ const val VOICE_FROM_HZ = 220f
+ const val VOICE_TO_HZ = 7_000f
+
/** And above this the air, where a voice has only its sibilance. */
const val KEEP_ABOVE_HZ = 12_000f
+ /** The most a centred cell is turned down: about twenty decibels. */
+ const val DEPTH = 0.9f
+
+ /** How far a bin's gain moves toward its target per frame, down and up. */
+ const val ATTACK = 0.6f
+ const val RELEASE = 0.25f
+
const val SCALE = 32768f
const val TINY = 1e-7f
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 232dcc9..069aa1d 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -2061,6 +2061,15 @@ fun SquareApp(
)
},
onEnqueue = onEnqueue,
+ // In order, whatever was left on. Shuffle
+ // is a mode, and the one a playlist's
+ // shuffle button turned on stayed on for the
+ // album played after it: its first song,
+ // and the rest in any order (#71).
+ onPlayInOrder = { tracks, asContext ->
+ player?.shuffleModeEnabled = false
+ onPlay(tracks, 0, page.uri, asContext, source, 0L)
+ },
onShuffle = { tracks ->
// Order does not matter: the player
// treats shuffle as a mode and stamps it
@@ -2921,12 +2930,15 @@ fun SquareApp(
// controls legible.
toneFloor = 0.08f,
scrimFloor = 0.22f,
- // Still while the sheet is travelling: under a
- // moving, scaling, fading layer the drift
- // cannot be seen, and it is the one moment in
- // the app where an always-invalidating
- // background would be competing for the frame.
- motion = playerSettled,
+ // Still, always. This layer is what every pane
+ // of glass on the player refracts, so a drift
+ // here redrew all of them on every frame,
+ // about a hundred render passes sixty times a
+ // second, for a movement the cover and its
+ // extension hide almost entirely. Measured: the
+ // player never stopped drawing, and most of
+ // its frames came in late.
+ motion = false,
columns = clipColumns,
)
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/components/FrameCopy.kt b/app/src/main/java/dev/lelonio/square/ui/components/FrameCopy.kt
new file mode 100644
index 0000000..3893bee
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/ui/components/FrameCopy.kt
@@ -0,0 +1,70 @@
+package dev.lelonio.square.ui.components
+
+import android.graphics.Bitmap
+import android.os.Handler
+import android.os.HandlerThread
+import android.view.PixelCopy
+import android.view.Surface
+import android.view.TextureView
+import kotlinx.coroutines.suspendCancellableCoroutine
+import kotlin.coroutines.resume
+
+/**
+ * A small copy of what a video's [TextureView] is showing, read without
+ * stopping the screen.
+ *
+ * `TextureView.getBitmap` is the obvious way, and the costly one: it runs on
+ * the main thread and waits there for the render thread and the GPU to finish
+ * whatever they are doing before it copies the layer. Measured on the player
+ * with a moving cover, that was 27 milliseconds a copy, ten copies a second, a
+ * frame lost every time, and more than a fifth of the main thread gone.
+ *
+ * This asks for the last frame the video queued instead, through PixelCopy,
+ * which answers on a thread of its own. Nothing on the main thread waits for
+ * it. The surface the video draws into is the same one the view shows, so the
+ * picture is the same; scaled to [width] by [height] as it is copied, as
+ * getBitmap scaled it.
+ *
+ * Null when there is nothing to copy yet, or the copy failed.
+ */
+internal suspend fun TextureView.copyFrame(width: Int, height: Int): Bitmap? {
+ val texture = surfaceTexture ?: return null
+ if (width <= 0 || height <= 0) return null
+ return suspendCancellableCoroutine { done ->
+ // Asked from the copy thread as well as answered there. Before Android
+ // 14 the request itself waits for the render thread to make the copy,
+ // and made from the main thread that wait was the main thread's again.
+ copies.post {
+ val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
+ // A second handle on the same queue, not a second queue: it is only
+ // read from, and releasing it leaves the video's own connection
+ // alone. Released once the copy is made, never while it is pending.
+ val surface = runCatching { Surface(texture) }.getOrNull()
+ if (surface == null) {
+ bitmap.recycle()
+ done.resume(null)
+ return@post
+ }
+ runCatching {
+ PixelCopy.request(surface, bitmap, { result ->
+ surface.release()
+ if (result == PixelCopy.SUCCESS) {
+ done.resume(bitmap)
+ } else {
+ bitmap.recycle()
+ done.resume(null)
+ }
+ }, copies)
+ }.onFailure {
+ surface.release()
+ bitmap.recycle()
+ done.resume(null)
+ }
+ }
+ }
+}
+
+/** Where the copies are answered: off the main thread, and one at a time. */
+private val copies: Handler by lazy {
+ Handler(HandlerThread("SquareFrameCopy").apply { start() }.looper)
+}
diff --git a/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt b/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
index da30197..7169c5f 100644
--- a/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/components/HeroBackdrop.kt
@@ -1,5 +1,7 @@
package dev.lelonio.square.ui.components
+import kotlinx.coroutines.flow.collectLatest
+import androidx.compose.runtime.LaunchedEffect
import androidx.compose.animation.Crossfade
import androidx.compose.animation.core.tween
import androidx.compose.runtime.getValue
@@ -141,6 +143,20 @@ fun HeroBackdrop(
// What the moving cover is showing now, for the blur and the extension;
// empty, and the still used, until it has shown something.
val motion = remember(motionUrl) { MotionFrames() }
+ // How far the newest copy has come in over the one before it, run again
+ // from nothing each time one arrives, for about as long as the next takes
+ // to come. Read only while drawing; see MotionFrames.previous.
+ val frameFade = remember(motion) { androidx.compose.animation.core.Animatable(1f) }
+ LaunchedEffect(motion) {
+ androidx.compose.runtime.snapshotFlow { motion.latest.value }.collectLatest { frame ->
+ if (frame == null) return@collectLatest
+ frameFade.snapTo(0f)
+ frameFade.animateTo(
+ 1f,
+ tween(MOTION_FRAME_FADE_MS, easing = androidx.compose.animation.core.LinearEasing),
+ )
+ }
+ }
// Every copy of the picture this draws, loaded together and swapped
// together. Each used to load on its own, and the small ones arrived
// first: on a change of song the extension and the blur were already the
@@ -168,6 +184,7 @@ fun HeroBackdrop(
imageAspect = imageAspect,
softenFrom = softenFrom,
softenTo = softenTo,
+ fade = { frameFade.value },
)
}
if (imageAspect != null && fadeToPage) {
@@ -288,10 +305,21 @@ fun HeroBackdrop(
layout(constraints.maxWidth, full) { placeable.place(0, top) }
}
.then(if (motionUrl != null) Modifier.smoothedUp() else Modifier)
+ // A layer of its own while the cover moves, which the
+ // blend below adds into; smoothedUp already is one where
+ // it applies.
+ .then(
+ if (motionUrl != null) {
+ Modifier.graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen }
+ } else {
+ Modifier
+ },
+ )
// Compose the bitmap and its alpha mask in one shader:
// no header-sized offscreen render target on each redraw.
.drawWithCache {
val frame = motion.latest.value
+ val before = motion.previous.value
val top = shift[0]
// Built for the whole height, as before, and drawn
// moved up by what this box starts below the top.
@@ -302,9 +330,23 @@ fun HeroBackdrop(
whole,
stops,
)
+ val earlier = before?.let { maskedPictureBrush(it.soft, shown.zoom, whole, stops) }
onDrawBehind {
translate(top = -top) {
- drawRect(brush = brush, topLeft = androidx.compose.ui.geometry.Offset(0f, top), size = size)
+ val at = androidx.compose.ui.geometry.Offset(0f, top)
+ if (earlier == null) {
+ drawRect(brush = brush, topLeft = at, size = size)
+ } else {
+ // The two copies mixed, not stacked: the one
+ // before at what is left of it, the newest
+ // added at its share. Same mask on both, so
+ // the sum keeps the mask's own opacity; laid
+ // one over the other, the fade went see-through
+ // halfway and the page showed in it.
+ val share = frameFade.value
+ drawRect(brush = earlier, topLeft = at, size = size, alpha = 1f - share)
+ drawRect(brush = brush, topLeft = at, size = size, alpha = share, blendMode = BlendMode.Plus)
+ }
}
}
},
@@ -525,6 +567,8 @@ private fun PictureExtension(
imageAspect: Float?,
softenFrom: Float,
softenTo: Float,
+ /** How far the newest frame has come in over the one before; see MotionFrames. */
+ fade: () -> Float,
) {
Crossfade(
targetState = art,
@@ -533,16 +577,83 @@ private fun PictureExtension(
modifier = Modifier.fillMaxSize(),
) { shown ->
val image = shown?.edge ?: return@Crossfade
- val imageZoom = shown.zoom
- val extension = remember { ExtensionPicture() }
// Whether the cover is moving, not each frame of it: read in
// composition, the frames themselves would recompose this every time.
val moving by remember(motion) { androidx.compose.runtime.derivedStateOf { motion.latest.value != null } }
+ // Two of it while the cover moves: the frame before underneath, and the
+ // newest coming in over it. Only the top one's opacity is animated,
+ // which costs a composite and not a redraw: each copy is blurred once,
+ // when it arrives, and the colours under the cover glide from one to
+ // the next instead of stepping every time a copy is read.
+ if (moving) {
+ ExtensionLayer(
+ image = image,
+ imageZoom = shown.zoom,
+ imageAspect = imageAspect,
+ softenFrom = softenFrom,
+ softenTo = softenTo,
+ moving = true,
+ frame = { (motion.previous.value ?: motion.latest.value)?.sharp },
+ opacity = { 1f },
+ fades = false,
+ )
+ }
+ ExtensionLayer(
+ image = image,
+ imageZoom = shown.zoom,
+ imageAspect = imageAspect,
+ softenFrom = softenFrom,
+ softenTo = softenTo,
+ moving = moving,
+ frame = { motion.latest.value?.sharp },
+ opacity = fade,
+ fades = moving,
+ )
+ }
+}
+
+/** One copy of [PictureExtension], from [frame] or the still [image]. */
+@Composable
+private fun ExtensionLayer(
+ image: ImageBitmap,
+ imageZoom: Float,
+ imageAspect: Float?,
+ softenFrom: Float,
+ softenTo: Float,
+ moving: Boolean,
+ frame: () -> ImageBitmap?,
+ opacity: () -> Float,
+ fades: Boolean,
+) {
+ val extension = remember { ExtensionPicture() }
+ // Laid out from just above where the extension starts, not from the
+ // top of the screen. The blur that smooths a moving cover's copies is
+ // paid by the pixel, and over the whole screen it was blurring the
+ // height of the cover too, where nothing of this is drawn, on every
+ // new frame of it.
+ val shift = remember { floatArrayOf(0f) }
Box(
Modifier
- .then(if (moving) Modifier.smoothedUp() else Modifier)
.fillMaxSize()
+ .layout { measurable, constraints ->
+ val full = constraints.maxHeight
+ val slot = if (imageAspect != null) constraints.maxWidth / imageAspect else full.toFloat()
+ val fadeFrom = if (imageAspect != null) 1f - SLOT_FADE else softenFrom
+ val top = (minOf(fadeFrom, 1f) * slot - SMOOTH_PX * 2).toInt().coerceIn(0, full)
+ shift[0] = top.toFloat()
+ val placeable = measurable.measure(
+ constraints.copy(minHeight = full - top, maxHeight = full - top),
+ )
+ layout(constraints.maxWidth, full) { placeable.place(0, top) }
+ }
+ .then(if (fades) Modifier.graphicsLayer { alpha = opacity() } else Modifier)
+ .then(if (moving) Modifier.smoothedUp() else Modifier)
.drawBehind {
+ val shifted = shift[0]
+ translate(top = -shifted) {
+ // The whole box as it would be from the top of the screen;
+ // everything below is worked out in those terms.
+ val size = androidx.compose.ui.geometry.Size(size.width, size.height + shifted)
// The slot, in this box's terms: across the top, as wide as
// the screen, at the picture's proportions.
val slotHeight =
@@ -566,7 +677,7 @@ private fun PictureExtension(
// drawn or the screen's own blurred background shows
// through the gap.
val top = (minOf(fadeFrom, 1f) * slotHeight).coerceIn(0f, size.height)
- if (top >= size.height) return@drawBehind
+ if (top >= size.height) return@translate
// How much of what is drawn is that carrying on: held at
// the picture's own colours, so the softening begins where
// the controls do and not where the picture stopped.
@@ -578,7 +689,7 @@ private fun PictureExtension(
// The slot crops the picture to fill it, centred; so does this.
// The moving cover's frame while it plays; see MotionFrames.
- val source = motion.latest.value?.sharp ?: image
+ val source = frame() ?: image
val w = source.width
val h = source.height
val slotRatio = if (slotHeight > 0f) size.width / slotHeight else 1f
@@ -594,9 +705,9 @@ private fun PictureExtension(
dstSize = IntSize(size.width.roundToInt(), (size.height - top).roundToInt()),
filterQuality = FilterQuality.Low,
)
+ }
},
)
- }
}
/**
@@ -606,9 +717,9 @@ private fun PictureExtension(
*
* Its top row is the cover's row, barely softened, since that is where the two
* meet. On the way down it is blurred across more and more, and drawn towards
- * the row's dominant colour: the one the eye takes the picture to be, weighted
- * to the vivid pixels, so a pink sky beside a white ramp comes out pink rather
- * than the grey their average would be. And it darkens on the way down by
+ * the colour the foot of the picture is mostly made of, see groundOf, so a
+ * black cover with light running down it ends in black and not in the grey its
+ * average would be. And it darkens on the way down by
* taking its own colours down, a little more saturated as they go, rather
* than by laying black over them, which turned everything under the controls
* the same grey.
@@ -661,7 +772,7 @@ private class ExtensionPicture {
rows++
}
for (i in sampled.indices) sampled[i] /= rows.coerceAtLeast(1)
- val dominant = dominantOf(sampled, width)
+ val dominant = groundOf(bitmap, left, width, top, bottom, row)
val blurred = FloatArray(width * 3)
val hsv = FloatArray(3)
@@ -703,32 +814,59 @@ private class ExtensionPicture {
}
/**
- * The colour a row reads as: its pixels averaged, each counted by how
- * vivid it is, so a few greys and whites do not wash out the hue that is
- * actually there. A row with no colour in it at all comes out as its plain
- * average, which is what it is.
+ * The colour the foot of the picture is made of: what the bottom of the
+ * screen settles on.
+ *
+ * It used to be the row where the fade begins, averaged with the vivid
+ * pixels counted most, and one row is a thin thing to judge a picture by.
+ * A black cover with shafts of light running down it was cut by that row
+ * across the shafts, and the screen under it came out grey, a colour
+ * that is nowhere in the picture. This reads the last stretch of the
+ * picture instead and takes the middle of it, channel by channel, each
+ * pixel counted a little more the more vivid it is: what covers most of the
+ * picture wins, black there, and a pink sky beside a white ramp still comes
+ * out pink.
*/
- private fun dominantOf(rgb: FloatArray, width: Int): FloatArray {
+ private fun groundOf(
+ bitmap: android.graphics.Bitmap,
+ left: Int,
+ width: Int,
+ top: Int,
+ bottom: Int,
+ row: Int,
+ ): FloatArray {
+ val from = maxOf(top, row - ((bottom - top) * GROUND_BAND).roundToInt())
+ val counts = Array(3) { FloatArray(256) }
+ val line = IntArray(width)
val hsv = FloatArray(3)
- var red = 0f
- var green = 0f
- var blue = 0f
var total = 0f
- for (x in 0 until width) {
- android.graphics.Color.RGBToHSV(
- rgb[x * 3].roundToInt(),
- rgb[x * 3 + 1].roundToInt(),
- rgb[x * 3 + 2].roundToInt(),
- hsv,
- )
- val vivid = hsv[1] * hsv[2]
- val weight = 0.05f + vivid * vivid
- red += rgb[x * 3] * weight
- green += rgb[x * 3 + 1] * weight
- blue += rgb[x * 3 + 2] * weight
- total += weight
+ for (r in from..row) {
+ if (r < top || r >= bottom) continue
+ bitmap.getPixels(line, 0, width, left, r, width, 1)
+ for (c in line) {
+ val red = (c shr 16) and 0xFF
+ val green = (c shr 8) and 0xFF
+ val blue = c and 0xFF
+ android.graphics.Color.RGBToHSV(red, green, blue, hsv)
+ val vivid = hsv[1] * hsv[2]
+ val weight = 1f + GROUND_VIVID * vivid * vivid
+ counts[0][red] += weight
+ counts[1][green] += weight
+ counts[2][blue] += weight
+ total += weight
+ }
+ }
+ if (total == 0f) return floatArrayOf(0f, 0f, 0f)
+ return FloatArray(3) { channel ->
+ var seen = 0f
+ var value = 0
+ while (value < 255) {
+ seen += counts[channel][value]
+ if (seen >= total / 2f) break
+ value++
+ }
+ value.toFloat()
}
- return floatArrayOf(red / total, green / total, blue / total)
}
/** A Gaussian across one row, the edges held rather than wrapped. */
@@ -784,6 +922,13 @@ private class MotionFrames {
val latest = mutableStateOf<Frame?>(null)
+ /**
+ * The copy before [latest], which the newest fades in over. A copy is read
+ * every few frames of the cover, and drawn one after the other the colours
+ * under it changed in steps.
+ */
+ val previous = mutableStateOf<Frame?>(null)
+
private var blend: FloatArray? = null
private var blendWidth = 0
private var blendHeight = 0
@@ -814,7 +959,13 @@ private class MotionFrames {
}
val smooth = android.graphics.Bitmap
.createBitmap(pixels, width, height, android.graphics.Bitmap.Config.ARGB_8888)
- latest.value = Frame(smooth.asImageBitmap(), softened(smooth).asImageBitmap())
+ val next = Frame(smooth.asImageBitmap(), softened(smooth).asImageBitmap())
+ // Both at once, so nothing is drawn with the new copy and the old
+ // "before" in between.
+ androidx.compose.runtime.snapshots.Snapshot.withMutableSnapshot {
+ previous.value = latest.value
+ latest.value = next
+ }
}
/**
@@ -893,10 +1044,18 @@ private const val FRAME_SOFTEN = 4
/**
* How far each copy of a frame moves [MotionFrames]' blend towards itself: at
- * thirty copies a second, a little each time, so the colours glide after the
- * cover a fraction of a second behind instead of stepping.
+ * about seven copies a second, close to half the way each time, so the colours
+ * glide after the cover a fraction of a second behind instead of stepping.
+ */
+private const val FRAME_FOLLOW = 0.45f
+
+/**
+ * How long a copy of a moving cover takes to come in over the one before:
+ * a little under the time between copies (see MotionCover's FRAME_EVERY_MS,
+ * plus the moment the copy itself takes), so one has finished arriving before
+ * the next starts.
*/
-private const val FRAME_FOLLOW = 0.25f
+private const val MOTION_FRAME_FADE_MS = 140
/** The same cropped picture and DstIn fade, without an intermediate surface. */
private fun maskedPictureBrush(
@@ -971,6 +1130,12 @@ private const val EXTENSION_ROWS = 24
private const val EXTENSION_NEAR = 0.05f
private const val EXTENSION_FAR = 0.40f
+/** The share of the picture, from its foot up, that [ExtensionPicture.groundOf] reads. */
+private const val GROUND_BAND = 0.35f
+
+/** How much more a fully vivid pixel counts there than a grey one. */
+private const val GROUND_VIVID = 3f
+
/** How much of the way to the dominant colour the bottom of the screen goes. */
private const val EXTENSION_CONVERGE = 0.85f
diff --git a/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt b/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt
index 73dc11c..e0e0948 100644
--- a/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/components/MotionCover.kt
@@ -135,7 +135,15 @@ fun MotionCover(
) {
val height = (FRAME_PX.toLong() * view.height / view.width).toInt()
.coerceAtLeast(1)
- view.getBitmap(FRAME_PX, height)?.let { latest(it) }
+ // Copied and worked on away from the main thread; see
+ // copyFrame. Both used to run on it, a copy every
+ // frame or two, and the player dropped more than half
+ // of its frames while a cover moved.
+ view.copyFrame(FRAME_PX, height)?.let { frame ->
+ kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) {
+ latest(frame)
+ }
+ }
}
delay(FRAME_EVERY_MS)
}
@@ -190,6 +198,12 @@ private const val APPEAR_MS = 450
* trailed the picture and stepped from one to the next. The cover only moves
* on the player's own screen (a panel stills it), so this is the one place it
* costs anything.
+ *
+ * Less often since then, about seven times a second. Every copy is a read of
+ * the video on the render thread, and redraws the blurred band below the
+ * cover; at fifteen a second those two were most of what the player's frames
+ * spent. The blend that follows the copies takes bigger steps to keep pace;
+ * see FRAME_FOLLOW.
*/
private const val FRAME_PX = 96
-private const val FRAME_EVERY_MS = 66L
+private const val FRAME_EVERY_MS = 150L
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.kt
index d7c6d5c..94cbdfb 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
@@ -195,6 +195,12 @@ fun PlaylistScreen(
onPlay: (List<CatalogTrack>, Int, Boolean) -> Unit,
onEnqueue: (CatalogTrack) -> Unit,
onShuffle: (List<CatalogTrack>) -> Unit,
+ /**
+ * The page's own play button: the list from the top, in its order, as
+ * [onShuffle] is the same list in none. Separate from [onPlay] because a
+ * row tapped keeps whatever shuffle the listener has on.
+ */
+ onPlayInOrder: (List<CatalogTrack>, Boolean) -> Unit,
/** Opens the app-wide "add to playlist" sheet for one track. */
onAddToPlaylist: (CatalogTrack) -> Unit,
/**
@@ -664,7 +670,7 @@ fun PlaylistScreen(
topPadding = topPadding,
searching = searching,
onBack = onBack,
- onPlay = { visible.takeIf { it.isNotEmpty() }?.let { onPlay(it, 0, asContext) } },
+ onPlay = { visible.takeIf { it.isNotEmpty() }?.let { onPlayInOrder(it, asContext) } },
onShuffle = { visible.takeIf { it.isNotEmpty() }?.let(onShuffle) },
onToggleSearch = {
searching = !searching
@@ -711,7 +717,7 @@ fun PlaylistScreen(
topPadding = topPadding,
onBack = onBack,
onPlay = {
- visible.takeIf { it.isNotEmpty() }?.let { onPlay(it, 0, asContext) }
+ visible.takeIf { it.isNotEmpty() }?.let { onPlayInOrder(it, asContext) }
},
onShuffle = { visible.takeIf { it.isNotEmpty() }?.let(onShuffle) },
onToggleSearch = {
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/CanvasSurface.kt b/app/src/main/java/dev/lelonio/square/ui/player/CanvasSurface.kt
index f077bf1..6e120e0 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/CanvasSurface.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/CanvasSurface.kt
@@ -1,5 +1,6 @@
package dev.lelonio.square.ui.player
+import dev.lelonio.square.ui.components.copyFrame
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
@@ -111,10 +112,13 @@ fun CanvasSurface(
kotlinx.coroutines.delay(TONE_INTERVAL_MS)
continue
}
- val frame = runCatching { view.getBitmap(TONE_SAMPLE, TONE_SAMPLE) }.getOrNull()
+ // Off the main thread, copy and sums alike; see copyFrame.
+ val frame = view.copyFrame(TONE_SAMPLE, TONE_SAMPLE)
if (frame != null) {
- tone.value(frame.footColumns())
- frame.recycle()
+ val columns = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) {
+ frame.footColumns().also { frame.recycle() }
+ }
+ tone.value(columns)
}
kotlinx.coroutines.delay(TONE_INTERVAL_MS)
}
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.kt
index d883b52..16d4282 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
@@ -1,5 +1,6 @@
package dev.lelonio.square.ui.player
+import dev.lelonio.square.ui.components.copyFrame
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.togetherWith
@@ -539,7 +540,7 @@ fun PlayerScreen(
// there it keeps refracting it.
var clipBottom by remember { mutableFloatStateOf(Float.MAX_VALUE) }
var lowerTop by remember { mutableFloatStateOf(0f) }
- val lowerBackdrop = if (lowerTop >= clipBottom) groundBackdrop else glassBackdrop
+ val lowerBackdropAlone = if (lowerTop >= clipBottom) groundBackdrop else glassBackdrop
// The panes over a moving Apple cover read the page's own blurred ground
// instead of the cover. The cover is in the layer they refract, and every
@@ -548,6 +549,12 @@ fun PlayerScreen(
// it was costing the player half its frames.
val coverMoving = coverMotionUrl != null && canvas == null
val upperBackdrop = if (coverMoving) groundBackdrop else glassBackdrop
+ // The panes below it too. The cover's picture carries on under it to the
+ // foot of the screen, moving with it, so the layer changed under the title,
+ // the transport and the tabs with every frame as well, and each of them
+ // redid its blur and its lens: a trace of the player over a moving cover
+ // counted close to a hundred render passes a frame.
+ val lowerBackdrop = if (coverMoving) groundBackdrop else lowerBackdropAlone
// Lyrics take over the middle of the screen rather than opening a panel at
// the bottom, and the Canvas goes out of focus behind them: a clip is
@@ -590,6 +597,16 @@ fun PlayerScreen(
kotlinx.coroutines.delay(LYRICS_ALONE_AFTER_MS)
lyricsAlone = true
}
+ // The screen stays on while the words are being sung along to. Left to
+ // the phone's own timeout it dimmed and locked halfway through a verse,
+ // which on a page meant to be read from across the room is the page going
+ // away. Only while it plays: paused, the phone sleeps as it would anyway.
+ val hostView = androidx.compose.ui.platform.LocalView.current
+ val keepAwake = lyricsReading && state.wantsPlay
+ DisposableEffect(hostView, keepAwake) {
+ if (keepAwake) hostView.keepScreenOn = true
+ onDispose { if (keepAwake) hostView.keepScreenOn = false }
+ }
val chrome by animateFloatAsState(
targetValue = if (lyricsAlone) 0f else 1f,
animationSpec = tween(if (lyricsAlone) LYRICS_ALONE_LEAVE_MS else LYRICS_ALONE_RETURN_MS),
@@ -1829,11 +1846,12 @@ private fun VideoStage(
// light in a room does not flicker.
kotlinx.coroutines.delay(AMBIENT_INTERVAL_MS)
if (!texture.isAvailable) continue
- val frame = runCatching {
- texture.getBitmap(AMBIENT_SAMPLE, AMBIENT_SAMPLE)
- }.getOrNull() ?: continue
- report(frame.edges())
- frame.recycle()
+ // Off the main thread, copy and sums alike; see copyFrame.
+ val frame = texture.copyFrame(AMBIENT_SAMPLE, AMBIENT_SAMPLE) ?: continue
+ val edges = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) {
+ frame.edges().also { frame.recycle() }
+ }
+ report(edges)
}
}
diff --git a/app/src/main/java/dev/lelonio/square/ui/theme/ArtworkColor.kt b/app/src/main/java/dev/lelonio/square/ui/theme/ArtworkColor.kt
index 41814f9..145620f 100644
--- a/app/src/main/java/dev/lelonio/square/ui/theme/ArtworkColor.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/theme/ArtworkColor.kt
@@ -94,7 +94,15 @@ fun lightPage(): Boolean = LocalLightTheme.current
* under dark. The same gradient with this colour in it works both ways round.
*/
@Composable
-fun scrimColor(): Color = if (lightPage()) Color.White else Color.Black
+fun scrimColor(): Color {
+ // The ink a page has set for itself before the theme's. The player's is
+ // light whatever the phone's theme, and read from the theme its scrim was
+ // white on a light phone: a black cover ended in a grey wash under the
+ // controls, a third of the way to white.
+ val ink = LocalInkOverride.current
+ val lightPaper = if (ink != null) ink.luminance() < 0.5f else lightPage()
+ return if (lightPaper) Color.White else Color.Black
+}
/** The colour every page's own colour is mixed towards. */
@Composable