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
Turn the singing down to sing along
Leloniocb7966319c2d
diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt b/app/src/main/java/dev/lelonio/square/playback/AudioEffects.ktindex fcc8a94..1f3a5c6 100644--- a/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt+++ b/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt@@ -51,6 +51,28 @@ object AudioEffects {private val _pitch = MutableStateFlow(1f)val pitch: StateFlow<Float> = _pitch.asStateFlow()+ private val _karaoke = MutableStateFlow(0f)++ /**+ * How much of the singing to take out, 0 (as recorded) to 1 (as far as this+ * can go).+ *+ * Not what Apple does. Their karaoke is separate recordings — the label's+ * own stems, one per instrument — and there is no such thing to ask Spotify+ * for. What this does is the oldest trick in the book and the only one+ * available to a player holding a finished stereo mix: a voice is almost+ * always mixed dead centre, so taking away what the two channels have in+ * common takes the voice with it. See AudioOutput.+ */+ val karaoke: StateFlow<Float> = _karaoke.asStateFlow()++ fun setKaraoke(amount: Float) {+ val wanted = amount.coerceIn(0f, 1f)+ if (wanted == _karaoke.value) return+ _karaoke.value = wanted+ prefs?.edit()?.putFloat(KEY_KARAOKE, wanted)?.commit()+ }+fun load(context: Context) {if (prefs != null) returnval store = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)@@ -58,6 +80,7 @@ object AudioEffects {_reverb.value = store.getFloat(KEY_REVERB, 0f)_speed.value = store.getFloat(KEY_SPEED, 1f)_pitch.value = store.getFloat(KEY_PITCH, 1f)+ _karaoke.value = store.getFloat(KEY_KARAOKE, 0f)android.util.Log.i("SpotAudio","effects restored: reverb=${_reverb.value} speed=${_speed.value} pitch=${_pitch.value}",@@ -101,4 +124,5 @@ object AudioEffects {private const val KEY_REVERB = "reverb"private const val KEY_SPEED = "speed"private const val KEY_PITCH = "pitch"+ private const val KEY_KARAOKE = "karaoke"}diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.ktindex 9f9eaa1..3a6bfe5 100644--- a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt+++ b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt@@ -503,6 +503,14 @@ class AudioOutput {val currentSpeed = speedval currentPitch = pitch+ // Before anything else touches the audio: this reads where each sound+ // sits between the two channels, and a stretcher that has resynthesised+ // them no longer has the same two channels. See CentreExtractor.+ val karaoke = AudioEffects.karaoke.value+ if (karaoke > 0f) {+ vocals.apply(data, sizeInBytes, channels, sampleRate, karaoke)+ }+// The platform does it below this app when the light path is chosen, so// the packets go out untouched and the rate is set on the track itself.if (lightEffects) {@@ -553,6 +561,9 @@ class AudioOutput {}.onFailure { android.util.Log.w(TAG, "the platform refused $wantedSpeed x: ${it.message}") }}+ /** Takes the middle of the stage away; see [CentreExtractor]. */+ private val vocals = CentreExtractor()+/** What the track was last told, so it is not told again every packet. */private var platformSpeed = 1fprivate var platformPitch = 1fdiff --git a/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.kt b/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.ktindex 130dcff..25851a8 100644--- a/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.kt+++ b/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.kt@@ -146,6 +146,50 @@ class BungeeAudioProcessor : BaseAudioProcessor() {* from whoever is doing the stretching, or the position on screen runs at a* different rate from the sound.*/+/**+ * The same vocal removal, for the players that are not the Spotify engine.+ *+ * In the chain before the stretcher, because the trick rests on what the two+ * channels have in common and a resynthesised pair no longer has the same two.+ * See [CentreExtractor].+ */+@OptIn(UnstableApi::class)+class VocalAudioProcessor : BaseAudioProcessor() {++ private val vocals = CentreExtractor()++ override fun onConfigure(+ inputAudioFormat: AudioProcessor.AudioFormat,+ ): AudioProcessor.AudioFormat {+ if (inputAudioFormat.encoding != C.ENCODING_PCM_16BIT) {+ throw AudioProcessor.UnhandledAudioFormatException(inputAudioFormat)+ }+ return inputAudioFormat+ }++ override fun queueInput(inputBuffer: ByteBuffer) {+ val size = inputBuffer.remaining()+ if (size == 0) return++ val amount = AudioEffects.karaoke.value+ val out = replaceOutputBuffer(size)+ val start = out.position()+ out.put(inputBuffer)+ out.position(start)+ if (amount > 0f) {+ vocals.apply(+ out,+ size,+ inputAudioFormat.channelCount,+ inputAudioFormat.sampleRate,+ amount,+ )+ }+ out.position(start)+ out.limit(start + size)+ }+}+@OptIn(UnstableApi::class)class BungeeProcessorChain(private val bungee: BungeeAudioProcessor = BungeeAudioProcessor(),@@ -153,7 +197,9 @@ class BungeeProcessorChain(private var parameters = PlaybackParameters.DEFAULT- override fun getAudioProcessors(): Array<AudioProcessor> = arrayOf(bungee)+ private val vocals = VocalAudioProcessor()++ override fun getAudioProcessors(): Array<AudioProcessor> = arrayOf(vocals, bungee)override fun applyPlaybackParameters(playbackParameters: PlaybackParameters,diff --git a/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.kt b/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.ktnew file mode 100644index 0000000..58a7530--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/playback/CentreExtractor.kt@@ -0,0 +1,342 @@+package dev.lelonio.square.playback++import kotlin.math.PI+import kotlin.math.abs+import kotlin.math.atan2+import kotlin.math.cos+import kotlin.math.exp+import kotlin.math.hypot+import kotlin.math.sin++/**+ * Takes the singing out of a finished stereo mix, one frequency at a time.+ *+ * The idea is the one DUET is built on: in a two-channel recording, each source+ * has its own place on the stage, and that place shows up as a pair of numbers+ * between the channels — how much louder it is on one side, and how much later+ * it arrives there. Cut the sound into short overlapping frames and look at+ * every frequency of every frame separately, and each of those little cells+ * belongs almost entirely to whichever source is loudest there. A lead voice+ * sits dead centre: equal in both channels, no delay. So the cells that look+ * centred can be turned down while everything to the sides is left alone.+ *+ * The earlier attempt here subtracted one channel from the other across the+ * whole signal at once, which is the same idea with no ears: everything centred+ * went, everything else stayed, and a record is not built in those two boxes.+ * Working per cell is what makes the difference audible — a guitar panned three+ * degrees off centre survives, cymbals survive, and so does most of what the+ * voice's reverb throws to the sides.+ *+ * References: Rickard, "The DUET blind source separation algorithm" (2007);+ * Yilmaz & Rickard, "Blind separation of speech mixtures via time-frequency+ * masking" (2004). The masking here is soft rather than binary, because a+ * listener asked for a slider rather than a switch.+ *+ * What it is not: the separation Apple ships, which is not separation at all —+ * those are the label's own multitrack stems. Nothing a player can compute from+ * a finished mix comes close to a recording that never had the voice in it.+ */+class CentreExtractor {++ private var rate = 0+ private var input = FloatArray(0)+ private var overlap = FloatArray(0)+ private var done = FloatArray(0)+ private var pending = 0+ private var ready = 0++ private val real = FloatArray(WINDOW)+ private val imaginary = FloatArray(WINDOW)+ private val leftReal = FloatArray(WINDOW)+ private val leftImaginary = FloatArray(WINDOW)+ private val rightReal = FloatArray(WINDOW)+ private val rightImaginary = FloatArray(WINDOW)++ 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.+ (0.5f - 0.5f * cos(2.0 * PI * i / WINDOW).toFloat())+ }++ /** Delay this adds, in frames: one window, since a frame is only whole once. */+ val latencyFrames: Int get() = WINDOW++ fun reset() {+ input = FloatArray(0)+ overlap = FloatArray(0)+ done = FloatArray(0)+ pending = 0+ ready = 0+ rate = 0+ }++ /**+ * The same, for audio arriving as bytes.+ *+ * Interleaved 16-bit stereo only: there is nothing to compare in one+ * channel, and no other depth reaches either of this app's outputs.+ */+ fun apply(+ data: java.nio.ByteBuffer,+ sizeInBytes: Int,+ channels: Int,+ sampleRate: Int,+ amount: Float,+ ) {+ if (amount <= 0f || channels != 2 || sizeInBytes < 4) return+ val frames = sizeInBytes / 4+ if (scratch.size < frames * 2) scratch = ShortArray(frames * 2)++ val at = data.position()+ val shorts = data.order(java.nio.ByteOrder.LITTLE_ENDIAN).asShortBuffer()+ shorts.get(scratch, 0, frames * 2)+ process(scratch, frames, sampleRate, amount)+ shorts.position(0)+ shorts.put(scratch, 0, frames * 2)+ data.position(at)+ }++ private var scratch = ShortArray(0)++ /**+ * Rewrites [pcm] in place: interleaved stereo, 16-bit, [frames] frames.+ *+ * The audio comes back a window late — a frame cannot be judged until it is+ * whole — so the first ninety milliseconds after this is switched on are+ * the tail of what came before it. Nothing else in the app is delayed with+ * it, which is why the position on screen is left alone: ninety+ * milliseconds is under the threshold where a lyric line looks early.+ */+ fun process(pcm: ShortArray, frames: Int, sampleRate: Int, amount: Float) {+ if (frames <= 0) return+ val samples = frames * 2+ if (sampleRate != rate) {+ rate = sampleRate+ pending = 0+ ready = 0+ java.util.Arrays.fill(overlap, 0f)+ }++ grow(samples)++ // Everything arriving joins what was left over from last time.+ for (i in 0 until samples) input[pending + i] = pcm[i] / SCALE+ pending += samples++ // Every whole frame that can be built is built.+ var consumed = 0+ while (pending - consumed >= WINDOW * 2) {+ transformFrame(consumed, amount)+ consumed += HOP * 2+ }++ // What the overlap has finished with — everything before the last frame+ // start — moves to the queue waiting to be played.+ if (consumed > 0) {+ grow(consumed)+ System.arraycopy(overlap, 0, done, ready, consumed)+ ready += consumed+ System.arraycopy(overlap, consumed, overlap, 0, overlap.size - consumed)+ java.util.Arrays.fill(overlap, overlap.size - consumed, overlap.size, 0f)+ System.arraycopy(input, consumed, input, 0, pending - consumed)+ pending -= consumed+ }++ // And exactly as much as came in goes out — a window later than it+ // arrived, which is the price of judging a frame only once it is whole.+ if (ready >= samples) {+ for (i in 0 until samples) pcm[i] = clamp(done[i])+ System.arraycopy(done, samples, done, 0, ready - samples)+ ready -= samples+ } else {+ // Only while the pipeline fills, at the very start.+ java.util.Arrays.fill(pcm, 0, samples, 0)+ }+ }++ /** Keeps the three working buffers big enough for what is being asked. */+ private fun grow(extra: Int) {+ val wanted = pending + extra + WINDOW * 4+ if (input.size < wanted) input = input.copyOf(wanted)+ if (overlap.size < wanted) overlap = overlap.copyOf(wanted)+ if (done.size < ready + extra + WINDOW * 4) done = done.copyOf(ready + extra + WINDOW * 4)+ }++ /** One frame: window it, look at every bin, put it back. */+ private fun transformFrame(at: Int, amount: Float) {+ for (i in 0 until WINDOW) {+ val w = window[i]+ leftReal[i] = input[at + i * 2] * w+ leftImaginary[i] = 0f+ rightReal[i] = input[at + i * 2 + 1] * w+ rightImaginary[i] = 0f+ }+ fft(leftReal, leftImaginary, false)+ fft(rightReal, rightImaginary, false)++ val lowBin = (KEEP_BELOW_HZ * WINDOW / rate).toInt()+ val highBin = (KEEP_ABOVE_HZ * WINDOW / rate).toInt()++ for (bin in 0 until BINS) {+ val lr = leftReal[bin]+ val li = leftImaginary[bin]+ val rr = rightReal[bin]+ val ri = rightImaginary[bin]++ val gain = if (bin < lowBin || bin > highBin) {+ 1f+ } else {+ centreGain(lr, li, rr, ri, amount)+ }++ leftReal[bin] = lr * gain+ leftImaginary[bin] = li * gain+ rightReal[bin] = rr * gain+ rightImaginary[bin] = ri * gain++ // The upper half of the spectrum is the mirror of the lower one.+ if (bin in 1 until BINS - 1) {+ val mirror = WINDOW - bin+ leftReal[mirror] = leftReal[bin]+ leftImaginary[mirror] = -leftImaginary[bin]+ rightReal[mirror] = rightReal[bin]+ rightImaginary[mirror] = -rightImaginary[bin]+ }+ }++ fft(leftReal, leftImaginary, true)+ fft(rightReal, rightImaginary, true)++ for (i in 0 until WINDOW) {+ val w = window[i] * NORMALISE+ overlap[at + i * 2] += leftReal[i] * w+ overlap[at + i * 2 + 1] += rightReal[i] * w+ }+ }++ /**+ * How much of this cell to keep, given where it sits on the stage.+ *+ * Two measurements, both DUET's: how far apart the two channels are in+ * level, and how far apart in phase. A cell in the middle scores near zero+ * on both, and the further it is from there the less of the slider applies+ * to it.+ */+ private fun centreGain(lr: Float, li: Float, rr: Float, ri: Float, amount: Float): Float {+ val leftSize = hypot(lr, li)+ val rightSize = hypot(rr, ri)+ if (leftSize < TINY && rightSize < TINY) return 1f++ // Symmetric, so a voice pushed slightly left scores the same as one+ // pushed slightly right.+ val level = abs(leftSize - rightSize) / (leftSize + rightSize)++ // The angle between the two channels at this frequency.+ val crossReal = lr * rr + li * ri+ val crossImaginary = li * rr - lr * ri+ val phase = abs(atan2(crossImaginary, crossReal))++ val centred = exp(+ -(level * level) / (LEVEL_WIDTH * LEVEL_WIDTH) -+ (phase * phase) / (PHASE_WIDTH * PHASE_WIDTH),+ )+ return 1f - amount * centred+ }++ /**+ * Radix-2, in place, iterative.+ *+ * Written out rather than pulled in: this runs on the audio thread a+ * hundred and seventy times a second and the app has no other use for a+ * transform, so a dependency would be all cost.+ */+ private fun fft(re: FloatArray, im: FloatArray, inverse: Boolean) {+ var j = 0+ for (i in 1 until WINDOW) {+ var bit = WINDOW shr 1+ while (j and bit != 0) {+ j = j xor bit+ bit = bit shr 1+ }+ j = j or bit+ if (i < j) {+ var t = re[i]; re[i] = re[j]; re[j] = t+ t = im[i]; im[i] = im[j]; im[j] = t+ }+ }++ var length = 2+ while (length <= WINDOW) {+ val angle = (if (inverse) 2.0 * PI else -2.0 * PI) / length+ val stepReal = cos(angle).toFloat()+ val stepImaginary = sin(angle).toFloat()+ var i = 0+ while (i < WINDOW) {+ var wr = 1f+ var wi = 0f+ for (k in 0 until length / 2) {+ val ar = re[i + k]+ val ai = im[i + k]+ val br = re[i + k + length / 2] * wr - im[i + k + length / 2] * wi+ val bi = re[i + k + length / 2] * wi + im[i + k + length / 2] * wr+ re[i + k] = ar + br+ im[i + k] = ai + bi+ re[i + k + length / 2] = ar - br+ im[i + k + length / 2] = ai - bi+ val nextR = wr * stepReal - wi * stepImaginary+ wi = wr * stepImaginary + wi * stepReal+ wr = nextR+ }+ i += length+ }+ length = length shl 1+ }++ if (inverse) {+ for (i in 0 until WINDOW) {+ re[i] /= WINDOW+ im[i] /= WINDOW+ }+ }+ }++ private fun clamp(value: Float): Short {+ val scaled = value * SCALE+ return when {+ scaled > Short.MAX_VALUE -> Short.MAX_VALUE+ scaled < Short.MIN_VALUE -> Short.MIN_VALUE+ else -> scaled.toInt().toShort()+ }+ }++ private companion object {+ /** ~93 ms at 44.1 kHz: long enough to tell a voice from a snare. */+ const val WINDOW = 4096+ const val HOP = WINDOW / 4+ const val BINS = WINDOW / 2 + 1++ /**+ * How near the middle a cell has to be before it counts as the voice.+ *+ * In the two numbers DUET works in: how far the level may differ+ * between the channels, and how far the phase may. Both are generous —+ * a voice is never mathematically centred once a room has been added to+ * it — and the mask falls away smoothly rather than at an edge, which+ * is what keeps this from sounding like a gate.+ */+ const val LEVEL_WIDTH = 0.28f+ const val PHASE_WIDTH = 0.55f++ /** Below this the record keeps its bass and its kick, centred or not. */+ const val KEEP_BELOW_HZ = 120f++ /** And above this the air, where a voice has only its sibilance. */+ const val KEEP_ABOVE_HZ = 12_000f++ const val SCALE = 32768f+ const val TINY = 1e-7f++ /** Hann at three-quarters overlap sums to 1.5; this takes that back out. */+ const val NORMALISE = 2f / 3f+ }+}diff --git a/app/src/main/java/dev/lelonio/square/ui/player/KaraokeDial.kt b/app/src/main/java/dev/lelonio/square/ui/player/KaraokeDial.ktnew file mode 100644index 0000000..101b0be--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/ui/player/KaraokeDial.kt@@ -0,0 +1,281 @@+package dev.lelonio.square.ui.player++import androidx.compose.animation.core.animateDpAsState+import androidx.compose.animation.core.animateFloatAsState+import androidx.compose.animation.core.tween+import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.width+import androidx.compose.material3.Icon+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+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.graphicsLayer+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.platform.LocalDensity+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.MicrophoneStage+import com.adamglin.phosphoricons.regular.MicrophoneStage+import dev.lelonio.square.R+import dev.lelonio.square.ui.glass.LocalGlassEffectConfig+import dev.lelonio.square.ui.glass.backdrop.Backdrop+import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.awaitFirstDown+import androidx.compose.ui.input.pointer.positionChange+import dev.lelonio.square.ui.glass.liquidGlass+import dev.lelonio.square.ui.glass.shapes.ContinuousCapsule++/**+ * How much of the singing to take away.+ *+ * Closed it is a button with a microphone on it, the width of a thumb; touched,+ * it grows into a standing capsule filled from the bottom, which is dragged to+ * set the level and closes again when it is let go of. That is the shape Apple+ * gives the same idea, and the reason for it is the same: a lyric sheet is what+ * the panel is for, and a control that is used once a song should not hold a+ * band of it open the rest of the time.+ */+@Composable+fun KaraokeDial(+ amount: Float,+ onChange: (Float) -> Unit,+ backdrop: Backdrop,+ modifier: Modifier = Modifier,+ /**+ * Bumped from outside to open it, for the times somebody arrives here on+ * purpose — tapping the badge over the cover is asking for this control,+ * and finding it closed would be arriving at the wrong place.+ */+ expandSignal: Int = 0,+) {+ var open by remember { mutableStateOf(false) }+ var dragging by remember { mutableStateOf(false) }+ var pressed by remember { mutableStateOf(false) }+ val change by rememberUpdatedState(onChange)+ // Read inside the gesture rather than captured by it.+ //+ // The handler is built once and lives for the length of the touch, so a+ // plain reference to the parameter is the value as it was when the finger+ // landed: every move then measured itself from the same starting point,+ // which is why dragging moved the level once and then refused to climb.+ val latest = rememberUpdatedState(amount)+ // Read and written from inside the gesture, where composition state cannot+ // be trusted to have caught up yet.+ val openNow = remember { androidx.compose.runtime.mutableStateOf(false) }+ val density = LocalDensity.current++ // Closes itself after a moment's stillness, so nobody has to put it away.+ var lastTouch by remember { mutableStateOf(0L) }++ LaunchedEffect(expandSignal) {+ if (expandSignal > 0) {+ open = true+ openNow.value = true+ lastTouch = System.currentTimeMillis()+ }+ }++ LaunchedEffect(open, lastTouch) {+ if (!open) return@LaunchedEffect+ kotlinx.coroutines.delay(IDLE_MS)+ open = false+ }++ val height by animateDpAsState(+ targetValue = if (open) OPEN_HEIGHT else CLOSED_SIZE,+ animationSpec = tween(220),+ label = "karaokeHeight",+ )+ // Grows under the finger while it is being dragged, and gives a little+ // when it is merely pressed — which is what every other control in the+ // player does, and the only thing that says a tap landed.+ val grip by animateFloatAsState(+ targetValue = when {+ dragging -> 1.08f+ pressed -> 0.94f+ else -> 1f+ },+ animationSpec = tween(140),+ label = "karaokeGrip",+ )+ val lit by animateFloatAsState(+ targetValue = if (pressed || dragging) 0.12f else 0f,+ animationSpec = tween(140),+ label = "karaokePress",+ )+ val fill by animateFloatAsState(+ targetValue = if (open) amount else 0f,+ animationSpec = tween(160),+ label = "karaokeFill",+ )+ // A little longer than the control is tall.+ //+ // Mapped one to one the range was hard to aim; at twice the travel a short+ // slow drag moved the level by a percent or two and read as nothing+ // happening at all. This is between the two, and what keeps the bottom from+ // being a cliff is the step to silence below, not the sensitivity.+ val travelPx = with(density) { OPEN_HEIGHT.toPx() } * 1.25f++ Box(+ modifier+ .width(CLOSED_SIZE)+ .height(height)+ .graphicsLayer {+ scaleX = grip+ scaleY = grip+ }+ .clip(ContinuousCapsule())+ .liquidGlass(+ // The player's own material, not a lighter one: this sits in a+ // row of glass controls and reading as a different substance is+ // exactly what made it look bolted on.+ config = LocalGlassEffectConfig.current,+ shape = ContinuousCapsule(),+ ownBackdrop = backdrop,+ highlightAlpha = BarHighlightAlpha,+ backdropScale = 0.4f,+ )+ // The touch is claimed the moment it lands, before anything else+ // can decide what it was.+ //+ // A drag detector waits for the finger to travel far enough to be+ // sure it is a drag, and the lyrics behind this are a scrolling+ // list that is just as sure it was a scroll: whoever asks second+ // loses. Taking the pointer at the first touch and consuming every+ // move keeps the list out of it, and a touch that never moves is+ // read as a tap on the way up.+ // Keyed on nothing, deliberately.+ //+ // Keyed on `open`, the handler was rebuilt the instant a drag+ // opened the capsule — and rebuilding it cancels the gesture in+ // progress, so the movement that opened it was the movement that+ // got thrown away. The state is read through a holder instead, and+ // the touch lives from the finger landing to the finger leaving.+ .pointerInput(Unit) {+ awaitEachGesture {+ // Taken in the first pass, before anybody upstream sees it.+ //+ // Two ancestors want vertical drags: the lyrics list, and+ // the swipe that closes the player. Both are detectors that+ // wait for the finger to travel, and both sit above this in+ // the tree — so a drag starting on this button was usually+ // theirs by the time it counted as a drag here. Claiming the+ // pointer in the initial pass and consuming every move+ // leaves them nothing to act on.+ val first = awaitFirstDown(+ requireUnconsumed = false,+ pass = androidx.compose.ui.input.pointer.PointerEventPass.Initial,+ )+ first.consume()+ pressed = true+ lastTouch = System.currentTimeMillis()+ var moved = 0f+ var level = latest.value+ openNow.value = open++ while (true) {+ val event = awaitPointerEvent(+ androidx.compose.ui.input.pointer.PointerEventPass.Initial,+ )+ val pointer = event.changes.firstOrNull { it.id == first.id } ?: break+ if (!pointer.pressed) break+ val step = pointer.positionChange().y+ // Consumed whether or not it moved: a still finger held+ // on this control is still this control's.+ pointer.consume()+ if (step != 0f) {+ moved += kotlin.math.abs(step)+ // A finger that starts moving on the closed button+ // is setting the level, not looking for a tap: it+ // opens under the finger and carries on from where+ // the level already is.+ if (!openNow.value && moved >= viewConfiguration.touchSlop) {+ openNow.value = true+ open = true+ level = latest.value+ }+ if (openNow.value) {+ dragging = true+ // Up is more, which is how a level reads. Kept+ // here as it is dragged, so the finger and the+ // fill move together.+ level = (level - step / travelPx).coerceIn(0f, 1f)+ // Off is a place you have to mean: without a+ // step down to it, easing the level towards+ // quiet turned the effect off altogether.+ if (level < OFF_BELOW) level = 0f+ change(level)+ lastTouch = System.currentTimeMillis()+ }+ }+ }++ dragging = false+ pressed = false+ // A touch that went nowhere opens it, or puts it away.+ if (moved < viewConfiguration.touchSlop) {+ open = !open+ openNow.value = open+ lastTouch = System.currentTimeMillis()+ }+ }+ },+ contentAlignment = Alignment.BottomCenter,+ ) {+ if (lit > 0f) {+ Box(+ Modifier+ .fillMaxWidth()+ .height(height)+ .background(Color.White.copy(alpha = lit)),+ )+ }++ if (fill > 0f) {+ Box(+ Modifier+ .fillMaxWidth()+ .height(height * fill)+ .background(Color.White.copy(alpha = 0.24f)),+ )+ }++ Icon(+ if (amount > 0f) PhosphorIcons.Fill.MicrophoneStage+ else PhosphorIcons.Regular.MicrophoneStage,+ contentDescription = stringResource(R.string.karaoke),+ // White throughout: the filled glyph is what says it is on, and a+ // coloured one competed with the artwork behind it.+ tint = Color.White,+ modifier = Modifier+ .align(if (open) Alignment.BottomCenter else Alignment.Center)+ .padding(bottom = if (open) 11.dp else 0.dp)+ .size(20.dp),+ )+ }+}++private val CLOSED_SIZE = 44.dp+private val OPEN_HEIGHT = 136.dp++/** How long it stays open with nothing happening. */+private const val IDLE_MS = 2_600L++/** Under this there is nothing left to hear, so it reads as off. */+private const val OFF_BELOW = 0.04fdiff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.ktindex 6b10ea0..36a8976 100644--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerPanel.kt@@ -128,6 +128,9 @@ fun PlayerPanelSection(val reverb by dev.lelonio.square.playback.AudioEffects.reverb.collectAsStateWithLifecycle()val effectsOn = speed != 1f || pitch != 1f || reverb > 0f+ val karaoke by dev.lelonio.square.playback.AudioEffects.karaoke+ .collectAsStateWithLifecycle()+ val karaokeOn = karaoke > 0fval views = remember {listOf(PlayerPanel.NONE, PlayerPanel.LYRICS, PlayerPanel.EFFECTS, PlayerPanel.INFO)@@ -167,6 +170,9 @@ fun PlayerPanelSection(activeIcon = PhosphorIcons.Fill.TextAlignLeft,label = stringResource(R.string.lyrics),selected = selected == 1,+ // The karaoke lives in this view and keeps working with the+ // panel shut; see the same halo on the effects tab.+ marked = karaokeOn,) { onSelect(PlayerPanel.LYRICS) }PanelTab(icon = PhosphorIcons.Regular.SlidersHorizontal,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.ktindex ec96ff6..73d3781 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@@ -259,6 +259,11 @@ fun PlayerScreen() {var panel by remember { mutableStateOf(PlayerPanel.NONE) }+ /** Bumped to open the karaoke control; see KaraokeDial. */+ var karaokeExpand by remember { androidx.compose.runtime.mutableIntStateOf(0) }+++// The one panel whose contents are fetched rather than already here, and// the one nobody opens for most songs. Asking on open keeps a request per// track from being made for a page most listeners never see.@@ -519,7 +524,103 @@ fun PlayerScreen(// simply going. A plain fade is what opening a lyric// sheet should look like, and holding the slot open// keeps everything below it still.- Crossfade(+ // The cover is drawn here rather than as one of the+ // states below.+ //+ // As a state it could not be made to dissolve: whichever+ // container held it — Crossfade first, then+ // AnimatedContent — dropped it from the composition in+ // the same frame the panel arrived, so there was nothing+ // left to fade. The panels go on swapping between+ // themselves; the cover is simply the floor of this+ // slot, and opening a panel takes its opacity away.+ val coverShowing = panel == PlayerPanel.NONE &&+ !(videoOn && videoPlayer != null && LocalGlassEnabled.current) &&+ (canvas == null || !canvasReady)+ // One at a time, from one clock.+ //+ // Faded together, the cover and the panel are both half+ // visible through the middle of the change: lyrics+ // printed over a record sleeve, which reads as a mistake+ // rather than as a transition. Two animations with the+ // same duration and opposite delays were still two+ // clocks, and they overlapped by however far apart they+ // started. This is a single number: the first half of it+ // takes the cover away, the second half brings the panel+ // in, and neither half can begin before the other ends.+ // Slower when the cover is handing over to a clip.+ //+ // With a panel it is a change the listener asked for and+ // wants over with. A Canvas arriving is not: the clip+ // fades up behind the glass over four hundred+ // milliseconds, and a cover that left in a hundred and+ // eighty made the two look like separate events instead+ // of one dissolve.+ val toClip = panel == PlayerPanel.NONE+ val phase by animateFloatAsState(+ targetValue = if (coverShowing) 0f else 1f,+ animationSpec = tween(+ if (toClip) CLIP_FADE_MS else STAGE_FADE_MS * 2,+ ),+ label = "stageFade",+ )+ val coverAlpha = (1f - phase * 2f).coerceIn(0f, 1f)+ val panelAlpha = ((phase - 0.5f) * 2f).coerceIn(0f, 1f)++ Box(Modifier.weight(1f), contentAlignment = Alignment.Center) {+ // Says so in words, over whatever the slot is+ // showing.+ //+ // The lit microphone says it to whoever is looking+ // at the control; this says it to whoever comes+ // back to a song that sounds wrong and does not+ // remember why. It rides above the cover rather+ // than beside the title, where the transport+ // already has all the room it needs.+ val karaokeAmount by dev.lelonio.square.playback.AudioEffects.karaoke+ .collectAsStateWithLifecycle()+ androidx.compose.animation.AnimatedVisibility(+ visible = karaokeAmount > 0f,+ enter = fadeIn(tween(220)),+ exit = fadeOut(tween(180)),+ modifier = Modifier+ .align(Alignment.TopCenter)+ .then(androidx.compose.ui.Modifier)++ ) {+ KaraokeBadge(karaokeAmount, glassBackdrop) {+ panel = PlayerPanel.LYRICS+ karaokeExpand+++ }+ }++ if (coverAlpha > 0f) {+ Box(+ Modifier+ .fillMaxSize()+ .graphicsLayer { alpha = coverAlpha },+ contentAlignment = Alignment.Center,+ ) {+ Cover(+ state,+ // Always as if nothing were open: the+ // cover shrinks towards the size it+ // takes beside a panel, and doing that+ // while it fades is two movements where+ // one was asked for. It is only ever+ // seen at full size now.+ PlayerPanel.NONE,+ onNext,+ onPrevious,+ // Only for the journey the bar's+ // thumbnail makes into this cover.+ sharedScope.takeIf { coverShowing },+ animatedScope.takeIf { coverShowing },+ )+ }+ }++ AnimatedContent(targetState = when (panel) {PlayerPanel.LYRICS -> Stage.LYRICSPlayerPanel.EFFECTS -> Stage.EFFECTS@@ -588,6 +689,8 @@ fun PlayerScreen(positionMs = positionMs,isPlaying = state.isPlaying,onSeek = onSeek,+ backdrop = glassBackdrop,+ expandSignal = karaokeExpand,)// Effects and the queue share the lyrics' space@@ -1516,6 +1619,51 @@ internal fun Controls(* bar has. A capsule with equal sides is a circle, so no separate shape is* needed.*/+/**+ * A word for the setting that is quietly changing the record.+ *+ * Small, unclickable and out of the way: it is a note to self, not a control.+ * The control is the microphone at the other end of the panel.+ */+@Composable+private fun KaraokeBadge(amount: Float, backdrop: Backdrop, onClick: () -> Unit) {+ Row(+ Modifier+ .padding(top = 4.dp)+ .clip(dev.lelonio.square.ui.glass.shapes.ContinuousCapsule())+ // A way to the thing it is talking about: the lyrics, with the+ // control open. A label about a setting that cannot be reached from+ // where it is read is half a message.+ .pressable(onClick, pressedScale = 0.94f)+ // The app's material, not a grey plate: it sits among glass and a+ // painted rectangle is the one thing that reads as pasted on.+ .liquidGlass(+ config = LocalGlassEffectConfig.current,+ shape = dev.lelonio.square.ui.glass.shapes.ContinuousCapsule(),+ ownBackdrop = backdrop,+ highlightAlpha = BarHighlightAlpha,+ backdropScale = 0.4f,+ )+ .padding(horizontal = 12.dp, vertical = 5.dp),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ Icon(+ PhosphorIcons.Fill.MicrophoneStage,+ contentDescription = null,+ // White, like the control it refers to: an accent pulled from the+ // cover lands anywhere, including on the cover itself.+ tint = Color.White,+ modifier = Modifier.size(13.dp),+ )+ Text(+ stringResource(R.string.karaoke_on, (amount * 100).toInt()),+ style = MaterialTheme.typography.labelMedium,+ color = Color.White,+ modifier = Modifier.padding(start = 6.dp),+ )+ }+}+@Composableprivate fun RoundGlassButton(backdrop: Backdrop,@@ -1613,7 +1761,60 @@ private fun LyricsStage(positionMs: State<Long>,isPlaying: Boolean,onSeek: (Long) -> Unit,+ backdrop: Backdrop,+ /** Bumped when somebody arrives here asking for the karaoke control. */+ expandSignal: Int,) {+ // Off when a song starts: turning it on is asking to read this one.+ var translated by androidx.compose.runtime.saveable.rememberSaveable(lyrics) {+ androidx.compose.runtime.mutableStateOf(false)+ }++ // The language the app itself is read in, which is the one to translate+ // into — not the phone's, when the two have been made to differ on purpose.+ val context = androidx.compose.ui.platform.LocalContext.current+ val target = remember(context) {+ (context.applicationContext as dev.lelonio.square.SquareApplication)+ .language+ .language()+ }++ // What the translator answered for this song, once it has been asked.+ // Kept per song, so turning the toggle off and on again costs nothing.+ var machine by remember(lyrics) {+ androidx.compose.runtime.mutableStateOf<List<String>?>(null)+ }+ var translating by remember(lyrics) { androidx.compose.runtime.mutableStateOf(false) }++ // A document that ships its own translation is already answered; anything+ // else is put through the translator the first time it is asked for.+ val hasOwn = lyrics?.lines.orEmpty().any { !it.translation.isNullOrBlank() }++ androidx.compose.runtime.LaunchedEffect(lyrics, translated) {+ if (!translated || lyrics == null || hasOwn || machine != null) return@LaunchedEffect+ translating = true+ machine = runCatching {+ dev.lelonio.square.backend.lyrics.Translate.lines(+ lyrics.lines.map { it.text },+ target,+ )+ }.getOrNull()+ translating = false+ }++ val shown = remember(lyrics, machine) {+ val answers = machine+ if (lyrics == null || answers == null) {+ lyrics+ } else {+ lyrics.copy(+ lines = lyrics.lines.mapIndexed { index, line ->+ line.copy(translation = answers.getOrNull(index)?.takeIf { it.isNotBlank() })+ },+ )+ }+ }+Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {when {loading -> androidx.compose.material3.CircularProgressIndicator(@@ -1628,13 +1829,48 @@ private fun LyricsStage()else -> LyricsView(- lyrics = lyrics,+ lyrics = shown ?: lyrics,positionMs = positionMs,isPlaying = isPlaying,+ showTranslation = translated,onSeek = onSeek,modifier = Modifier.fillMaxSize(),)}++ // Beside the words rather than under them: a band across the panel took+ // the room the lyrics need, for a control touched once a song.+ val karaoke by dev.lelonio.square.playback.AudioEffects.karaoke+ .collectAsStateWithLifecycle()+ // Offered on every song with words. Where the document carries no+ // translation of its own the lines are put through a translator, so+ // there is always something behind the switch.+ if (lyrics != null) {+ TranslationToggle(+ on = translated,+ busy = translating,+ onChange = { translated = it },+ backdrop = backdrop,+ modifier = Modifier+ // Above the karaoke dial, in the same column: the two are+ // the panel's controls, and a second one loose on the other+ // side would read as belonging to something else.+ .align(Alignment.BottomEnd)+ .padding(end = 6.dp, bottom = 56.dp),+ )+ }++ KaraokeDial(+ amount = karaoke,+ onChange = dev.lelonio.square.playback.AudioEffects::setKaraoke,+ backdrop = backdrop,+ expandSignal = expandSignal,+ modifier = Modifier+ // Low on the right, just above the transport — where Apple puts+ // it, and out of the way of the line being sung, which sits in+ // the middle of the panel.+ .align(Alignment.BottomEnd)+ .padding(end = 6.dp, bottom = 6.dp),+ )}}-diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xmlindex 7c10e62..0109cac 100644--- a/app/src/main/res/values-de/strings.xml+++ b/app/src/main/res/values-de/strings.xml@@ -9,6 +9,11 @@<string name="save">Speichern</string><string name="clear">Löschen</string><string name="credits">Credits</string>+ <string name="karaoke">Karaoke</string>+ <string name="translation">Übersetzung</string>+ <string name="karaoke_on">Karaoke %d%%</string>+ <string name="pip_listen">Nur Ton</string>+ <string name="remove_from_queue">Aus der Warteschlange entfernen</string><string name="credits_none">Keine Angaben</string><string name="credits_source">Quelle</string><string name="credits_performers">Mitwirkende</string>diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xmlindex 1b572aa..901e7d3 100644--- a/app/src/main/res/values-es/strings.xml+++ b/app/src/main/res/values-es/strings.xml@@ -9,6 +9,11 @@<string name="save">Guardar</string><string name="clear">Borrar</string><string name="credits">Créditos</string>+ <string name="karaoke">Karaoke</string>+ <string name="translation">Traducción</string>+ <string name="karaoke_on">Karaoke %d%%</string>+ <string name="pip_listen">Solo audio</string>+ <string name="remove_from_queue">Quitar de la cola</string><string name="credits_none">Sin créditos</string><string name="credits_source">Fuente</string><string name="credits_performers">Intérpretes</string>diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xmlindex f361940..e12d778 100644--- a/app/src/main/res/values-fr/strings.xml+++ b/app/src/main/res/values-fr/strings.xml@@ -9,6 +9,11 @@<string name="save">Enregistrer</string><string name="clear">Effacer</string><string name="credits">Crédits</string>+ <string name="karaoke">Karaoké</string>+ <string name="translation">Traduction</string>+ <string name="karaoke_on">Karaoké %d%%</string>+ <string name="pip_listen">Audio seul</string>+ <string name="remove_from_queue">Retirer de la file</string><string name="credits_none">Aucun crédit</string><string name="credits_source">Source</string><string name="credits_performers">Interprètes</string>diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xmlindex effe0ca..9fcaf0b 100644--- a/app/src/main/res/values-hi/strings.xml+++ b/app/src/main/res/values-hi/strings.xml@@ -14,6 +14,11 @@<string name="save">सहेजें</string><string name="clear">साफ़ करें</string><string name="credits">श्रेय</string>+ <string name="karaoke">कराओके</string>+ <string name="translation">अनुवाद</string>+ <string name="karaoke_on">कराओके %d%%</string>+ <string name="pip_listen">केवल ऑडियो</string>+ <string name="remove_from_queue">कतार से हटाएं</string><string name="credits_none">कोई श्रेय नहीं</string><string name="credits_source">स्रोत</string><string name="credits_performers">कलाकार</string>diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xmlindex c35c7f4..df73c96 100644--- a/app/src/main/res/values-it/strings.xml+++ b/app/src/main/res/values-it/strings.xml@@ -10,6 +10,11 @@<string name="save">Salva</string><string name="clear">Cancella</string><string name="credits">Crediti</string>+ <string name="karaoke">Karaoke</string>+ <string name="translation">Traduzione</string>+ <string name="karaoke_on">Karaoke %d%%</string>+ <string name="pip_listen">Solo audio</string>+ <string name="remove_from_queue">Togli dalla coda</string><string name="credits_none">Nessun credito</string><string name="credits_source">Fonte</string><string name="credits_performers">Interpreti</string>diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xmlindex 62c3921..f88ac16 100644--- a/app/src/main/res/values-pt/strings.xml+++ b/app/src/main/res/values-pt/strings.xml@@ -9,6 +9,11 @@<string name="save">Salvar</string><string name="clear">Limpar</string><string name="credits">Créditos</string>+ <string name="karaoke">Karaoke</string>+ <string name="translation">Tradução</string>+ <string name="karaoke_on">Karaoke %d%%</string>+ <string name="pip_listen">Só áudio</string>+ <string name="remove_from_queue">Remover da fila</string><string name="credits_none">Sem créditos</string><string name="credits_source">Fonte</string><string name="credits_performers">Intérpretes</string>diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xmlindex 7d3e2e5..4347457 100644--- a/app/src/main/res/values-tr/strings.xml+++ b/app/src/main/res/values-tr/strings.xml@@ -14,6 +14,11 @@<string name="save">Kaydet</string><string name="clear">Temizle</string><string name="credits">Katkıda bulunanlar</string>+ <string name="karaoke">Karaoke</string>+ <string name="translation">Çeviri</string>+ <string name="karaoke_on">Karaoke %%%d</string>+ <string name="pip_listen">Yalnızca ses</string>+ <string name="remove_from_queue">Sıradan çıkar</string><string name="credits_none">Kayıt yok</string><string name="credits_source">Kaynak</string><string name="credits_performers">Sanatçılar</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex ccb1b36..335a1a5 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -20,6 +20,11 @@<string name="save">Save</string><string name="clear">Clear</string><string name="credits">Credits</string>+ <string name="karaoke">Karaoke</string>+ <string name="translation">Translation</string>+ <string name="karaoke_on">Karaoke %d%%</string>+ <string name="pip_listen">Listen only</string>+ <string name="remove_from_queue">Remove from queue</string><string name="credits_none">Nobody credited</string><string name="credits_source">Source</string><string name="credits_performers">Performers</string>