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

chore: FLEX builds log silent runs from the mixer and the player's buffering reports

My-Name-Is-Jeffbb2902ef115e
diff --git a/tweak/Sources/Shared/Player/SpeedPitch.h b/tweak/Sources/Shared/Player/SpeedPitch.h
index f6dbb9e1..2de6f4c3 100644
--- a/tweak/Sources/Shared/Player/SpeedPitch.h
+++ b/tweak/Sources/Shared/Player/SpeedPitch.h
@@ -103,3 +103,9 @@ BOOL SGPlayerMusicClientFormat(AudioStreamBasicDescription *format);
// Spotify's output cannot be reached at all. Register from a %ctor; up to four.
typedef void (*SGPlayerOutputWatcher)(AudioUnit output);
BOOL SGPlayerWatchMusicOutput(SGPlayerOutputWatcher watcher);
+
+#if SG_DRIVER
+// Speed-rebuffer test (FLEX builds): completed runs of silence from the music's mixer so far, and the last one's
+// start (mach time) and length; silentNow while one is running.
+uint64_t SGSpeedProbeSilence(uint64_t *host, double *ms, bool *silentNow);
+#endif
diff --git a/tweak/Sources/Shared/Player/SpeedPitch.x b/tweak/Sources/Shared/Player/SpeedPitch.x
index c65a9d2d..eb8973c7 100644
--- a/tweak/Sources/Shared/Player/SpeedPitch.x
+++ b/tweak/Sources/Shared/Player/SpeedPitch.x
@@ -189,6 +189,41 @@ static void silence(AudioBufferList *data) {
for (UInt32 b = 0; b < data->mNumberBuffers; b++) if (data->mBuffers[b].mData) memset(data->mBuffers[b].mData, 0, data->mBuffers[b].mDataByteSize);
}
+#if SG_DRIVER
+// Runs of silence from the music's mixer (speed-rebuffer test): written on the render thread only, read by
+// SGSpeedProbeSilence on main. A run is reported once it ends.
+static _Atomic uint64_t sg_runStart, sg_runFrames, sg_lastRunHost, sg_lastRunFrames, sg_runs;
+static _Atomic uint64_t sg_runRateBits;
+
+static void noteSilence(bool silent, UInt32 frames, double rate, uint64_t host) {
+ uint64_t start = atomic_load_explicit(&sg_runStart, memory_order_relaxed);
+ if (silent) {
+ if (!start) atomic_store_explicit(&sg_runStart, host, memory_order_relaxed);
+ atomic_fetch_add_explicit(&sg_runFrames, frames, memory_order_relaxed);
+ } else if (start) {
+ uint64_t bits;
+ memcpy(&bits, &rate, sizeof bits);
+ atomic_store_explicit(&sg_runRateBits, bits, memory_order_relaxed);
+ atomic_store_explicit(&sg_lastRunHost, start, memory_order_relaxed);
+ atomic_store_explicit(&sg_lastRunFrames, atomic_load_explicit(&sg_runFrames, memory_order_relaxed), memory_order_relaxed);
+ atomic_fetch_add_explicit(&sg_runs, 1, memory_order_release);
+ atomic_store_explicit(&sg_runStart, 0, memory_order_relaxed);
+ atomic_store_explicit(&sg_runFrames, 0, memory_order_relaxed);
+ }
+}
+
+uint64_t SGSpeedProbeSilence(uint64_t *host, double *ms, bool *silentNow) {
+ uint64_t runs = atomic_load_explicit(&sg_runs, memory_order_acquire);
+ uint64_t bits = atomic_load_explicit(&sg_runRateBits, memory_order_relaxed);
+ double rate;
+ memcpy(&rate, &bits, sizeof rate);
+ *host = atomic_load_explicit(&sg_lastRunHost, memory_order_relaxed);
+ *ms = rate > 0 ? atomic_load_explicit(&sg_lastRunFrames, memory_order_relaxed) * 1000.0 / rate : 0;
+ *silentNow = atomic_load_explicit(&sg_runStart, memory_order_relaxed) != 0;
+ return runs;
+}
+#endif
+
// The output's mixer's next `frames` frames into `data`, in chunks of at most its largest slice, each with a
// sample time of the output's own, so Spotify's units never see a time twice (an AU renders a time it has
// seen from cache). With `sounding`, it stops at the first chunk marked silent (SGPlayerPull).
@@ -234,6 +269,13 @@ static OSStatus pullSource(Output *output, AudioUnit source, const AudioTimeStam
if ((status != noErr || (flags & kAudioUnitRenderAction_OutputIsSilence))
&& observesSource && output == music()) SGPlayerInvalidateSource();
#if SG_DRIVER
+ if (output == music()) {
+ // Silent as flagged, or as heard: every sample of the first channel under -80 dBFS.
+ bool quiet = status == noErr && !(flags & kAudioUnitRenderAction_OutputIsSilence) && target->mBuffers[0].mData;
+ const float *samples = target->mBuffers[0].mData;
+ for (UInt32 i = 0; quiet && i < count; i++) if (fabsf(samples[i]) > 1e-4f) quiet = false;
+ noteSilence((flags & kAudioUnitRenderAction_OutputIsSilence) || status != noErr || quiet, count, rateOf(&output->client), anchorHost);
+ }
if (output == music()) {
double rate = rateOf(&output->client);
// About ten samples per second at the current speed. Sing's refill may burst.
diff --git a/tweak/Sources/Shared/Player/SpeedProbe.x b/tweak/Sources/Shared/Player/SpeedProbe.x
new file mode 100644
index 00000000..5f5ad83f
--- /dev/null
+++ b/tweak/Sources/Shared/Player/SpeedProbe.x
@@ -0,0 +1,69 @@
+// FLEX builds only: the speed-rebuffer test (notes/speed-rebuffer.md). Every line is "speed-probe:" with t, seconds on
+// the mach clock, so the mixer's silent runs line up with the player's buffering and speed-0 reports.
+#if SG_DRIVER
+#import <mach/mach_time.h>
+#import "Core/SGCore.h"
+#import "PlayerState.h"
+#import "SpeedPitch.h"
+
+static const int kMaxLines = 400;
+
+static double seconds(uint64_t host) {
+ static mach_timebase_info_data_t base;
+ if (!base.denom) mach_timebase_info(&base);
+ return (double)host * base.numer / base.denom / 1e9;
+}
+
+static void probeLog(NSString *line) {
+ static int lines;
+ if (lines++ < kMaxLines) SGLog(@"speed-probe: t %.3f %@", seconds(mach_absolute_time()), line);
+}
+
+@interface SGSpeedProbe : NSObject <SGPlayerStateObserver>
+@end
+
+@implementation SGSpeedProbe {
+ int _buffering; // -1 unknown
+ BOOL _stalled;
+}
+- (instancetype)init {
+ if ((self = [super init])) _buffering = -1;
+ return self;
+}
+- (void)playerStateDidChange:(SPTPlayerState *)state {}
+- (void)playerStateDidReport:(SPTPlayerState *)state {
+ int buffering = [state respondsToSelector:NSSelectorFromString(@"isBuffering")] ? [[state valueForKey:@"isBuffering"] boolValue] : -1;
+ BOOL stalled = state.isPlaying && !state.isPaused && state.playbackSpeed == 0;
+ if (buffering != _buffering || stalled != _stalled) {
+ probeLog([NSString stringWithFormat:@"report buffering %d speed %.2f playing %d paused %d position %.2f", buffering,
+ state.playbackSpeed, state.isPlaying, state.isPaused, state.position]);
+ }
+ _buffering = buffering;
+ _stalled = stalled;
+}
+@end
+
+%ctor {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ static SGSpeedProbe *probe;
+ probe = [SGSpeedProbe new];
+ SGAddPlayerStateObserver(probe);
+ static uint64_t seen;
+ static double speed = 1;
+ [NSTimer scheduledTimerWithTimeInterval:0.25 repeats:YES block:^(NSTimer *timer) {
+ double now = SGPlayerSpeed();
+ if (now != speed) probeLog([NSString stringWithFormat:@"speed %.2f -> %.2f", speed, now]);
+ speed = now;
+ uint64_t host;
+ double ms;
+ bool silentNow;
+ uint64_t runs = SGSpeedProbeSilence(&host, &ms, &silentNow);
+ if (runs != seen && speed != 1) {
+ probeLog([NSString stringWithFormat:@"silent run %llu: %.0f ms from t %.3f%@", runs, ms, seconds(host),
+ runs - seen > 1 ? [NSString stringWithFormat:@" (%llu more since last)", runs - seen - 1] : @""]);
+ }
+ seen = runs;
+ }];
+ });
+}
+#endif