Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: lock screen Lyrics applies without a restart and keeps up with the lines
My-Name-Is-Jeffec75e5e15548
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex e1913c50..9927b9e0 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -130,7 +130,10 @@ Shared:LockScreenLyrics/ the line being sung in the system's now playing, and on iOS 26 the lyrics as the lock screen'sfull-screen artwork (LyricsArtwork.x): a new artwork ID per line, its 3:4 H.264 clip (the line and thenext one dimmed over the blurred cover, SGLyricsClip.m) written only when the lock screen asks for it,- Still (one frame) or Animated. Rendered on the Mac against harness/lyrics-clip/. What reaches the+ Still (one frame) or Animated (15 fps). The backdrop is blurred small and drawn large, on the CPU as the+ lock screen asks in the background; previews are drawn on a queue apart from the clips, and while the+ lock screen is asking, the next line's preview and clip are drawn ahead and a ready preview is handed+ over at once. iOS still shows the plain cover for about half a second at each change of artwork. Picking it or another artwork applies at once. Rendered on the Mac against harness/lyrics-clip/. What reaches thesystem's now playing through this hook and Player/NowPlayingExtras.x is checked by harness/now-playing/AnimatedArtwork/ moving artwork (AnimatedArtwork.h): the track's Canvas, else Apple Music's animated album cover(SGMotionCatalog.m: searched without the album's edition, the edition of the same name first, and@@ -478,7 +481,7 @@ Redesigned:harness/lyrics/; the page against harness/lyrics-page/Home/ Home decluttered to music on black (an allow list of its sections: shortcuts, the DJ without its heading andtranscript, the shelves of cards), a large title where the filter pills were with the avatar at the trailing- edge, the shelves' headings at the Music app's size, each shortcut tile's cover run across it blurred+ edge, the title rising and fading with the feed as it scrolls, as the Music app's does, the shelves' headings at the Music app's size, each shortcut tile's cover run across it blurred(SGRPalette's extension), continuous corners on the covers, and in FLEX builds a meter of each scroll'sframes and the hooks' time (Home.h lists its files)Search/ the Browse page decluttered to its category cards (an allow list of the list's cells: the watch feeddiff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.mindex 5d995273..212bc8d2 100644--- a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m@@ -30,8 +30,7 @@ NSArray *SGLockScreenMotionRows(void) {sources.visible = lowData.visible;return @[SGChoiceRow(@"Full-screen artwork", @"The Canvas or Apple Music's animated cover, or the lyrics a line at a time. "- @"Every song puts the cover over a moving blur of it where a song has neither. "- @"A change to or from Lyrics applies after a restart, the others at once",+ @"Every song puts the cover over a moving blur of it where a song has neither",SGKeyLockScreenArtwork, @[@"Off", @"Moving artwork", @"Lyrics", @"Every song"], SGLockArtworkOff),style,sources,diff --git a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.xindex ac3b8464..7fc9090d 100644--- a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x+++ b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x@@ -27,12 +27,12 @@ static const CGFloat kSameShape = 0.02;static NSString *sg_title;static NSString *sg_picture; // the picture the track names, for the cover's clipstatic NSUInteger sg_walk; // counts the tracks walked, so a poster that comes late is told from the newest-static SGLockArtwork sg_choice, sg_launchChoice;+static SGLockArtwork sg_choice;static dispatch_queue_t sg_queue; // cuts clips and makes the cover's, one at a timestatic SGMotionFollower *sg_follower;static BOOL motionOn(SGLockArtwork choice) {- return sg_launchChoice != SGLockArtworkLyrics && (choice == SGLockArtworkMotion || choice == SGLockArtworkEverySong);+ return choice == SGLockArtworkMotion || choice == SGLockArtworkEverySong;}// The key a clip shown at `shown` goes under, and the shape it is cut to (width over height): 1:1 for a@@ -151,7 +151,7 @@ static void showCover(NSString *picture) {%ctor {if (@available(iOS 26.0, *)) {- sg_launchChoice = sg_choice = SGLockScreenArtwork();+ sg_choice = SGLockScreenArtwork();sg_queue = dispatch_queue_create("spotifyglass.lockscreen.motion", DISPATCH_QUEUE_SERIAL);// Each walk takes the last one's clip off first, so the lock screen falls back to the still cover.sg_follower = [[SGMotionFollower alloc] initWithBegin:^BOOL(NSString *uri, SPTPlayerState *state) {diff --git a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.xindex 564d00f0..78f88960 100644--- a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x+++ b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x@@ -54,9 +54,11 @@ static NSArray<NSArray<SGKaraokeWord *> *> *piecesOf(SGKaraokeLine *line) {return pieces;}-// Seconds into the track at `now`, run on from what Spotify last reported.+// Seconds into the track at `now`, run on from what Spotify last reported. A rate it left out runs at 1, as+// playingBy counts it: read as 0, every line written pinned the lock screen's time to Spotify's last report.static double elapsedAt(NSDictionary *info, CFAbsoluteTime reportedAt, CFAbsoluteTime now) {- double rate = [info[MPNowPlayingInfoPropertyPlaybackRate] doubleValue];+ NSNumber *reported = info[MPNowPlayingInfoPropertyPlaybackRate];+ double rate = reported ? reported.doubleValue : 1;return [info[MPNowPlayingInfoPropertyElapsedPlaybackTime] doubleValue] + rate * (now - reportedAt);}diff --git a/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x b/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.xindex 3c04eb0f..f532ac09 100644--- a/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x+++ b/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x@@ -21,23 +21,31 @@ static NSString *sg_track; // the track the clips are forstatic UIImage *sg_cover; // its cover, once Spotify's now playing info has one for itstatic NSString *sg_shownID; // the artwork handed over last, nil for none; read on sg_queue toostatic NSObject *sg_lock;-static dispatch_queue_t sg_queue; // draws and writes, one at a time+static dispatch_queue_t sg_queue; // writes the clips, one at a time+// Draws the previews, apart from the clips: one waiting behind a clip that takes seconds left the lock screen blank.+static dispatch_queue_t sg_previewQueue;static NSURL *folder(void) {NSURL *caches = [NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject;return [caches URLByAppendingPathComponent:@"Vitrine/Lyrics" isDirectory:YES];}-// The blurred cover, made once per cover on sg_queue.-static CGImageRef backdropFor(UIImage *cover, CGSize size) {+// The blurred cover, made once per cover and shared by both queues; the caller releases it.+static CGImageRef copyBackdropFor(UIImage *cover, CGSize size) {static id made;static CGImageRef backdrop;- id key = cover ?: NSNull.null;- if (backdrop && made == key) return backdrop;- CGImageRelease(backdrop);- backdrop = SGLyricsClipBackdrop(cover.CGImage, size);- made = key;- return backdrop;+ static NSObject *lock;+ static dispatch_once_t once;+ dispatch_once(&once, ^{ lock = [NSObject new]; });+ @synchronized (lock) {+ id key = cover ?: NSNull.null;+ if (!backdrop || made != key) {+ CGImageRelease(backdrop);+ backdrop = SGLyricsClipBackdrop(cover.CGImage, size);+ made = key;+ }+ return CGImageRetain(backdrop);+ }}static BOOL stillShown(NSString *artworkID) {@@ -52,31 +60,79 @@ static void setShown(NSString *artworkID) {}}+// The lock screen's last asks, as absolute times: lines are drawn ahead only while it is showing them, previews and+// clips each by their own asks, as it asks for clips far less often.+static CFAbsoluteTime sg_askedAt, sg_previewAskedAt;+// Previews drawn ahead, by artwork ID.+static NSCache<NSString *, UIImage *> *sg_previews;++static NSURL *clipFile(NSString *artworkID) {+ return [folder() URLByAppendingPathComponent:[artworkID stringByAppendingPathExtension:@"mp4"]];+}++// On sg_previewQueue.+static UIImage *previewFor(NSString *artworkID, NSString *line, NSString *next, UIImage *cover, CGSize size) {+ UIImage *preview = [sg_previews objectForKey:artworkID];+ if (preview) return preview;+ CGImageRef backdrop = copyBackdropFor(cover, size);+ CGImageRef frame = SGLyricsClipFrame(backdrop, line, next, size);+ CGImageRelease(backdrop);+ if (frame) [sg_previews setObject:(preview = [UIImage imageWithCGImage:frame]) forKey:artworkID];+ CGImageRelease(frame);+ return preview;+}++// On sg_queue. A clip drawn ahead is found on disk and handed over at once.+static BOOL writeClip(NSString *artworkID, NSString *line, NSString *next, UIImage *cover, SGLyricsClipStyle style, CGSize size) {+ NSURL *file = clipFile(artworkID);+ if ([NSFileManager.defaultManager fileExistsAtPath:file.path]) return YES;+ CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();+ CGImageRef backdrop = copyBackdropFor(cover, size);+ BOOL ok = SGLyricsClipWrite(file, backdrop, line, next, size, style);+ CGImageRelease(backdrop);+ // The first few of a launch, and any that fails: one a line would fill the log.+ static NSUInteger told;+ if (!ok || told++ < 5) SGLog(@"lock lyrics: %@ %@ in %.0f ms", artworkID, ok ? @"written" : @"not written", (CFAbsoluteTimeGetCurrent() - start) * 1000);+ return ok;+}++// The next line's preview and clip, drawn while this one shows: a line change then hands both over at once, where+// drawing them on request left the lock screen on the plain cover for half a second.+static void drawAhead(NSString *artworkID, NSString *line, NSString *next, UIImage *cover, SGLyricsClipStyle style) {+ CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();+ CGSize size = SGLyricsClipSize(SGMotionPixels());+ if (time - sg_previewAskedAt <= 15) dispatch_async(sg_previewQueue, ^{ previewFor(artworkID, line, next, cover, size); });+ if (time - sg_askedAt <= 15) dispatch_async(sg_queue, ^{ writeClip(artworkID, line, next, cover, style, size); });+}+API_AVAILABLE(ios(26.0))static MPMediaItemAnimatedArtwork *artworkFor(NSString *artworkID, NSString *line, NSString *next, UIImage *cover,SGLyricsClipStyle style) {CGSize size = SGLyricsClipSize(SGMotionPixels());return [[MPMediaItemAnimatedArtwork alloc] initWithArtworkID:artworkIDpreviewImageRequestHandler:^(CGSize wanted, void (^completion)(UIImage *)) {- dispatch_async(sg_queue, ^{- CGImageRef frame = SGLyricsClipFrame(backdropFor(cover, size), line, next, size);- completion(frame ? [UIImage imageWithCGImage:frame] : nil);- CGImageRelease(frame);+ sg_previewAskedAt = CFAbsoluteTimeGetCurrent();+ // One drawn ahead is handed over at once, as Apple asks of a preview; only a miss waits for drawing.+ UIImage *ready = [sg_previews objectForKey:artworkID];+ if (ready) {+ completion(ready);+ return;+ }+ dispatch_async(sg_previewQueue, ^{+ completion(previewFor(artworkID, line, next, cover, size));+ static NSUInteger told;+ if (told++ < 10) SGLog(@"lock lyrics: preview of %@ was not drawn ahead", artworkID);});}videoAssetFileURLRequestHandler:^(CGSize wanted, void (^completion)(NSURL *)) {+ sg_askedAt = CFAbsoluteTimeGetCurrent();dispatch_async(sg_queue, ^{// A line already sung by the time its turn comes is not drawn.if (!stillShown(artworkID)) {completion(nil);return;}- NSURL *file = [folder() URLByAppendingPathComponent:[artworkID stringByAppendingPathExtension:@"mp4"]];- CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();- BOOL ok = [NSFileManager.defaultManager fileExistsAtPath:file.path]- || SGLyricsClipWrite(file, backdropFor(cover, size), line, next, size, style);- SGLog(@"lock lyrics: %@ %@ in %.0f ms", artworkID, ok ? @"written" : @"not written", (CFAbsoluteTimeGetCurrent() - start) * 1000);- completion(ok ? file : nil);+ completion(writeClip(artworkID, line, next, cover, style, size) ? clipFile(artworkID) : nil);});}];}@@ -112,6 +168,11 @@ static void tick(void) {if (!lines) SGKaraokeRequestLyrics(trackID);// Plain text has no line being sung; the lock screen keeps the cover.if (!lines.count || SGKaraokeLinesTiming(lines) == SGKaraokeTimingNone) {+ static NSString *told;+ if (lines && ![told isEqualToString:trackID]) {+ told = trackID;+ SGLog(@"lock lyrics: %@ has no synced lines, the lock screen keeps the cover", trackID);+ }clear();return;}@@ -134,9 +195,14 @@ static void tick(void) {}// Resting, the line sung is let go and the one coming shows alone.NSString *line = resting ? nil : textAt(lines, index), *next = textAt(lines, index + 1);+ if (![sg_shownID hasPrefix:trackID]) SGLog(@"lock lyrics: lines offered for %@", trackID);setShown(artworkID);SGNowPlayingSetExtras(@"lyrics", @{MPNowPlayingInfoProperty3x4AnimatedArtwork: artworkFor(artworkID, line, next, sg_cover, style)},state.track.trackTitle);+ if (index + 1 < (NSInteger)lines.count) {+ NSString *ahead = [NSString stringWithFormat:@"%@-%ld-%ld", trackID, (long)index + 1, (long)style];+ drawAhead(ahead, textAt(lines, index + 1), textAt(lines, index + 2), sg_cover, style);+ }}}@@ -156,21 +222,39 @@ static void setTicking(BOOL on) {@end@implementation SGLyricsArtwork-// A paused player's line does not move, and the lock screen keeps the clip it was left on.+// A paused player's line does not move, and the lock screen keeps the clip it was left on. Picked or not is read+// each time, so a change on the Lock screen page applies at once.- (void)playerStateDidChange:(SPTPlayerState *)state {- setTicking(!state.isPaused);- tick();+ BOOL on = SGLockScreenArtwork() == SGLockArtworkLyrics;+ setTicking(on && !state.isPaused);+ if (on) tick();+ else clear();}@end%ctor {- if (SGLockScreenArtwork() != SGLockArtworkLyrics) return;if (@available(iOS 26.0, *)) {sg_lock = [NSObject new];sg_queue = dispatch_queue_create("spotifyglass.lockscreen.lyrics", DISPATCH_QUEUE_SERIAL);+ sg_previewQueue = dispatch_queue_create("spotifyglass.lockscreen.lyrics.preview", DISPATCH_QUEUE_SERIAL);+ sg_previews = [NSCache new];+ sg_previews.countLimit = 4;static SGLyricsArtwork *observer;observer = [SGLyricsArtwork new];SGAddPlayerStateObserver(observer);- SGLog(@"lock lyrics: on, the lock screen takes %@", MPNowPlayingInfoCenter.supportedAnimatedArtworkKeys);+ // Heard on the writing thread and handed to main without waiting (AGENTS.md).+ static SGLockArtwork choice;+ choice = SGLockScreenArtwork();+ [NSNotificationCenter.defaultCenter addObserverForName:NSUserDefaultsDidChangeNotification object:nil queue:nil+ usingBlock:^(NSNotification *note) {+ dispatch_async(dispatch_get_main_queue(), ^{+ if (SGLockScreenArtwork() == choice) return;+ choice = SGLockScreenArtwork();+ SGLog(@"lock lyrics: %@", choice == SGLockArtworkLyrics ? @"picked" : @"no longer picked");+ [observer playerStateDidChange:SGPlayerState()];+ });+ }];+ SGLog(@"lock lyrics: %@, the lock screen takes %@", choice == SGLockArtworkLyrics ? @"on" : @"off",+ MPNowPlayingInfoCenter.supportedAnimatedArtworkKeys);}}diff --git a/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m b/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.mindex c179c196..e22ffd16 100644--- a/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m+++ b/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m@@ -21,7 +21,7 @@ static const CGFloat kNextAlpha = 0.4;// plays before the lock screen loops it.static const int32_t kStillSeconds = 2;static const int32_t kLoopSeconds = 4;-static const int32_t kFPS = 24;+static const int32_t kFPS = 15; // the breath is slow; at 24 a line took about 4 s to write on the CPUstatic const CGFloat kBreath = 0.06; // how much bigger the cover grows at the top of a breathCGSize SGLyricsClipSize(CGFloat pixels) {@@ -37,6 +37,9 @@ static CGContextRef newContext(CGSize size, void *data, size_t bytesPerRow) {return context;}+// The width the cover is blurred at, before it is drawn at the clip's size.+static const CGFloat kBackdropWidth = 128;+CGImageRef SGLyricsClipBackdrop(CGImageRef cover, CGSize size) {CGContextRef context = newContext(size, NULL, 0);CGRect bounds = {CGPointZero, size};@@ -47,15 +50,19 @@ CGImageRef SGLyricsClipBackdrop(CGImageRef cover, CGSize size) {static dispatch_once_t once;// On the CPU: the lock screen asks while Spotify is in the background, where GPU work gets it killed.dispatch_once(&once, ^{ renderer = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer: @YES}]; });+ // Blurred small and drawn large: a blur this wide looks the same either way, and on the CPU at full size it+ // took 12 to 20 s for a song's first line.+ CGSize small = CGSizeMake(kBackdropWidth, round(kBackdropWidth * size.height / size.width));// Filled the way the lock screen fills the screen, the square cover's sides overflowing.CIImage *image = [CIImage imageWithCGImage:cover];- CGFloat scale = MAX(size.width / image.extent.size.width, size.height / image.extent.size.height);+ CGFloat scale = MAX(small.width / image.extent.size.width, small.height / image.extent.size.height);image = [image imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)];- image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation((size.width - image.extent.size.width) / 2,- (size.height - image.extent.size.height) / 2)];+ image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation((small.width - image.extent.size.width) / 2,+ (small.height - image.extent.size.height) / 2)];// Clamped first, so the blur does not pull the dark in from past the edges.- image = [[image imageByClampingToExtent] imageByApplyingGaussianBlurWithSigma:size.width * 0.05];- CGImageRef blurred = [renderer createCGImage:image fromRect:bounds];+ image = [[image imageByClampingToExtent] imageByApplyingGaussianBlurWithSigma:small.width * 0.05];+ CGImageRef blurred = [renderer createCGImage:image fromRect:(CGRect){CGPointZero, small}];+ CGContextSetInterpolationQuality(context, kCGInterpolationHigh);if (blurred) CGContextDrawImage(context, bounds, blurred);CGImageRelease(blurred);// Darkened, so white words read over a light cover.diff --git a/tweak/Sources/Shared/Player/NowPlayingExtras.x b/tweak/Sources/Shared/Player/NowPlayingExtras.xindex d82cff6f..fe63fdac 100644--- a/tweak/Sources/Shared/Player/NowPlayingExtras.x+++ b/tweak/Sources/Shared/Player/NowPlayingExtras.x@@ -50,7 +50,9 @@ void SGNowPlayingSetExtras(NSString *owner, NSDictionary *extras, NSString *titl@synchronized (sg_lock) {if (info[MPNowPlayingInfoPropertyElapsedPlaybackTime]) {sg_elapsed = [info[MPNowPlayingInfoPropertyElapsedPlaybackTime] doubleValue];- sg_rate = [info[MPNowPlayingInfoPropertyPlaybackRate] doubleValue];+ // A rate left out runs at 1, as LockScreenLyrics.x reads it.+ NSNumber *rate = info[MPNowPlayingInfoPropertyPlaybackRate];+ sg_rate = rate ? rate.doubleValue : 1;sg_clockAt = CFAbsoluteTimeGetCurrent();}id title = info[MPMediaItemPropertyTitle];