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

fix: Lossless puts the streaming quality back only once Spotify has loaded its settings

My-Name-Is-Jeffe4cc34b6e284
diff --git a/docs/tweaks.md b/docs/tweaks.md
index bb31420e..b790346d 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -282,6 +282,10 @@ Shared:
never touched. Checked on the Mac by `harness/audio-effects/build.sh route`.
Tested on the Mac against harness/audio-effects/ and harness/autoeq/, the hook in the simulator
against its sim/, the page against harness/audio-effects-page/
+ SpotifyPrefs/ Spotify's preferences object, the one Settings > Playback writes, captured as Spotify makes it at
+ launch, for Lossless's streaming quality. Its user's settings read 0 until they are read
+ in, about 3 s into a launch, so SGSpotifyPreferences is nil until the playback speed (a percent, never
+ 0) is set; then what Lossless left to do is done
Sing/ Karaoke (Sing in the code and its keys), a song's vocals turned down while it plays (Sing.h lists
its files): the voice model Voice model chooses (spotifyglass.sing.model, read live: Best quality,
the default, is Mel-Band RoFormer, MIT, exported by Vitrine for the Neural Engine and palettized to
diff --git a/tweak/Sources/App/About/AboutSettings.m b/tweak/Sources/App/About/AboutSettings.m
index 5aa14d2c..29aafe6a 100644
--- a/tweak/Sources/App/About/AboutSettings.m
+++ b/tweak/Sources/App/About/AboutSettings.m
@@ -2,10 +2,13 @@
#import "Settings/SGPageStyle.h"
#import "About.h"
#import "App/Onboarding/Onboarding.h"
+#import "Shared/Lossless/LosslessQuality.h"
// Every key of the mod's is under one prefix, so a reset is a sweep of the defaults with the stock
// marker of SGPrefs.h left behind; the hooks read them at launch, so it ends in a restart.
static void resetAll(void) {
+ // Spotify's own setting that Lossless changed goes back first, while the key that holds it is there.
+ SGLosslessLowerQuality(NO);
NSUserDefaults *store = NSUserDefaults.standardUserDefaults;
NSUInteger removed = 0;
for (NSString *key in [store persistentDomainForName:NSBundle.mainBundle.bundleIdentifier].allKeys) {
diff --git a/tweak/Sources/App/About/Backup.m b/tweak/Sources/App/About/Backup.m
index f09b57e6..7a815c19 100644
--- a/tweak/Sources/App/About/Backup.m
+++ b/tweak/Sources/App/About/Backup.m
@@ -14,7 +14,7 @@ static BOOL isSetting(NSString *key) {
if (![key hasPrefix:@"spotifyglass."]) return NO;
for (NSString *local in @[@"spotifyglass.adblock.counts", @"spotifyglass.privacy.counts", @"spotifyglass.update.",
@"spotifyglass.signing.", @"spotifyglass.onboarding.", @"spotifyglass.navbar.stock",
- @"spotifyglass.redesign.navbar.stock"]) {
+ @"spotifyglass.redesign.navbar.stock", @"spotifyglass.lossless.savedQuality"]) {
if ([key hasPrefix:local]) return NO;
}
return YES;
diff --git a/tweak/Sources/Shared/AdBlock/Crossfade.x b/tweak/Sources/Shared/AdBlock/Crossfade.x
index ea71958a..adc678a3 100644
--- a/tweak/Sources/Shared/AdBlock/Crossfade.x
+++ b/tweak/Sources/Shared/AdBlock/Crossfade.x
@@ -1,12 +1,9 @@
// Crossfade under Spoof Premium (EeveeSpotify's): the fade engine reads the stored crossfade switch,
// but the duration slider only writes the duration, so the fade stayed off at any length.
#import "Core/SGCore.h"
+#import "Shared/SpotifyPrefs/SpotifyPrefs.h"
#import "AdBlock.h"
-@interface _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation : NSObject
-- (void)setAudioCrossfade:(BOOL)on;
-@end
-
%hook _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation
- (void)setAudioCrossfadeTime:(NSInteger)time {
%orig;
diff --git a/tweak/Sources/Shared/Lossless/LosslessQuality.h b/tweak/Sources/Shared/Lossless/LosslessQuality.h
index daa2fca5..c50741bb 100644
--- a/tweak/Sources/Shared/Lossless/LosslessQuality.h
+++ b/tweak/Sources/Shared/Lossless/LosslessQuality.h
@@ -1,7 +1,7 @@
#import <Foundation/Foundation.h>
// Spotify's streaming quality settings, cellular (metered) and Wi-Fi (non-metered), as its own levels.
-// NO until Spotify has made its preferences object.
+// NO until Spotify has read its user's settings (SGSpotifyPreferences).
BOOL SGStreamQuality(NSInteger *metered, NSInteger *nonMetered);
BOOL SGSetStreamQuality(NSInteger metered, NSInteger nonMetered);
// Main thread. Spotify's lowest quality while `lower` (where low is not offered, Spotify plays its next lowest),
diff --git a/tweak/Sources/Shared/Lossless/LosslessQuality.x b/tweak/Sources/Shared/Lossless/LosslessQuality.x
index 85540bfa..fe495070 100644
--- a/tweak/Sources/Shared/Lossless/LosslessQuality.x
+++ b/tweak/Sources/Shared/Lossless/LosslessQuality.x
@@ -1,32 +1,14 @@
-// Spotify's streaming quality, read and set through its own preferences object (captured as Spotify makes it).
+// Spotify's streaming quality, read and set through its own preferences object (Shared/SpotifyPrefs).
#import "Core/SGCore.h"
+#import "Shared/SpotifyPrefs/SpotifyPrefs.h"
#import "LosslessQuality.h"
#import "SGLossless.h"
#define SGKeyLosslessSavedQuality @"spotifyglass.lossless.savedQuality" // [cellular, Wi-Fi] while lowered
static const NSInteger kLowest = 1;
-@interface _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation : NSObject
-- (NSInteger)audioPlayBitrate;
-- (NSInteger)audioPlayBitrateNonMetered;
-- (void)setAudioPlayBitrate:(NSInteger)level;
-- (void)setAudioPlayBitrateNonMetered:(NSInteger)level;
-@end
-
-static __weak _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation *sg_preferences;
-
-%hook _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation
-- (id)initWithScheduler:(id)scheduler {
- id made = %orig;
- sg_preferences = made;
- // Lowered when Spotify last quit, and lossless is off now: nothing else would put it back.
- if (!SGHidden(SGKeyLossless)) dispatch_async(dispatch_get_main_queue(), ^{ SGLosslessLowerQuality(NO); });
- return made;
-}
-%end
-
BOOL SGStreamQuality(NSInteger *metered, NSInteger *nonMetered) {
- __auto_type preferences = sg_preferences;
+ SPTPreferences *preferences = SGSpotifyPreferences();
if (!preferences) return NO;
*metered = preferences.audioPlayBitrate;
*nonMetered = preferences.audioPlayBitrateNonMetered;
@@ -34,7 +16,7 @@ BOOL SGStreamQuality(NSInteger *metered, NSInteger *nonMetered) {
}
BOOL SGSetStreamQuality(NSInteger metered, NSInteger nonMetered) {
- __auto_type preferences = sg_preferences;
+ SPTPreferences *preferences = SGSpotifyPreferences();
if (!preferences) return NO;
preferences.audioPlayBitrate = metered;
preferences.audioPlayBitrateNonMetered = nonMetered;
@@ -57,11 +39,3 @@ void SGLosslessLowerQuality(BOOL lower) {
SGLog(@"lossless: Spotify's quality back to %ld, %ld", (long)saved[0].integerValue, (long)saved[1].integerValue);
}
}
-
-%ctor {
- #if !SG_DRIVER
- if (!SGHidden(SGKeyLossless) && ![NSUserDefaults.standardUserDefaults arrayForKey:SGKeyLosslessSavedQuality]) return;
- #endif
- %init;
- SGRequireClasses(@[@"_TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation"]);
-}
diff --git a/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h
new file mode 100644
index 00000000..8f8ddf46
--- /dev/null
+++ b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h
@@ -0,0 +1,21 @@
+// Spotify's preferences object, the one Settings > Playback reads and writes, captured as Spotify makes it at launch
+// (SpotifyPrefs.x). Lossless lowers the streaming quality through it, and AdBlock/Crossfade.x
+// hooks it.
+#import <Foundation/Foundation.h>
+
+@interface _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation : NSObject
+- (NSInteger)audioPlayBitrate;
+- (NSInteger)audioPlayBitrateNonMetered;
+- (void)setAudioPlayBitrate:(NSInteger)level;
+- (void)setAudioPlayBitrateNonMetered:(NSInteger)level;
+- (BOOL)audioCrossfade;
+- (void)setAudioCrossfade:(BOOL)on;
+- (NSInteger)audioCrossfadeTime; // milliseconds
+- (void)setAudioCrossfadeTime:(NSInteger)milliseconds;
+- (NSInteger)playbackSpeed; // percent, 100 at normal speed
+@end
+@compatibility_alias SPTPreferences _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation;
+
+// Main thread. Nil until Spotify has made the object and read its user's settings into it, about 3 s into a launch:
+// before that every user setting reads 0, which is not the user's.
+SPTPreferences *SGSpotifyPreferences(void);
diff --git a/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x
new file mode 100644
index 00000000..a21dbc55
--- /dev/null
+++ b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x
@@ -0,0 +1,41 @@
+// Spotify's preferences object: made on Spotify's core thread at launch, and read only when Settings > Playback
+// opens, so its getters are too late to capture it.
+#import "Core/SGCore.h"
+#import "Shared/Lossless/LosslessQuality.h"
+#import "Shared/Lossless/SGLossless.h"
+#import "SpotifyPrefs.h"
+
+static __weak SPTPreferences *sg_preferences, *sg_loaded;
+
+SPTPreferences *SGSpotifyPreferences(void) {
+ SPTPreferences *preferences = sg_preferences;
+ // The playback speed is the user's one setting that is never 0, so it tells the settings loaded from not yet.
+ if (preferences && preferences != sg_loaded && preferences.playbackSpeed > 0) sg_loaded = preferences;
+ return preferences == sg_loaded ? preferences : nil;
+}
+
+// Every half second until the user's settings are in, then what a switch left to do when they were not.
+static void reconcileWhenLoaded(void) {
+ if (!sg_preferences) return;
+ if (!SGSpotifyPreferences()) {
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2), dispatch_get_main_queue(), ^{ reconcileWhenLoaded(); });
+ return;
+ }
+ SGLog(@"spotify prefs: the user's settings are in");
+ // Lowered when Spotify last quit, and lossless is off now: nothing else would put it back.
+ if (!SGHidden(SGKeyLossless)) SGLosslessLowerQuality(NO);
+}
+
+%hook _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation
+- (id)initWithScheduler:(id)scheduler {
+ id made = %orig;
+ sg_preferences = made;
+ dispatch_async(dispatch_get_main_queue(), ^{ reconcileWhenLoaded(); });
+ return made;
+}
+%end
+
+%ctor {
+ %init;
+ SGRequireClasses(@[@"_TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation"]);
+}