Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: a headphone correction can follow its output
My-Name-Is-Jeff9ca3200e55f4
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 778f6b2c..d459e04a 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -194,7 +194,11 @@ Shared:settings from before the page's first pick saved as one of them, Before presets, and AutoEq'scorrections, its INDEX.md fetched from GitHub and cached a month in Caches/Vitrine/AutoEq, aheadphone's GraphicEQ file put into the Graphic EQ (AutoEq.m), None taking it off; a pick turns the- master switch on.+ master switch on. A pick can be kept per output (spotifyglass.audioOutputs, by the port's UID): on+ every AVAudioSessionRouteChangeNotification to another output (AudioEffects.x), a remembered output+ gets its correction or None, and any other output takes off a correction that came on for one; a+ pick made with nothing remembered and a curve of the user's own stay, and the master switch is+ 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 simulatoragainst its sim/, the page against harness/audio-effects-page/Sing/ Sing, a song's vocals turned down while it plays (Sing.h lists its files): the voice model (Mel-Band@@ -496,7 +500,8 @@ row saying so whose tap opens its ⓘ, all applying straight away. Live Activityshows, Lyrics, Queue or Control menu, and for Lyrics its Translations (off until switched on) and Textsize (Small, Medium or Large), all applying straight away, the row reading out the view or Off. Audio effects, in either look (Shared/AudioEffects/AudioEffectsPage.m):the effects' switch with what the engine is doing under it, Presets (built-in ones and your own, saved, loaded-and deleted there) and Headphones (AutoEq's corrections, searched and applied to the Graphic EQ), either of which+and deleted there) and Headphones (AutoEq's corrections, searched and applied to the Graphic EQ, with Use for <the+output playing now> keeping the pick for that output and the remembered outputs listed, a swipe removing one), either of whichturns the effects on, then a card per effect, each opening out into its sliders, choices, curve or file librarywhile its switch is on (Reverb has its Room and its Amount, the amount the player's ⋯ menu also sets), everythingapplying as it changes; the row reads out Off, On or how many effects are on. Home & Library, in the native look only:diff --git a/harness/audio-effects-page/README.md b/harness/audio-effects-page/README.mdindex 8c55c3b5..723b0158 100644--- a/harness/audio-effects-page/README.md+++ b/harness/audio-effects-page/README.md@@ -25,6 +25,13 @@ equalizer's curve the cascade's own response through its handles, the width showthe stored 60), long choice names moving under the title at 375pt, an error row in the engine's wording, thecredits under the reset; the values stored under the right keys, snapped to their steps.+`output=<uid>:<name>` hands the page a route change to that output, the way AudioEffects.x does, and+`push=headphones` opens the Headphones page, with Use for <output> and the remembered outputs over AutoEq's list. `remember`+switches Use for <output> on, and `search=<text>` types into the page's search field (`search=` puts the keyboard+away and keeps the text). 2026-10-06, iPhone 17 Pro on iOS 27.0: the field sits right over None and the list, under+the remembered outputs, and lines up with the cards; typing keeps the keyboard up, leaves only the matches (or No+headphones match) under the field, and keeps them after the keyboard goes.+`save=<name>` stores a preset of the user's behind the page's back and `rows` logs each visible row with its tickand VoiceOver value. 2026-10-05, iPhone 17 Pro on iOS 27.0: the Presets page's first pick saves Before presets atthe top of Yours and a tap on it puts the settings back; a swipe delete above the ticked preset keeps the tick ondiff --git a/harness/audio-effects-page/main.m b/harness/audio-effects-page/main.mindex 9415dbd6..a7c93774 100644--- a/harness/audio-effects-page/main.m+++ b/harness/audio-effects-page/main.m@@ -11,14 +11,16 @@// Actions: scroll=<y>, section=<n> (that card at the top), toggle=<n> (flips card n's switch the way a tap// does), drag=<band>:<gain> (the equalizer's band mid-drag, the bubble up), release (lets it go),// preset=<i>, reset=eq, select=<section>.<row> (a tap on a row of the page on top), push=convolver|ddc|-// liveprog|geq, import=<name> (a file of that name picked in the document picker), paste=<text> (the+// liveprog|geq|headphones, output=<uid>:<name> (audio going to that output, as a route change hands it over),+// import=<name> (a file of that name picked in the document picker), paste=<text> (the// GraphicEQ editor's Paste with that on the clipboard), slide=<section>.<row>:<value> (a slider dragged// there), swipe=<section>.<row>:<n> and band=<band>:<n> (VoiceOver's swipe on a slider or an equalizer band,// n times), string=<key after spotifyglass.dsp.>:<text> (stored behind the page's back, as the engine's// errors turn up), pop, delete=<row> (the library's swipe to delete on that file), hit (which band a finger// takes around each handle, to the log), confirm (the alert's destructive button), dump (the stored dsp keys and the first sections' frames to the log),// save=<name> (the settings saved as a preset of the user's, behind the page's back), rows (each visible row's-// title, whether it is ticked and what VoiceOver reads as its value, to the log).+// title, whether it is ticked and what VoiceOver reads as its value, to the log), remember (Headphones' Use for+// <output> switched on), search=<text> (the search field on the page typed into; * only taps it; empty puts the keyboard away).// Without keep, the user's presets are cleared too.#import <UIKit/UIKit.h>#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>@@ -161,8 +163,31 @@ static void findViews(UIView *root, Class kind, NSMutableArray *found) {} else if ([verb isEqualToString:@"push"]) {NSDictionary<NSString *, NSNumber *> *kinds = @{@"convolver": @(SGDSPFileImpulseResponse), @"ddc": @(SGDSPFileDDC), @"liveprog": @(SGDSPFileLiveprog)};UIViewController *page = kinds[value] ? SGDSPLibraryPage(kinds[value].integerValue)- : [value isEqualToString:@"reference"] ? [self referencePage] : SGDSPGraphicEqPage();+ : [value isEqualToString:@"reference"] ? [self referencePage]+ : [value isEqualToString:@"headphones"] ? SGDSPHeadphonesPage() : SGDSPGraphicEqPage();[self.nav pushViewController:page animated:NO];+ } else if ([verb isEqualToString:@"output"]) {+ // output=<uid>:<name>, audio going to that output the way AudioEffects.x hands a route change over.+ NSRange colon = [value rangeOfString:@":"];+ SGAutoEqOutputChanged([value substringToIndex:colon.location], [value substringFromIndex:NSMaxRange(colon)]);+ } else if ([verb isEqualToString:@"remember"]) {+ // remember: Headphones' Use for <output> switched on for the output playing now.+ SGAutoEqRememberOutput(YES);+ } else if ([verb isEqualToString:@"search"]) {+ // search=<text>: the page's search field tapped and that typed into it (empty: the keyboard put away).+ NSMutableArray<UISearchBar *> *bars = [NSMutableArray array];+ findViews(table, UISearchBar.class, bars);+ UISearchBar *bar = bars.firstObject;+ if ([value isEqualToString:@"*"]) {+ [bar becomeFirstResponder];+ } else if (value.length) {+ [bar becomeFirstResponder];+ bar.text = value;+ [bar.delegate searchBar:bar textDidChange:value];+ } else {+ [bar resignFirstResponder];+ }+ NSLog(@"[harness] search field holds \"%@\", %@", bar.text, bar.isFirstResponder ? @"keyboard up" : @"keyboard down");} else if ([verb isEqualToString:@"import"]) {NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:value];[@"imported" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];diff --git a/harness/audio-effects/README.md b/harness/audio-effects/README.mdindex 3afd538f..c8131b80 100644--- a/harness/audio-effects/README.md+++ b/harness/audio-effects/README.md@@ -30,6 +30,10 @@ and 471 in turn, 512, and a mix down to single frames), and each check prints a./build.sh thread && build/audio-effects-thread <song> <out dir>./build.sh address && build/audio-effects-address <song> <out dir>+`./build.sh route && build/route-check` checks the corrections remembered per output (AutoEq.m) through the real+settings, a route change being a direct call: the speaker, a pair remembered with a pick, another with None, a pick+made with nothing remembered, a hand-made curve, the effects left off, forgetting and renaming.+A last word `stress` runs the convolver, the threaded checks, rate changes and Liveprog only.## The hook in the simulator (`sim/main.m`)diff --git a/harness/audio-effects/build-sim.sh b/harness/audio-effects/build-sim.shindex 1fa38fff..e8dc71d9 100755--- a/harness/audio-effects/build-sim.sh+++ b/harness/audio-effects/build-sim.sh@@ -19,7 +19,7 @@ mkdir -p "$OUT/gen" "$OUT/AudioEffectsHarness.app"SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g -O1 -isysroot "$SDK" -Wno-deprecated-declarations \-I"$SRC" -I"$FX" -I"$SRC/Shared/Player" -isystem "$VENDOR/libbs2b" -isystem "$VENDOR/wdl/eel2" \- sim/main.m ../scene.m "$OUT/gen/AudioEffects.m" "$OUT/gen/SpeedPitch.m" "$SRC/Shared/Player/SGTimePitch.m" "$FX/AudioEffectsSettings.m" "$FX/AudioEffectsFiles.m" "$FX"/SGDSPEngine.m "$FX"/SGDSPFilters.m \+ sim/main.m ../scene.m "$OUT/gen/AudioEffects.m" "$OUT/gen/SpeedPitch.m" "$SRC/Shared/Player/SGTimePitch.m" "$FX/AudioEffectsSettings.m" "$FX/AudioEffectsFiles.m" "$FX/AudioEffectsPresets.m" "$FX/AutoEq.m" "$FX"/SGDSPEngine.m "$FX"/SGDSPFilters.m \"$FX"/SGDSPConvolver.m "$FX"/SGDSPTone.m "$FX"/SGDSPDynamics.m "$FX"/SGDSPCrossfeed.m "$FX"/SGDSPReverb.m "$FX"/SGDSPLiveprog.m \"$SRC/Core/SGRebind.m" "$SRC/Core/SGLog.m" "$SRC/Core/SGPrefs.m" "$SRC/Core/SGUIMode.m" "$SRC/Core/SGFlagForce.m" "$VENDOR/build/sim/libsgaudio.a" \-framework UIKit -framework AudioToolbox -framework AVFoundation -framework Accelerate -framework Foundation \diff --git a/harness/audio-effects/build.sh b/harness/audio-effects/build.shindex 4316baa1..0f86aa04 100755--- a/harness/audio-effects/build.sh+++ b/harness/audio-effects/build.sh@@ -4,8 +4,19 @@# ./build.sh build/audio-effects# ./build.sh thread build/audio-effects-thread, under ThreadSanitizer# ./build.sh address build/audio-effects-address, under AddressSanitizer+# ./build.sh route build/route-check, the corrections remembered per output (route.m)set -ecd "$(dirname "$0")"+if [ "$1" = route ]; then+ SRC=../../tweak/Sources+ FX=$SRC/Shared/AudioEffects+ mkdir -p build+ xcrun clang -fobjc-arc -O0 -g -Wall -Werror -target arm64-apple-macos13.0 -I ../autoeq/shim -I "$SRC" -I "$FX" \+ route.m "$FX"/AudioEffectsPresets.m "$FX"/AutoEq.m "$FX"/AudioEffectsSettings.m "$SRC"/Core/SGPrefs.m \+ -framework Foundation -o build/route-check+ echo "built build/route-check"+ exit+fiVENDOR=../../vendor/audioENGINE=../../tweak/Sources/Shared/AudioEffectsSANITIZE=$1diff --git a/harness/audio-effects/route.m b/harness/audio-effects/route.mnew file mode 100644index 00000000..3c6f3f24--- /dev/null+++ b/harness/audio-effects/route.m@@ -0,0 +1,130 @@+// Checks the corrections remembered per output (AutoEq.m) on the Mac, through the real settings: what a route+// change to each kind of output does to the Graphic EQ, with AVAudioSession's part played by direct calls.+//+// ./build.sh route && build/route-check+//+// Every line prints ok or FAIL; the exit status is the number of failures. The settings live in the check's+// own defaults domain, cleared before and after.+#import <Foundation/Foundation.h>+#import "AudioEffects.h"+#import "AudioEffectsApply.h"+#import "AudioEffectsPresets.h"++static int failures;++static void check(BOOL ok, NSString *what) {+ printf("%s %s\n", ok ? "ok " : "FAIL", what.UTF8String);+ failures += !ok;+}++// The engine is not here.+void SGDSPApply(NSString *effect) {+}++static void clearDefaults(void) {+ NSUserDefaults *store = NSUserDefaults.standardUserDefaults;+ for (NSString *key in store.dictionaryRepresentation) {+ if ([key hasPrefix:@"spotifyglass."]) [store removeObjectForKey:key];+ }+}++static NSString *const kAirPods = @"AA-BB-tacl", *const kSony = @"CC-DD-tacl", *const kSpeaker = @"Speaker";+static NSString *const kLine = @"GraphicEQ: 20 -1.0; 1000 0.0; 20000 2.0";+static NSString *const kSonyLine = @"GraphicEQ: 20 3.0; 1000 0.0; 20000 -2.0";++// What the Graphic EQ plays: the headphone's folder, or "" for nothing; "off" when its switch is off.+static NSString *playing(void) {+ if (!SGDSPSwitch(SGKeyDSPGraphicEq)) return @"off";+ return SGDSPString(SGKeyDSPGraphicEqHeadphone);+}++int main(void) {+ @autoreleasepool {+ clearDefaults();++ // Launch on the speaker: nothing remembered, nothing changes.+ SGAutoEqOutputChanged(kSpeaker, @"Speaker");+ check([playing() isEqualToString:@"off"], @"launch on the speaker: the Graphic EQ stays off");++ // AirPods connect, the user picks a correction and turns Use for AirPods on.+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ SGAutoEqApplyText(@"crinacle/AirPods%20Pro", kLine);+ check(!SGAutoEqOutputRemembered(kAirPods), @"a pick alone remembers nothing");+ SGAutoEqRememberOutput(YES);+ check(SGAutoEqOutputRemembered(kAirPods), @"Use for AirPods Pro remembers it");+ check(SGDSPSwitch(SGKeyDSP), @"the pick turned Audio effects on");++ // Back to the speaker: the AirPods' correction goes off.+ SGAutoEqOutputChanged(kSpeaker, @"Speaker");+ check([playing() isEqualToString:@"off"], [NSString stringWithFormat:@"speaker: the AirPods' correction is off (%@)", playing()]);+ check([SGDSPString(SGKeyDSPGraphicEqHeadphone) isEqualToString:@""], @"speaker: no headphone named");++ // AirPods again: their correction comes back, nodes and all, with no download.+ SGDSPSetString(SGKeyDSPGraphicEqNodes, @"GraphicEQ: 20 0.0");+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ check([playing() isEqualToString:@"crinacle/AirPods%20Pro"], [NSString stringWithFormat:@"AirPods again: their correction is on (%@)", playing()]);+ check([SGDSPString(SGKeyDSPGraphicEqNodes) isEqualToString:kLine], @"AirPods again: their own curve");++ // The same output again (a category change, not a new route): nothing happens.+ SGDSPSetSwitch(SGKeyDSPGraphicEq, NO);+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ check([playing() isEqualToString:@"off"], @"the same output again changes nothing");+ SGDSPSetSwitch(SGKeyDSPGraphicEq, YES);++ // Another pair, remembered with its own correction, then the AirPods: each gets its own.+ SGAutoEqOutputChanged(kSony, @"WH-1000XM5");+ check([playing() isEqualToString:@"off"], @"a new pair with nothing remembered: the AirPods' correction goes off");+ SGAutoEqRememberOutput(YES);+ SGAutoEqApplyText(@"oratory1990/Sony%20WH-1000XM5", kSonyLine);+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ check([playing() isEqualToString:@"crinacle/AirPods%20Pro"], @"AirPods: theirs");+ SGAutoEqOutputChanged(kSony, @"WH-1000XM5");+ check([playing() isEqualToString:@"oratory1990/Sony%20WH-1000XM5"] && [SGDSPString(SGKeyDSPGraphicEqNodes) isEqualToString:kSonyLine],+ @"Sony: its own pick, not the AirPods'");++ // None remembered for the Sony: a pick of None on it sticks.+ SGAutoEqTakeOff();+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ SGAutoEqOutputChanged(kSony, @"WH-1000XM5");+ check([playing() isEqualToString:@"off"], @"None picked for the Sony stays None for it");++ // A correction picked on the speaker, with nothing remembered there, is the user's: it stays everywhere.+ SGAutoEqForgetOutput(kSony);+ SGAutoEqOutputChanged(kSpeaker, @"Speaker");+ SGAutoEqApplyText(@"someone/Speaker%20Fix", kLine);+ SGAutoEqOutputChanged(kSony, @"WH-1000XM5");+ check([playing() isEqualToString:@"someone/Speaker%20Fix"], @"a pick made with nothing remembered is not taken off");++ // A curve of the user's own is never taken off by a route change.+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ SGDSPSetString(SGKeyDSPGraphicEqNodes, @"GraphicEQ: 100 6.0");+ SGDSPSetString(SGKeyDSPGraphicEqHeadphone, @"");+ SGAutoEqOutputChanged(kSpeaker, @"Speaker");+ check(SGDSPSwitch(SGKeyDSPGraphicEq) && [SGDSPString(SGKeyDSPGraphicEqNodes) isEqualToString:@"GraphicEQ: 100 6.0"],+ @"a hand-made curve stays on the speaker");++ // A route change never turns the effects on.+ SGDSPSetSwitch(SGKeyDSP, NO);+ SGAutoEqOutputChanged(kAirPods, @"AirPods Pro");+ check(!SGDSPSwitch(SGKeyDSP) && [playing() isEqualToString:@"crinacle/AirPods%20Pro"], @"AirPods with Audio effects off: correction set, effects left off");++ // Forgetting the AirPods: listed no more, and their correction stays until the next change.+ NSUInteger before = SGAutoEqRememberedOutputs().count;+ SGAutoEqRememberOutput(NO);+ check(!SGAutoEqOutputRemembered(kAirPods) && SGAutoEqRememberedOutputs().count == before - 1, @"Use for AirPods Pro off forgets them");+ check([playing() isEqualToString:@"crinacle/AirPods%20Pro"], @"forgetting leaves what plays");+ SGAutoEqOutputChanged(kSpeaker, @"Speaker");+ check([playing() isEqualToString:@"crinacle/AirPods%20Pro"], @"once forgotten, the speaker keeps it: it is a plain pick now");++ // A renamed output keeps its pick under its new name.+ SGAutoEqOutputChanged(kSony, @"WH-1000XM5");+ SGAutoEqRememberOutput(YES);+ SGAutoEqOutputChanged(kSpeaker, @"Speaker");+ SGAutoEqOutputChanged(kSony, @"Sony at work");+ check([SGAutoEqRememberedOutputs().firstObject[@"name"] isEqualToString:@"Sony at work"], @"a renamed output is listed by its new name");++ clearDefaults();+ printf("%d failed\n", failures);+ }+ return failures;+}diff --git a/tweak/Sources/Shared/AudioEffects/AudioEffects.x b/tweak/Sources/Shared/AudioEffects/AudioEffects.xindex 89a5a8ea..4dea0be6 100644--- a/tweak/Sources/Shared/AudioEffects/AudioEffects.x+++ b/tweak/Sources/Shared/AudioEffects/AudioEffects.x@@ -23,12 +23,14 @@// engine's render side. Spotify starts its output on a thread of its own. The effects are set on the queue;// SGDSPApply and the curves are main thread; SGDSPStatus and SGDSPError any thread.#import <AudioToolbox/AudioToolbox.h>+#import <AVFoundation/AVFoundation.h>#import <os/lock.h>#import <stdatomic.h>#import "Core/SGCore.h"#import "Shared/Player/SpeedPitch.h"#import "AudioEffects.h"#import "AudioEffectsApply.h"+#import "AudioEffectsPresets.h"#import "SGDSPEngine.h"// A burst of changes to a setting is gathered this long before it applies.@@ -566,7 +568,20 @@ void SGDSPCompanderResponse(NSArray<NSNumber *> *gains, NSInteger count, doubleSGDSPEngineCompanderCurve(SGDSPCompanderFrequencies, bands, (int)count, frequencies, values);}+// The output audio goes to now, for the corrections remembered per output (AutoEq.m).+static void followRoute(void) {+ AVAudioSessionPortDescription *port = AVAudioSession.sharedInstance.currentRoute.outputs.firstObject;+ if (port) SGAutoEqOutputChanged(port.UID, port.portName);+}+%ctor {+ [NSNotificationCenter.defaultCenter addObserverForName:AVAudioSessionRouteChangeNotification object:nil queue:NSOperationQueue.mainQueue+ usingBlock:^(NSNotification *note) {+ followRoute();+ }];+ dispatch_async(dispatch_get_main_queue(), ^{+ followRoute();+ });sg_reachable = SGPlayerWatchMusicOutput(listenTo);if (!sg_reachable) {SGLog(@"dsp: Spotify's output cannot be reached, the effects cannot reach its sound");diff --git a/tweak/Sources/Shared/AudioEffects/AudioEffectsPresets.h b/tweak/Sources/Shared/AudioEffects/AudioEffectsPresets.hindex f73953d0..89d65e5d 100644--- a/tweak/Sources/Shared/AudioEffects/AudioEffectsPresets.h+++ b/tweak/Sources/Shared/AudioEffects/AudioEffectsPresets.h@@ -56,5 +56,34 @@ void SGAutoEqLoadIndex(BOOL refresh, void (^done)(NSArray<SGAutoEqHeadphone *> *// Downloads the headphone's GraphicEQ and applies it; `done` on the main queue with nil or why not.void SGAutoEqApply(SGAutoEqHeadphone *headphone, void (^done)(NSString *error));// The second half of that: the file's text into the Graphic EQ, its switch and the master switch on. NO when-// it is not GraphicEQ.+// it is not GraphicEQ. When the output playing now is remembered (below), the pick becomes its correction.BOOL SGAutoEqApplyText(NSString *path, NSString *text);+// None: the headphone's correction off (a curve of the user's own, with no headphone, stays), and None+// remembered for the output playing now when it is remembered.+void SGAutoEqTakeOff(void);++#pragma mark - a correction per output++// The corrections remembered per output, by the port's UID: {uid: {name, path, nodes}}, `path` and `nodes`+// empty for None. Outside the dsp. prefix, so presets never hold it. And the UID whose correction the Graphic+// EQ plays because its output connected, empty when none does.+#define SGKeyDSPOutputs @"spotifyglass.audioOutputs"+#define SGKeyDSPOutputFollowed @"spotifyglass.audioOutputs.followed"+// Posted on the main thread when the output or what is remembered changes.+#define SGAutoEqOutputsChangedNotification @"SGAutoEqOutputsChangedNotification"++// Audio went to another output (AVAudioSession's route, its first output; AudioEffects.x calls this at+// launch and on every route change). A remembered output gets its correction, or None. Another output takes+// off a correction that came on for a remembered one; a correction picked with no output remembered, and a+// curve of the user's own, stay. Nothing happens while the UID stays the same. The master switch is left as+// it is: a route change never turns the effects on.+void SGAutoEqOutputChanged(NSString *uid, NSString *name);+// The output playing now, nil before the first call.+NSString *SGAutoEqOutputUID(void);+NSString *SGAutoEqOutputName(void);+// Remembers the correction in use (or None) for the output playing now, or forgets that output.+void SGAutoEqRememberOutput(BOOL remember);+BOOL SGAutoEqOutputRemembered(NSString *uid);+void SGAutoEqForgetOutput(NSString *uid);+// The remembered outputs, sorted by name: {uid, name, path}.+NSArray<NSDictionary<NSString *, NSString *> *> *SGAutoEqRememberedOutputs(void);diff --git a/tweak/Sources/Shared/AudioEffects/AudioEffectsPresetsPage.m b/tweak/Sources/Shared/AudioEffects/AudioEffectsPresetsPage.mindex 67394465..3c99a54b 100644--- a/tweak/Sources/Shared/AudioEffects/AudioEffectsPresetsPage.m+++ b/tweak/Sources/Shared/AudioEffects/AudioEffectsPresetsPage.m@@ -198,16 +198,33 @@ UIViewController *SGDSPPresetsPage(void) {// AutoEq's headphones under a search field, the one in use ticked, with None above them. A tap downloads its// GraphicEQ and turns the Graphic EQ on with it; None turns it off again. Pulling down fetches the list again.+// Above the field, Use for <the output playing now> remembers the pick for that output (AutoEq.m), and the+// outputs remembered so far are listed with their corrections, a swipe removing one. The field sits right over+// the list it searches, and while it is in use only the list stays under it, the way Settings' search does.@interface SGDSPHeadphoneList : SGPage <UISearchBarDelegate>@end+static const CGFloat kSearchTop = 8; // the field's gap from the card over it++typedef NS_ENUM(NSInteger, SGDSPHeadphoneSection) {+ SGDSPHeadphoneOutput, // Use for <output>, while an output is known and no search is on+ SGDSPHeadphoneRemembered, // the remembered outputs, while there are any and no search is on+ SGDSPHeadphoneSearch, // no rows: the search field is its header+ SGDSPHeadphoneNone, // while no search is on+ SGDSPHeadphoneMessage, // loading, or why the list did not come+ SGDSPHeadphoneAll,+ SGDSPHeadphoneSections,+};+@implementation SGDSPHeadphoneList {NSArray<SGAutoEqHeadphone *> *_all, *_shown;NSString *_message; // loading, or why the list did not comeBOOL _failed;NSString *_applying; // the path being downloaded+ NSArray<NSDictionary<NSString *, NSString *> *> *_remembered;UISearchBar *_search;- UIView *_header, *_footer;+ UIView *_searchHeader, *_header, *_footer;+ BOOL _searching; // the field in use, so only the list shows under it}- (instancetype)init {@@ -226,20 +243,44 @@ UIViewController *SGDSPPresetsPage(void) {_search.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;_search.keyboardAppearance = UIKeyboardAppearanceDark;_search.delegate = self;+ // The table insets the header to the cards' width; the bar reaches past them by the room it keeps+ // around its field, so the field lines up with the cards.+ _searchHeader = [UIView new];+ _search.translatesAutoresizingMaskIntoConstraints = NO;+ [_searchHeader addSubview:_search];+ [NSLayoutConstraint activateConstraints:@[+ [_search.leadingAnchor constraintEqualToAnchor:_searchHeader.leadingAnchor constant:-8],+ [_search.trailingAnchor constraintEqualToAnchor:_searchHeader.trailingAnchor constant:8],+ [_search.topAnchor constraintEqualToAnchor:_searchHeader.topAnchor constant:kSearchTop],+ [_search.heightAnchor constraintEqualToConstant:44],+ ]];_header = SGNote(@"Corrections that make your headphones sound neutral, measured by AutoEq. Picking one turns Audio effects and the Graphic EQ on with it.");- [_header addSubview:_search];table.tableHeaderView = _header;_footer = SGNote(@"From AutoEq by Jaakko Pasanen, MIT License. Pull down to fetch the list again.");table.tableFooterView = _footer;self.refreshControl = [UIRefreshControl new];[self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];+ _remembered = SGAutoEqRememberedOutputs();+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(outputsChanged) name:SGAutoEqOutputsChangedNotification object:nil];[self load:NO];}+// Another output connected, or a pick changed what one remembers.+- (void)outputsChanged {+ _remembered = SGAutoEqRememberedOutputs();+ [self reload:UITableViewRowAnimationNone];+}++// Every section but the field's: reloading the header the field is in would take the keyboard away mid-word.+- (void)reload:(UITableViewRowAnimation)animation {+ NSMutableIndexSet *sections = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, SGDSPHeadphoneSections)];+ [sections removeIndex:SGDSPHeadphoneSearch];+ [self.tableView reloadSections:sections withRowAnimation:animation];+}+- (void)viewWillLayoutSubviews {[super viewWillLayoutSubviews];- _search.frame = CGRectMake(8, 4, self.tableView.bounds.size.width - 16, 44);- SGFitNote(self.tableView, _header, 52, 8);+ SGFitNote(self.tableView, _header, 4, 8);SGFitNote(self.tableView, _footer, 16, 24);}@@ -255,7 +296,8 @@ UIViewController *SGDSPPresetsPage(void) {- (void)load:(BOOL)refresh {if (!_all) _message = @"Loading AutoEq's list…";_failed = NO;- [self.tableView reloadData];+ // From viewDidLoad the table has not asked for its rows yet, and will.+ if (self.tableView.window) [self reload:UITableViewRowAnimationNone];SGAutoEqLoadIndex(refresh, ^(NSArray<SGAutoEqHeadphone *> *headphones, NSString *error) {[self.refreshControl endRefreshing];if (headphones) self->_all = headphones;@@ -267,13 +309,29 @@ UIViewController *SGDSPPresetsPage(void) {- (void)filter {_shown = SGAutoEqSearch(_all ?: @[], _search.text);- [self.tableView reloadData];+ BOOL searching = _search.isFirstResponder || _search.text.length > 0;+ BOOL changed = searching != _searching;+ _searching = searching;+ // No fade: one over thousands of rows leaves the cards half drawn while the field moves up.+ [self reload:UITableViewRowAnimationNone];+ // What went from over the field could have taken it off the screen.+ if (changed && searching) {+ [self.tableView scrollRectToVisible:[self.tableView rectForHeaderInSection:SGDSPHeadphoneSearch] animated:YES];+ }}- (void)searchBar:(UISearchBar *)bar textDidChange:(NSString *)text {[self filter];}+- (void)searchBarTextDidBeginEditing:(UISearchBar *)bar {+ [self filter];+}++- (void)searchBarTextDidEndEditing:(UISearchBar *)bar {+ [self filter];+}+- (void)searchBarSearchButtonClicked:(UISearchBar *)bar {[bar resignFirstResponder];}@@ -283,42 +341,86 @@ static BOOL headphoneInUse(void) {return SGDSPSwitch(SGKeyDSPGraphicEq) && SGDSPString(SGKeyDSPGraphicEqHeadphone).length;}-// Sections: the loading or error message, None, the headphones.- (NSInteger)numberOfSectionsInTableView:(UITableView *)table {- return 3;+ return SGDSPHeadphoneSections;}- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {- if (section == 0) return _message ? 1 : 0;- if (section == 1) return 1;- return _all && !_shown.count && _search.text.length ? 1 : (NSInteger)_shown.count;+ switch (section) {+ case SGDSPHeadphoneMessage: return _message ? 1 : 0;+ case SGDSPHeadphoneOutput: return SGAutoEqOutputUID() && !_searching ? 1 : 0;+ case SGDSPHeadphoneRemembered: return _searching ? 0 : (NSInteger)_remembered.count;+ case SGDSPHeadphoneSearch: return 0;+ case SGDSPHeadphoneNone: return _searching ? 0 : 1;+ default: return _all && !_shown.count && _search.text.length ? 1 : (NSInteger)_shown.count;+ }}- (UIView *)tableView:(UITableView *)table viewForHeaderInSection:(NSInteger)section {- return nil;+ if (section == SGDSPHeadphoneSearch) return _searchHeader;+ return section == SGDSPHeadphoneRemembered && [self tableView:table numberOfRowsInSection:section] ? SGSectionHeader(table, @"Remembered outputs") : nil;}- (CGFloat)tableView:(UITableView *)table heightForHeaderInSection:(NSInteger)section {- return section == 0 && !_message ? CGFLOAT_MIN : SGSectionGap;+ if (section == SGDSPHeadphoneSearch) return kSearchTop + 44 + 4;+ if ([self tableView:table numberOfRowsInSection:section] == 0) return CGFLOAT_MIN;+ return section == SGDSPHeadphoneRemembered ? SGSectionHeaderHeight : SGSectionGap;+}++- (NSString *)outputFooter {+ NSString *name = SGAutoEqOutputName();+ return [NSString stringWithFormat:@"With this on, the correction you pick is kept for %@. It comes back each time audio plays through "+ "%@, and goes off on outputs with none remembered.", name, name];}- (UIView *)tableView:(UITableView *)table viewForFooterInSection:(NSInteger)section {- return nil;+ return section == SGDSPHeadphoneOutput && [self tableView:table numberOfRowsInSection:section] ? SGSectionFooter(table, [self outputFooter]) : nil;}- (CGFloat)tableView:(UITableView *)table heightForFooterInSection:(NSInteger)section {- return CGFLOAT_MIN;+ return section == SGDSPHeadphoneOutput && [self tableView:table numberOfRowsInSection:section] ? SGSectionFooterHeight(table, [self outputFooter]) : CGFLOAT_MIN;+}++// A remembered output's correction, by name.+static NSString *correctionName(NSString *path) {+ return path.length ? SGAutoEqNameOf(path) : @"None";}- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path {UITableViewCell *cell = SGDequeueCell(table, @"headphone");- if (path.section == 0) {+ cell.accessoryView = nil;+ if (path.section == SGDSPHeadphoneOutput) {+ NSString *uid = SGAutoEqOutputUID();+ BOOL remembered = SGAutoEqOutputRemembered(uid);+ NSString *current = correctionName(headphoneInUse() ? SGDSPString(SGKeyDSPGraphicEqHeadphone) : nil);+ SGFillCell(cell, [NSString stringWithFormat:@"Use for %@", SGAutoEqOutputName()],+ remembered ? [NSString stringWithFormat:@"%@ while it plays", current] : [NSString stringWithFormat:@"Remember %@ for this output", current], nil, nil);+ cell.selectionStyle = UITableViewCellSelectionStyleNone;+ UISwitch *toggle = [UISwitch new];+ toggle.onTintColor = SGGreen();+ toggle.on = remembered;+ [toggle addTarget:self action:@selector(useToggled:) forControlEvents:UIControlEventValueChanged];+ cell.accessoryView = toggle;+ cell.accessibilityTraits = UIAccessibilityTraitNone;+ return cell;+ }+ if (path.section == SGDSPHeadphoneRemembered) {+ NSDictionary<NSString *, NSString *> *output = _remembered[(NSUInteger)path.row];+ BOOL now = [output[@"uid"] isEqualToString:SGAutoEqOutputUID()];+ SGFillCell(cell, output[@"name"], now ? [NSString stringWithFormat:@"%@ · playing now", correctionName(output[@"path"])] : correctionName(output[@"path"]), nil, nil);+ cell.selectionStyle = UITableViewCellSelectionStyleNone;+ cell.accessibilityTraits = UIAccessibilityTraitNone;+ cell.accessibilityHint = @"Swipe left to remove";+ return cell;+ }+ cell.accessibilityHint = nil;+ if (path.section == SGDSPHeadphoneMessage) {SGFillCell(cell, _message, nil, _failed ? SGRed() : SGGrey(), _failed ? @"exclamationmark.triangle.fill" : nil);cell.selectionStyle = _failed ? UITableViewCellSelectionStyleDefault : UITableViewCellSelectionStyleNone;return cell;}cell.accessibilityValue = nil;- if (path.section == 1) {+ if (path.section == SGDSPHeadphoneNone) {SGFillCell(cell, @"None", nil, nil, nil);cell.selectionStyle = UITableViewCellSelectionStyleDefault;tick(cell, !headphoneInUse());@@ -347,32 +449,47 @@ static BOOL headphoneInUse(void) {}- (BOOL)tableView:(UITableView *)table shouldHighlightRowAtIndexPath:(NSIndexPath *)path {- if (path.section == 0) return _failed;- return (path.section == 1 || _shown.count > 0) && !_applying;+ if (path.section == SGDSPHeadphoneMessage) return _failed;+ if (path.section == SGDSPHeadphoneOutput || path.section == SGDSPHeadphoneRemembered) return NO;+ return (path.section == SGDSPHeadphoneNone || _shown.count > 0) && !_applying;+}++- (void)useToggled:(UISwitch *)toggle {+ SGAutoEqRememberOutput(toggle.on);+}++- (UISwipeActionsConfiguration *)tableView:(UITableView *)table trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)path {+ if (path.section != SGDSPHeadphoneRemembered) return nil;+ NSString *uid = _remembered[(NSUInteger)path.row][@"uid"];+ UIContextualAction *remove = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Remove"+ handler:^(UIContextualAction *action, UIView *view, void (^done)(BOOL)) {+ SGAutoEqForgetOutput(uid);+ done(YES);+ }];+ remove.image = [UIImage systemImageNamed:@"trash"];+ return [UISwipeActionsConfiguration configurationWithActions:@[remove]];}- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path {[table deselectRowAtIndexPath:path animated:YES];- if (path.section == 0) {+ if (path.section == SGDSPHeadphoneMessage) {if (_failed) [self load:YES];return;}- if (_applying) return;- if (path.section == 1) {+ if (_applying || path.section == SGDSPHeadphoneOutput || path.section == SGDSPHeadphoneRemembered) return;+ if (path.section == SGDSPHeadphoneNone) {// Only a headphone's correction is taken off: a curve of the user's own, with no headphone, stays.- if (!headphoneInUse()) return;- SGDSPSetString(SGKeyDSPGraphicEqHeadphone, @"");- SGDSPSetSwitch(SGKeyDSPGraphicEq, NO);- [table reloadData];+ SGAutoEqTakeOff();+ [self reload:UITableViewRowAnimationNone];return;}if (!_shown.count) return;SGAutoEqHeadphone *headphone = _shown[(NSUInteger)path.row];_applying = headphone.path;- [table reloadData];+ [self reload:UITableViewRowAnimationNone];SGAutoEqApply(headphone, ^(NSString *error) {self->_applying = nil;- [self.tableView reloadData];+ [self reload:UITableViewRowAnimationNone];if (error) showAlert(self, [NSString stringWithFormat:@"Could not apply %@", headphone.name], error);});}diff --git a/tweak/Sources/Shared/AudioEffects/AutoEq.m b/tweak/Sources/Shared/AudioEffects/AutoEq.mindex f0088968..2a0d4bf2 100644--- a/tweak/Sources/Shared/AudioEffects/AutoEq.m+++ b/tweak/Sources/Shared/AudioEffects/AutoEq.m@@ -113,6 +113,54 @@ void SGAutoEqLoadIndex(BOOL refresh, void (^done)(NSArray<SGAutoEqHeadphone *> *});}+#pragma mark - a correction per output++static NSString *sg_outputUID, *sg_outputName;++static NSDictionary<NSString *, NSDictionary *> *outputs(void) {+ id value = [NSUserDefaults.standardUserDefaults objectForKey:SGKeyDSPOutputs];+ return [value isKindOfClass:NSDictionary.class] ? value : @{};+}++static void setOutput(NSString *uid, NSDictionary *entry) {+ NSMutableDictionary *all = [outputs() mutableCopy];+ all[uid] = entry;+ [NSUserDefaults.standardUserDefaults setObject:all forKey:SGKeyDSPOutputs];+}++static NSString *followed(void) {+ return [NSUserDefaults.standardUserDefaults stringForKey:SGKeyDSPOutputFollowed] ?: @"";+}++static void setFollowed(NSString *uid) {+ [NSUserDefaults.standardUserDefaults setObject:uid ?: @"" forKey:SGKeyDSPOutputFollowed];+}++static void changed(void) {+ [NSNotificationCenter.defaultCenter postNotificationName:SGAutoEqOutputsChangedNotification object:nil];+}++// A headphone's correction is what the Graphic EQ plays, rather than nothing or the user's own curve.+static BOOL correctionInUse(void) {+ return SGDSPSwitch(SGKeyDSPGraphicEq) && SGDSPString(SGKeyDSPGraphicEqHeadphone).length;+}++static void takeOff(void) {+ if (!correctionInUse()) return;+ SGDSPSetString(SGKeyDSPGraphicEqHeadphone, @"");+ SGDSPSetSwitch(SGKeyDSPGraphicEq, NO);+}++// The output playing now remembers what the Graphic EQ plays, when it is remembered.+static void rememberPick(void) {+ if (!sg_outputUID.length || !outputs()[sg_outputUID]) return;+ BOOL on = correctionInUse();+ setOutput(sg_outputUID, @{@"name": sg_outputName ?: sg_outputUID, @"path": on ? SGDSPString(SGKeyDSPGraphicEqHeadphone) : @"",+ @"nodes": on ? SGDSPString(SGKeyDSPGraphicEqNodes) : @""});+ setFollowed(sg_outputUID);+ changed();+}+BOOL SGAutoEqApplyText(NSString *path, NSString *text) {NSString *line = SGDSPGraphicEqLine(text);if (!line || !path.length) return NO;@@ -121,9 +169,88 @@ BOOL SGAutoEqApplyText(NSString *path, NSString *text) {SGDSPSetSwitch(SGKeyDSPGraphicEq, YES);// The correction is meant to be heard: the master switch, off until asked for, comes on with it.if (!SGDSPSwitch(SGKeyDSP)) SGDSPSetSwitch(SGKeyDSP, YES);+ rememberPick();return YES;}+void SGAutoEqTakeOff(void) {+ takeOff();+ rememberPick();+}++void SGAutoEqOutputChanged(NSString *uid, NSString *name) {+ if (!uid.length || [uid isEqualToString:sg_outputUID]) return;+ sg_outputUID = [uid copy];+ sg_outputName = name.length ? [name copy] : sg_outputUID;+ NSDictionary *entry = outputs()[uid];+ if (entry) {+ if (![entry[@"name"] isEqual:sg_outputName]) {+ NSMutableDictionary *renamed = [entry mutableCopy];+ renamed[@"name"] = sg_outputName;+ setOutput(uid, renamed);+ }+ NSString *path = entry[@"path"], *nodes = entry[@"nodes"];+ if (path.length && nodes.length) {+ SGDSPSetString(SGKeyDSPGraphicEqNodes, nodes);+ SGDSPSetString(SGKeyDSPGraphicEqHeadphone, path);+ SGDSPSetSwitch(SGKeyDSPGraphicEq, YES);+ SGLog(@"autoeq: %@ connected, its correction %@ on%@", sg_outputName, SGAutoEqNameOf(path), SGDSPSwitch(SGKeyDSP) ? @"" : @" (Audio effects are off)");+ } else {+ takeOff();+ SGLog(@"autoeq: %@ connected, None remembered for it", sg_outputName);+ }+ setFollowed(uid);+ } else if (followed().length) {+ // The correction came on for another output; this one has none.+ if (correctionInUse()) SGLog(@"autoeq: %@ connected, with no correction remembered: %@ off", sg_outputName, SGAutoEqNameOf(SGDSPString(SGKeyDSPGraphicEqHeadphone)));+ takeOff();+ setFollowed(@"");+ }+ changed();+}++NSString *SGAutoEqOutputUID(void) {+ return sg_outputUID;+}++NSString *SGAutoEqOutputName(void) {+ return sg_outputName;+}++void SGAutoEqRememberOutput(BOOL remember) {+ if (!sg_outputUID.length) return;+ if (!remember) {+ SGAutoEqForgetOutput(sg_outputUID);+ return;+ }+ setOutput(sg_outputUID, @{@"name": sg_outputName, @"path": @"", @"nodes": @""});+ rememberPick();+}++BOOL SGAutoEqOutputRemembered(NSString *uid) {+ return uid.length && outputs()[uid] != nil;+}++// What plays stays: forgetting an output does not take its correction off now.+void SGAutoEqForgetOutput(NSString *uid) {+ if (!uid.length) return;+ NSMutableDictionary *all = [outputs() mutableCopy];+ [all removeObjectForKey:uid];+ [NSUserDefaults.standardUserDefaults setObject:all forKey:SGKeyDSPOutputs];+ if ([followed() isEqualToString:uid]) setFollowed(@"");+ changed();+}++NSArray<NSDictionary<NSString *, NSString *> *> *SGAutoEqRememberedOutputs(void) {+ NSMutableArray *list = [NSMutableArray array];+ [outputs() enumerateKeysAndObjectsUsingBlock:^(NSString *uid, NSDictionary *entry, BOOL *stop) {+ if (![entry isKindOfClass:NSDictionary.class]) return;+ [list addObject:@{@"uid": uid, @"name": entry[@"name"] ?: uid, @"path": entry[@"path"] ?: @""}];+ }];+ [list sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]]];+ return list;+}+void SGAutoEqApply(SGAutoEqHeadphone *headphone, void (^done)(NSString *)) {NSURL *url = SGAutoEqGraphicEqURL(headphone.path);if (!url) {