Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix(speed): recover from buffer starvation during high-speed playback (#103)

anna063ca6bc0108
diff --git a/tweak/Sources/Shared/Player/SpeedPitch.x b/tweak/Sources/Shared/Player/SpeedPitch.x
index 4ec594e9..8ea5ccb5 100644
--- a/tweak/Sources/Shared/Player/SpeedPitch.x
+++ b/tweak/Sources/Shared/Player/SpeedPitch.x
@@ -38,12 +38,10 @@
//
// Spotify's clock keeps running at its own speed between the player's reports: -[SPTPlayerState position]
// is positionAsOfTimestamp minus timeIntervalSinceNow times [self playbackSpeed] (disassembly,
-// 0x1057735ec), so playbackSpeed is hooked to include this speed. Away from 1x the player's reports fall
-// behind the sound: on 9.1.90 at 1.5x, after about 30 s it reports a stall every 3 s that the sound never
-// has, each 1.2 s, and its clock stays that far behind until a seek or the next track (24 s after 100 s).
-// So from the moment the speed leaves 1x, -position counts its own clock: Spotify's position then, plus the
-// song the mixer has handed over since (sg_clock). A seek, skip, stop or new track hands the clock back to
-// Spotify's, and away from 1x takes Spotify's next report as the new start.
+// 0x1057735ec), so playbackSpeed is hooked to include this speed. From the moment the speed leaves 1x,
+// -position counts its own clock: Spotify's position then, plus the song the mixer has handed over since
+// (sg_clock). A seek, skip, stop, new track or stall (SGSpeedObserver) hands the clock back to Spotify's, and
+// away from 1x takes Spotify's next report as the new start.
//
// When the music's output was never connected, pitch falls back to the way it first shipped: a render notify
// on the RemoteIO unit runs each finished buffer through a unit working in place; speed is then unavailable.
@@ -1079,13 +1077,28 @@ static NSString *trackOf(SPTPlayerState *state) {
return [state.track.URI description];
}
-// Spotify's clock again from here; away from 1x, its next report starts the own clock over.
-static void handBack(void) {
+// Away from 1x, Spotify's next report from now starts the own clock over.
+static void rearmClock(void) {
os_unfair_lock_lock(&sg_clockLock);
sg_clock = (typeof(sg_clock)){.armed = loadFloat(&sg_speedBits) != 1, .notBefore = CFAbsoluteTimeGetCurrent()};
os_unfair_lock_unlock(&sg_clockLock);
}
+// Drops what the unit holds from before (93 ms when it is in).
+static void resetUnit(void) {
+ SGTimePitch *unit = atomic_load(tapped() ? &sg_pull : &sg_inPlace);
+ if (!unit) return;
+ disengage();
+ SGTimePitchReset(unit);
+ if (sg_speed != 1 || sg_semitones != 0) atomic_store(&sg_engaged, true);
+}
+
+// Spotify's clock again from here, and the unit starts clean at the new place.
+static void handBack(void) {
+ rearmClock();
+ resetUnit();
+}
+
void SGSetPlayerSpeed(double speed) {
if (!tapped()) return;
// Spotify's clock is right until the speed leaves 1x, so it is read before, as Spotify counts it: Sing's
@@ -1256,12 +1269,33 @@ BOOL SGPlayerWatchMusicOutput(SGPlayerOutputWatcher watcher) {
}
%end
+// Away from 1x, Spotify's core cannot keep up and stalls: the driver queue empties, the mixer hands over zeros
+// and the core reports speed 0 at the place it stopped, then resumes from there (notes/speed-rebuffer.md). Its
+// clock is right and the own one, which counted the zeros, is ahead. Each stall report hands the clock back to
+// Spotify's, and the report that resumes starts it over at the right place.
+@interface SGSpeedObserver : NSObject <SGPlayerStateObserver>
+@end
+
+@implementation SGSpeedObserver
+- (void)playerStateDidChange:(SPTPlayerState *)state {}
+- (void)playerStateDidReport:(SPTPlayerState *)state {
+ if (loadFloat(&sg_speedBits) == 1 || !state.isPlaying || state.isPaused) return;
+ BOOL buffering = [state respondsToSelector:NSSelectorFromString(@"isBuffering")] && [[state valueForKey:@"isBuffering"] boolValue];
+ if (buffering || state.playbackSpeed == 0) rearmClock();
+}
+@end
+
%ctor {
storeFloat(&sg_speedBits, 1);
storeFloat(&sg_gainBits, 1);
rebind();
%init;
SGRequireClasses(@[@"SPTPlayerState"]);
+ dispatch_async(dispatch_get_main_queue(), ^{
+ static SGSpeedObserver *observer;
+ observer = [SGSpeedObserver new];
+ SGAddPlayerStateObserver(observer);
+ });
}
#if SG_DRIVER