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: AutoMix blends songs with Spotify's own Automix and crossfade (#41)

My-Name-Is-Jeffbe40239e4814
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 591607a3..dd2a266b 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -282,10 +282,19 @@ 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/
+ AutoMix/ Spotify's own Automix and Crossfade, which its player core does, set from one page (AutoMix.h lists
+ its files): Style picks Beat-matched (Automix and crossfade on) or Crossfade (Automix off), Blend
+ length is Spotify's crossfade time (1 to 12 s, kept in milliseconds as Spotify does). Every write goes
+ through Spotify's preferences object (SpotifyPrefs/), so Settings > Playback shows the same values,
+ and the player takes a change at the next track change, with no restart. Turning the switch on saves
+ Spotify's three settings; turning it off puts them back, and so does Reset all settings. A switch
+ flipped before Spotify has read its settings takes effect once it has. While the switch
+ is on, one log line per track change (the probe) gives the current and next track's automix.* and md_*
+ metadata and the settings
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
+ launch, for Lossless's streaming quality and AutoMix. 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
+ 0) is set; then what Lossless and AutoMix 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
@@ -748,7 +757,7 @@ one page, the mod's own rows first and Spotify's flags below them or on a sub pa
they change. Its main page groups the rows by what they are, in cards with no headings as Settings has its own,
under the signing and environment warnings: Redesigned UI, Appearance and Tab bar, with a restart note; Home (Home &
Library in the native look), Player, Lyrics, Albums and Artists; Karaoke, Spatial voice (where the iPhone reads headphone
-motion), Audio effects, Live Activity, AirPods gestures and Listening stats; Lock screen, Vibrations and Premium, ads &
+motion), Audio effects, AutoMix, Live Activity, AirPods gestures and Listening stats; Lock screen, Vibrations and Premium, ads &
privacy; Labs and All flags; Mod. It is checked in the simulator
against harness/mod-settings/. The Appearance page has, in the native look, AMOLED (the redesign is always black), then the stored look's Accent color
preset, a pull-down of Spotify, Apple Music and Custom read off the color stored (a color set before the presets
diff --git a/harness/mod-settings/stubs.m b/harness/mod-settings/stubs.m
index ecd60bc2..5a7d3ae1 100644
--- a/harness/mod-settings/stubs.m
+++ b/harness/mod-settings/stubs.m
@@ -11,7 +11,7 @@ static UIViewController *blank(NSString *title) {
}
#define PAGE(name) UIViewController *name(void) { return blank(@#name); }
-PAGE(SGAboutPage) PAGE(SGAdsSettingsPage) PAGE(SGAllFlagsPage) PAGE(SGArtistBlockSettingsPage) PAGE(SGDSPSettingsPage)
+PAGE(SGAboutPage) PAGE(SGAdsSettingsPage) PAGE(SGAllFlagsPage) PAGE(SGArtistBlockSettingsPage) PAGE(SGDSPSettingsPage) PAGE(SGAutoMixSettingsPage)
PAGE(SGGesturesSettingsPage) PAGE(SGHeadGesturesSettingsPage) PAGE(SGHomeSettingsPage) PAGE(SGLabsPage)
PAGE(SGListeningStatsPage) PAGE(SGLiveActivitySettingsPage) PAGE(SGLockScreenWidgetPage) PAGE(SGNavbarSettingsPage)
PAGE(SGNowPlayingBarSettingsPage) PAGE(SGQueueSettingsPage) PAGE(SGRAlbumSettingsPage) PAGE(SGRArtistSettingsPage)
@@ -31,6 +31,7 @@ NSArray<SGModSection *> *SGNativePlayerScreenSections(void) { return @[]; }
NSArray<NSDictionary *> *SGBlockedArtists(void) { return @[]; }
NSString *SGDSPSummary(void) { return @"Off"; }
+NSString *SGAutoMixSummary(void) { return @"Off"; }
NSString *SGSingSummary(void) { return @"Off"; }
// An iPhone that reads headphone motion, with Spatial voice off.
BOOL SGSingSpatialAvailable(void) { return YES; }
diff --git a/tweak/Sources/App/About/AboutSettings.m b/tweak/Sources/App/About/AboutSettings.m
index 29aafe6a..cae8f5f2 100644
--- a/tweak/Sources/App/About/AboutSettings.m
+++ b/tweak/Sources/App/About/AboutSettings.m
@@ -2,12 +2,14 @@
#import "Settings/SGPageStyle.h"
#import "About.h"
#import "App/Onboarding/Onboarding.h"
+#import "Shared/AutoMix/AutoMix.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.
+ // Spotify's own settings that AutoMix and Lossless changed go back first, while the keys that hold them are there.
+ SGAutoMixApply(NO);
SGLosslessLowerQuality(NO);
NSUserDefaults *store = NSUserDefaults.standardUserDefaults;
NSUInteger removed = 0;
diff --git a/tweak/Sources/App/About/Backup.m b/tweak/Sources/App/About/Backup.m
index 7a815c19..526419d1 100644
--- a/tweak/Sources/App/About/Backup.m
+++ b/tweak/Sources/App/About/Backup.m
@@ -14,7 +14,8 @@ 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.lossless.savedQuality"]) {
+ @"spotifyglass.redesign.navbar.stock", @"spotifyglass.automix.saved",
+ @"spotifyglass.lossless.savedQuality"]) {
if ([key hasPrefix:local]) return NO;
}
return YES;
diff --git a/tweak/Sources/App/ModSettings.x b/tweak/Sources/App/ModSettings.x
index 402250b4..4953fef3 100644
--- a/tweak/Sources/App/ModSettings.x
+++ b/tweak/Sources/App/ModSettings.x
@@ -2,7 +2,7 @@
// grouped by what they are as the system Settings app groups its own, in cards with no headings: what the
// app looks like (Redesigned UI's switch, Appearance, Tab bar), the screens a page of its own changes
// (Home & Library in the native look, Player, Lyrics, and Albums and Artists in the redesign), the features
-// that work under either look (Sing, Spatial voice, Audio effects, Live Activity, AirPods gestures, Listening
+// that work under either look (Sing, Spatial voice, Audio effects, AutoMix, Live Activity, AirPods gestures, Listening
// stats), the system's side (Lock screen, Vibrations; Premium, ads & privacy), the advanced pages (Labs, All flags, a
// searchable list of every flag with an override per flag) and Mod, the build, its updates and links. The
// same row leads the side drawer's list (trees/test6.txt), above Your plan, so the page is a tap from
@@ -23,6 +23,7 @@
#import "Shared/AdBlock/AdBlock.h"
#import "Shared/Flags/Flags.h"
#import "Shared/AudioEffects/AudioEffectsPage.h"
+#import "Shared/AutoMix/AutoMix.h"
#import "Shared/LiveActivity/LiveActivity.h"
#import "Shared/ListeningStats/ListeningStats.h"
#import "Shared/HeadGestures/HeadGestures.h"
@@ -92,7 +93,10 @@ static UIViewController *modSettingsPage(void) {
// Spatial voice is Sing's too, and shows where the iPhone reads headphone motion, as on Sing's page.
SGModRow *spatial = pageRow(@"Spatial voice", @"person.wave.2.fill", SGColorRGB(0x5856D6), ^UIViewController *{ return SGSpatialVoiceSettingsPage(); });
spatial.visible = ^BOOL { return SGSingSpatialAvailable(); };
- NSMutableArray<SGModRow *> *features = [NSMutableArray arrayWithObjects:sing, spatial, audioEffects, nil];
+ // AutoMix sets Spotify's own Automix and Crossfade, which its player does under either look.
+ SGModRow *autoMix = pageRow(@"AutoMix", @"arrow.triangle.merge", SGColorRGB(0xF05D6C), ^UIViewController *{ return SGAutoMixSettingsPage(); });
+ autoMix.value = ^NSString *{ return SGAutoMixSummary(); };
+ NSMutableArray<SGModRow *> *features = [NSMutableArray arrayWithObjects:sing, spatial, audioEffects, autoMix, nil];
// The Live Activity works under both looks, where ActivityKit's card does.
if (@available(iOS 17.0, *)) {
SGModRow *liveActivity = pageRow(@"Live Activity", @"iphone", UIColor.systemOrangeColor, ^UIViewController *{ return SGLiveActivitySettingsPage(); });
diff --git a/tweak/Sources/Diagnostics/Diagnostics.h b/tweak/Sources/Diagnostics/Diagnostics.h
index f75c9ec6..b98d135b 100644
--- a/tweak/Sources/Diagnostics/Diagnostics.h
+++ b/tweak/Sources/Diagnostics/Diagnostics.h
@@ -25,4 +25,6 @@ void SGSetPlayerSpeed(double speed);
void SGSetSingOn(BOOL on);
// Defined in Shared/Lossless/LosslessProbe.m: lossless anchor --path P, check, stop. Nil with *error on failure.
NSDictionary *SGLosslessProbe(NSString *verb, NSDictionary *p, NSString **error);
+// Defined in Shared/AutoMix/AutoMix.x: Spotify's Automix and Crossfade settings, read or written.
+NSDictionary *SGAutoMixProbeCommand(NSDictionary *p, NSString **error);
#endif
diff --git a/tweak/Sources/Diagnostics/Driver.m b/tweak/Sources/Diagnostics/Driver.m
index 2011d670..713339ae 100644
--- a/tweak/Sources/Diagnostics/Driver.m
+++ b/tweak/Sources/Diagnostics/Driver.m
@@ -832,6 +832,13 @@ static NSDictionary<NSString *, SGCommand> *commands(void) {
if (!result) fail(@"%@", error);
return result;
},
+ // Spotify's Automix and Crossfade settings: automix [--automix 1] [--crossfade 1] [--ms 5000].
+ @"automix" : ^NSDictionary *(NSDictionary *p) {
+ NSString *error;
+ NSDictionary *result = SGAutoMixProbeCommand(p, &error);
+ if (!result) fail(@"%@", error);
+ return result;
+ },
// Karaoke on or off: sing 1, sing 0.
@"sing" : ^NSDictionary *(NSDictionary *p) {
BOOL on = [(p[@"value"] ?: p[@"arg"]) boolValue];
diff --git a/tweak/Sources/Shared/AutoMix/AutoMix.h b/tweak/Sources/Shared/AutoMix/AutoMix.h
new file mode 100644
index 00000000..1808fef7
--- /dev/null
+++ b/tweak/Sources/Shared/AutoMix/AutoMix.h
@@ -0,0 +1,45 @@
+// AutoMix (Mod Settings > AutoMix), under either look: Spotify's own Automix and Crossfade, which its player core
+// does by itself, set from one page. Every write goes through Spotify's preferences object, so Settings >
+// Playback reads the same values. The mod keeps only what Spotify has no setting for: the switch, the style to
+// apply while it is off, and the settings it found when it was turned on.
+//
+// Turning the switch off puts Spotify's three settings back as they were when it was turned on. While it is on,
+// both pages write the same object, so a change made in Spotify's Playback page counts as AutoMix's and goes too.
+//
+// AutoMix.x Spotify's product state, captured; the switch's writes, through Shared/SpotifyPrefs;
+// the probe, one log line per track change while the switch is on
+// AutoMixSettings.m its page
+#import <UIKit/UIKit.h>
+
+#define SGKeyAutoMix @"spotifyglass.automix"
+// SGAutoMixStyle, applied as the switch goes on and kept while it is off.
+#define SGKeyAutoMixStyle @"spotifyglass.automix.style"
+// Spotify's [automix, crossfade, crossfade time] from before the switch went on; set only while it is on.
+#define SGKeyAutoMixSaved @"spotifyglass.automix.saved"
+
+typedef NS_ENUM(NSInteger, SGAutoMixStyle) { SGAutoMixBeatMatched, SGAutoMixCrossfade };
+
+// Spotify's own playback settings, read and written through its preferences object.
+typedef struct {
+ BOOL automix;
+ BOOL crossfade;
+ NSInteger milliseconds; // the crossfade's length; Spotify's slider moves in whole seconds
+} SGTransitionSettings;
+
+// NO until Spotify has read its user's settings, a few seconds into the launch (SGSpotifyPreferences).
+BOOL SGTransitionSettingsRead(SGTransitionSettings *settings);
+BOOL SGTransitionSettingsWrite(SGTransitionSettings settings);
+
+// The switch's work: saves Spotify's settings and applies the style, or puts them back.
+void SGAutoMixApply(BOOL on);
+// Main thread, once Spotify's settings are read: SGAutoMixApply for a switch flipped before they were.
+void SGAutoMixReconcile(void);
+// The style in effect: Spotify's Automix while the switch is on, the stored one while it is off.
+SGAutoMixStyle SGAutoMixCurrentStyle(void);
+void SGAutoMixSetStyle(SGAutoMixStyle style);
+void SGAutoMixSetSeconds(NSInteger seconds);
+// YES on a Premium account, or a Free one under Spoof Premium; YES too before the product state is known.
+BOOL SGAutoMixAccountAllows(void);
+
+UIViewController *SGAutoMixSettingsPage(void);
+NSString *SGAutoMixSummary(void); // On or Off, beside the root page's row
diff --git a/tweak/Sources/Shared/AutoMix/AutoMix.x b/tweak/Sources/Shared/AutoMix/AutoMix.x
new file mode 100644
index 00000000..e5b71f82
--- /dev/null
+++ b/tweak/Sources/Shared/AutoMix/AutoMix.x
@@ -0,0 +1,241 @@
+// AutoMix's side of Spotify: its settings, through the preferences object Shared/SpotifyPrefs captures, its product
+// state, captured as Spotify makes it, and the probe.
+#import "Core/SGCore.h"
+#import "Shared/AdBlock/AdBlock.h"
+#import "Shared/Player/PlayerState.h"
+#import "Shared/SpotifyPrefs/SpotifyPrefs.h"
+#import "AutoMix.h"
+
+@interface SPTCoreProductState : NSObject
+- (NSString *)stringForKey:(NSString *)key;
+@end
+
+static __weak SPTCoreProductState *sg_productState;
+
+// Made once per session, as the account is known. Its argument is the core's C++ object, not an Objective-C one.
+%hook SPTCoreProductState
+- (id)initWithCoreProductState:(void *)state {
+ id made = %orig;
+ sg_productState = made;
+ return made;
+}
+%end
+
+BOOL SGTransitionSettingsRead(SGTransitionSettings *settings) {
+ SPTPreferences *preferences = SGSpotifyPreferences();
+ if (!preferences) return NO;
+ *settings = (SGTransitionSettings){preferences.audioAutomix, preferences.audioCrossfade, preferences.audioCrossfadeTime};
+ return YES;
+}
+
+BOOL SGTransitionSettingsWrite(SGTransitionSettings settings) {
+ SPTPreferences *preferences = SGSpotifyPreferences();
+ if (!preferences) return NO;
+ // The time first: under Spoof Premium Crossfade.x turns crossfade on or off with it.
+ preferences.audioCrossfadeTime = settings.milliseconds;
+ preferences.audioCrossfade = settings.crossfade;
+ preferences.audioAutomix = settings.automix;
+ return YES;
+}
+
+static NSString *describe(SGTransitionSettings s) {
+ return [NSString stringWithFormat:@"automix %d, crossfade %d, %ld ms", s.automix, s.crossfade, (long)s.milliseconds];
+}
+
+#pragma mark - the switch
+
+// Spotify's own default. A stored time of 0 would blend nothing; Spotify's slider never stores one (at 0 it turns
+// crossfade off and keeps the time), but another client or an older version might.
+static const NSInteger kDefaultMilliseconds = 5000;
+
+// Both styles keep crossfade on: Beat-matched falls back to the blend length on a song with no computed transition.
+// Called only once the settings have been read, so the write cannot fail.
+static void applyStyle(SGAutoMixStyle style, NSInteger milliseconds) {
+ SGTransitionSettings s = {style == SGAutoMixBeatMatched, YES, milliseconds > 0 ? milliseconds : kDefaultMilliseconds};
+ SGTransitionSettingsWrite(s);
+ SGLog(@"automix: Spotify set to %@", describe(s));
+}
+
+static void startProbe(void);
+
+void SGAutoMixApply(BOOL on) {
+ NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
+ SGTransitionSettings now;
+ if (!SGTransitionSettingsRead(&now)) {
+ SGLog(@"automix: Spotify's settings not read yet, applied once they are");
+ return;
+ }
+ NSArray<NSNumber *> *saved = [defaults arrayForKey:SGKeyAutoMixSaved];
+ if (on) {
+ if (saved.count != 3) {
+ [defaults setObject:@[@(now.automix), @(now.crossfade), @(now.milliseconds)] forKey:SGKeyAutoMixSaved];
+ SGLog(@"automix: on, Spotify was %@", describe(now));
+ }
+ applyStyle(SGInt(SGKeyAutoMixStyle, SGAutoMixBeatMatched), now.milliseconds);
+ startProbe();
+ } else if (saved.count == 3) {
+ SGTransitionSettings before = {saved[0].boolValue, saved[1].boolValue, saved[2].integerValue};
+ SGTransitionSettingsWrite(before);
+ [defaults removeObjectForKey:SGKeyAutoMixSaved];
+ SGLog(@"automix: off, Spotify back to %@", describe(before));
+ }
+}
+
+// The saved array is there exactly while the switch has taken effect, so a mismatch is a switch flipped before
+// Spotify's settings were read. Reapplying an applied switch would undo a change made since in Settings > Playback.
+void SGAutoMixReconcile(void) {
+ BOOL on = SGFlag(SGKeyAutoMix, NO);
+ if (on != ([NSUserDefaults.standardUserDefaults arrayForKey:SGKeyAutoMixSaved].count == 3)) SGAutoMixApply(on);
+}
+
+SGAutoMixStyle SGAutoMixCurrentStyle(void) {
+ SGTransitionSettings s;
+ if (SGFlag(SGKeyAutoMix, NO) && SGTransitionSettingsRead(&s)) return s.automix ? SGAutoMixBeatMatched : SGAutoMixCrossfade;
+ return SGInt(SGKeyAutoMixStyle, SGAutoMixBeatMatched);
+}
+
+void SGAutoMixSetStyle(SGAutoMixStyle style) {
+ SGSetInt(SGKeyAutoMixStyle, style);
+ SGTransitionSettings s;
+ if (SGFlag(SGKeyAutoMix, NO) && SGTransitionSettingsRead(&s)) applyStyle(style, s.milliseconds);
+}
+
+void SGAutoMixSetSeconds(NSInteger seconds) {
+ SGTransitionSettings s;
+ if (!SGTransitionSettingsRead(&s)) {
+ SGLog(@"automix: Spotify's settings not read yet, %ld s not written", (long)seconds);
+ return;
+ }
+ s.milliseconds = seconds * 1000;
+ if (SGFlag(SGKeyAutoMix, NO)) s.crossfade = YES;
+ SGTransitionSettingsWrite(s);
+}
+
+BOOL SGAutoMixAccountAllows(void) {
+ if (SGHidden(SGKeyFakePremium)) return YES;
+ NSString *type = [sg_productState stringForKey:@"type"];
+ return !type || [type isEqualToString:@"premium"];
+}
+
+NSString *SGAutoMixSummary(void) {
+ return SGFlag(SGKeyAutoMix, NO) ? @"On" : @"Off";
+}
+
+#pragma mark - the probe
+
+static NSString *automixKeys(id track) {
+ if (![track isKindOfClass:NSClassFromString(@"SPTPlayerTrack")]) return @"no track";
+ NSDictionary *metadata = ((SPTPlayerTrack *)track).metadata;
+ if (![metadata isKindOfClass:NSDictionary.class]) return @"no metadata";
+ NSMutableArray<NSString *> *found = [NSMutableArray array];
+ for (NSString *key in [metadata.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
+ if (![key isKindOfClass:NSString.class] || !([key hasPrefix:@"automix."] || [key hasPrefix:@"md_"])) continue;
+ NSString *value = [metadata[key] description];
+ if (value.length > 160) value = [[value substringToIndex:160] stringByAppendingString:@"…"];
+ [found addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
+ }
+ return [NSString stringWithFormat:@"%lu keys, {%@}", (unsigned long)metadata.count, [found componentsJoinedByString:@", "]];
+}
+
+static NSString *trackLine(id track) {
+ if (![track isKindOfClass:NSClassFromString(@"SPTPlayerTrack")]) return @"none";
+ SPTPlayerTrack *t = track;
+ NSDictionary *metadata = t.metadata;
+ return [NSString stringWithFormat:@"%@ \"%@\" album \"%@\" provider %@, %@", SGURIString(t.URI), t.trackTitle ?: metadata[@"title"],
+ metadata[@"album_title"], t.provider, automixKeys(t)];
+}
+
+// The previous state, kept from every report so the line can say how much of the old track was left at the change:
+// about the blend length when the core overlapped them, about 0 when it did not.
+@interface SGAutoMixProbe : NSObject <SGPlayerStateObserver>
+@end
+
+@implementation SGAutoMixProbe {
+ NSString *_uri;
+ NSUInteger _metadataCount;
+ SPTPlayerState *_last;
+}
+
+static NSUInteger metadataCount(id track) {
+ if (![track isKindOfClass:NSClassFromString(@"SPTPlayerTrack")]) return 0;
+ NSDictionary *metadata = ((SPTPlayerTrack *)track).metadata;
+ return [metadata isKindOfClass:NSDictionary.class] ? metadata.count : 0;
+}
+
+- (void)playerStateDidReport:(SPTPlayerState *)state {
+ if (!SGFlag(SGKeyAutoMix, NO)) return;
+ NSString *uri = SGURIString(state.track.URI);
+ id next = state.future.firstObject;
+ // Counted rather than formatted, since this runs on every report; a new count is metadata that came after its track.
+ NSUInteger count = metadataCount(state.track) + metadataCount(next);
+ if (uri && ![uri isEqualToString:_uri]) {
+ SGTransitionSettings s;
+ NSString *settings = SGTransitionSettingsRead(&s) ? describe(s) : @"not read yet";
+ double left = 0;
+ if (_last) {
+ // Where the old track stood at this report's time, not at now, which comes later.
+ double elapsed = [state.timestamp timeIntervalSinceDate:_last.timestamp] * (_last.isPaused ? 0 : _last.playbackSpeed);
+ left = MAX(0, _last.duration - (_last.positionAsOfTimestamp + elapsed));
+ }
+ SGLog(@"automix probe: change, %.2f s of the last track left; context %@; now %@; next %@; Spotify %@", left,
+ SGURIString(state.contextURI), trackLine(state.track), trackLine(next), settings);
+ } else if (uri && count != _metadataCount) {
+ SGLog(@"automix probe: update; now %@; next %@", trackLine(state.track), trackLine(next));
+ }
+ _uri = uri;
+ _metadataCount = count;
+ _last = state;
+}
+
+- (void)playerStateDidChange:(SPTPlayerState *)state {
+}
+
+@end
+
+static void startProbe(void) {
+ static SGAutoMixProbe *probe;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ probe = [SGAutoMixProbe new];
+ SGAddPlayerStateObserver(probe);
+ });
+}
+
+#pragma mark - driver
+
+#if SG_DRIVER
+#import "Diagnostics/Diagnostics.h"
+// automix: Spotify's three settings and the product state; --automix, --crossfade and --ms write Spotify's.
+NSDictionary *SGAutoMixProbeCommand(NSDictionary *p, NSString **error) {
+ __block NSDictionary *result;
+ dispatch_sync(dispatch_get_main_queue(), ^{
+ SGTransitionSettings s;
+ if (!SGTransitionSettingsRead(&s)) return;
+ if (p[@"automix"]) s.automix = [p[@"automix"] boolValue];
+ if (p[@"crossfade"]) s.crossfade = [p[@"crossfade"] boolValue];
+ if (p[@"ms"]) s.milliseconds = [p[@"ms"] integerValue];
+ if (p[@"automix"] || p[@"crossfade"] || p[@"ms"]) SGTransitionSettingsWrite(s);
+ SGTransitionSettingsRead(&s);
+ id next = SGPlayerState().future.firstObject;
+ result = @{
+ @"automix" : @(s.automix), @"crossfade" : @(s.crossfade), @"ms" : @(s.milliseconds),
+ @"type" : [sg_productState stringForKey:@"type"] ?: @"unknown",
+ @"catalogue" : [sg_productState stringForKey:@"catalogue"] ?: @"unknown",
+ @"switch" : @(SGFlag(SGKeyAutoMix, NO)), @"style" : @(SGAutoMixCurrentStyle()),
+ @"saved" : [NSUserDefaults.standardUserDefaults arrayForKey:SGKeyAutoMixSaved] ?: @[],
+ @"allows" : @(SGAutoMixAccountAllows()),
+ // Every metadata key of the playing track and the next, to look for what a transition carries.
+ @"now" : SGPlayerState().track.metadata ?: @{},
+ @"next" : ([next isKindOfClass:NSClassFromString(@"SPTPlayerTrack")] ? ((SPTPlayerTrack *)next).metadata : nil) ?: @{},
+ };
+ });
+ if (!result) *error = @"Spotify's settings are not read yet";
+ return result;
+}
+#endif
+
+%ctor {
+ %init;
+ SGRequireClasses(@[@"SPTCoreProductState"]);
+ if (SGFlag(SGKeyAutoMix, NO)) startProbe();
+}
diff --git a/tweak/Sources/Shared/AutoMix/AutoMixSettings.m b/tweak/Sources/Shared/AutoMix/AutoMixSettings.m
new file mode 100644
index 00000000..ff33835b
--- /dev/null
+++ b/tweak/Sources/Shared/AutoMix/AutoMixSettings.m
@@ -0,0 +1,33 @@
+// The AutoMix page (App/ModSettings.x links it from the root, under either look): the switch, then the style and
+// the blend length, which read Spotify's own settings and gray out while the switch is off.
+// It has no restart note: Spotify's player takes a new setting at the next track change.
+#import "Core/SGCore.h"
+#import "Settings/SGModPage.h"
+#import "AutoMix.h"
+
+// Spotify's own crossfade slider (Settings > Playback) runs from 0, which is off and is the switch here, to 12 s.
+static const double kShortest = 1, kLongest = 12;
+
+UIViewController *SGAutoMixSettingsPage(void) {
+ SGModRow *toggle = SGOptionRow(@"AutoMix", nil, SGKeyAutoMix);
+ toggle.changed = ^(BOOL on) { SGAutoMixApply(on); };
+
+ NSArray<NSString *> *styles = @[@"Beat-matched", @"Crossfade"];
+ SGModRow *style = SGMenuRow(@"Style", styles, ^NSString *{ return styles[(NSUInteger)SGAutoMixCurrentStyle()]; },
+ ^(NSInteger index) { SGAutoMixSetStyle(index); });
+ style.waitsOn = SGKeyAutoMix;
+
+ SGModRow *blend = SGSliderRow(@"Blend length", nil, kShortest, kLongest, 1,
+ ^double {
+ SGTransitionSettings s;
+ return SGTransitionSettingsRead(&s) ? MAX(kShortest, MIN(kLongest, s.milliseconds / 1000.0)) : 5;
+ },
+ ^(double value) { SGAutoMixSetSeconds(lround(value)); },
+ ^NSString *(double value) { return [NSString stringWithFormat:@"%ld s", lround(value)]; });
+ blend.waitsOn = SGKeyAutoMix;
+
+ NSString *note = SGAutoMixAccountAllows() ? nil : @"AutoMix needs Premium, or Spoof Premium under Premium, ads & privacy.";
+ return [[SGModPage alloc] initWithTitle:@"AutoMix" intro:nil sections:@[
+ SGNotedSection(nil, @[toggle, style, blend], note),
+ ] footer:nil];
+}
diff --git a/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h
index 8f8ddf46..1edaad9b 100644
--- a/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h
+++ b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.h
@@ -1,6 +1,6 @@
// 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.
+// (SpotifyPrefs.x). Lossless lowers the streaming quality through it, AutoMix sets Automix and Crossfade, and
+// AdBlock/Crossfade.x hooks it.
#import <Foundation/Foundation.h>
@interface _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation : NSObject
@@ -8,6 +8,8 @@
- (NSInteger)audioPlayBitrateNonMetered;
- (void)setAudioPlayBitrate:(NSInteger)level;
- (void)setAudioPlayBitrateNonMetered:(NSInteger)level;
+- (BOOL)audioAutomix;
+- (void)setAudioAutomix:(BOOL)on;
- (BOOL)audioCrossfade;
- (void)setAudioCrossfade:(BOOL)on;
- (NSInteger)audioCrossfadeTime; // milliseconds
diff --git a/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x
index a21dbc55..adfe8b81 100644
--- a/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x
+++ b/tweak/Sources/Shared/SpotifyPrefs/SpotifyPrefs.x
@@ -1,6 +1,8 @@
// 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.
+// opens, so its getters are too late to capture it. Captured in every launch, since AutoMix's switch takes effect
+// without a restart.
#import "Core/SGCore.h"
+#import "Shared/AutoMix/AutoMix.h"
#import "Shared/Lossless/LosslessQuality.h"
#import "Shared/Lossless/SGLossless.h"
#import "SpotifyPrefs.h"
@@ -24,6 +26,7 @@ static void reconcileWhenLoaded(void) {
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);
+ SGAutoMixReconcile();
}
%hook _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation