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

feat: the lock screen shows each lyrics line as still or animated artwork

My-Name-Is-Jeffcd5b5d9bd311
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 68706cab..7957013b 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -96,7 +96,10 @@ Shared:
lyrics and show the card. has_lyrics is forced on for every track, the walk starts at the track change
for it and the next, and the player's card-loading timeout flag is forced to its 5 s maximum while a
source is on
- LockScreenLyrics/ the line being sung in the system's now playing
+ LockScreenLyrics/ the line being sung in the system's now playing, and on iOS 26 the lyrics as the lock screen's
+ full-screen artwork (LyricsArtwork.x): a new artwork ID per line, its 3:4 H.264 clip (the line and the
+ next 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/
Navigation/ the page transition fix (PageTransition.x) and opening a spotify: link (Links.x)
Player/ the player's open and close announced (PlayerEvents.x), what the player is doing read through
one hook for every feature that wants it (PlayerState.x), the lock screen widget's flags, and in the
@@ -263,7 +266,8 @@ The pages show only what the stored look has: a page opened after flipping the s
what the restart will bring. Then a card of parts. Navbar: the tab editor of the stored look, each with
its own list of tabs. Player: Gestures, Lyrics (the ordered list of lyrics sources, lyrics for every track,
naming the source in the redesign, the lock screen, and glass lyrics in the native look; in the redesign also
-which of the lyrics, their pronunciation and their translation is set largest, and the translation's language), Blocked artists (with the count on the row) and Lock screen widget, which work with either look;
+which of the lyrics, their pronunciation and their translation is set largest, and the translation's language), Blocked artists (with the count on the row) and Lock screen widget (on iOS 26 its full-screen artwork: Off,
+Moving artwork or Lyrics, and the lyrics' style, Still or Animated), which work with either look;
in the native look also Now playing bar (its device button and its flags), Queue & devices, and
Spotify's own player screen (artwork background, glass header buttons, Disable Canvas and the sheet,
header, slider and sticky header flags, the cards under the player and the lyrics preview and player
diff --git a/harness/lyrics-clip/README.md b/harness/lyrics-clip/README.md
new file mode 100644
index 00000000..a649634a
--- /dev/null
+++ b/harness/lyrics-clip/README.md
@@ -0,0 +1,13 @@
+# Lyrics clip harness
+
+`SGLyricsClip.m`, the clips the lock screen shows a line of lyrics in, run on the Mac. It renders a
+short line, a long one, a CJK one, a break (the next line only), the last line and an empty clip, each
+as a Still and as Animated, over a stand-in cover or the image given. Every file is read back with
+AVFoundation and checked: its length (2 s for a still, 4 s for an animated loop), its 3:4 size, H.264,
+how many frames it holds (1 or 96), white words in the middle band and none under the clock or the
+controls, and the words the same at the end as at the start. It prints how long each clip took to
+write. The first frame of each clip is saved next to it as a PNG.
+
+ ./build.sh && build/lyrics-clip [cover.jpg]
+
+The clips land in `build/clips/`.
diff --git a/harness/lyrics-clip/build.sh b/harness/lyrics-clip/build.sh
new file mode 100755
index 00000000..240e80c4
--- /dev/null
+++ b/harness/lyrics-clip/build.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+# Builds the lyrics clip harness for the Mac: SGLyricsClip.m as the tweak compiles it, and main.m.
+set -e
+cd "$(dirname "$0")"
+mkdir -p build
+xcrun clang -fobjc-arc -O2 -Wno-deprecated-declarations -I ../../tweak/Sources \
+ -framework Foundation -framework AVFoundation -framework CoreMedia -framework CoreVideo -framework CoreImage \
+ -framework CoreText -framework CoreGraphics -framework ImageIO -framework UniformTypeIdentifiers \
+ main.m ../../tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m -o build/lyrics-clip
diff --git a/harness/lyrics-clip/main.m b/harness/lyrics-clip/main.m
new file mode 100644
index 00000000..c9111528
--- /dev/null
+++ b/harness/lyrics-clip/main.m
@@ -0,0 +1,166 @@
+// Renders lines of lyrics through the tweak's own SGLyricsClip.m, both styles, and reads every file back
+// with AVFoundation: its length, its size, its frames, and that the words came out where they should.
+// Each clip's first frame is kept beside it as a PNG to look at. Exits 1 on any failure.
+#import <AVFoundation/AVFoundation.h>
+#import <ImageIO/ImageIO.h>
+#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
+#import "Shared/LockScreenLyrics/SGLyricsClip.h"
+
+static int failures;
+
+static void check(BOOL ok, NSString *what) {
+ printf(" %s %s\n", ok ? "ok " : "FAIL", what.UTF8String);
+ if (!ok) failures++;
+}
+
+// A stand-in cover: two coloured halves and a light disc, so the blur and the darkening show.
+static CGImageRef newCover(void) {
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
+ CGContextRef context = CGBitmapContextCreate(NULL, 640, 640, 8, 0, space, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
+ CGContextSetRGBFillColor(context, 0.9, 0.3, 0.2, 1);
+ CGContextFillRect(context, CGRectMake(0, 0, 640, 320));
+ CGContextSetRGBFillColor(context, 0.2, 0.4, 0.9, 1);
+ CGContextFillRect(context, CGRectMake(0, 320, 640, 320));
+ CGContextSetRGBFillColor(context, 0.95, 0.9, 0.7, 1);
+ CGContextFillEllipseInRect(context, CGRectMake(200, 200, 240, 240));
+ CGImageRef cover = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ CGColorSpaceRelease(space);
+ return cover;
+}
+
+static CGImageRef newImageAt(NSString *path) {
+ CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path], NULL);
+ if (!source) return NULL;
+ CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
+ CFRelease(source);
+ return image;
+}
+
+static void savePNG(CGImageRef image, NSURL *url) {
+ CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, (__bridge CFStringRef)UTTypePNG.identifier, 1, NULL);
+ CGImageDestinationAddImage(destination, image, NULL);
+ CGImageDestinationFinalize(destination);
+ CFRelease(destination);
+}
+
+// The share of pixels in a band of rows lighter than the backdrop's by 30 of 255 a channel or more:
+// the words, white or dimmed, and not the cover. `backdrop` NULL counts near-white pixels instead.
+static double wordsIn(CGImageRef image, CGImageRef backdrop, CGFloat fromY, CGFloat toY) {
+ size_t width = CGImageGetWidth(image), height = CGImageGetHeight(image);
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
+ CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, space, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
+ const uint8_t *pixels = CGBitmapContextGetData(context);
+ CGContextRef under = CGBitmapContextCreate(NULL, width, height, 8, width * 4, space, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
+ if (backdrop) CGContextDrawImage(under, CGRectMake(0, 0, width, height), backdrop);
+ const uint8_t *base = CGBitmapContextGetData(under);
+ size_t white = 0, all = 0;
+ // Rows counted from the top.
+ for (size_t y = (size_t)(fromY * height); y < (size_t)(toY * height); y++) {
+ for (size_t x = 0; x < width; x++) {
+ const uint8_t *p = pixels + y * width * 4 + x * 4;
+ const uint8_t *b = base + y * width * 4 + x * 4;
+ BOOL lit = backdrop ? p[0] + p[1] + p[2] > b[0] + b[1] + b[2] + 90 : p[0] > 230 && p[1] > 230 && p[2] > 230;
+ if (lit) white++;
+ all++;
+ }
+ }
+ CGContextRelease(context);
+ CGContextRelease(under);
+ CGColorSpaceRelease(space);
+ return all ? (double)white / all : 0;
+}
+
+static void verify(NSURL *url, CGImageRef backdrop, CGSize size, SGLyricsClipStyle style, BOOL hasWords) {
+ AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:@{AVURLAssetPreferPreciseDurationAndTimingKey: @YES}];
+ AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
+ double seconds = CMTimeGetSeconds(asset.duration);
+ double wanted = style == SGLyricsClipAnimated ? 4 : 2;
+ check(track != nil, @"has a video track");
+ if (!track) return;
+ check(fabs(seconds - wanted) < 0.05, [NSString stringWithFormat:@"lasts %.3f s (want %.0f)", seconds, wanted]);
+ check(CGSizeEqualToSize(track.naturalSize, size), [NSString stringWithFormat:@"is %.0fx%.0f (want %.0fx%.0f)",
+ track.naturalSize.width, track.naturalSize.height, size.width, size.height]);
+ FourCharCode codec = CMFormatDescriptionGetMediaSubType((__bridge CMFormatDescriptionRef)track.formatDescriptions.firstObject);
+ check(codec == kCMVideoCodecType_H264, @"is H.264");
+
+ AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:asset error:nil];
+ AVAssetReaderTrackOutput *output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil];
+ [reader addOutput:output];
+ [reader startReading];
+ int samples = 0;
+ CMSampleBufferRef sample;
+ while ((sample = [output copyNextSampleBuffer])) {
+ if (CMSampleBufferGetNumSamples(sample)) samples++;
+ CFRelease(sample);
+ }
+ int wantedSamples = style == SGLyricsClipAnimated ? 96 : 1;
+ check(samples == wantedSamples, [NSString stringWithFormat:@"holds %d frames (want %d)", samples, wantedSamples]);
+
+ AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
+ generator.requestedTimeToleranceBefore = generator.requestedTimeToleranceAfter = kCMTimeZero;
+ CGImageRef first = [generator copyCGImageAtTime:kCMTimeZero actualTime:NULL error:nil];
+ CGImageRef late = [generator copyCGImageAtTime:CMTimeMakeWithSeconds(wanted - 0.1, 600) actualTime:NULL error:nil];
+ check(first && late, @"frames read back at the start and the end");
+ if (first) {
+ savePNG(first, [url URLByAppendingPathExtension:@"png"]);
+ double middle = wordsIn(first, backdrop, 0.3, 0.7), top = wordsIn(first, NULL, 0, 0.15), bottom = wordsIn(first, NULL, 0.85, 1);
+ if (hasWords) check(middle > 0.005, [NSString stringWithFormat:@"words in the middle band (%.2f%% white)", middle * 100]);
+ else check(middle < 0.001, [NSString stringWithFormat:@"no words when there are none (%.2f%% white)", middle * 100]);
+ check(top < 0.001 && bottom < 0.001, @"nothing white under the clock or the controls");
+ }
+ if (first && late) {
+ // A still holds its frame to the end; it is the frame the preview image shows too.
+ double a = wordsIn(first, NULL, 0.3, 0.7), b = wordsIn(late, NULL, 0.3, 0.7);
+ check(fabs(a - b) < 0.002, [NSString stringWithFormat:@"the words hold still to the end (%.3f%% / %.3f%%)", a * 100, b * 100]);
+ }
+ CGImageRelease(first);
+ CGImageRelease(late);
+}
+
+int main(int argc, char **argv) {
+ @autoreleasepool {
+ NSString *here = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
+ NSURL *out = [NSURL fileURLWithPath:[here stringByAppendingPathComponent:@"clips"] isDirectory:YES];
+ [NSFileManager.defaultManager createDirectoryAtURL:out withIntermediateDirectories:YES attributes:nil error:nil];
+ CGImageRef cover = argc > 1 ? newImageAt([NSString stringWithUTF8String:argv[1]]) : newCover();
+ // 1206 pixels wide, an iPhone 17 Pro, at SGMotionPixels' three quarters.
+ CGSize size = SGLyricsClipSize(round(1206 * 0.75));
+ CGImageRef backdrop = SGLyricsClipBackdrop(cover, size);
+ CGImageRef plain = SGLyricsClipBackdrop(NULL, size);
+ NSArray *cases = @[
+ @[@"short", @"Hold on", @"we're almost there"],
+ @[@"long", @"And every word I never said keeps ringing out across the quiet of the night", @"until the morning finds us"],
+ @[@"cjk", @"夜空に光る星を数えながら", @"君の名前を呼んだ"],
+ @[@"break", @"", @"The first line, coming up"],
+ @[@"last", @"The very last line", @""],
+ @[@"empty", @"", @""],
+ ];
+ for (NSArray *c in cases) {
+ for (SGLyricsClipStyle style = SGLyricsClipStill; style <= SGLyricsClipAnimated; style++) {
+ NSString *name = [NSString stringWithFormat:@"%@-%@.mp4", c[0], style == SGLyricsClipStill ? @"still" : @"animated"];
+ NSURL *url = [out URLByAppendingPathComponent:name];
+ CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
+ BOOL written = SGLyricsClipWrite(url, [c[0] isEqual:@"empty"] ? plain : backdrop, c[1], c[2], size, style);
+ double ms = (CFAbsoluteTimeGetCurrent() - start) * 1000;
+ printf("%s\n", name.UTF8String);
+ check(written, [NSString stringWithFormat:@"written in %.0f ms", ms]);
+ if (!written) continue;
+ NSNumber *bytes = nil;
+ [url getResourceValue:&bytes forKey:NSURLFileSizeKey error:nil];
+ printf(" %.0f KB\n", bytes.doubleValue / 1024);
+ verify(url, [c[0] isEqual:@"empty"] ? plain : backdrop, size, style, [c[1] length] || [c[2] length]);
+ }
+ }
+ CGImageRef preview = SGLyricsClipFrame(backdrop, @"Hold on", @"we're almost there", size);
+ check(preview && CGImageGetWidth(preview) == size.width && CGImageGetHeight(preview) == size.height, @"the preview frame is the clip's size");
+ savePNG(preview, [out URLByAppendingPathComponent:@"preview.png"]);
+ CGImageRelease(preview);
+ CGImageRelease(backdrop);
+ CGImageRelease(plain);
+ CGImageRelease(cover);
+ printf("%s, clips in %s\n", failures ? "FAILED" : "all passed", out.path.UTF8String);
+ return failures ? 1 : 0;
+ }
+}
diff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h
index d0a5adb1..aa9ece43 100644
--- a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h
+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h
@@ -3,8 +3,19 @@
// as a local file, the form the system's lock screen takes.
#import <UIKit/UIKit.h>
-// Off until switched on.
+// What the lock screen fills the screen with, stored as the index of SGLockArtwork. It took over from the
+// Moving artwork switch, whose key is moved onto it once: on is Moving artwork, off is Off.
+#define SGKeyLockScreenArtwork @"spotifyglass.lockscreen.artwork"
#define SGKeyLockScreenMotion @"spotifyglass.lockscreen.motion"
+typedef NS_ENUM(NSInteger, SGLockArtwork) {
+ SGLockArtworkOff = 0,
+ SGLockArtworkMotion, // the Canvas, else Apple Music's animated cover (LockScreenMotion.x)
+ SGLockArtworkLyrics, // a clip per line of lyrics (Shared/LockScreenLyrics/LyricsArtwork.x)
+};
+// Off below iOS 26, which has no animated artwork on the lock screen.
+SGLockArtwork SGLockScreenArtwork(void);
+// How the lyrics are drawn, stored as the index of SGLyricsClipStyle (Still unless changed).
+#define SGKeyLockScreenLyricsStyle @"spotifyglass.lockscreen.lyricsStyle"
// Off until switched on: download in Low Data Mode too.
#define SGKeyMotionLowData @"spotifyglass.motion.lowdata"
@@ -34,7 +45,7 @@ void SGMotionFile(NSURL *remote, void (^done)(NSURL *file));
// The first frame of a local video. Main queue.
void SGMotionPoster(NSURL *file, void (^done)(UIImage *poster));
-// Settings: the lock screen switch and Low Data Mode.
+// Settings: what the lock screen shows, the lyrics' style and Low Data Mode.
NSArray *SGLockScreenMotionRows(void);
// Pure steps, for the harness.
diff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m
index 1e000666..288e0b44 100644
--- a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m
+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m
@@ -1,13 +1,28 @@
-// The lock screen's moving artwork and whether it downloads in Low Data Mode. iOS 26 and up only.
+// What the lock screen fills the screen with, the lyrics' style and whether the downloads run in Low Data
+// Mode. iOS 26 and up only.
#import "Core/SGCore.h"
#import "Settings/SGModPage.h"
#import "AnimatedArtwork.h"
+SGLockArtwork SGLockScreenArtwork(void) {
+ if (@available(iOS 26.0, *)) {
+ // The old switch's YES and NO read as Moving artwork and Off, the enum's first two.
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ SGMigrateKey(SGKeyLockScreenMotion, SGKeyLockScreenArtwork); });
+ return (SGLockArtwork)SGInt(SGKeyLockScreenArtwork, SGLockArtworkOff);
+ }
+ return SGLockArtworkOff;
+}
+
NSArray *SGLockScreenMotionRows(void) {
if (@available(iOS 26.0, *)) {
+ SGLockScreenArtwork(); // moves the old switch over before the row reads the new key
return @[
- SGOptionRow(@"Moving artwork", @"The track's Canvas, else Apple Music's animated cover", SGKeyLockScreenMotion),
- SGOptionRow(@"Download in Low Data Mode", @"Up to about 7 MB a song", SGKeyMotionLowData),
+ SGChoiceRow(@"Full-screen artwork", @"The Canvas or Apple Music's animated cover, or the lyrics a line at a time",
+ SGKeyLockScreenArtwork, @[@"Off", @"Moving artwork", @"Lyrics"], SGLockArtworkOff),
+ SGChoiceRow(@"Lyrics style", @"Still draws each line once; Animated breathes the cover behind it",
+ SGKeyLockScreenLyricsStyle, @[@"Still", @"Animated"], 0),
+ SGOptionRow(@"Download in Low Data Mode", @"Moving artwork, up to about 7 MB a song", SGKeyMotionLowData),
];
}
return @[];
diff --git a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x
index 4924816a..9d4ee052 100644
--- a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x
+++ b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x
@@ -60,7 +60,7 @@ static void show(NSString *uri, NSURL *file) {
@end
%ctor {
- if (!SGFlag(SGKeyLockScreenMotion, NO)) return;
+ if (SGLockScreenArtwork() != SGLockArtworkMotion) return;
if (@available(iOS 26.0, *)) {
static SGLockScreenMotion *observer;
observer = [SGLockScreenMotion new];
diff --git a/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x b/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x
new file mode 100644
index 00000000..19e4b09a
--- /dev/null
+++ b/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x
@@ -0,0 +1,175 @@
+// The lock screen's full-screen artwork as the lyrics, a clip per line (SGLyricsClip.h draws it). A timer
+// works out the line being sung and hands the lock screen a new artwork under a new ID each time it
+// changes. Nothing is drawn until the lock screen asks for that artwork's video, so lines sung with the
+// screen off cost nothing. It rides on Spotify's now playing info as an extra (NowPlayingExtras.h), like
+// the moving artwork it takes the place of.
+#import <MediaPlayer/MediaPlayer.h>
+#import "Core/SGCore.h"
+#import "SGLyricsClip.h"
+#import "Shared/AnimatedArtwork/AnimatedArtwork.h"
+#import "Shared/Lyrics/Lyrics.h"
+#import "Shared/Player/NowPlayingExtras.h"
+#import "Shared/Player/PlayerState.h"
+
+static const NSTimeInterval kTick = 0.25;
+// Past a line's sung end by this much, with the next line at least this far off, the line is let go and
+// only the next one shows, dimmed (as the artist row does in LockScreenLyrics.x).
+static const NSInteger kBreakMs = 4000;
+
+static NSTimer *sg_timer;
+static NSString *sg_track; // the track the clips are for
+static UIImage *sg_cover; // its cover, once Spotify's now playing info has one for it
+static NSString *sg_shownID; // the artwork handed over last, nil for none; read on sg_queue too
+static NSObject *sg_lock;
+static dispatch_queue_t sg_queue; // draws and writes, one at a time
+
+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) {
+ 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 BOOL stillShown(NSString *artworkID) {
+ @synchronized (sg_lock) {
+ return [artworkID isEqualToString:sg_shownID];
+ }
+}
+
+static void setShown(NSString *artworkID) {
+ @synchronized (sg_lock) {
+ sg_shownID = artworkID;
+ }
+}
+
+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:artworkID
+ previewImageRequestHandler:^(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);
+ });
+ }
+ videoAssetFileURLRequestHandler:^(CGSize wanted, void (^completion)(NSURL *)) {
+ 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);
+ });
+ }];
+}
+
+static void clear(void) {
+ if (!sg_shownID) return;
+ setShown(nil);
+ SGNowPlayingSetExtras(@"lyrics", nil, nil);
+}
+
+static NSString *textAt(NSArray<SGKaraokeLine *> *lines, NSInteger index) {
+ return index >= 0 && index < (NSInteger)lines.count ? SGKaraokeLineText(lines[index]) : nil;
+}
+
+static void tick(void) {
+ if (@available(iOS 26.0, *)) {
+ SPTPlayerState *state = SGPlayerState();
+ NSString *trackID = SGKaraokePlayingTrack();
+ if (!trackID || !state) {
+ clear();
+ return;
+ }
+ if (![trackID isEqualToString:sg_track]) {
+ sg_track = trackID;
+ sg_cover = nil;
+ // The IDs name the track, so the last track's clips are never asked for again.
+ dispatch_async(sg_queue, ^{
+ [NSFileManager.defaultManager removeItemAtURL:folder() error:nil];
+ [NSFileManager.defaultManager createDirectoryAtURL:folder() withIntermediateDirectories:YES attributes:nil error:nil];
+ });
+ }
+ NSArray<SGKaraokeLine *> *lines = SGKaraokeLinesForTrack(trackID);
+ if (!lines) SGKaraokeRequestLyrics(trackID);
+ // Plain text has no line being sung; the lock screen keeps the cover.
+ if (!lines.count || SGKaraokeLinesTiming(lines) == SGKaraokeTimingNone) {
+ clear();
+ return;
+ }
+ NSInteger position = SGKaraokePositionMs();
+ if (position < 0) return;
+ NSInteger index = SGKaraokeLeadLine(lines, position);
+ BOOL nextFarOff = index + 1 == (NSInteger)lines.count || lines[index + 1].start - position > kBreakMs;
+ BOOL resting = index < 0 || (position > SGKaraokeSungEnd(lines[index]) + kBreakMs && nextFarOff);
+ SGLyricsClipStyle style = (SGLyricsClipStyle)SGInt(SGKeyLockScreenLyricsStyle, SGLyricsClipStill);
+ NSString *artworkID = [NSString stringWithFormat:@"%@-%ld%@-%ld", trackID, (long)index, resting ? @"r" : @"", (long)style];
+ if ([artworkID isEqualToString:sg_shownID]) return;
+ // Spotify's request block for its cover, read on main and kept once it is this track's.
+ if (!sg_cover) {
+ NSDictionary *info = MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo;
+ MPMediaItemArtwork *artwork = info[MPMediaItemPropertyArtwork];
+ if ([info[MPMediaItemPropertyTitle] isEqual:state.track.trackTitle] && [artwork isKindOfClass:MPMediaItemArtwork.class]) {
+ sg_cover = [artwork imageWithSize:CGSizeMake(600, 600)];
+ }
+ }
+ // 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);
+ setShown(artworkID);
+ SGNowPlayingSetExtras(@"lyrics", @{MPNowPlayingInfoProperty3x4AnimatedArtwork: artworkFor(artworkID, line, next, sg_cover, style)},
+ state.track.trackTitle);
+ }
+}
+
+// Main thread, as everything the timer touches is.
+static void setTicking(BOOL on) {
+ if (on == (sg_timer != nil)) return;
+ if (!on) {
+ [sg_timer invalidate];
+ sg_timer = nil;
+ return;
+ }
+ sg_timer = [NSTimer timerWithTimeInterval:kTick repeats:YES block:^(NSTimer *t) { tick(); }];
+ [NSRunLoop.mainRunLoop addTimer:sg_timer forMode:NSRunLoopCommonModes];
+}
+
+@interface SGLyricsArtwork : NSObject <SGPlayerStateObserver>
+@end
+
+@implementation SGLyricsArtwork
+// A paused player's line does not move, and the lock screen keeps the clip it was left on.
+- (void)playerStateDidChange:(SPTPlayerState *)state {
+ setTicking(!state.isPaused);
+ tick();
+}
+@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);
+ static SGLyricsArtwork *observer;
+ observer = [SGLyricsArtwork new];
+ SGAddPlayerStateObserver(observer);
+ SGLog(@"lock lyrics: on, the lock screen takes %@", MPNowPlayingInfoCenter.supportedAnimatedArtworkKeys);
+ }
+}
diff --git a/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.h b/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.h
new file mode 100644
index 00000000..ef594429
--- /dev/null
+++ b/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.h
@@ -0,0 +1,19 @@
+// A line of lyrics as a short video for the lock screen's full-screen artwork (LyricsArtwork.x): the
+// line, and the next one dimmed under it, over the cover blurred. Core Graphics, Core Text, Core Image
+// and AVFoundation only, so harness/lyrics-clip renders the same files on the Mac.
+#import <CoreGraphics/CoreGraphics.h>
+#import <Foundation/Foundation.h>
+
+typedef NS_ENUM(NSInteger, SGLyricsClipStyle) {
+ SGLyricsClipStill = 0, // one frame, held
+ SGLyricsClipAnimated, // the cover breathes behind the words, in a loop
+};
+
+// The 3:4 size of a clip `pixels` wide, both sides a multiple of 16 for the encoder.
+CGSize SGLyricsClipSize(CGFloat pixels);
+// The cover blurred and darkened to fill `size`, made once per track; a dark field for a NULL cover.
+CGImageRef SGLyricsClipBackdrop(CGImageRef cover, CGSize size) CF_RETURNS_RETAINED;
+// The clip's first frame, for the lock screen's preview image. `line` or `next` may be nil.
+CGImageRef SGLyricsClipFrame(CGImageRef backdrop, NSString *line, NSString *next, CGSize size) CF_RETURNS_RETAINED;
+// Writes the clip as H.264 to `file`, replacing it, and returns once it is written; NO if it was not.
+BOOL SGLyricsClipWrite(NSURL *file, CGImageRef backdrop, NSString *line, NSString *next, CGSize size, SGLyricsClipStyle style);
diff --git a/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m b/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m
new file mode 100644
index 00000000..d8fbfa66
--- /dev/null
+++ b/tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m
@@ -0,0 +1,202 @@
+// SGLyricsClip.h says what this draws. The words are laid out once per clip and only the backdrop moves,
+// so an animated clip costs a scaled image draw and an encode a frame.
+#import <AVFoundation/AVFoundation.h>
+#import <CoreImage/CoreImage.h>
+#import <CoreText/CoreText.h>
+#import "SGLyricsClip.h"
+
+// The lock screen fills the screen with a 3:4 clip, so a phone's taller screen cuts its sides off: the
+// words keep to this middle share of the width. The centre of the words sits this far down, clear of
+// the clock above and the controls below.
+// ponytail: guessed from the screen's shape, not measured on a lock screen; tune both on a device.
+static const CGFloat kColumn = 0.56;
+static const CGFloat kCentreY = 0.5;
+// The words' size as a share of the width, made smaller down to kSmallest of it while they run taller
+// than kTallest of the height.
+static const CGFloat kFont = 0.05;
+static const CGFloat kSmallest = 0.6;
+static const CGFloat kTallest = 0.4;
+static const CGFloat kNextAlpha = 0.4;
+// A still is one frame held this long; an animated clip breathes once over kLoopSeconds, which is all it
+// 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 CGFloat kBreath = 0.06; // how much bigger the cover grows at the top of a breath
+
+CGSize SGLyricsClipSize(CGFloat pixels) {
+ CGFloat width = MAX(floor(pixels / 16) * 16, 16);
+ return CGSizeMake(width, round(width * 4 / 3 / 16) * 16);
+}
+
+static CGContextRef newContext(CGSize size, void *data, size_t bytesPerRow) {
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
+ CGContextRef context = CGBitmapContextCreate(data, (size_t)size.width, (size_t)size.height, 8, bytesPerRow, space,
+ kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
+ CGColorSpaceRelease(space);
+ return context;
+}
+
+CGImageRef SGLyricsClipBackdrop(CGImageRef cover, CGSize size) {
+ CGContextRef context = newContext(size, NULL, 0);
+ CGRect bounds = {CGPointZero, size};
+ CGContextSetRGBFillColor(context, 0.11, 0.11, 0.12, 1);
+ CGContextFillRect(context, bounds);
+ if (cover) {
+ static CIContext *renderer;
+ 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}]; });
+ // 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);
+ image = [image imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)];
+ image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation((size.width - image.extent.size.width) / 2,
+ (size.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];
+ if (blurred) CGContextDrawImage(context, bounds, blurred);
+ CGImageRelease(blurred);
+ // Darkened, so white words read over a light cover.
+ CGContextSetRGBFillColor(context, 0, 0, 0, 0.45);
+ CGContextFillRect(context, bounds);
+ }
+ CGImageRef backdrop = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ return backdrop;
+}
+
+static NSAttributedString *wordsAt(NSString *line, NSString *next, CGFloat fontSize) {
+ CTFontRef font = CTFontCreateUIFontForLanguage(kCTFontUIFontEmphasizedSystem, fontSize, NULL);
+ CGFloat spacing = fontSize * 0.6;
+ CTParagraphStyleSetting settings[] = {
+ {kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(spacing), &spacing},
+ };
+ CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, 1);
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
+ CGFloat white[] = {1, 1, 1, 1}, dim[] = {1, 1, 1, kNextAlpha};
+ CGColorRef lit = CGColorCreate(space, white), unlit = CGColorCreate(space, dim);
+ NSMutableAttributedString *words = [NSMutableAttributedString new];
+ NSDictionary *base = @{(id)kCTFontAttributeName: (__bridge id)font, (id)kCTParagraphStyleAttributeName: (__bridge id)paragraph};
+ if (line.length) {
+ NSMutableDictionary *attributes = [base mutableCopy];
+ attributes[(id)kCTForegroundColorAttributeName] = (__bridge id)lit;
+ [words appendAttributedString:[[NSAttributedString alloc] initWithString:line attributes:attributes]];
+ }
+ if (next.length) {
+ NSMutableDictionary *attributes = [base mutableCopy];
+ attributes[(id)kCTForegroundColorAttributeName] = (__bridge id)unlit;
+ NSString *text = words.length ? [@"\n" stringByAppendingString:next] : next;
+ [words appendAttributedString:[[NSAttributedString alloc] initWithString:text attributes:attributes]];
+ }
+ CFRelease(font);
+ CFRelease(paragraph);
+ CGColorRelease(lit);
+ CGColorRelease(unlit);
+ CGColorSpaceRelease(space);
+ return words;
+}
+
+// The words alone on a clear image the size of the clip, nil when there are none.
+static CGImageRef newWords(NSString *line, NSString *next, CGSize size) CF_RETURNS_RETAINED {
+ if (!line.length && !next.length) return NULL;
+ CGFloat width = round(size.width * kColumn);
+ CGFloat fontSize = size.width * kFont;
+ CTFramesetterRef setter = NULL;
+ CGSize fits = CGSizeZero;
+ // A long line is set smaller until the two lines fit; past the smallest size it runs taller.
+ for (CGFloat scale = 1; scale >= kSmallest - 0.01; scale -= 0.1) {
+ if (setter) CFRelease(setter);
+ setter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)wordsAt(line, next, fontSize * scale));
+ fits = CTFramesetterSuggestFrameSizeWithConstraints(setter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), NULL);
+ if (fits.height <= size.height * kTallest) break;
+ }
+ CGFloat height = ceil(fits.height) + 1;
+ // Core Graphics counts up from the bottom.
+ CGRect box = CGRectMake(round((size.width - width) / 2), round(size.height * (1 - kCentreY) - height / 2), width, height);
+ CGPathRef path = CGPathCreateWithRect(box, NULL);
+ CTFrameRef frame = CTFramesetterCreateFrame(setter, CFRangeMake(0, 0), path, NULL);
+ CGContextRef context = newContext(size, NULL, 0);
+ CTFrameDraw(frame, context);
+ CGImageRef words = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ CFRelease(frame);
+ CGPathRelease(path);
+ CFRelease(setter);
+ return words;
+}
+
+// A frame `phase` of the way through the loop, 0 to 1, into `context`.
+static void drawFrame(CGContextRef context, CGImageRef backdrop, CGImageRef words, CGSize size, double phase) {
+ CGFloat grow = kBreath * (1 - cos(2 * M_PI * phase)) / 2;
+ CGRect bounds = {CGPointZero, size};
+ CGContextDrawImage(context, CGRectInset(bounds, -size.width * grow / 2, -size.height * grow / 2), backdrop);
+ if (words) CGContextDrawImage(context, bounds, words);
+}
+
+CGImageRef SGLyricsClipFrame(CGImageRef backdrop, NSString *line, NSString *next, CGSize size) {
+ CGImageRef words = newWords(line, next, size);
+ CGContextRef context = newContext(size, NULL, 0);
+ drawFrame(context, backdrop, words, size, 0);
+ CGImageRef frame = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ CGImageRelease(words);
+ return frame;
+}
+
+BOOL SGLyricsClipWrite(NSURL *file, CGImageRef backdrop, NSString *line, NSString *next, CGSize size, SGLyricsClipStyle style) {
+ // Written beside the file and moved over it once whole, so a clip cut off halfway is never found.
+ NSURL *partial = [file URLByAppendingPathExtension:@"partial"];
+ [NSFileManager.defaultManager removeItemAtURL:partial error:nil];
+ AVAssetWriter *writer = [AVAssetWriter assetWriterWithURL:partial fileType:AVFileTypeMPEG4 error:nil];
+ if (!writer) return NO;
+ int32_t frames = style == SGLyricsClipAnimated ? kLoopSeconds * kFPS : 1;
+ CMTime length = CMTimeMake(style == SGLyricsClipAnimated ? kLoopSeconds : kStillSeconds, 1);
+ AVAssetWriterInput *input = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:@{
+ AVVideoCodecKey: AVVideoCodecTypeH264,
+ AVVideoWidthKey: @(size.width),
+ AVVideoHeightKey: @(size.height),
+ AVVideoCompressionPropertiesKey: @{AVVideoAverageBitRateKey: @(2000000), AVVideoMaxKeyFrameIntervalKey: @(kFPS)},
+ }];
+ AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:input
+ sourcePixelBufferAttributes:@{
+ (id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA),
+ (id)kCVPixelBufferWidthKey: @(size.width),
+ (id)kCVPixelBufferHeightKey: @(size.height),
+ }];
+ if (![writer canAddInput:input]) return NO;
+ [writer addInput:input];
+ if (![writer startWriting]) return NO;
+ [writer startSessionAtSourceTime:kCMTimeZero];
+ CGImageRef words = newWords(line, next, size);
+ BOOL ok = YES;
+ for (int32_t i = 0; i < frames && ok; i++) {
+ while (!input.readyForMoreMediaData && writer.status == AVAssetWriterStatusWriting) [NSThread sleepForTimeInterval:0.002];
+ CVPixelBufferRef buffer = NULL;
+ ok = writer.status == AVAssetWriterStatusWriting && adaptor.pixelBufferPool
+ && CVPixelBufferPoolCreatePixelBuffer(NULL, adaptor.pixelBufferPool, &buffer) == kCVReturnSuccess;
+ if (!ok) break;
+ CVPixelBufferLockBaseAddress(buffer, 0);
+ CGContextRef context = newContext(size, CVPixelBufferGetBaseAddress(buffer), CVPixelBufferGetBytesPerRow(buffer));
+ drawFrame(context, backdrop, words, size, (double)i / frames);
+ CGContextRelease(context);
+ CVPixelBufferUnlockBaseAddress(buffer, 0);
+ ok = [adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(i, kFPS)];
+ CVPixelBufferRelease(buffer);
+ }
+ CGImageRelease(words);
+ if (!ok) {
+ [writer cancelWriting];
+ return NO;
+ }
+ [input markAsFinished];
+ // The last frame, or a still's only one, is held to the end of the loop.
+ [writer endSessionAtSourceTime:length];
+ dispatch_semaphore_t written = dispatch_semaphore_create(0);
+ [writer finishWritingWithCompletionHandler:^{ dispatch_semaphore_signal(written); }];
+ dispatch_semaphore_wait(written, DISPATCH_TIME_FOREVER);
+ if (writer.status != AVAssetWriterStatusCompleted) return NO;
+ [NSFileManager.defaultManager removeItemAtURL:file error:nil];
+ return [NSFileManager.defaultManager moveItemAtURL:partial toURL:file error:nil];
+}
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
index 7beba1e1..14b84c67 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x
+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
@@ -7,6 +7,7 @@
#import "Core/SGCore.h"
#import "Lyrics.h"
#import "Shared/LockScreenLyrics/LockScreenLyrics.h"
+#import "Shared/AnimatedArtwork/AnimatedArtwork.h"
#import "Shared/LyricsSources/LyricsSources.h"
#import "Headers/SPTPlayer.h"
@@ -341,7 +342,8 @@ static void prefetch(SPTPlayerTrack *track, NSString *trackID, SPTPlayerState *s
%ctor {
// The sources that search by name learn the name from the player, so the player is caught
// whenever one is on, not only for the redesign's lyrics and the lock screen.
- if (!SGRedesignedUI() && !SGFlag(SGKeyLockScreenLyrics, NO) && !SGLyricsEnabled()) return;
+ if (!SGRedesignedUI() && !SGFlag(SGKeyLockScreenLyrics, NO) && !SGLyricsEnabled()
+ && SGLockScreenArtwork() != SGLockArtworkLyrics) return;
sg_seenTracks = [NSMutableDictionary dictionary];
sg_lyrics = [NSMutableDictionary dictionary];
sg_requested = [NSMutableSet set];
diff --git a/tweak/Sources/Shared/Player/PlayerSettings.m b/tweak/Sources/Shared/Player/PlayerSettings.m
index 7405d559..577408de 100644
--- a/tweak/Sources/Shared/Player/PlayerSettings.m
+++ b/tweak/Sources/Shared/Player/PlayerSettings.m
@@ -6,7 +6,7 @@
UIViewController *SGLockScreenWidgetPage(void) {
NSArray *motion = SGLockScreenMotionRows();
- NSArray<SGModSection *> *mod = motion.count ? @[SGSection(@"Moving artwork", motion)] : @[];
+ NSArray<SGModSection *> *mod = motion.count ? @[SGSection(@"Lock screen artwork", motion)] : @[];
return [[SGModPage alloc] initWithTitle:@"Lock screen widget" intro:SGRestartNote sections:[mod arrayByAddingObjectsFromArray:@[
SGSection(@"Controls", @[
SGFlagRow(@"Like and dislike buttons", @"ios-feature-lockscreen.like_dislike_enabled"),