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 animates every song even without moving artwork
My-Name-Is-Jeff90ec0dcc6c99
diff --git a/harness/fluid-clip/README.md b/harness/fluid-clip/README.mdnew file mode 100644index 00000000..3f79b863--- /dev/null+++ b/harness/fluid-clip/README.md@@ -0,0 +1,12 @@+# Fluid clip harness++`SGFluidClip.m`, the clip the lock screen shows for a song with no Canvas and no animated cover when+Full-screen artwork is Every song, run on the Mac. It renders the loop over a stand-in cover or the image+given, reads the file back with AVFoundation and checks it: 8 s long, 3:4 at the size the tweak asks+for, H.264, 192 frames, the field moving between the start and the middle, no jump where the loop goes+round from the last frame to the first, the cover holding still and sharp in the middle, and the preview+image the same as the first frame. It prints how long the clip took to write.++ ./build.sh && build/fluid-clip [cover.jpg]++The clip lands in `build/clips/`, with its first and middle frames as PNGs.diff --git a/harness/fluid-clip/build.sh b/harness/fluid-clip/build.shnew file mode 100755index 00000000..3d31d0fa--- /dev/null+++ b/harness/fluid-clip/build.sh@@ -0,0 +1,10 @@+#!/bin/sh+# Builds the Fluid clip harness for the Mac: SGFluidClip.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/AnimatedArtwork/SGFluidClip.m ../../tweak/Sources/Shared/LockScreenLyrics/SGLyricsClip.m \+ -o build/fluid-clipdiff --git a/harness/fluid-clip/main.m b/harness/fluid-clip/main.mnew file mode 100644index 00000000..26565817--- /dev/null+++ b/harness/fluid-clip/main.m@@ -0,0 +1,167 @@+// Renders the lock screen's Fluid artwork through the tweak's own SGFluidClip.m and reads the file back+// with AVFoundation: its length, its size, its codec and frames, that the field moves, that the loop has+// no seam, and that the cover sits sharp in the middle. The 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/AnimatedArtwork/SGFluidClip.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 in the middle.+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);+}++// RGBA bytes of an image, rows from the top.+static NSData *pixelsOf(CGImageRef image, size_t *width, size_t *height) {+ *width = CGImageGetWidth(image);+ *height = CGImageGetHeight(image);+ NSMutableData *data = [NSMutableData dataWithLength:*width * *height * 4];+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);+ CGContextRef context = CGBitmapContextCreate(data.mutableBytes, *width, *height, 8, *width * 4, space, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);+ CGContextDrawImage(context, CGRectMake(0, 0, *width, *height), image);+ CGContextRelease(context);+ CGColorSpaceRelease(space);+ return data;+}++// The mean difference of two images' channels, out of 255, over a band of rows (shares from the top) and+// columns, so the cover in the middle can be left out of what the field is judged by.+static double difference(CGImageRef a, CGImageRef b, CGFloat fromY, CGFloat toY, CGFloat fromX, CGFloat toX) {+ size_t w, h, w2, h2;+ NSData *pa = pixelsOf(a, &w, &h), *pb = pixelsOf(b, &w2, &h2);+ if (w != w2 || h != h2) return 255;+ const uint8_t *x = pa.bytes, *y = pb.bytes;+ double sum = 0;+ size_t count = 0;+ for (size_t row = (size_t)(fromY * h); row < (size_t)(toY * h); row++) {+ for (size_t column = (size_t)(fromX * w); column < (size_t)(toX * w); column++) {+ size_t i = (row * w + column) * 4;+ sum += abs(x[i] - y[i]) + abs(x[i + 1] - y[i + 1]) + abs(x[i + 2] - y[i + 2]);+ count += 3;+ }+ }+ return count ? sum / count : 255;+}++// The colour at a point, given as shares from the top left.+static void colourAt(CGImageRef image, CGFloat sx, CGFloat sy, int rgb[3]) {+ size_t w, h;+ NSData *data = pixelsOf(image, &w, &h);+ const uint8_t *p = (const uint8_t *)data.bytes + ((size_t)(sy * h) * w + (size_t)(sx * w)) * 4;+ for (int i = 0; i < 3; i++) rgb[i] = p[i];+}++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, as LockScreenMotion.x asks.+ CGSize size = SGLyricsClipSize(round(1206 * 0.75));+ NSURL *url = [out URLByAppendingPathComponent:@"fluid.mp4"];+ printf("fluid.mp4\n");+ CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();+ BOOL written = SGFluidClipWrite(url, cover, size);+ check(written, [NSString stringWithFormat:@"written in %.0f ms", (CFAbsoluteTimeGetCurrent() - start) * 1000]);+ check(!SGFluidClipWrite([out URLByAppendingPathComponent:@"none.mp4"], NULL, size), @"no cover, no clip");+ if (!written) return 1;+ NSNumber *bytes = nil;+ [url getResourceValue:&bytes forKey:NSURLFileSizeKey error:nil];+ printf(" %.0f KB\n", bytes.doubleValue / 1024);++ AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:@{AVURLAssetPreferPreciseDurationAndTimingKey: @YES}];+ AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;+ check(track != nil, @"has a video track");+ if (!track) return 1;+ double seconds = CMTimeGetSeconds(asset.duration);+ check(fabs(seconds - 8) < 0.05, [NSString stringWithFormat:@"lasts %.3f s (want 8)", seconds]);+ check(CGSizeEqualToSize(track.naturalSize, size) && fabs(size.height / size.width - 4.0 / 3) < 0.02,+ [NSString stringWithFormat:@"is %.0fx%.0f, 3:4", track.naturalSize.width, track.naturalSize.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);+ }+ check(samples == 192, [NSString stringWithFormat:@"holds %d frames (want 192, 8 s at 24)", samples]);++ AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];+ generator.requestedTimeToleranceBefore = generator.requestedTimeToleranceAfter = kCMTimeZero;+ CGImageRef first = [generator copyCGImageAtTime:kCMTimeZero actualTime:NULL error:nil];+ CGImageRef second = [generator copyCGImageAtTime:CMTimeMake(1, 24) actualTime:NULL error:nil];+ CGImageRef middle = [generator copyCGImageAtTime:CMTimeMake(96, 24) actualTime:NULL error:nil];+ CGImageRef last = [generator copyCGImageAtTime:CMTimeMake(191, 24) actualTime:NULL error:nil];+ check(first && second && middle && last, @"frames read back");+ if (first && second && middle && last) {+ savePNG(first, [url URLByAppendingPathExtension:@"png"]);+ savePNG(middle, [[out URLByAppendingPathComponent:@"fluid-middle"] URLByAppendingPathExtension:@"png"]);+ // Measured against the preview, which is the loop's start drawn exactly, over the field in the top+ // quarter, clear of the cover: H.264 alone puts a decoded frame a little over 2 of 255 off it.+ CGImageRef preview = SGFluidClipFrame(cover, size);+ double toFirst = difference(preview, first, 0, 0.25, 0, 1), toSecond = difference(preview, second, 0, 0.25, 0, 1);+ double toLast = difference(preview, last, 0, 0.25, 0, 1), toMiddle = difference(preview, middle, 0, 0.25, 0, 1);+ check(toMiddle > 4, [NSString stringWithFormat:@"the field moves over the loop (%.2f of 255 from the start to the middle)", toMiddle]);+ check(toFirst < 4 && toFirst < toMiddle / 2, [NSString stringWithFormat:@"the preview is the clip's first frame (%.2f of 255 apart)", toFirst]);+ // The last frame is one step before the start as the second is one step after it: no seam.+ check(toLast < toSecond + 0.5, [NSString stringWithFormat:@"no seam where it loops (the last frame %.2f from the start, the second %.2f)", toLast, toSecond]);+ // The cover stays where it is, sharp: its middle is the stand-in's light disc, and it does not move.+ double still = difference(first, middle, 0.38, 0.52, 0.35, 0.65);+ check(still < 2, [NSString stringWithFormat:@"the cover holds still (%.2f of 255)", still]);+ if (argc <= 1) {+ int rgb[3];+ colourAt(first, 0.5, 0.45, rgb);+ check(rgb[0] > 200 && rgb[1] > 190 && rgb[2] > 140, [NSString stringWithFormat:@"the cover's middle is drawn sharp (%d, %d, %d)", rgb[0], rgb[1], rgb[2]]);+ }+ CGImageRelease(preview);+ }+ CGImageRelease(first);+ CGImageRelease(second);+ CGImageRelease(middle);+ CGImageRelease(last);+ 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.hindex aa9ece43..bb0aa366 100644--- a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h@@ -1,4 +1,5 @@-// Moving artwork for a track: its Canvas, or Apple Music's animated album cover where the album has one.+// Moving artwork for a track: its Canvas, or Apple Music's animated album cover where the album has one,+// and for the lock screen, failing both, the cover over a moving blur of itself (SGFluidClip.h).// SGMotionCatalog.m looks covers and logos up in Apple Music's catalog. SGMotionStore.m keeps every video// as a local file, the form the system's lock screen takes.#import <UIKit/UIKit.h>@@ -11,6 +12,7 @@ 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)+ SGLockArtworkEverySong, // Moving artwork, else the cover over a moving blur of it (LockScreenMotion.x)};// Off below iOS 26, which has no animated artwork on the lock screen.SGLockArtwork SGLockScreenArtwork(void);@@ -41,6 +43,9 @@ void SGMotionSongISRC(NSString *artist, NSString *title, void (^done)(NSString *// Any remote video (a Canvas) as a local file, or nil. Main queue.void SGMotionFile(NSURL *remote, void (^done)(NSURL *file));+// Where a video the mod makes itself is kept under `key`, among the downloads, which are kept to the+// newest few. Whether it is there yet is the caller's to check. Any thread.+NSURL *SGMotionMadeFile(NSString *key);// The first frame of a local video. Main queue.void SGMotionPoster(NSURL *file, void (^done)(UIImage *poster));diff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.mindex 288e0b44..a4cca41e 100644--- a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m@@ -17,12 +17,20 @@ SGLockArtwork SGLockScreenArtwork(void) {NSArray *SGLockScreenMotionRows(void) {if (@available(iOS 26.0, *)) {SGLockScreenArtwork(); // moves the old switch over before the row reads the new key+ SGModRow *style = SGChoiceRow(@"Lyrics style", @"Still draws each line once; Animated breathes the cover behind it",+ SGKeyLockScreenLyricsStyle, @[@"Still", @"Animated"], 0);+ style.visible = ^BOOL { return SGLockScreenArtwork() == SGLockArtworkLyrics; };+ SGModRow *lowData = SGOptionRow(@"Download in Low Data Mode", @"Moving artwork, up to about 7 MB a song", SGKeyMotionLowData);+ lowData.visible = ^BOOL {+ SGLockArtwork artwork = SGLockScreenArtwork();+ return artwork == SGLockArtworkMotion || artwork == SGLockArtworkEverySong;+ };return @[- 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),+ 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",+ SGKeyLockScreenArtwork, @[@"Off", @"Moving artwork", @"Lyrics", @"Every song"], SGLockArtworkOff),+ style,+ lowData,];}return @[];diff --git a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.xindex 9d4ee052..fab2bbbc 100644--- a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x+++ b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x@@ -1,15 +1,22 @@-// The lock screen's artwork moves: the track's Canvas, else Apple Music's animated album cover. iOS 26-// takes a local video through MPMediaItemAnimatedArtwork under one of two now playing keys, 1:1 or 3:4,-// picked here by the video's own shape. It rides on Spotify's now playing info as an extra+// The lock screen's artwork moves: the track's Canvas, else Apple Music's animated album cover, and with+// Every song chosen, failing both, the cover over a moving blur of itself (SGFluidClip.h). iOS 26 takes a+// local video through MPMediaItemAnimatedArtwork under one of two now playing keys, 1:1 or 3:4, picked+// here by the video's own shape. It rides on Spotify's now playing info as an extra// (Shared/Player/NowPlayingExtras.h).+//+// The cover's clip is made only when the lock screen asks for its video, from the cover Spotify gave the+// system, and kept by the picture the track names, so an album's songs share one.#import <MediaPlayer/MediaPlayer.h>#import "Core/SGCore.h"#import "AnimatedArtwork.h"+#import "SGFluidClip.h"+#import "Shared/LockScreenLyrics/SGLyricsClip.h"#import "Shared/Player/NowPlayingExtras.h"#import "Shared/Player/PlayerState.h"static NSString *sg_track; // the URI the artwork is forstatic NSString *sg_title;+static dispatch_queue_t sg_queue; // makes the cover's clips, one at a timeAPI_AVAILABLE(ios(26.0))static void show(NSString *uri, NSURL *file) {@@ -27,6 +34,54 @@ static void show(NSString *uri, NSURL *file) {});}+// The cover Spotify handed the system for `title`, read on the main thread as LyricsArtwork.x reads it,+// then `then` on sg_queue with it, or with NULL when the info is another song's or has none.+static void withCover(NSString *title, void (^then)(CGImageRef cover)) {+ dispatch_async(dispatch_get_main_queue(), ^{+ NSDictionary *info = MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo;+ MPMediaItemArtwork *artwork = info[MPMediaItemPropertyArtwork];+ UIImage *cover = [info[MPMediaItemPropertyTitle] isEqual:title] && [artwork isKindOfClass:MPMediaItemArtwork.class]+ ? [artwork imageWithSize:CGSizeMake(600, 600)] : nil;+ dispatch_async(sg_queue, ^{ then(cover.CGImage); });+ });+}++// The picture the track names, so the songs of an album share a clip; the track itself when it names none.+static NSString *pictureOf(SPTPlayerTrack *track, NSDictionary *metadata) {+ for (NSString *field in @[@"image_xlarge_url", @"image_large_url", @"image_url"]) {+ id value = metadata[field];+ if ([value isKindOfClass:NSString.class] && [value length]) return value;+ }+ return SGURIString(track.URI);+}++API_AVAILABLE(ios(26.0))+static void showCover(NSString *uri, NSString *picture) {+ if (![uri isEqualToString:sg_track]) return;+ NSURL *file = SGMotionMadeFile([@"fluid\n" stringByAppendingString:picture]);+ NSString *title = sg_title;+ CGSize size = SGLyricsClipSize(SGMotionPixels());+ MPMediaItemAnimatedArtwork *artwork = [[MPMediaItemAnimatedArtwork alloc] initWithArtworkID:file.lastPathComponent+ previewImageRequestHandler:^(CGSize wanted, void (^completion)(UIImage *)) {+ withCover(title, ^(CGImageRef cover) {+ CGImageRef frame = SGFluidClipFrame(cover, size);+ completion(frame ? [UIImage imageWithCGImage:frame] : nil);+ CGImageRelease(frame);+ });+ }+ videoAssetFileURLRequestHandler:^(CGSize wanted, void (^completion)(NSURL *)) {+ withCover(title, ^(CGImageRef cover) {+ CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();+ BOOL ok = [NSFileManager.defaultManager fileExistsAtPath:file.path] || SGFluidClipWrite(file, cover, size);+ SGLog(@"lock motion: the cover's clip %@ %@ in %.0f ms", file.lastPathComponent, ok ? @"ready" : @"not written",+ (CFAbsoluteTimeGetCurrent() - start) * 1000);+ completion(ok ? file : nil);+ });+ }];+ SGNowPlayingSetExtras(@"motion", @{MPNowPlayingInfoProperty3x4AnimatedArtwork: artwork}, title);+ SGLog(@"lock motion: no moving artwork, the cover's clip offered as %@", file.lastPathComponent);+}+@interface SGLockScreenMotion : NSObject <SGPlayerStateObserver>@end@@ -44,8 +99,13 @@ static void show(NSString *uri, NSURL *file) {BOOL video = [type isKindOfClass:NSString.class] && [type rangeOfString:@"video" options:NSCaseInsensitiveSearch].location != NSNotFound;NSURL *canvas = video && [address isKindOfClass:NSString.class] ? [NSURL URLWithString:address] : nil;CGFloat pixels = SGMotionPixels();+ BOOL everySong = SGLockScreenArtwork() == SGLockArtworkEverySong;+ NSString *picture = pictureOf(state.track, metadata);void (^apple)(void) = ^{- SGMotionAlbumCover(artist, album, SGMotionTall, pixels, ^(NSURL *file) { show(uri, file); });+ SGMotionAlbumCover(artist, album, SGMotionTall, pixels, ^(NSURL *file) {+ if (file) show(uri, file);+ else if (everySong) showCover(uri, picture);+ });};if (!canvas) {apple();@@ -60,11 +120,14 @@ static void show(NSString *uri, NSURL *file) {@end%ctor {- if (SGLockScreenArtwork() != SGLockArtworkMotion) return;+ SGLockArtwork choice = SGLockScreenArtwork();+ if (choice != SGLockArtworkMotion && choice != SGLockArtworkEverySong) return;if (@available(iOS 26.0, *)) {+ sg_queue = dispatch_queue_create("spotifyglass.lockscreen.motion", DISPATCH_QUEUE_SERIAL);static SGLockScreenMotion *observer;observer = [SGLockScreenMotion new];SGAddPlayerStateObserver(observer);- SGLog(@"lock motion: on, the lock screen takes %@", MPNowPlayingInfoCenter.supportedAnimatedArtworkKeys);+ SGLog(@"lock motion: on%@, the lock screen takes %@", choice == SGLockArtworkEverySong ? @" for every song" : @"",+ MPNowPlayingInfoCenter.supportedAnimatedArtworkKeys);}}diff --git a/tweak/Sources/Shared/AnimatedArtwork/SGFluidClip.h b/tweak/Sources/Shared/AnimatedArtwork/SGFluidClip.hnew file mode 100644index 00000000..62061766--- /dev/null+++ b/tweak/Sources/Shared/AnimatedArtwork/SGFluidClip.h@@ -0,0 +1,13 @@+// The lock screen's artwork for a song with no moving artwork of its own (LockScreenMotion.x): its cover,+// sharp, over copies of it blurred and slowly turning, the way the redesign's Fluid field draws the player+// (Redesigned/Kit/SGRFluid.m, which Shared cannot use). A short 3:4 clip that loops without a seam: every+// copy sways and drifts through whole cycles over the loop. Core Graphics, Core Image and AVFoundation on+// the CPU only, since the lock screen asks while Spotify is in the background, so harness/fluid-clip+// renders the same files on the Mac.+#import <CoreGraphics/CoreGraphics.h>+#import <Foundation/Foundation.h>++// The clip's first frame, for the lock screen's preview image. `size` is SGLyricsClipSize's.+CGImageRef SGFluidClipFrame(CGImageRef cover, CGSize size) CF_RETURNS_RETAINED;+// Writes the loop as H.264 to `file`, replacing it, and returns once it is written; NO if it was not.+BOOL SGFluidClipWrite(NSURL *file, CGImageRef cover, CGSize size);diff --git a/tweak/Sources/Shared/AnimatedArtwork/SGFluidClip.m b/tweak/Sources/Shared/AnimatedArtwork/SGFluidClip.mnew file mode 100644index 00000000..2969c3a1--- /dev/null+++ b/tweak/Sources/Shared/AnimatedArtwork/SGFluidClip.m@@ -0,0 +1,215 @@+// SGFluidClip.h says what this draws. The blurred field is drawn small and scaled up, since it has no+// detail to lose, and the cover is drawn once with its shadow and laid on each frame, so a frame costs two+// image draws at the clip's size and an encode.+#import <AVFoundation/AVFoundation.h>+#import <CoreImage/CoreImage.h>+#import "SGFluidClip.h"++// Each copy, as in SGRFluid.m: its side as a share of the clip's longer side, its centre as shares of the+// clip, its opacity, and its resting angle. Over the loop it sways by kSway either side of that angle and+// its centre circles kDrift of the clip, a quarter cycle after the copy before it.+static const struct { CGFloat side, x, y, opacity, angle; } kCopy[] = {+ {2.2, 0.25, 0.20, 1.0, 0.0},+ {2.0, 0.80, 0.35, 0.7, 1.1},+ {2.4, 0.30, 0.75, 0.7, 2.3},+ {1.8, 0.70, 0.85, 0.7, 4.0},+};+enum { kCopies = sizeof(kCopy) / sizeof(kCopy[0]) };+static const CGFloat kSway = 0.35, kDrift = 0.06;+// The blurred cover is small, as in SGRFluid.m, and the field is drawn at this fraction of the clip's size.+static const CGFloat kBlurSide = 128, kBlurRadius = 10, kSaturation = 1.5, kBrightness = -0.08, kFieldScale = 0.125;+// The shade over the bottom, where the lock screen's controls are.+static const CGFloat kShadeFrom = 0.55, kShadeAlpha = 0.45;+// The cover's side as a share of the clip's width, and its centre's height from the top. The lock screen+// cuts a 3:4 clip's sides off on a phone's taller screen, so the cover keeps to the middle.+// ponytail: guessed from the screen's shape, as SGLyricsClip.m's column is; tune both on a device.+static const CGFloat kCoverSide = 0.5, kCoverY = 0.45, kCoverRadius = 0.035, kShadowBlur = 0.08;+// An eight second loop: long enough that the sway reads as slow, short enough to write in a few seconds.+static const int32_t kLoopSeconds = 8, kFPS = 24;++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;+}++// The cover small, blurred and saturated, as SGRFluid.m makes it.+static CGImageRef newBlurred(CGImageRef cover) CF_RETURNS_RETAINED {+ 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}]; });+ CIImage *input = [CIImage imageWithCGImage:cover];+ CGFloat scale = kBlurSide / MAX(input.extent.size.width, input.extent.size.height);+ input = [input imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)];+ CGRect extent = input.extent;+ CIImage *output = [[[input imageByClampingToExtent]+ imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey: @(kBlurRadius)}]+ imageByApplyingFilter:@"CIColorControls" withInputParameters:@{kCIInputSaturationKey: @(kSaturation), kCIInputBrightnessKey: @(kBrightness)}];+ return [renderer createCGImage:[output imageByCroppingToRect:extent] fromRect:extent];+}++// Where the cover's picture goes with its shadow around it: the frame of the image newCover makes.+static CGRect coverFrame(CGSize size) {+ CGFloat side = round(size.width * kCoverSide), margin = ceil(side * kShadowBlur * 1.5);+ CGFloat x = round((size.width - side) / 2), top = round(size.height * kCoverY - side / 2);+ // Core Graphics counts up from the bottom.+ return CGRectMake(x - margin, size.height - top - side - margin, side + 2 * margin, side + 2 * margin);+}++// The cover with its rounded corners and a soft shadow, on a clear image the size of coverFrame.+static CGImageRef newCover(CGImageRef cover, CGSize size) CF_RETURNS_RETAINED {+ CGRect frame = coverFrame(size);+ CGFloat side = round(size.width * kCoverSide), margin = (frame.size.width - side) / 2;+ CGContextRef context = newContext(frame.size, NULL, 0);+ CGRect picture = CGRectMake(margin, margin, side, side);+ CGPathRef rounded = CGPathCreateWithRoundedRect(picture, side * kCoverRadius, side * kCoverRadius, NULL);+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);+ CGFloat shade[] = {0, 0, 0, 0.5};+ CGColorRef shadow = CGColorCreate(space, shade);+ CGContextSaveGState(context);+ CGContextSetShadowWithColor(context, CGSizeMake(0, -side * 0.02), side * kShadowBlur, shadow);+ CGContextAddPath(context, rounded);+ CGContextSetRGBFillColor(context, 0, 0, 0, 1);+ CGContextFillPath(context);+ CGContextRestoreGState(context);+ CGContextAddPath(context, rounded);+ CGContextClip(context);+ CGContextSetInterpolationQuality(context, kCGInterpolationHigh);+ // Filled, should the cover not be square.+ CGFloat width = CGImageGetWidth(cover), height = CGImageGetHeight(cover);+ CGFloat scale = MAX(side / MAX(width, 1), side / MAX(height, 1));+ CGContextDrawImage(context, CGRectMake(margin + (side - width * scale) / 2, margin + (side - height * scale) / 2, width * scale, height * scale), cover);+ CGImageRef image = CGBitmapContextCreateImage(context);+ CGContextRelease(context);+ CGPathRelease(rounded);+ CGColorRelease(shadow);+ CGColorSpaceRelease(space);+ return image;+}++typedef struct {+ CGImageRef blurred, cover;+ CGContextRef field; // the small canvas the field is drawn on+ CGSize fieldSize;+ CGGradientRef shade;+} Parts;++static Parts makeParts(CGImageRef cover, CGSize size) {+ Parts parts = {0};+ parts.blurred = newBlurred(cover);+ parts.cover = newCover(cover, size);+ parts.fieldSize = CGSizeMake(ceil(size.width * kFieldScale), ceil(size.height * kFieldScale));+ parts.field = newContext(parts.fieldSize, NULL, 0);+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);+ CGFloat colors[] = {0, 0, 0, 0, 0, 0, 0, kShadeAlpha}, locations[] = {0, 1};+ parts.shade = CGGradientCreateWithColorComponents(space, colors, locations, 2);+ CGColorSpaceRelease(space);+ return parts;+}++static void freeParts(Parts parts) {+ CGImageRelease(parts.blurred);+ CGImageRelease(parts.cover);+ CGContextRelease(parts.field);+ CGGradientRelease(parts.shade);+}++// A frame `phase` of the way through the loop, 0 to 1, into `context`.+static void drawFrame(CGContextRef context, Parts parts, CGSize size, double phase) {+ CGContextRef field = parts.field;+ CGSize small = parts.fieldSize;+ CGFloat longer = MAX(small.width, small.height);+ CGContextSetRGBFillColor(field, 0, 0, 0, 1);+ CGContextFillRect(field, (CGRect){CGPointZero, small});+ CGContextSetInterpolationQuality(field, kCGInterpolationMedium);+ for (int i = 0; i < kCopies; i++) {+ double turn = 2 * M_PI * phase + i * M_PI_2;+ CGFloat side = longer * kCopy[i].side;+ CGFloat x = small.width * (kCopy[i].x + kDrift * cos(turn)), y = small.height * (1 - kCopy[i].y + kDrift * sin(turn));+ CGContextSaveGState(field);+ CGContextSetAlpha(field, kCopy[i].opacity);+ CGContextTranslateCTM(field, x, y);+ CGContextRotateCTM(field, kCopy[i].angle + (i % 2 ? -kSway : kSway) * sin(turn));+ CGContextDrawImage(field, CGRectMake(-side / 2, -side / 2, side, side), parts.blurred);+ CGContextRestoreGState(field);+ }+ // From kShadeFrom of the way down to the bottom, which Core Graphics counts up from.+ CGContextDrawLinearGradient(field, parts.shade, CGPointMake(0, small.height * (1 - kShadeFrom)), CGPointZero,+ kCGGradientDrawsAfterEndLocation);+ CGImageRef drawn = CGBitmapContextCreateImage(field);+ CGContextSetInterpolationQuality(context, kCGInterpolationHigh);+ CGContextDrawImage(context, (CGRect){CGPointZero, size}, drawn);+ CGImageRelease(drawn);+ CGContextDrawImage(context, coverFrame(size), parts.cover);+}++CGImageRef SGFluidClipFrame(CGImageRef cover, CGSize size) {+ if (!cover) return NULL;+ Parts parts = makeParts(cover, size);+ CGContextRef context = newContext(size, NULL, 0);+ drawFrame(context, parts, size, 0);+ CGImageRef frame = CGBitmapContextCreateImage(context);+ CGContextRelease(context);+ freeParts(parts);+ return frame;+}++// ponytail: the writing loop is SGLyricsClip.m's again, kept apart so neither clip's drawing leaks into the+// other's; one writer taking a draw block is the step up if a third clip comes.+BOOL SGFluidClipWrite(NSURL *file, CGImageRef cover, CGSize size) {+ if (!cover) return NO;+ // 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 = kLoopSeconds * kFPS;+ 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];+ Parts parts = makeParts(cover, size);+ BOOL ok = parts.blurred && parts.cover;+ 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, parts, size, (double)i / frames);+ CGContextRelease(context);+ CVPixelBufferUnlockBaseAddress(buffer, 0);+ ok = [adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(i, kFPS)];+ CVPixelBufferRelease(buffer);+ }+ freeParts(parts);+ if (!ok) {+ [writer cancelWriting];+ return NO;+ }+ [input markAsFinished];+ [writer endSessionAtSourceTime:CMTimeMake(kLoopSeconds, 1)];+ 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/AnimatedArtwork/SGMotionStore.m b/tweak/Sources/Shared/AnimatedArtwork/SGMotionStore.mindex c1f7b2f7..0cb2688d 100644--- a/tweak/Sources/Shared/AnimatedArtwork/SGMotionStore.m+++ b/tweak/Sources/Shared/AnimatedArtwork/SGMotionStore.m@@ -1,5 +1,6 @@-// Remote videos kept as local files in Caches/Vitrine/Motion, named by a hash of their address. A video-// asked for again while it downloads joins the download in flight. The folder keeps the newest kFiles.+// Remote videos kept as local files in Caches/Vitrine/Motion, named by a hash of their address, and the+// videos the mod makes, named by a hash of their key. A video asked for again while it downloads joins the+// download in flight. The folder keeps the newest kFiles.#import <AVFoundation/AVFoundation.h>#import <CommonCrypto/CommonDigest.h>#import "Core/SGCore.h"@@ -14,8 +15,8 @@ static NSURL *folder(void) {return url;}-static NSURL *localFor(NSURL *remote) {- NSData *address = [remote.absoluteString dataUsingEncoding:NSUTF8StringEncoding];+static NSURL *localFor(NSString *key) {+ NSData *address = [key dataUsingEncoding:NSUTF8StringEncoding];unsigned char digest[CC_SHA1_DIGEST_LENGTH];CC_SHA1(address.bytes, (CC_LONG)address.length, digest);NSMutableString *name = [NSMutableString string];@@ -41,6 +42,11 @@ CGFloat SGMotionPixels(void) {return round(UIScreen.mainScreen.nativeBounds.size.width * 0.75);}+NSURL *SGMotionMadeFile(NSString *key) {+ trim();+ return localFor([@"made\n" stringByAppendingString:key]);+}+static NSMutableDictionary<NSURL *, NSMutableArray *> *sg_waiting;void SGMotionFile(NSURL *remote, void (^done)(NSURL *file)) {@@ -48,7 +54,7 @@ void SGMotionFile(NSURL *remote, void (^done)(NSURL *file)) {done(nil);return;}- NSURL *local = localFor(remote);+ NSURL *local = localFor(remote.absoluteString);if ([NSFileManager.defaultManager fileExistsAtPath:local.path]) {done(local);return;