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 a skip land in a third of a second, and stop the screen lying

Lelonio38fb39ad723a
diff --git a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
index a3cdbb5..59a8a76 100644
--- a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
+++ b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
@@ -2,27 +2,27 @@ C/C++ Structured Logc
a
_/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  ҏ˴�3  ��߈�3`
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �٣��3  ��߈�3`
^
-\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build.json  ӏ˴�3� ��߈�3e
+\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build.json  �٣��3� ��߈�3e
c
-a/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build_mini.json  ӏ˴�3� ��߈�3R
+a/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build_mini.json  �٣��3� ��߈�3R
P
-N/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja  ӏ˴�3� ��߈�3V
+N/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja  �٣��3� ��߈�3V
T
-R/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja.txt  ӏ˴�3[
+R/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja.txt  �٣��3[
Y
-W/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build_file_index.txt  ӏ˴�3 @ ��߈�3\
+W/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build_file_index.txt  �٣��3 @ ��߈�3\
Z
-X/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json  ӏ˴�3֚ ��߈�3`
+X/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json  �٣��3֚ ��߈�3`
^
-\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json.bin  ӏ˴�3 �" ��߈�3f
+\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json.bin  �٣��3 �" ��߈�3f
d
-b/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/metadata_generation_command.txt  ӏ˴�3
+b/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/metadata_generation_command.txt  �٣��3
� ��߈�3Y
W
-U/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/prefab_config.json  ӏ˴�3  ( ��߈�3^
+U/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/prefab_config.json  �٣��3  ( ��߈�3^
\
-Z/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/symbol_folder_index.txt  ӏ˴�3  Y ��߈�3D
+Z/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/symbol_folder_index.txt  �٣��3  Y ��߈�3D
B
-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  ӏ˴�3 � �Ȅ��3
\ No newline at end of file
+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �٣��3 � �Ȅ��3
\ No newline at end of file
diff --git a/app/src/main/java/dev/emanuele/spot/playback/AudioOutput.kt b/app/src/main/java/dev/emanuele/spot/playback/AudioOutput.kt
index 6b81cce..6470508 100644
--- a/app/src/main/java/dev/emanuele/spot/playback/AudioOutput.kt
+++ b/app/src/main/java/dev/emanuele/spot/playback/AudioOutput.kt
@@ -289,6 +289,10 @@ class AudioOutput {
* called from the main looper.
*/
fun fadeOutThen(action: () -> Unit) {
+ fadeOutThen(SKIP_FADE_OUT_MS, action)
+ }
+
+ fun fadeOutThen(durationMs: Long, action: () -> Unit) {
val output = synchronized(this) {
track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED }
}
@@ -296,8 +300,9 @@ class AudioOutput {
action()
return
}
+ val generation = fadeGeneration.incrementAndGet()
fadeExecutor.execute {
- ramp(output, 0f, FADE_OUT_MS)
+ ramp(output, 0f, durationMs, generation)
// Order matters: the gate closes before the buffer is emptied, so
// nothing can slip back in between the two.
discarding = true
@@ -311,14 +316,26 @@ class AudioOutput {
val output = synchronized(this) {
track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED }
} ?: return
+ val generation = fadeGeneration.incrementAndGet()
fadeExecutor.execute {
// Whatever arrived while the gate was closing is still old audio.
discardBuffered(output)
discarding = false
- ramp(output, 1f, FADE_IN_MS)
+ ramp(output, 1f, FADE_IN_MS, generation)
}
}
+ /**
+ * Which fade is the current one.
+ *
+ * Fades run one at a time on a single thread, and a fade-in takes a quarter
+ * of a second: a skip during one used to wait for it to finish before its
+ * own fade could even start, which is a quarter of a second between the tap
+ * and the engine hearing about it. A ramp now stops as soon as a later one
+ * is asked for, and the later one begins immediately.
+ */
+ private val fadeGeneration = java.util.concurrent.atomic.AtomicLong(0)
+
/**
* Empties the track's buffer and leaves it running again.
*
@@ -348,10 +365,17 @@ class AudioOutput {
* scaling the PCM on the way in would leave whatever is already buffered
* playing at full level after the fade had supposedly finished.
*/
- private fun ramp(output: AudioTrack, target: Float, durationMs: Long) {
+ private fun ramp(
+ output: AudioTrack,
+ target: Float,
+ durationMs: Long,
+ /** Which fade this is; see [fadeGeneration]. Zero means "cannot be superseded". */
+ generation: Long = 0,
+ ) {
val steps = (durationMs / FADE_STEP_MS).toInt().coerceAtLeast(1)
val start = currentGain
for (step in 1..steps) {
+ if (generation != 0L && fadeGeneration.get() != generation) return
val gain = start + (target - start) * step / steps
currentGain = gain
if (runCatching { output.setVolume(gain) }.isFailure) return
@@ -560,6 +584,15 @@ class AudioOutput {
*/
const val FADE_IN_MS = 260L
const val FADE_OUT_MS = 200L
+
+ /**
+ * The fade before a skip, which is shorter than the one before a pause.
+ *
+ * It is time the listener waits between asking for the next track and
+ * hearing it, and a skip is a request to be somewhere else now. A pause
+ * has no such hurry, so it keeps the longer, softer fade.
+ */
+ const val SKIP_FADE_OUT_MS = 70L
const val FADE_STEP_MS = 10L
/** Decay range, in milliseconds: a live room up to a large hall. */
diff --git a/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt b/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
index d8ef90a..791847f 100644
--- a/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
@@ -219,6 +219,9 @@ class LibrespotPlayer(
/** A skip the user has made and the engine has not been told about yet. */
private var skipPending = false
+ /** A skip already handed to the engine, still to be confirmed by an event. */
+ private var skipInFlight = false
+
/**
* Moves to a track, without making the user wait for the engine.
*
@@ -235,13 +238,22 @@ class LibrespotPlayer(
*/
private fun requestSkipTo(index: Int, positionMs: Long) {
queue.currentIndex = index.coerceIn(0, maxOf(0, queue.items.lastIndex))
- this.positionMs = positionMs
+ // A skip starts the new track at its beginning. Media3 sends
+ // C.TIME_UNSET for "wherever it starts", which as a number is very
+ // negative and made the bar draw itself backwards.
+ this.positionMs = positionMs.coerceAtLeast(0)
playbackState = Player.STATE_BUFFERING
skipPending = true
invalidateState()
+ // The first skip of a burst goes out at once; only the ones on top of
+ // it wait. Waiting for the first was a fifth of a second between the
+ // tap and anything happening at all — the delay this settle exists to
+ // avoid, spent on the one skip that never needed it.
+ val delay = if (skipInFlight) SKIP_SETTLE_MS else 0L
+ skipInFlight = true
handler.removeCallbacks(settleSkip)
- handler.postDelayed(settleSkip, SKIP_SETTLE_MS)
+ handler.postDelayed(settleSkip, delay)
}
private val settleSkip = Runnable {
@@ -547,11 +559,27 @@ class LibrespotPlayer(
val index = queue.items.indexOfFirst { it.uri == uri }
if (index >= 0) {
engineIndex = index
- // Not while the user is mid-skip: those events describe the
- // track being left, and adopting them would drag the screen
- // back to it between taps.
- if (!skipPending && index != queue.currentIndex) {
+ if (index == queue.currentIndex) {
+ skipInFlight = false
+ if (type == "playing" || type == "loading") positionMs = 0
+ } else if (
+ !skipPending &&
+ (type == "playing" || (!skipInFlight && type == "loading"))
+ ) {
+ // The engine is the truth about what is coming out of the
+ // speaker. While a skip is on its way its events are about
+ // the track being left, and following them dragged the
+ // screen backwards — but once nothing is outstanding, the
+ // screen was simply wrong to disagree.
+ //
+ // Loading counts, not only playing: a run of tracks the
+ // account cannot play — region-locked, delisted — is
+ // skipped by the engine one after another, and each of
+ // those is a load with no play. Following only plays left
+ // the app three songs behind what was in the speaker,
+ // which is exactly how the two came apart.
queue.currentIndex = index
+ skipInFlight = false
positionMs = 0
}
}
@@ -564,7 +592,7 @@ class LibrespotPlayer(
"playing" -> {
playbackState = Player.STATE_READY
playWhenReady = true
- positionMs = eventPositionMs
+ if (!skipInFlight) positionMs = eventPositionMs
// Here as well as in handleSetPlayWhenReady: a pause can arrive
// from the notification, from a headset button or from another
// Connect device, and only this path sees all of them.
@@ -573,11 +601,14 @@ class LibrespotPlayer(
"paused" -> {
playbackState = Player.STATE_READY
playWhenReady = false
- positionMs = eventPositionMs
+ if (!skipInFlight) positionMs = eventPositionMs
onPlaybackActive(false)
}
"position" -> {
- if (skipPending) return
+ // The old track goes on reporting until the new one starts.
+ // Taking those would run the bar forward on a song that is no
+ // longer the one on screen.
+ if (skipPending || skipInFlight) return
positionMs = eventPositionMs
}
"stopped" -> {
diff --git a/app/src/main/java/dev/emanuele/spot/ui/player/PlayerState.kt b/app/src/main/java/dev/emanuele/spot/ui/player/PlayerState.kt
index 4c8f9b6..5bdacdc 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/player/PlayerState.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/player/PlayerState.kt
@@ -114,7 +114,10 @@ fun rememberQueue(player: Player?): State<List<QueueEntry>> {
fun rebuild() {
val current = player.currentMediaItemIndex
- queue.value = (0 until player.mediaItemCount).map { index ->
+ // From the playing track onwards. What has already been heard is
+ // not a queue — it is history, and it pushed what comes next off
+ // the bottom of the panel on any list longer than a screen.
+ queue.value = (current until player.mediaItemCount).map { index ->
val metadata = player.getMediaItemAt(index).mediaMetadata
QueueEntry(
index = index,
diff --git a/native/src/engine.rs b/native/src/engine.rs
index 8f2c9e0..1a38b8f 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -327,6 +327,11 @@ fn spawn_event_pump(
while let Some(event) = events.blocking_recv() {
history.observe(&event);
+ // As soon as something starts, fetch what comes after it.
+ if let PlayerEvent::Playing { track_id, .. } = &event {
+ preload_after(&player, &uri_string(track_id));
+ }
+
let (kind, uri, position_ms) = match event {
PlayerEvent::Playing {
track_id,
@@ -673,6 +678,33 @@ fn emit(listener: &GlobalRef, kind: &str, uri: &str, position_ms: i64) -> Result
/// lock from the one that owns playback.
static CONTEXT_URI: Mutex<String> = Mutex::new(String::new());
+/// The queue as the app handed it over, in play order.
+///
+/// Kept so the engine can fetch the next track before it is asked for; see
+/// [`preload_after`].
+static QUEUE: Mutex<Vec<String>> = Mutex::new(Vec::new());
+
+/// Warms up the track after `uri`, so a skip does not start from nothing.
+///
+/// librespot preloads on its own, but only in the last thirty seconds of a
+/// track — that is for gapless playback, and it does nothing for someone
+/// skipping. Between the tap and the first sound there was most of a second of
+/// audio key and first chunk; fetched now, while the current track plays, the
+/// skip has them already.
+fn preload_after(player: &Player, uri: &str) {
+ let next = {
+ let Ok(queue) = QUEUE.lock() else { return };
+ let Some(position) = queue.iter().position(|item| item == uri) else {
+ return;
+ };
+ queue.get(position + 1).cloned()
+ };
+ let Some(next) = next else { return };
+ if let Ok(parsed) = SpotifyUri::from_uri(&next) {
+ player.preload(parsed);
+ }
+}
+
pub fn current_context() -> String {
CONTEXT_URI.lock().map(|c| c.clone()).unwrap_or_default()
}
@@ -699,6 +731,9 @@ pub fn load_queue(
if let Ok(mut stored) = CONTEXT_URI.lock() {
*stored = context_uri;
}
+ if let Ok(mut stored) = QUEUE.lock() {
+ *stored = uris.clone();
+ }
let options = LoadRequestOptions {
start_playing,