Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Karaoke prepares the Neural Engine in the background after an update
My-Name-Is-Jeffe6d9dc410aa0
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 9867920b..4568dfb9 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -253,7 +253,11 @@ Shared:Neural Engine copy loads only with 0.5 GB left to the process, its first load after every installcompiling it for up to ten minutes while Karaoke runs on the CPU (while it loads, the CPU's fallingbehind spends none of the 8 s after which Karaoke gives a song up, nor counts toward its three in a- row), and it is dropped for the launch, the CPU copy carrying on, once it fails to load, warm up or+ row), unless Prepare after updates (on by default) compiled it already: with the model in and the mic off, 15 s after+ Spotify comes to the front and below the thermal state Serious, the Neural Engine copy alone loads once for this+ iOS and install and is let go (22 s on a fair iPhone 15 Pro, 48-65 s on a serious one; a want meanwhile takes that+ load over), so the mic's copy loads in 0.3 s; Karaoke's status reads "On, Neural Engine" or "On, CPU", and "Off ·+ preparing for the Neural Engine" while it compiles ahead, and it is dropped for the launch, the CPU copy carrying on, once it fails to load, warm up orrun a window (one that falls behind keeps its place); Runs on is Automatic or CPU only, the GPU andNeural Engine choices stored before read as Automatic; the STFT around it on Accelerate,and an engine that stands in Speed and pitch's chain between Spotify's mixer and its output@@ -662,7 +666,7 @@ from gone through as sung to the vocals alone, with Sing along, Original and Vocfollows the head through AirPods, or sways gently without them and holds still under Reduce Motion, a line under itsaying which, then the switch; its row is on the main page too, under Karaoke's), the voice model's download (Paused andChecking among its states) and its removal, Ignore heat warnings, and under Advanced Runs on (Automatic: the Neural Engine-beside the CPU; CPU only), all applying straight away. Lock screen, on the main+beside the CPU; CPU only) and Prepare after updates, all applying straight away. Lock screen, on the mainpage under either look, opens the lock screen widget's page, titled Lock screen (Moving artwork, Lyrics or Every song, and the lyrics' style,Still or Animated, and Spotify's like and dislike buttons' flag; its podcast, audiobook and artwork flags stay inAll flags). Player: Gestures and Blocked artists (with the count on the row), which work with either look;diff --git a/tweak/Sources/Shared/Sing/SGSingLoader.h b/tweak/Sources/Shared/Sing/SGSingLoader.hindex 6ecc0b4a..25ab5ab6 100644--- a/tweak/Sources/Shared/Sing/SGSingLoader.h+++ b/tweak/Sources/Shared/Sing/SGSingLoader.h@@ -40,6 +40,12 @@ void SGSingLoaderSetChanged(void (^changed)(void));// without it, any Neural Engine copy is dropped. Another `url` than the last drops the copies of that one (and a// Failed) first. Does nothing while Failed. Call it only while Spotify is in the foreground.void SGSingLoaderWant(NSURL *url, BOOL neural);+// Compiles the Neural Engine copy of `url` ahead of the mic, with nothing else loaded or loading and the memory for it,+// then lets it go: Core ML keeps the compiled form. A Neural Engine copy wanted meanwhile takes this load over. `done`+// runs on the main thread with whether it loaded, unless the copy wanted took it over. NO when it did not start.+BOOL SGSingLoaderPrepareNeural(NSURL *url, void (^done)(BOOL loaded));+// Seconds the compile ahead of the mic has run, -1 when none runs (or the mic's copy took it over).+NSTimeInterval SGSingLoaderPreparingSeconds(void);// The model last wanted, nil before the first want.NSURL *SGSingLoaderURL(void);// The mic is off: the copies are kept for SGSingLoaderKeepSeconds, then dropped unless wanted again by then.diff --git a/tweak/Sources/Shared/Sing/SGSingLoader.m b/tweak/Sources/Shared/Sing/SGSingLoader.mindex 525b66be..1f615e3d 100644--- a/tweak/Sources/Shared/Sing/SGSingLoader.m+++ b/tweak/Sources/Shared/Sing/SGSingLoader.m@@ -22,6 +22,7 @@ static const int kStillEvery = 30;@interface SGSingLoad : NSObject@property (nonatomic) unsigned number;@property (nonatomic, copy) NSString *name; // the copy's, for the log+@property (nonatomic, copy) NSURL *url;@property (nonatomic) CFAbsoluteTime began;@property (nonatomic) BOOL finished, abandoned;@end@@ -84,6 +85,7 @@ static SGSingLoad *startLoad(NSURL *url, BOOL neural, double deadline, void (^doSGSingLoad *load = [SGSingLoad new];load.number = ++sg_attempts;load.name = neural ? @"Neural Engine" : @"CPU";+ load.url = url;load.began = CFAbsoluteTimeGetCurrent();sg_outstanding++;SGLog(@"sing: load %u, the %@ copy of %@, starts (Spotify %@, thermal state %s, %@, %@, %u loads out)", load.number, load.name,@@ -121,8 +123,31 @@ static SGSingLoad *startLoad(NSURL *url, BOOL neural, double deadline, void (^doreturn load;}+static SGSingLoad *sg_prepareLoad; // SGSingLoaderPrepareNeural's, until it comes back or is taken over++static void fastLoaded(MLModel *model) {+ sg_fastLoad = nil;+ sg_fastState = model ? SGSingFastReady : SGSingFastFailed;+ if (model) [sg_separator setFastModel:model named:@"Neural Engine"];+ changed();+}++static void fastLate(void) {+ sg_fastLoad = nil;+ sg_fastState = SGSingFastTimedOut;+ changed();+}+static void startFast(void) {if (!sg_wanted || sg_state != SGSingLoaderReady || !sg_foreground || !sg_neural || sg_fastState != SGSingFastNone) return;+ // The compile running ahead of the mic is this copy's: a second load would compile it again beside it.+ if (sg_prepareLoad && [sg_prepareLoad.url isEqual:sg_url] && !sg_prepareLoad.finished && !sg_prepareLoad.abandoned) {+ sg_fastState = SGSingFastLoading;+ sg_fastLoad = sg_prepareLoad;+ SGLog(@"sing: the Neural Engine copy is the one already compiling (load %u)", sg_fastLoad.number);+ changed();+ return;+ }if (!roomFor(kNeuralRoom)) {sg_fastState = SGSingFastSkipped;SGLog(@"sing: no Neural Engine copy: %@, it wants %.1f GB, so Sing stays on the CPU", memoryText(), kNeuralRoom / 1e9);@@ -131,18 +156,30 @@ static void startFast(void) {}sg_fastState = SGSingFastLoading;sg_fastLoad = startLoad(sg_url, YES, SGSingLoaderNeuralDeadline, ^(MLModel *model, NSError *error) {- sg_fastLoad = nil;- sg_fastState = model ? SGSingFastReady : SGSingFastFailed;- if (model) [sg_separator setFastModel:model named:@"Neural Engine"];- changed();+ fastLoaded(model);}, ^{- sg_fastLoad = nil;- sg_fastState = SGSingFastTimedOut;- changed();+ fastLate();});changed();}+BOOL SGSingLoaderPrepareNeural(NSURL *url, void (^done)(BOOL loaded)) {+ if (!url || sg_outstanding || sg_state != SGSingLoaderIdle || !sg_foreground || !roomFor(kNeuralRoom)) return NO;+ __block SGSingLoad *load = startLoad(url, YES, SGSingLoaderNeuralDeadline, ^(MLModel *model, NSError *error) {+ BOOL taken = sg_fastLoad == load;+ sg_prepareLoad = nil;+ if (taken) fastLoaded(model);+ else if (done) done(model != nil);+ }, ^{+ BOOL taken = sg_fastLoad == load;+ sg_prepareLoad = nil;+ if (taken) fastLate();+ else if (done) done(NO);+ });+ sg_prepareLoad = load;+ return YES;+}+static void startCPU(void) {if (sg_outstanding && !roomFor(kCopyRoom)) {sg_state = SGSingLoaderFailed;@@ -279,3 +316,9 @@ unsigned SGSingLoaderAttempts(void) {unsigned SGSingLoaderOutstanding(void) {return sg_outstanding;}++NSTimeInterval SGSingLoaderPreparingSeconds(void) {+ SGSingLoad *load = sg_prepareLoad;+ if (!load || load.finished || load.abandoned || sg_fastLoad == load) return -1;+ return CFAbsoluteTimeGetCurrent() - load.began;+}diff --git a/tweak/Sources/Shared/Sing/Sing.h b/tweak/Sources/Shared/Sing/Sing.hindex ad106418..4efbb97e 100644--- a/tweak/Sources/Shared/Sing/Sing.h+++ b/tweak/Sources/Shared/Sing/Sing.h@@ -39,6 +39,9 @@// stored before (2-4), and the earlier key's every value, are Automatic now, carried over at launch.#define SGKeySingComputeUnits @"spotifyglass.sing.runsOn"#define SGKeySingComputeUnitsBefore @"spotifyglass.sing.computeUnits"+// Compiles the Neural Engine copy in the background after an install or an iOS update, with the mic off; on unless+// switched off.+#define SGKeySingPrepareAhead @"spotifyglass.sing.prepareAhead"// Spatial voice: through headphones that track the head, the vocals stay in front as it turns; off until// switched on, and at once.#define SGKeySingSpatial @"spotifyglass.sing.spatial"diff --git a/tweak/Sources/Shared/Sing/Sing.x b/tweak/Sources/Shared/Sing/Sing.xindex 74ee4025..5cb17593 100644--- a/tweak/Sources/Shared/Sing/Sing.x+++ b/tweak/Sources/Shared/Sing/Sing.x@@ -201,8 +201,8 @@ static void announce(void) {// until Spotify is opened again or Runs on changes (falling behind is not a failure: that is the budget's, as on any// copy). A copy past its deadline is not remembered as failed: Core ML carries on compiling a load given up on and// keeps the result, so the next launch may load it at once.-// The iOS and the folder the Neural Engine copy last loaded on and from: Core ML has kept its compiled form, so it-// loads in seconds.+// The iOS, the app's bundle and the model's folder the Neural Engine copy last loaded on and from: Core ML has kept its+// compiled form, so it loads in seconds. An install moves the bundle (device 2026-10-07: 48-65 s again after each).static NSString *const kNeuralReadyOn = @"spotifyglass.sing.neuralReadyOn";static NSString *sg_neuralFailed; // why the Neural Engine copy is not used for the rest of this launch@@ -237,9 +237,9 @@ static BOOL neuralWanted(void) {return runsOn() != 1 && hasNeuralEngine() && !sg_neuralFailed && !SGSingModelUpdateAvailable();}-// What Core ML keeps the compiled model for: this iOS and the model's folder.+// What Core ML keeps the compiled model for: this iOS, this install and the model's folder.static NSString *neuralCompiled(void) {- return [NSString stringWithFormat:@"%@ %@", osBuild(), SGSingModelURL().path];+ return [NSString stringWithFormat:@"%@ %@ %@", osBuild(), NSBundle.mainBundle.bundlePath, SGSingModelURL().path];}static void wantModel(void) {@@ -287,6 +287,36 @@ static void loaderChanged(void) {announce();}+// After an install or an iOS update the Neural Engine copy compiles again (48-65 s on an iPhone 15 Pro), a minute in+// which the mic has only the CPU's copy, which a warm iPhone cannot keep up with. With the model in and the mic off it+// compiles ahead instead, once Spotify has been in front a while, the iPhone is below Serious and nothing else loads.+static const NSTimeInterval kPrepareAfter = 15;++static void prepareAhead(void) {+ NSString *compiled = neuralCompiled();+ if (!SGEnabled(SGKeySingPrepareAhead) || SGSingOn() || !sg_active || !neuralWanted() || SGSingModelCurrentState() != SGSingModelReady+ || [[NSUserDefaults.standardUserDefaults stringForKey:kNeuralReadyOn] isEqualToString:compiled]) return;+ if (NSProcessInfo.processInfo.thermalState >= NSProcessInfoThermalStateSerious) {+ static BOOL told;+ if (!told) SGLog(@"sing: the Neural Engine copy waits to be prepared until the iPhone is cooler (thermal state %s)", SGSingThermalName());+ told = YES;+ return;+ }+ CFAbsoluteTime began = CFAbsoluteTimeGetCurrent();+ BOOL started = SGSingLoaderPrepareNeural(SGSingModelURL(), ^(BOOL loaded) {+ if (loaded) [NSUserDefaults.standardUserDefaults setObject:compiled forKey:kNeuralReadyOn];+ SGLog(@"sing: the Neural Engine copy %@ ahead of the mic in %.0f s, and is let go (thermal state now %s)",+ loaded ? @"is prepared" : @"was not prepared", CFAbsoluteTimeGetCurrent() - began, SGSingThermalName());+ });+ if (started) SGLog(@"sing: prepares the Neural Engine copy ahead of the mic, for this install");+}++static void prepareAheadSoon(void) {+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kPrepareAfter * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ prepareAhead();+ });+}+#pragma mark - spatial voicestatic BOOL sg_spatialListening;@@ -522,7 +552,7 @@ NSString *SGSingStatusText(void) {if (SGSingModelChecking()) return @"Checking the download";if (SGSingModelWaitingForNetwork()) return SGSingModelOverCellular() ? @"Waiting for the network" : @"Waiting for Wi-Fi";return [NSString stringWithFormat:@"Downloading %.0f%%", SGSingModelProgress() * 100];- case SGSingStateOff: return @"Off";+ case SGSingStateOff: return SGSingLoaderPreparingSeconds() >= 0 ? @"Off · preparing for the Neural Engine" : @"Off";case SGSingStatePreparing: {NSTimeInterval seconds = SGSingLoaderSeconds();return seconds >= 1 ? [NSString stringWithFormat:@"Loading the voice model, %.0f s", seconds] : @"Loading the voice model";@@ -587,6 +617,12 @@ NSString *SGSingStatusDetail(void) {SGSingModelProgress() * 100, SGSingModelSizeText()];}return [NSString stringWithFormat:@"The voice model is coming in, %.0f%% of %@. Karaoke starts once it is checked.", SGSingModelProgress() * 100, SGSingModelSizeText()];+ case SGSingStateOff: {+ NSTimeInterval seconds = SGSingLoaderPreparingSeconds();+ if (seconds < 0) return nil;+ return [NSString stringWithFormat:@"After an update, the iPhone prepares the voice model for the Neural Engine once, which takes about a minute. "+ @"Vitrine does it now, in the background, so Karaoke starts at full speed when it is switched on (%.0f s so far).", seconds];+ }case SGSingStateWaiting:if (sg_resting) {return @"The vocals are as sung and Spatial voice is off, so the song plays as it is and the voice model rests. Karaoke starts again "@@ -1033,6 +1069,7 @@ static void readHeat(void) {last = thermal;// A cooler iPhone runs the model faster.if (cooler) retry([NSString stringWithFormat:@"the iPhone is cooler (%s)", SGSingThermalName()], NO);+ if (cooler) prepareAhead();// Serious and Critical hold; Fair lets it run again.BOOL hot = thermal >= NSProcessInfoThermalStateSerious;if (hot == sg_hot) return;@@ -1179,6 +1216,7 @@ static void readHeat(void) {sg_active = YES;SGSingLoaderSetForeground(YES);if (SGSingOn()) apply();+ prepareAheadSoon();}];[center addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {sg_active = NO;@@ -1210,5 +1248,6 @@ static void readHeat(void) {readHeat();readRefusal();if (SGSingOn()) apply();+ prepareAheadSoon();});}diff --git a/tweak/Sources/Shared/Sing/SingSettings.m b/tweak/Sources/Shared/Sing/SingSettings.mindex 78ec1c64..4c94e071 100644--- a/tweak/Sources/Shared/Sing/SingSettings.m+++ b/tweak/Sources/Shared/Sing/SingSettings.m@@ -200,13 +200,16 @@ UIViewController *SGSingSettingsPage(void) {units.choiceNotes = @[@"The Neural Engine, the fastest, with the CPU while it loads", @"Slower and warmer, and a little less memory"];units.choiceFooter = @"Karaoke loads the voice model on the CPU first and starts with it. On Automatic a second copy then loads on "@"the Neural Engine and takes over, in the background too. The iPhone prepares it the first time after Vitrine "- @"is installed or updated, for a minute or so, and Karaoke runs on the CPU until then. If it does not load, "+ @"is installed or updated, for a minute or so, and Karaoke runs on the CPU until then, unless Prepare after "+ @"updates did it already. If it does not load, "@"Karaoke stays on the CPU until Spotify is opened again.";units.chosen = ^(NSInteger index) { SGSingComputeUnitsChanged(); };+ SGModRow *prepare = SGSwitchRow(@"Prepare after updates",+ @"Readies the Neural Engine in the background after Vitrine or iOS updates, so Karaoke starts at full speed", SGKeySingPrepareAhead);SGSingPage *made = [[SGSingPage alloc] initWithTitle:@"Karaoke" intro:nil sections:@[SGSection(nil, @[sing, heat, model, remove]),SGSection(nil, @[spatial]),- SGNotedSection(@"Advanced", @[units],+ SGNotedSection(@"Advanced", @[units, prepare],@"Karaoke turns a song's vocals down to sing over, or the rest down to hear the vocals alone, with a voice model that "@"runs only on this iPhone: no audio leaves it. It listens a few seconds ahead of what plays, so the vocals change a "@"few seconds after a song starts or after a seek."),