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: speed and pitch sliders in the redesigned player's more menu, done on Spotify's audio between its mixer and its output

Vojtěch Škopekc3fccc1040f4
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 720f1535..c5e641b3 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -108,7 +108,10 @@ Redesigned:
Navbar/ the glass tab bar (TabBar.x) over its own composition (Navbar.x, NavbarLayout.m) and editor, the glass search field
NowPlayingBar/ the glass now playing bar (NowPlayingBar.x), with Spotify's device button on it hidden on request
(BarConnect.x, its own key and its own Now playing page, apart from the native look's)
- Player/ the redesigned full screen player (Player.h lists its files)
+ Player/ the redesigned full screen player (Player.h lists its files), and in its more menu Speed and pitch:
+ both done to Spotify's audio by Apple's time and pitch unit, put between its mixer and its RemoteIO unit
+ by taking over the connection Spotify makes between them (PlayerSpeedPitch.x, SGRTimePitch.m). Tested on the Mac against harness/pitch/ and
+ harness/menu/
Lyrics/ the full screen lyrics page on glass with Apple Music style lyrics over it, always on (SGRKaraokeView,
which the player shows in itself too, Player/PlayerLyrics.x)
Home/ Home decluttered to music on black (an allow list of its sections: shortcuts, the DJ without its heading and
diff --git a/harness/menu/README.md b/harness/menu/README.md
new file mode 100644
index 00000000..7dc4d1f4
--- /dev/null
+++ b/harness/menu/README.md
@@ -0,0 +1,13 @@
+# Menu harness
+
+Spotify's context menu sheet (`ContextMenu_InternalImpl.ContextMenuViewController`) mocked under its
+class name and presented from a now playing controller, so `PlayerMenu.x`'s hook adds Speed and pitch
+the way it would on the phone. Speed and pitch themselves are stubs that log.
+
+ THEOS=$HOME/theos ./build.sh
+ xcrun simctl install booted build/MenuHarness.app
+ xcrun simctl launch --console-pty booted com.vojta.menuharness [footer] [nospeed]
+
+The menu opens at 1 s, the block at 3 s, both sliders move at 5 s and the block closes at 7 s.
+`footer` gives the mock table a header of Spotify's, so the block goes to the footer; `nospeed` has
+the player refuse speed.
diff --git a/harness/menu/build.sh b/harness/menu/build.sh
new file mode 100755
index 00000000..57d9533d
--- /dev/null
+++ b/harness/menu/build.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Builds the menu harness for the simulator: PlayerMenu.x's hook run for real on a mock of Spotify's
+# context menu sheet.
+set -e
+SRC=$(cd "$(dirname "$0")/../../tweak/Sources" && pwd)
+OUT=$(dirname "$0")/build
+rm -rf "$OUT"; mkdir -p "$OUT/gen" "$OUT/MenuHarness.app"
+"$THEOS/bin/logos.pl" -c generator=internal "$SRC/Redesigned/Player/PlayerMenu.x" > "$OUT/gen/PlayerMenu.m"
+
+SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)
+xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g -O0 \
+ -I"$SRC" -I"$SRC/Redesigned/Player" -isysroot "$SDK" -Wno-deprecated-declarations \
+ "$(dirname "$0")/main.m" "$OUT"/gen/*.m \
+ "$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \
+ "$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \
+ "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRRestyle.m "$SRC"/Redesigned/Kit/SGRedesign.m \
+ -framework UIKit -framework QuartzCore -framework CoreGraphics -framework CoreImage -framework Foundation \
+ -o "$OUT/MenuHarness.app/MenuHarness"
+
+cat > "$OUT/MenuHarness.app/Info.plist" <<'PLIST'
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0"><dict>
+<key>CFBundleExecutable</key><string>MenuHarness</string>
+<key>CFBundleIdentifier</key><string>com.vojta.menuharness</string>
+<key>CFBundleName</key><string>MenuHarness</string>
+<key>CFBundleVersion</key><string>1</string>
+<key>CFBundleShortVersionString</key><string>1.0</string>
+<key>UIUserInterfaceStyle</key><string>Dark</string>
+<key>UILaunchScreen</key><dict/>
+<key>UIApplicationSceneManifest</key><dict>
+ <key>UIApplicationSupportsMultipleScenes</key><false/>
+</dict>
+</dict></plist>
+PLIST
+echo "built $OUT/MenuHarness.app"
diff --git a/harness/menu/main.m b/harness/menu/main.m
new file mode 100644
index 00000000..6dd268ed
--- /dev/null
+++ b/harness/menu/main.m
@@ -0,0 +1,168 @@
+// Spotify's context menu sheet mocked under its class name, presented from a now playing controller,
+// so PlayerMenu.x's hook adds Speed and pitch the way it would on the phone. The script opens the menu at
+// 1 s, opens the block at 3 s, moves both sliders at 5 s and closes the block at 7 s; screenshot between.
+//
+// THEOS=$HOME/theos ./build.sh && xcrun simctl install booted build/MenuHarness.app
+// xcrun simctl launch --console-pty booted com.vojta.menuharness [footer] [nospeed]
+#import <UIKit/UIKit.h>
+
+#pragma mark - what PlayerMenu.x calls
+
+static double sg_speed = 1;
+static float sg_pitch;
+static BOOL sg_speedAllowed = YES;
+UIColor *SGRAccentColor(void) { return nil; }
+void SGRPlayFeedback(NSInteger feedback) { NSLog(@"[harness] feedback %ld", (long)feedback); }
+void SGRPrepareFeedback(NSInteger feedback) {}
+double SGRPlayerSpeed(void) { return sg_speed; }
+BOOL SGRPlayerSpeedAllowed(void) { return sg_speedAllowed; }
+void SGRSetPlayerSpeed(double speed) { sg_speed = speed; NSLog(@"[harness] speed %.2f", speed); }
+float SGRPlayerPitch(void) { return sg_pitch; }
+void SGRSetPlayerPitch(float semitones) { sg_pitch = semitones; NSLog(@"[harness] pitch %.0f", semitones); }
+BOOL SGRPlayerPitchAvailable(void) { return YES; }
+
+#pragma mark - Spotify's sheet
+
+@interface _TtC24ContextMenu_InternalImpl25ContextMenuViewController : UIViewController <UITableViewDataSource>
+@property (nonatomic, strong) UITableView *table;
+@end
+
+@implementation _TtC24ContextMenu_InternalImpl25ContextMenuViewController
+
+- (NSArray<NSArray<NSString *> *> *)rows {
+ return @[@[@"plus.circle", @"Add to playlist"], @[@"music.note.list", @"Add to queue"], @[@"person", @"Go to artist"],
+ @[@"square.stack", @"Go to album"], @[@"square.and.arrow.up", @"Share"], @[@"moon", @"Sleep timer"],
+ @[@"dot.radiowaves.left.and.right", @"Go to song radio"], @[@"info.circle", @"View credits"]];
+}
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+ self.view.backgroundColor = [UIColor colorWithWhite:0.07 alpha:1];
+ self.table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
+ self.table.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+ self.table.backgroundColor = UIColor.clearColor;
+ self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
+ self.table.rowHeight = 56;
+ self.table.dataSource = self;
+ [self.table registerClass:UITableViewCell.class forCellReuseIdentifier:@"row"];
+ if ([NSProcessInfo.processInfo.arguments containsObject:@"footer"]) {
+ UILabel *heading = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
+ heading.text = @" Spotify's heading";
+ heading.textColor = UIColor.whiteColor;
+ self.table.tableHeaderView = heading;
+ }
+ [self.view addSubview:self.table];
+}
+
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
+ return self.rows.count;
+}
+
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"row" forIndexPath:indexPath];
+ UIListContentConfiguration *content = cell.defaultContentConfiguration;
+ content.text = self.rows[indexPath.row][1];
+ content.textProperties.color = UIColor.whiteColor;
+ content.image = [UIImage systemImageNamed:self.rows[indexPath.row][0]];
+ content.imageProperties.tintColor = [UIColor colorWithWhite:0.7 alpha:1];
+ cell.contentConfiguration = content;
+ cell.backgroundColor = UIColor.clearColor;
+ return cell;
+}
+
+- (void)viewDidLayoutSubviews {
+ [super viewDidLayoutSubviews];
+}
+
+- (void)viewDidAppear:(BOOL)animated {
+ [super viewDidAppear:animated];
+}
+
+@end
+
+#pragma mark - the player
+
+@interface NowPlayingHarnessViewController : UIViewController
+@end
+
+@implementation NowPlayingHarnessViewController
+- (void)viewDidLoad {
+ [super viewDidLoad];
+ self.view.backgroundColor = [UIColor colorWithRed:0.25 green:0.1 blue:0.2 alpha:1];
+}
+@end
+
+@interface SGRHarnessDelegate : UIResponder <UIApplicationDelegate>
+@property (nonatomic, strong) UIWindow *window;
+@end
+
+static UIView *findBlock(UIView *root) {
+ if ([NSStringFromClass(root.class) isEqualToString:@"SGRSpeedPitchView"]) return root;
+ for (UIView *child in root.subviews) {
+ UIView *found = findBlock(child);
+ if (found) return found;
+ }
+ return nil;
+}
+
+static NSArray<UISlider *> *sliders(UIView *root) {
+ NSMutableArray *found = [NSMutableArray array];
+ if ([root isKindOfClass:UISlider.class]) [found addObject:root];
+ for (UIView *child in root.subviews) [found addObjectsFromArray:sliders(child)];
+ return found;
+}
+
+@implementation SGRHarnessDelegate
+
+- (void)after:(double)seconds do:(void (^)(void))block {
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
+}
+
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
+ sg_speedAllowed = ![NSProcessInfo.processInfo.arguments containsObject:@"nospeed"];
+ self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
+ NowPlayingHarnessViewController *player = [NowPlayingHarnessViewController new];
+ self.window.rootViewController = player;
+ [self.window makeKeyAndVisible];
+
+ __block _TtC24ContextMenu_InternalImpl25ContextMenuViewController *menu;
+ [self after:1 do:^{
+ menu = [_TtC24ContextMenu_InternalImpl25ContextMenuViewController new];
+ UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:menu];
+ navigation.navigationBarHidden = YES;
+ navigation.sheetPresentationController.detents = @[UISheetPresentationControllerDetent.mediumDetent, UISheetPresentationControllerDetent.largeDetent];
+ [player presentViewController:navigation animated:YES completion:nil];
+ }];
+ [self after:3 do:^{
+ UIControl *row = (UIControl *)findBlock(menu.view).subviews.firstObject;
+ NSLog(@"[harness] block %@", findBlock(menu.view));
+ [row sendActionsForControlEvents:UIControlEventTouchUpInside];
+ }];
+ [self after:5 do:^{
+ NSArray<UISlider *> *found = sliders(findBlock(menu.view));
+ found[0].value = 1.27;
+ [found[0] sendActionsForControlEvents:UIControlEventValueChanged];
+ [found[0] sendActionsForControlEvents:UIControlEventTouchUpInside];
+ found[1].value = -3.2;
+ [found[1] sendActionsForControlEvents:UIControlEventValueChanged];
+ [found[1] sendActionsForControlEvents:UIControlEventTouchUpInside];
+ }];
+ [self after:7 do:^{
+ UIControl *row = (UIControl *)findBlock(menu.view).subviews.firstObject;
+ [row sendActionsForControlEvents:UIControlEventTouchUpInside];
+ }];
+ return YES;
+}
+
+@end
+
+// Before every %ctor, so the redesign's gate reads on.
+__attribute__((constructor(101))) static void sgr_harnessDefaults(void) {
+ [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"spotifyglass.redesign"];
+}
+
+int main(int argc, char *argv[]) {
+ @autoreleasepool {
+ return UIApplicationMain(argc, argv, nil, NSStringFromClass(SGRHarnessDelegate.class));
+ }
+}
diff --git a/harness/pitch/README.md b/harness/pitch/README.md
new file mode 100644
index 00000000..b8ebf6a0
--- /dev/null
+++ b/harness/pitch/README.md
@@ -0,0 +1,9 @@
+# Pitch harness
+
+`SGRTimePitch.m` run on the Mac both ways the tweak runs it: in place over IO buffers of 1024, 4096
+and odd sizes (pitch alone), and pulling a source at rates from 0.5 to 2 (speed). Checks a 440 Hz sine
+comes out at the pitch asked for and the source is consumed at the rate asked for, and reports the
+unit's pulls, underruns, delay and cost. With a song path it also writes the song shifted up 3 and down 4
+semitones into `build/` to listen to.
+
+ ./build.sh && build/pitch ../../../song.mp3
diff --git a/harness/pitch/build.sh b/harness/pitch/build.sh
new file mode 100755
index 00000000..9a0387e9
--- /dev/null
+++ b/harness/pitch/build.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+# Builds the pitch harness for the Mac: SGRTimePitch.m as the tweak compiles it, and main.m.
+set -e
+cd "$(dirname "$0")"
+mkdir -p build
+xcrun clang -fobjc-arc -O2 -I ../../tweak/Sources -framework Foundation -framework AudioToolbox \
+ main.m ../../tweak/Sources/Redesigned/Player/SGRTimePitch.m -o build/pitch
diff --git a/harness/pitch/main.m b/harness/pitch/main.m
new file mode 100644
index 00000000..96c716fe
--- /dev/null
+++ b/harness/pitch/main.m
@@ -0,0 +1,160 @@
+// Runs SGRTimePitch.m on the Mac both ways the tweak runs it: in place over IO buffers of Spotify's sizes
+// (pitch alone), and pulling a source at a rate (speed, with or without pitch). Checks a sine comes out
+// at the pitch asked for and the source is consumed at the rate asked for, reports the unit's pulls,
+// underruns, delay and cost, and writes a song shifted up and down to listen to.
+//
+// ./build.sh && build/pitch [song]
+#import <Foundation/Foundation.h>
+#import <AudioToolbox/AudioToolbox.h>
+#import <mach/mach_time.h>
+#import "Redesigned/Player/SGRTimePitch.h"
+
+static const double kRate = 44100;
+
+// Frequency by the zero crossings of the steady part.
+static double frequency(const float *samples, size_t count) {
+ size_t first = 0, last = 0, crossings = 0;
+ for (size_t i = 1; i < count; i++) {
+ if (samples[i - 1] < 0 && samples[i] >= 0) {
+ if (!crossings) first = i;
+ last = i;
+ crossings++;
+ }
+ }
+ return crossings > 1 ? (crossings - 1) * kRate / (last - first) : 0;
+}
+
+// The frame where the output first gets loud: the delay the shifter adds.
+static size_t onset(const float *samples, size_t count) {
+ for (size_t i = 0; i < count; i++) if (fabsf(samples[i]) > 0.1f) return i;
+ return count;
+}
+
+static UInt32 bufferSize(size_t index, int pattern) {
+ if (pattern == 0) return 1024;
+ if (pattern == 1) return 4096;
+ static const UInt32 odd[] = {470, 471, 512, 1024, 256, 941};
+ return odd[index % 6];
+}
+
+static void runSine(float semitones, int pattern) {
+ size_t total = (size_t)(kRate * 3);
+ float *left = calloc(total, sizeof(float)), *right = calloc(total, sizeof(float));
+ for (size_t i = 0; i < total; i++) left[i] = right[i] = 0.5f * sinf(2 * M_PI * 440 * i / kRate);
+ SGRTimePitch *shifter = SGRTimePitchCreate(kRate, 2, NULL, NULL);
+ if (!shifter) {
+ printf("no shifter\n");
+ exit(1);
+ }
+ SGRTimePitchSetSemitones(shifter, semitones);
+ SGRTimePitchReset(shifter);
+ uint64_t start = mach_absolute_time();
+ size_t done = 0, index = 0;
+ while (done < total) {
+ UInt32 frames = (UInt32)MIN((size_t)bufferSize(index++, pattern), total - done);
+ float *channels[2] = {left + done, right + done};
+ if (!SGRTimePitchProcess(shifter, channels, frames)) printf(" process failed at %zu\n", done);
+ done += frames;
+ }
+ mach_timebase_info_data_t timebase;
+ mach_timebase_info(&timebase);
+ double seconds = (mach_absolute_time() - start) * (double)timebase.numer / timebase.denom / 1e9;
+ double expected = 440 * pow(2, semitones / 12);
+ double measured = frequency(left + (size_t)kRate, total - (size_t)kRate);
+ printf("sine %+5.1f st, buffers %-8s: %6.1f Hz (want %6.1f, %+.2f%%), delay %4zu frames (unit %.1f ms), largest pull %u, underruns %u, failures %u, cost %.2f%% of real time\n",
+ semitones, pattern == 0 ? "1024" : pattern == 1 ? "4096" : "mixed", measured, expected, (measured / expected - 1) * 100,
+ onset(left, total), SGRTimePitchLatency(shifter) * 1000, SGRTimePitchLargestPull(shifter),
+ SGRTimePitchUnderruns(shifter), SGRTimePitchFailures(shifter), seconds / 3 * 100);
+ free(left);
+ free(right);
+}
+
+static void runSong(NSString *path, float semitones, NSString *outPath) {
+ ExtAudioFileRef file;
+ if (ExtAudioFileOpenURL((__bridge CFURLRef)[NSURL fileURLWithPath:path], &file)) {
+ printf("cannot open %s\n", path.UTF8String);
+ return;
+ }
+ AudioStreamBasicDescription format = {
+ .mSampleRate = kRate, .mFormatID = kAudioFormatLinearPCM,
+ .mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved,
+ .mBytesPerPacket = 4, .mFramesPerPacket = 1, .mBytesPerFrame = 4, .mChannelsPerFrame = 2, .mBitsPerChannel = 32,
+ };
+ ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat, sizeof format, &format);
+ ExtAudioFileRef output;
+ AudioStreamBasicDescription wav = {
+ .mSampleRate = kRate, .mFormatID = kAudioFormatLinearPCM,
+ .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
+ .mBytesPerPacket = 4, .mFramesPerPacket = 1, .mBytesPerFrame = 4, .mChannelsPerFrame = 2, .mBitsPerChannel = 16,
+ };
+ ExtAudioFileCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:outPath], kAudioFileWAVEType, &wav, NULL, kAudioFileFlags_EraseFile, &output);
+ ExtAudioFileSetProperty(output, kExtAudioFileProperty_ClientDataFormat, sizeof format, &format);
+ SGRTimePitch *shifter = SGRTimePitchCreate(kRate, 2, NULL, NULL);
+ SGRTimePitchSetSemitones(shifter, semitones);
+ float left[1024], right[1024];
+ size_t seconds = 0;
+ for (;;) {
+ struct { AudioBufferList list; AudioBuffer second; } buffers = {{2, {{1, sizeof left, left}}}, {1, sizeof right, right}};
+ UInt32 frames = 1024;
+ if (ExtAudioFileRead(file, &frames, &buffers.list) || !frames) break;
+ float *channels[2] = {left, right};
+ SGRTimePitchProcess(shifter, channels, frames);
+ buffers.list.mBuffers[0].mDataByteSize = buffers.second.mDataByteSize = frames * 4;
+ ExtAudioFileWrite(output, frames, &buffers.list);
+ seconds += frames;
+ if (seconds > kRate * 40) break;
+ }
+ ExtAudioFileDispose(file);
+ ExtAudioFileDispose(output);
+ printf("wrote %s (%+.0f st), underruns %u\n", outPath.UTF8String, semitones, SGRTimePitchUnderruns(shifter));
+}
+
+typedef struct { double phase; } Sine;
+
+static OSStatus sineSource(void *context, UInt32 frames, AudioBufferList *data) {
+ Sine *sine = context;
+ for (UInt32 i = 0; i < frames; i++) {
+ float value = 0.5f * sinf(2 * M_PI * 440 * (sine->phase + i) / kRate);
+ for (UInt32 c = 0; c < data->mNumberBuffers; c++) ((float *)data->mBuffers[c].mData)[i] = value;
+ }
+ sine->phase += frames;
+ return noErr;
+}
+
+static void runPull(float rate, float semitones) {
+ Sine sine = {0};
+ SGRTimePitch *unit = SGRTimePitchCreate(kRate, 2, sineSource, &sine);
+ SGRTimePitchSetRate(unit, rate);
+ SGRTimePitchSetSemitones(unit, semitones);
+ size_t total = (size_t)(kRate * 4);
+ float *left = calloc(total, sizeof(float)), *right = calloc(total, sizeof(float));
+ for (size_t done = 0; done < total; done += 1024) {
+ struct { AudioBufferList list; AudioBuffer second; } buffers = {{2, {{1, 4096, left + done}}}, {1, 4096, right + done}};
+ if (SGRTimePitchRender(unit, 1024, &buffers.list) != noErr) printf(" render failed\n");
+ }
+ double measured = frequency(left + (size_t)kRate, total - (size_t)kRate);
+ double expected = 440 * pow(2, semitones / 12);
+ double consumedRate = (double)SGRTimePitchConsumed(unit) / total;
+ printf("pull rate %.2f %+3.0f st: %6.1f Hz (want %6.1f, %+.2f%%), consumed %.3fx, largest pull %u, failures %u\n", rate, semitones,
+ measured, expected, (measured / expected - 1) * 100, consumedRate, SGRTimePitchLargestPull(unit), SGRTimePitchFailures(unit));
+ free(left);
+ free(right);
+}
+
+int main(int argc, const char **argv) {
+ @autoreleasepool {
+ for (int pattern = 0; pattern < 3; pattern++) {
+ for (NSNumber *semitones in @[@-12, @-5, @-1, @0.5, @3, @7, @12]) runSine(semitones.floatValue, pattern);
+ }
+ for (NSNumber *rate in @[@0.5, @0.75, @1, @1.25, @1.5, @2]) {
+ runPull(rate.floatValue, 0);
+ runPull(rate.floatValue, 3);
+ }
+ if (argc > 1) {
+ NSString *song = @(argv[1]);
+ runSong(song, 3, @"build/song+3.wav");
+ runSong(song, -4, @"build/song-4.wav");
+ }
+ }
+ return 0;
+}
diff --git a/harness/speed/README.md b/harness/speed/README.md
new file mode 100644
index 00000000..0540ceb1
--- /dev/null
+++ b/harness/speed/README.md
@@ -0,0 +1,15 @@
+# Speed harness
+
+Spotify's audio chain (AudioUnitDriver2: a converter fed by a render callback, a mixer and RemoteIO, wired
+with MakeConnection, slices of 4096) rebuilt with real units in the simulator, with `PlayerSpeedPitch.x`
+compiled in: its AudioUnitSetProperty rebinding takes over the mixer-to-output connection exactly as on
+the phone (the harness is the main executable), and its SPTPlayerState hook runs on a mock state that
+computes -position the way Spotify's does.
+
+ THEOS=$HOME/theos ./build.sh
+ xcrun simctl install booted build/SpeedHarness.app
+ xcrun simctl launch --console-pty booted com.vojta.speedharness
+
+The script plays a quiet sine and steps through normal, 1.5x, 1.5x at +3 semitones, 0.75x and back,
+logging how fast the converter's callback (the decoder) was drained and how far the state's position is
+from the content played. 2026-09-18: drained 1.00x, 1.50x, 1.50x, 0.75x, 1.00x; position within 12 ms.
diff --git a/harness/speed/build.sh b/harness/speed/build.sh
new file mode 100755
index 00000000..1e82d907
--- /dev/null
+++ b/harness/speed/build.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+# Builds the speed harness for the simulator: Spotify's audio chain rebuilt with real Core Audio units and
+# PlayerSpeedPitch.x's rebinding and render callback run on it for real.
+set -e
+SRC=$(cd "$(dirname "$0")/../../tweak/Sources" && pwd)
+OUT=$(dirname "$0")/build
+rm -rf "$OUT"; mkdir -p "$OUT/gen" "$OUT/SpeedHarness.app"
+"$THEOS/bin/logos.pl" -c generator=internal "$SRC/Redesigned/Player/PlayerSpeedPitch.x" > "$OUT/gen/PlayerSpeedPitch.m"
+SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)
+xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g -O1 \
+ -I"$SRC" -I"$SRC/Redesigned/Player" -isysroot "$SDK" -Wno-deprecated-declarations \
+ "$(dirname "$0")/main.m" "$OUT"/gen/*.m "$SRC"/Redesigned/Player/SGRTimePitch.m "$SRC"/Core/SGRebind.m \
+ "$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGUIMode.m "$SRC"/Core/SGFlagForce.m \
+ -framework UIKit -framework QuartzCore -framework AudioToolbox -framework AVFoundation -framework Foundation \
+ -o "$OUT/SpeedHarness.app/SpeedHarness"
+cat > "$OUT/SpeedHarness.app/Info.plist" <<'PLIST'
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0"><dict>
+<key>CFBundleExecutable</key><string>SpeedHarness</string>
+<key>CFBundleIdentifier</key><string>com.vojta.speedharness</string>
+<key>CFBundleName</key><string>SpeedHarness</string>
+<key>CFBundleVersion</key><string>1</string>
+<key>CFBundleShortVersionString</key><string>1.0</string>
+<key>UILaunchScreen</key><dict/>
+<key>UIApplicationSceneManifest</key><dict><key>UIApplicationSupportsMultipleScenes</key><false/></dict>
+</dict></plist>
+PLIST
+echo "built $OUT/SpeedHarness.app"
diff --git a/harness/speed/main.m b/harness/speed/main.m
new file mode 100644
index 00000000..cdf37721
--- /dev/null
+++ b/harness/speed/main.m
@@ -0,0 +1,157 @@
+// Spotify's audio chain (AudioUnitDriver2: a converter fed by a render callback, a mixer, RemoteIO, wired
+// with MakeConnection and slices of 4096) rebuilt with real units in the simulator, with
+// PlayerSpeedPitch.x's rebinding, render callback and SPTPlayerState hook running on it for real. The
+// converter's callback stands in for Spotify's decoder and counts what it hands over, so the log shows
+// how fast the song is drained at each speed, and how far a mock player state's position drifts from it.
+//
+// THEOS=$HOME/theos ./build.sh && xcrun simctl install booted build/SpeedHarness.app
+// xcrun simctl launch --console-pty booted com.vojta.speedharness
+#import <UIKit/UIKit.h>
+#import <AudioToolbox/AudioToolbox.h>
+#import <AVFoundation/AVFoundation.h>
+#import <stdatomic.h>
+
+double SGRPlayerSpeed(void);
+void SGRSetPlayerSpeed(double speed);
+void SGRSetPlayerPitch(float semitones);
+BOOL SGRPlayerSpeedAllowed(void);
+
+static const double kRate = 44100;
+static atomic_uint_fast64_t sg_decoded;
+
+// Spotify's player state as far as -position goes (disassembly of -[SPTPlayerState position]).
+@interface SPTPlayerState : NSObject
+@property (nonatomic) double positionAsOfTimestamp;
+@property (nonatomic, strong) NSDate *timestamp;
+@end
+
+@implementation SPTPlayerState
+- (double)playbackSpeed {
+ return 1;
+}
+- (double)position {
+ return MAX(0, self.positionAsOfTimestamp - self.timestamp.timeIntervalSinceNow * [self playbackSpeed]);
+}
+@end
+
+static OSStatus decoder(void *refCon, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *timestamp, UInt32 bus,
+ UInt32 frames, AudioBufferList *data) {
+ uint64_t start = atomic_fetch_add(&sg_decoded, frames);
+ for (UInt32 i = 0; i < frames; i++) {
+ float value = 0.05f * sinf(2 * M_PI * 440 * (start + i) / kRate);
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) ((float *)data->mBuffers[b].mData)[i] = value;
+ }
+ return noErr;
+}
+
+static AudioUnit make(OSType type, OSType subType) {
+ AudioComponentDescription description = {type, subType, kAudioUnitManufacturer_Apple, 0, 0};
+ AudioUnit unit = NULL;
+ AudioComponentInstanceNew(AudioComponentFindNext(NULL, &description), &unit);
+ return unit;
+}
+
+static void check(OSStatus status, const char *what) {
+ if (status) NSLog(@"[harness] %s failed: %d", what, (int)status);
+}
+
+@interface SGRHarnessDelegate : UIResponder <UIApplicationDelegate>
+@property (nonatomic, strong) UIWindow *window;
+@end
+
+@implementation SGRHarnessDelegate {
+ SPTPlayerState *_state;
+ uint64_t _lastDecoded;
+ NSTimeInterval _lastAt, _startedAt;
+}
+
+- (void)startChain {
+ [AVAudioSession.sharedInstance setCategory:AVAudioSessionCategoryPlayback error:nil];
+ [AVAudioSession.sharedInstance setActive:YES error:nil];
+ AudioUnit converter = make(kAudioUnitType_FormatConverter, kAudioUnitSubType_AUConverter);
+ AudioUnit mixer = make(kAudioUnitType_Mixer, kAudioUnitSubType_MultiChannelMixer);
+ AudioUnit output = make(kAudioUnitType_Output, kAudioUnitSubType_RemoteIO);
+ AURenderCallbackStruct callback = {decoder, NULL};
+ check(AudioUnitSetProperty(converter, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, sizeof callback), "callback");
+ AudioUnitConnection toMixer = {converter, 0, 0}, toOutput = {mixer, 0, 0};
+ check(AudioUnitSetProperty(mixer, kAudioUnitProperty_MakeConnection, kAudioUnitScope_Input, 0, &toMixer, sizeof toMixer), "connect mixer");
+ check(AudioUnitSetProperty(output, kAudioUnitProperty_MakeConnection, kAudioUnitScope_Input, 0, &toOutput, sizeof toOutput), "connect output");
+ AudioStreamBasicDescription format = {kRate, kAudioFormatLinearPCM, kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved, 4, 1, 4, 2, 32, 0};
+ check(AudioUnitSetProperty(converter, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, sizeof format), "converter in");
+ check(AudioUnitSetProperty(converter, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &format, sizeof format), "converter out");
+ check(AudioUnitSetProperty(mixer, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, sizeof format), "mixer in");
+ check(AudioUnitSetProperty(mixer, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &format, sizeof format), "mixer out");
+ check(AudioUnitSetProperty(output, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, sizeof format), "output in");
+ UInt32 slice = 4096;
+ AudioUnit units[] = {converter, mixer, output};
+ for (int i = 0; i < 3; i++) check(AudioUnitSetProperty(units[i], kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &slice, sizeof slice), "slice");
+ for (int i = 0; i < 3; i++) check(AudioUnitInitialize(units[i]), "initialize");
+ check(AudioOutputUnitStart(output), "start");
+}
+
+- (void)report:(NSString *)what {
+ NSTimeInterval now = CACurrentMediaTime();
+ uint64_t decoded = atomic_load(&sg_decoded);
+ double content = decoded / kRate;
+ NSLog(@"[harness] %-28@ decoder drained %.2fx over the last %.1f s; content %.2f s, state position %.2f s (off %+.0f ms), state speed %.2f",
+ what, (decoded - _lastDecoded) / kRate / (now - _lastAt), now - _lastAt, content, _state.position,
+ (_state.position - content) * 1000, [_state playbackSpeed]);
+ _lastDecoded = decoded;
+ _lastAt = now;
+}
+
+// The player reporting: a state whose position is what the decoder has handed over, as of now.
+- (void)playerReports {
+ _state = [SPTPlayerState new];
+ _state.positionAsOfTimestamp = atomic_load(&sg_decoded) / kRate;
+ _state.timestamp = [NSDate date];
+}
+
+- (void)after:(double)seconds do:(void (^)(void))block {
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
+}
+
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
+ self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
+ self.window.rootViewController = [UIViewController new];
+ [self.window makeKeyAndVisible];
+ [self startChain];
+ NSLog(@"[harness] speed allowed: %d", SGRPlayerSpeedAllowed());
+ [self after:1 do:^{ [self playerReports]; self->_lastDecoded = atomic_load(&sg_decoded); self->_lastAt = CACurrentMediaTime(); }];
+ NSArray *script = @[
+ @[@3, @"normal", @1, @0],
+ @[@6, @"1.5x", @1.5, @0],
+ @[@9, @"1.5x, +3 st", @1.5, @3],
+ @[@12, @"0.75x", @0.75, @3],
+ @[@15, @"back to normal", @1, @0],
+ @[@18, @"normal, unit out", @1, @0],
+ ];
+ __block NSString *label = @"normal";
+ for (NSArray *step in script) {
+ [self after:[step[0] doubleValue] do:^{
+ [self report:label];
+ label = step[1];
+ SGRSetPlayerSpeed([step[2] doubleValue]);
+ SGRSetPlayerPitch([step[3] floatValue]);
+ }];
+ // A report mid step, the way Spotify's player reports now and then.
+ [self after:[step[0] doubleValue] + 1.5 do:^{ [self playerReports]; }];
+ }
+ [self after:21 do:^{
+ [self report:label];
+ exit(0);
+ }];
+ return YES;
+}
+
+@end
+
+__attribute__((constructor(101))) static void sgr_harnessDefaults(void) {
+ [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"spotifyglass.redesign"];
+}
+
+int main(int argc, char *argv[]) {
+ @autoreleasepool {
+ return UIApplicationMain(argc, argv, nil, NSStringFromClass(SGRHarnessDelegate.class));
+ }
+}
diff --git a/tweak/Sources/Redesigned/Player/Player.h b/tweak/Sources/Redesigned/Player/Player.h
index 2598a77a..48cb2567 100644
--- a/tweak/Sources/Redesigned/Player/Player.h
+++ b/tweak/Sources/Redesigned/Player/Player.h
@@ -14,6 +14,9 @@
// PlayerScroll.x the list pinned to the top, so the player is one screen and cannot be scrolled
// PlayerLyrics.x the lyrics in the player: the cover as a thumbnail, the title up beside it
// PlayerGestures.x the gestures' hookup
+// PlayerMenu.x Speed and pitch, an expandable row with two sliders in the more button's menu
+// PlayerSpeedPitch.x speed and pitch done to Spotify's audio, between its mixer and its speaker unit
+// SGRTimePitch.m Apple's time and pitch unit, pulling the mixer or working in place
//
// Every hook installs only while Redesigned UI is on (SGRedesignedUI); the native look's do not then.
// Threading: main thread only.
@@ -47,6 +50,22 @@ void SGRPlayerToggleLyrics(void);
// (PlayerFooter.x). It returns at once when nothing changed.
void SGRPlayerLyricsChanged(void);
+#pragma mark - the more menu's speed and pitch (PlayerMenu.x, PlayerSpeedPitch.x)
+
+// Marks a menu opened soon after a tap on `button`, the player's more button, as the player's, so it gets
+// Speed and pitch (PlayerHeader.x hands it over; watching it twice does nothing).
+void SGRPlayerMenuWatchMoreButton(UIView *button);
+// The speed Spotify's sound plays at, 1 when normal; lasts until Spotify quits.
+double SGRPlayerSpeed(void);
+// Whether speed can apply: Spotify's output was taken over when it wired it.
+BOOL SGRPlayerSpeedAllowed(void);
+void SGRSetPlayerSpeed(double speed);
+// Semitones Spotify's output is moved by, 0 when it is not; lasts until Spotify quits.
+float SGRPlayerPitch(void);
+void SGRSetPlayerPitch(float semitones);
+// Whether the output could be reached to change its pitch.
+BOOL SGRPlayerPitchAvailable(void);
+
// Alpha 0, no touches, hidden from accessibility, set again on every call: for Spotify's Swift views,
// which SGRSuppress cannot keep (PlayerControls.x).
void SGRPlayerVanish(UIView *view);
diff --git a/tweak/Sources/Redesigned/Player/PlayerHeader.x b/tweak/Sources/Redesigned/Player/PlayerHeader.x
index 7a3ebab9..d163c857 100644
--- a/tweak/Sources/Redesigned/Player/PlayerHeader.x
+++ b/tweak/Sources/Redesigned/Player/PlayerHeader.x
@@ -38,6 +38,7 @@ static void glassInside(UIViewController *unit, NSArray<NSString *> *identifiers
continue;
}
SGRGlassInside(button, &kGlassKey, SGRGlassCircleSize);
+ if ([identifiers[i] isEqualToString:@"Context menu"]) SGRPlayerMenuWatchMoreButton(button);
found++;
}
diff --git a/tweak/Sources/Redesigned/Player/PlayerMenu.x b/tweak/Sources/Redesigned/Player/PlayerMenu.x
new file mode 100644
index 00000000..68f5d95d
--- /dev/null
+++ b/tweak/Sources/Redesigned/Player/PlayerMenu.x
@@ -0,0 +1,487 @@
+// The player's more menu gets Speed and pitch: one row in Spotify's own context menu sheet that opens,
+// right there in the sheet, onto two sliders, the playback speed and the pitch (PlayerSpeedPitch.x
+// applies them).
+//
+// The sheet is Spotify's ContextMenu_InternalImpl.ContextMenuViewController, a table of its rows
+// (ContextMenuTableView, sized to its content). Its rows come from Swift item factories with no way in,
+// so the block is the table's header view, or its footer when Spotify already uses the header, and is
+// laid out by hand at the table's width. Opening and closing it resizes the header inside a table update,
+// so the rows under it move with it, and the table's intrinsic size is invalidated for the sheet to
+// follow.
+//
+// Only the menu the player's more button opens gets it: the button (id=Context menu,
+// trees/lyrics.txt:183, an Encore Tertiary button) is watched from PlayerHeader.x, and a menu shown
+// within a few seconds of its tap, or presented from a now playing controller, is the player's. The
+// first time, the menu's structure is logged, since no recorded tree shows it yet.
+//
+// The block keeps whether it was open for the rest of the session; speed and pitch last until Spotify
+// quits.
+#import <objc/runtime.h>
+#import "Core/SGCore.h"
+#import "Redesigned/Kit/SGRKit.h"
+#import "Redesigned/Haptics/Haptics.h"
+#import "Player.h"
+
+// A menu this soon after the more button's tap is the player's.
+static const NSTimeInterval kMenuAfterTap = 3;
+static const CGFloat kRowHeight = 56, kSliderBlockHeight = 72, kPanelBottom = 12;
+static const float kMinSpeed = 0.5f, kMaxSpeed = 2, kSpeedStep = 0.05f;
+static const float kMaxPitch = 12;
+// Speed is applied at most this often while the slider moves.
+static const NSTimeInterval kSpeedInterval = 0.05;
+
+static NSTimeInterval sg_moreTappedAt;
+static BOOL sg_open;
+static char kBlockKey, kDecidedKey, kWatchedKey;
+
+#pragma mark - the block
+
+@interface SGRSpeedPitchView : UIView
+@property (nonatomic, weak) UITableView *table;
+@property (nonatomic) BOOL inFooter;
+@end
+
+@implementation SGRSpeedPitchView {
+ UIControl *_row;
+ UIImageView *_icon, *_chevron;
+ UILabel *_title, *_summary;
+ UIView *_panel;
+ UILabel *_speedName, *_pitchName;
+ UIButton *_speedValue, *_pitchValue;
+ UISlider *_speed, *_pitch;
+ float _shownSpeed, _shownPitch;
+ NSTimeInterval _speedSentAt;
+ BOOL _speedPending;
+}
+
+static UIImage *symbol(NSString *name, CGFloat size, UIImageSymbolWeight weight) {
+ return [UIImage systemImageNamed:name withConfiguration:[UIImageSymbolConfiguration configurationWithPointSize:size weight:weight]];
+}
+
+static UILabel *makeLabel(UIFont *font, UIColor *color) {
+ UILabel *label = [UILabel new];
+ label.font = font;
+ label.textColor = color;
+ label.adjustsFontForContentSizeCategory = NO;
+ return label;
+}
+
+- (UIButton *)valueButton:(SEL)reset {
+ UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
+ button.titleLabel.font = SGRMonospacedDigitsFont(SGRFont(UIFontTextStyleSubheadline, UIFontWeightSemibold, UIContentSizeCategoryExtraLarge));
+ button.tintColor = SGRPrimary();
+ button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
+ button.accessibilityHint = @"Resets it";
+ [button addTarget:self action:reset forControlEvents:UIControlEventTouchUpInside];
+ return button;
+}
+
+// A glyph centred in a box of one size, so both sliders' tracks start and end at the same x.
+static UIImage *endImage(NSString *name, CGFloat size) {
+ UIImage *glyph = [symbol(name, size, UIImageSymbolWeightMedium) imageWithTintColor:SGRSecondary() renderingMode:UIImageRenderingModeAlwaysOriginal];
+ CGSize box = CGSizeMake(24, 24);
+ return [[[UIGraphicsImageRenderer alloc] initWithSize:box] imageWithActions:^(UIGraphicsImageRendererContext *context) {
+ [glyph drawAtPoint:CGPointMake((box.width - glyph.size.width) / 2, (box.height - glyph.size.height) / 2)];
+ }];
+}
+
+// Both sliders have their normal in the middle of the range rather than at one end, so neither track
+// fills: a tick marks where normal is, under the thumb until it moves away.
+- (UISlider *)slider:(float)min max:(float)max normal:(float)normal minImage:(NSString *)minImage maxImage:(NSString *)maxImage {
+ UISlider *slider = [UISlider new];
+ slider.minimumValue = min;
+ slider.maximumValue = max;
+ UIColor *track = [UIColor colorWithWhite:1 alpha:0.2];
+ slider.minimumTrackTintColor = track;
+ slider.maximumTrackTintColor = track;
+ slider.minimumValueImage = endImage(minImage, 13);
+ slider.maximumValueImage = endImage(maxImage, 17);
+ UIView *tick = [UIView new];
+ tick.backgroundColor = SGRSecondary();
+ tick.layer.cornerRadius = 1;
+ tick.userInteractionEnabled = NO;
+ tick.tag = (NSInteger)(normal * 1000);
+ [slider insertSubview:tick atIndex:0];
+ [slider addTarget:self action:@selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];
+ [slider addTarget:self action:@selector(sliderReleased:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
+ return slider;
+}
+
+// The tick at the normal value's x on the track, found the way the thumb is placed.
+static void placeTick(UISlider *slider) {
+ UIView *tick = slider.subviews.firstObject;
+ if (tick.subviews.count || tick.class != UIView.class) return;
+ CGRect track = [slider trackRectForBounds:slider.bounds];
+ float normal = tick.tag / 1000.0f;
+ CGRect thumb = [slider thumbRectForBounds:slider.bounds trackRect:track value:normal];
+ tick.frame = CGRectMake(roundf(CGRectGetMidX(thumb)) - 1, CGRectGetMidY(track) - 6, 2, 12);
+ [slider sendSubviewToBack:tick];
+}
+
+- (instancetype)initWithFrame:(CGRect)frame {
+ if (!(self = [super initWithFrame:frame])) return nil;
+ self.clipsToBounds = YES;
+ self.backgroundColor = UIColor.clearColor;
+
+ _row = [UIControl new];
+ [_row addTarget:self action:@selector(toggle) forControlEvents:UIControlEventTouchUpInside];
+ [_row addTarget:self action:@selector(rowHighlight) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
+ [_row addTarget:self action:@selector(rowUnhighlight) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel | UIControlEventTouchDragExit];
+ _row.isAccessibilityElement = YES;
+ _row.accessibilityTraits = UIAccessibilityTraitButton;
+ [self addSubview:_row];
+
+ _icon = [[UIImageView alloc] initWithImage:symbol(@"slider.horizontal.3", 20, UIImageSymbolWeightRegular)];
+ _icon.tintColor = SGRSecondary();
+ _icon.contentMode = UIViewContentModeCenter;
+ _title = makeLabel(SGRFont(UIFontTextStyleBody, UIFontWeightRegular, UIContentSizeCategoryExtraLarge), SGRPrimary());
+ _title.text = @"Speed and pitch";
+ _summary = makeLabel(SGRMonospacedDigitsFont(SGRFont(UIFontTextStyleSubheadline, UIFontWeightRegular, UIContentSizeCategoryExtraLarge)), SGRSecondary());
+ _summary.textAlignment = NSTextAlignmentRight;
+ _chevron = [[UIImageView alloc] initWithImage:symbol(@"chevron.down", 13, UIImageSymbolWeightSemibold)];
+ _chevron.tintColor = SGRSecondary();
+ _chevron.contentMode = UIViewContentModeCenter;
+ for (UIView *view in @[_icon, _title, _summary, _chevron]) {
+ view.userInteractionEnabled = NO;
+ [_row addSubview:view];
+ }
+
+ _panel = [UIView new];
+ [self addSubview:_panel];
+ UIFont *nameFont = SGRFont(UIFontTextStyleSubheadline, UIFontWeightRegular, UIContentSizeCategoryExtraLarge);
+ _speedName = makeLabel(nameFont, SGRSecondary());
+ _speedName.text = @"Speed";
+ _pitchName = makeLabel(nameFont, SGRSecondary());
+ _pitchName.text = @"Pitch";
+ _speedValue = [self valueButton:@selector(resetSpeed)];
+ _pitchValue = [self valueButton:@selector(resetPitch)];
+ _speed = [self slider:kMinSpeed max:kMaxSpeed normal:1 minImage:@"tortoise.fill" maxImage:@"hare.fill"];
+ _speed.accessibilityLabel = @"Speed";
+ _pitch = [self slider:-kMaxPitch max:kMaxPitch normal:0 minImage:@"arrow.down" maxImage:@"arrow.up"];
+ _pitch.accessibilityLabel = @"Pitch";
+ for (UIView *view in @[_speedName, _speedValue, _speed, _pitchName, _pitchValue, _pitch]) [_panel addSubview:view];
+
+ [self refresh];
+ return self;
+}
+
++ (CGFloat)heightOpen:(BOOL)open {
+ return kRowHeight + (open ? 2 * kSliderBlockHeight + kPanelBottom : 0);
+}
+
+- (void)layoutSubviews {
+ [super layoutSubviews];
+ CGFloat width = self.bounds.size.width, side = SGRSideMargin;
+ _row.frame = CGRectMake(0, 0, width, kRowHeight);
+ _icon.frame = CGRectMake(side, (kRowHeight - 24) / 2, 24, 24);
+ _chevron.frame = CGRectMake(width - side - 20, (kRowHeight - 20) / 2, 20, 20);
+ CGFloat titleX = side + 24 + side;
+ CGFloat titleWidth = MIN([_title sizeThatFits:CGSizeMake(CGFLOAT_MAX, kRowHeight)].width, CGRectGetMinX(_chevron.frame) - titleX - SGRGrid);
+ _title.frame = CGRectMake(titleX, 0, titleWidth, kRowHeight);
+ CGFloat summaryX = CGRectGetMaxX(_title.frame) + SGRGrid;
+ _summary.frame = CGRectMake(summaryX, 0, MAX(0, CGRectGetMinX(_chevron.frame) - SGRGrid - summaryX), kRowHeight);
+
+ _panel.frame = CGRectMake(0, kRowHeight, width, 2 * kSliderBlockHeight + kPanelBottom);
+ CGFloat y = 0;
+ for (NSArray<UIView *> *line in @[@[_speedName, _speedValue, _speed], @[_pitchName, _pitchValue, _pitch]]) {
+ line[0].frame = CGRectMake(side, y + 4, width / 2 - side, 24);
+ line[1].frame = CGRectMake(width / 2, y + 4, width / 2 - side, 24);
+ line[2].frame = CGRectMake(side, y + 30, width - 2 * side, 36);
+ [line[2] layoutIfNeeded];
+ placeTick((UISlider *)line[2]);
+ y += kSliderBlockHeight;
+ }
+}
+
+#pragma mark state
+
+static float snappedSpeed(float value) {
+ float speed = roundf(value / kSpeedStep) * kSpeedStep;
+ return fabsf(speed - 1) < kSpeedStep * 0.6f ? 1 : speed;
+}
+
+static NSString *speedText(float speed) {
+ return [NSString stringWithFormat:@"%.2f×", speed];
+}
+
+static NSString *pitchText(float pitch) {
+ if (pitch == 0) return @"0";
+ return [NSString stringWithFormat:@"%@%.0f", pitch > 0 ? @"+" : @"−", fabsf(pitch)];
+}
+
+// What the player and the pitch say now, onto the controls (not while a finger is on one).
+- (void)refresh {
+ BOOL speedAllowed = SGRPlayerSpeedAllowed(), pitchAvailable = SGRPlayerPitchAvailable();
+ if (!_speed.tracking) _shownSpeed = snappedSpeed(SGRPlayerSpeed());
+ if (!_pitch.tracking) _shownPitch = SGRPlayerPitch();
+ _speed.value = _shownSpeed;
+ _pitch.value = _shownPitch;
+ _speed.enabled = speedAllowed;
+ _pitch.enabled = pitchAvailable;
+ _speed.alpha = speedAllowed ? 1 : 0.4;
+ _pitch.alpha = pitchAvailable ? 1 : 0.4;
+ [self showValues];
+}
+
+- (void)showValues {
+ BOOL speedAllowed = SGRPlayerSpeedAllowed(), pitchAvailable = SGRPlayerPitchAvailable();
+ [UIView performWithoutAnimation:^{
+ [_speedValue setTitle:speedAllowed ? speedText(_shownSpeed) : @"Unavailable here" forState:UIControlStateNormal];
+ [_pitchValue setTitle:pitchAvailable ? [pitchText(_shownPitch) stringByAppendingString:_shownPitch ? @" st" : @""] : @"Unavailable" forState:UIControlStateNormal];
+ [_speedValue layoutIfNeeded];
+ [_pitchValue layoutIfNeeded];
+ }];
+ _speedValue.enabled = speedAllowed && _shownSpeed != 1;
+ _pitchValue.enabled = pitchAvailable && _shownPitch != 0;
+ [_speedValue setTitleColor:SGRPrimary() forState:UIControlStateDisabled];
+ [_pitchValue setTitleColor:SGRPrimary() forState:UIControlStateDisabled];
+ _speed.accessibilityValue = speedAllowed ? speedText(_shownSpeed) : @"Unavailable";
+ _pitch.accessibilityValue = _shownPitch == 0 ? @"Original pitch" : [NSString stringWithFormat:@"%.0f semitones %@", fabsf(_shownPitch), _shownPitch > 0 ? @"up" : @"down"];
+
+ NSMutableArray<NSString *> *changed = [NSMutableArray array];
+ if (_shownSpeed != 1) [changed addObject:speedText(_shownSpeed)];
+ if (_shownPitch != 0) [changed addObject:[pitchText(_shownPitch) stringByAppendingString:@" st"]];
+ _summary.text = sg_open ? nil : [changed componentsJoinedByString:@" "];
+ _row.accessibilityLabel = changed.count ? [@"Speed and pitch, " stringByAppendingString:[changed componentsJoinedByString:@", "]] : @"Speed and pitch";
+ _row.accessibilityValue = sg_open ? @"Expanded" : @"Collapsed";
+ _chevron.transform = sg_open ? CGAffineTransformMakeRotation(M_PI) : CGAffineTransformIdentity;
+ _panel.alpha = sg_open ? 1 : 0;
+ _panel.accessibilityElementsHidden = !sg_open;
+}
+
+#pragma mark actions
+
+- (void)rowHighlight {
+ _row.backgroundColor = [UIColor colorWithWhite:1 alpha:0.08];
+}
+
+- (void)rowUnhighlight {
+ [UIView animateWithDuration:0.2 animations:^{ _row.backgroundColor = UIColor.clearColor; }];
+}
+
+- (void)toggle {
+ sg_open = !sg_open;
+ if (sg_open) [self refresh];
+ UITableView *table = self.table;
+ CGRect frame = self.frame;
+ frame.size.height = [SGRSpeedPitchView heightOpen:sg_open];
+ SGRAnimate(SGRMotionLayout, ^{
+ [table beginUpdates];
+ self.frame = frame;
+ if (self.inFooter) table.tableFooterView = self;
+ else table.tableHeaderView = self;
+ [self showValues];
+ [self layoutIfNeeded];
+ [table endUpdates];
+ }, ^(BOOL finished) {
+ if (sg_open) [table scrollRectToVisible:[table convertRect:self.bounds fromView:self] animated:YES];
+ });
+ [table invalidateIntrinsicContentSize];
+ UIView *sheet = table.superview;
+ for (int i = 0; sheet && i < 4; i++, sheet = sheet.superview) [sheet setNeedsLayout];
+ UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, sg_open ? _speed : nil);
+ SGLog(@"redesign menu: speed and pitch %@, table %.0f tall showing %.0f", sg_open ? @"opened" : @"closed", table.contentSize.height, table.bounds.size.height);
+}
+
+- (void)sendSpeed {
+ _speedPending = NO;
+ _speedSentAt = CACurrentMediaTime();
+ SGRSetPlayerSpeed(_shownSpeed);
+}
+
+- (void)sliderMoved:(UISlider *)slider {
+ if (slider == _speed) {
+ float speed = snappedSpeed(slider.value);
+ if (speed == _shownSpeed) return;
+ if (speed == 1 || _shownSpeed == 1) SGRPlayFeedback(SGRFeedbackDetent);
+ _shownSpeed = speed;
+ NSTimeInterval wait = kSpeedInterval - (CACurrentMediaTime() - _speedSentAt);
+ if (wait <= 0) [self sendSpeed];
+ else if (!_speedPending) {
+ _speedPending = YES;
+ __weak SGRSpeedPitchView *weakSelf = self;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(wait * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ SGRSpeedPitchView *view = weakSelf;
+ if (view && view->_speedPending) [view sendSpeed];
+ });
+ }
+ } else {
+ float pitch = roundf(slider.value);
+ if (pitch == _shownPitch) return;
+ if (pitch == 0 || _shownPitch == 0) SGRPlayFeedback(SGRFeedbackDetent);
+ _shownPitch = pitch;
+ SGRSetPlayerPitch(pitch);
+ }
+ [self showValues];
+}
+
+- (void)sliderReleased:(UISlider *)slider {
+ if (slider == _speed) {
+ slider.value = _shownSpeed;
+ if (_speedPending || snappedSpeed(SGRPlayerSpeed()) != _shownSpeed) [self sendSpeed];
+ } else {
+ slider.value = _shownPitch;
+ }
+}
+
+- (void)resetSpeed {
+ _shownSpeed = 1;
+ [self sendSpeed];
+ [_speed setValue:1 animated:YES];
+ [self showValues];
+}
+
+- (void)resetPitch {
+ _shownPitch = 0;
+ SGRSetPlayerPitch(0);
+ [_pitch setValue:0 animated:YES];
+ [self showValues];
+}
+
+@end
+
+#pragma mark - the player's more button
+
+@interface SGRMoreTapWatcher : NSObject <UIGestureRecognizerDelegate>
+@end
+
+@implementation SGRMoreTapWatcher
+- (void)tapped {
+ sg_moreTappedAt = CACurrentMediaTime();
+}
+- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)other {
+ return YES;
+}
+@end
+
+void SGRPlayerMenuWatchMoreButton(UIView *button) {
+ if (!button || objc_getAssociatedObject(button, &kWatchedKey)) return;
+ static SGRMoreTapWatcher *watcher;
+ if (!watcher) watcher = [SGRMoreTapWatcher new];
+ objc_setAssociatedObject(button, &kWatchedKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ // An Encore button may read its touches through a gesture recognizer rather than as a control, so
+ // both are watched.
+ if ([button isKindOfClass:UIControl.class]) {
+ [(UIControl *)button addTarget:watcher action:@selector(tapped) forControlEvents:UIControlEventTouchUpInside | UIControlEventPrimaryActionTriggered];
+ }
+ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:watcher action:@selector(tapped)];
+ tap.cancelsTouchesInView = NO;
+ tap.delaysTouchesEnded = NO;
+ tap.delegate = watcher;
+ [button addGestureRecognizer:tap];
+}
+
+#pragma mark - the menu
+
+// The presenter or one of its parents is a now playing controller. Only the chain up is looked at: the
+// root's children include the now playing bar's, which every menu would match.
+static BOOL presentedFromPlayer(UIViewController *menu) {
+ UIViewController *presenter = menu.navigationController.presentingViewController ?: menu.presentingViewController;
+ for (UIViewController *vc = presenter; vc; vc = vc.parentViewController) {
+ if ([NSStringFromClass(vc.class) containsString:@"NowPlaying"]) return YES;
+ }
+ return NO;
+}
+
+static void logStructure(UIView *view, int depth, NSMutableString *out) {
+ if (depth > 6 || out.length > 3000) return;
+ [out appendFormat:@"\n%*s%@ %@", depth * 2, "", NSStringFromClass(view.class), NSStringFromCGRect(view.frame)];
+ for (UIView *child in view.subviews) {
+ if ([child isKindOfClass:UITableViewCell.class]) {
+ [out appendFormat:@"\n%*scell %@", (depth + 1) * 2, "", NSStringFromCGRect(child.frame)];
+ continue;
+ }
+ logStructure(child, depth + 1, out);
+ }
+}
+
+static UITableView *findTable(UIView *root, int depth) {
+ if ([root isKindOfClass:UITableView.class]) return (UITableView *)root;
+ if (depth > 5) return nil;
+ for (UIView *child in root.subviews) {
+ UITableView *table = findTable(child, depth + 1);
+ if (table) return table;
+ }
+ return nil;
+}
+
+static BOOL isPlayerMenu(UIViewController *menu) {
+ NSNumber *decided = objc_getAssociatedObject(menu, &kDecidedKey);
+ if (decided) return decided.boolValue;
+ BOOL tapped = sg_moreTappedAt && CACurrentMediaTime() - sg_moreTappedAt < kMenuAfterTap;
+ BOOL fromPlayer = presentedFromPlayer(menu);
+ BOOL ours = tapped || fromPlayer;
+ if (tapped) sg_moreTappedAt = 0;
+ objc_setAssociatedObject(menu, &kDecidedKey, @(ours), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ UIViewController *presenter = menu.navigationController.presentingViewController ?: menu.presentingViewController;
+ SGLog(@"redesign menu: a context menu, %@ (tapped %d, presented by %@)", ours ? @"the player's" : @"not the player's", tapped,
+ presenter ? NSStringFromClass(presenter.class) : @"nothing");
+ return ours;
+}
+
+static void install(UIViewController *menu) {
+ UIView *root = menu.viewIfLoaded;
+ if (!root || !isPlayerMenu(menu)) return;
+ UITableView *table = findTable(root, 0);
+ SGRSpeedPitchView *block = objc_getAssociatedObject(menu, &kBlockKey);
+ if (!table) {
+ static int logged;
+ if (logged++ < 3) SGLog(@"redesign menu: no table in %@, speed and pitch left out", NSStringFromClass(menu.class));
+ return;
+ }
+ CGFloat width = table.bounds.size.width;
+ if (width <= 0) return;
+ if (!block) {
+ static BOOL described;
+ if (!described) {
+ described = YES;
+ NSMutableString *structure = [NSMutableString string];
+ logStructure(root, 0, structure);
+ SGLog(@"redesign menu: table %@ header %@ footer %@, structure:%@", NSStringFromClass(table.class),
+ table.tableHeaderView ? NSStringFromClass(table.tableHeaderView.class) : @"none",
+ table.tableFooterView ? NSStringFromClass(table.tableFooterView.class) : @"none", structure);
+ }
+ BOOL headerFree = !table.tableHeaderView || table.tableHeaderView.bounds.size.height < 1;
+ BOOL footerFree = !table.tableFooterView || table.tableFooterView.bounds.size.height < 1;
+ if (!headerFree && !footerFree) {
+ SGLog(@"redesign menu: the table's header and footer are both Spotify's, speed and pitch left out");
+ objc_setAssociatedObject(menu, &kDecidedKey, @NO, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ return;
+ }
+ block = [[SGRSpeedPitchView alloc] initWithFrame:CGRectMake(0, 0, width, [SGRSpeedPitchView heightOpen:sg_open])];
+ block.table = table;
+ block.inFooter = !headerFree;
+ objc_setAssociatedObject(menu, &kBlockKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ if (block.inFooter) table.tableFooterView = block;
+ else table.tableHeaderView = block;
+ [table invalidateIntrinsicContentSize];
+ return;
+ }
+ // Spotify replaced the view, or the table changed width: put it back at the table's width.
+ UIView *placed = block.inFooter ? table.tableFooterView : table.tableHeaderView;
+ if (placed != block || fabs(block.frame.size.width - width) > 0.5) {
+ block.frame = CGRectMake(0, block.frame.origin.y, width, [SGRSpeedPitchView heightOpen:sg_open]);
+ if (block.inFooter) table.tableFooterView = block;
+ else table.tableHeaderView = block;
+ [table invalidateIntrinsicContentSize];
+ }
+}
+
+%hook _TtC24ContextMenu_InternalImpl25ContextMenuViewController
+- (void)viewDidLayoutSubviews {
+ %orig;
+ install((UIViewController *)self);
+}
+
+- (void)viewDidAppear:(BOOL)animated {
+ %orig;
+ SGRSpeedPitchView *block = objc_getAssociatedObject(self, &kBlockKey);
+ [block refresh];
+}
+%end
+
+%ctor {
+ if (!SGRedesignedUI()) return;
+ %init;
+ SGRequireClasses(@[@"_TtC24ContextMenu_InternalImpl25ContextMenuViewController"]);
+}
diff --git a/tweak/Sources/Redesigned/Player/PlayerSpeedPitch.x b/tweak/Sources/Redesigned/Player/PlayerSpeedPitch.x
new file mode 100644
index 00000000..d2d44071
--- /dev/null
+++ b/tweak/Sources/Redesigned/Player/PlayerSpeedPitch.x
@@ -0,0 +1,438 @@
+// The player menu's Speed and Pitch (PlayerMenu.x draws them), both done on Spotify's audio.
+//
+// Spotify's player takes a speed only for podcasts: for songs its restrictions refuse it (device test,
+// 2026-09-18: the slider read "Unavailable here"), its own music speed being a per track setting kept on
+// playlist items. So speed, like pitch, is done to the sound, by SGRTimePitch between Spotify's mixer and
+// its speaker unit.
+//
+// Spotify's audio (AudioUnitDriver2 in the binary) is a chain of units it wires with MakeConnection:
+// converter (fed by its decoder through a render callback), EQ, mixer, RemoteIO. Its import of
+// AudioUnitSetProperty is rebound (Core/SGRebind.h), and the one call connecting the mixer to the RemoteIO
+// unit's input is answered by a render callback of this file's instead, which pulls the mixer itself. At
+// normal speed and pitch the callback passes the mixer's sound straight through. Otherwise it renders the
+// time and pitch unit, which pulls the mixer for rate times the frames it hands back, so Spotify's decoder
+// is drained that much faster: the song plays faster or slower, at its own pitch unless Pitch moves it.
+// Switching the unit in or out skips or repeats its 93 ms, so it stays in for a moment after both return
+// to normal, and a finger dragging across normal does not switch it back and forth.
+//
+// Spotify's clock keeps running at its own speed between the player's reports: -[SPTPlayerState position]
+// is positionAsOfTimestamp minus timeIntervalSinceNow times [self playbackSpeed] (disassembly,
+// 0x1057735ec), so playbackSpeed is hooked to include this speed, and the scrubber, the lyrics and the lock
+// screen move with the sound. That bets the player's reported positions follow what the decoder handed
+// over, which is what it counts.
+//
+// When the connection is never seen, pitch falls back to the way it first shipped: a render notify on the
+// RemoteIO unit (after Music Haptics' own rebinding of AudioOutputUnitStart) runs each finished buffer
+// through a unit working in place; speed is then unavailable.
+//
+// Speed and pitch last until Spotify quits.
+//
+// Threading: the render callback and the notify run on the render thread and touch only atomics and the
+// units; Spotify sets its properties and starts its unit on its audio thread; everything else is main
+// thread.
+#import <AudioToolbox/AudioToolbox.h>
+#import <pthread.h>
+#import <stdatomic.h>
+#import "Core/SGCore.h"
+#import "Core/SGRebind.h"
+#import "Headers/SPTPlayer.h"
+#import "Player.h"
+#import "SGRTimePitch.h"
+
+// The unit stays in this long after speed and pitch both came back to normal.
+static const double kOffAfter = 1.5;
+
+#pragma mark - shared between the threads
+
+static float sg_speed = 1, sg_semitones; // main thread
+static atomic_uint sg_speedBits; // sg_speed for SPTPlayerState, read on any thread
+
+// The chain as Spotify wired it: the unit and bus feeding its RemoteIO unit (NULL until the
+// connection was taken over), pulled in chunks no larger than Spotify's own maximum slice.
+static _Atomic(AudioUnit) sg_source;
+static UInt32 sg_sourceBus;
+static atomic_uint sg_chunk = 1024;
+static Float64 sg_sourceTime; // render thread only
+
+// The unit in use and whether the render thread runs it: in the chain (pull), else in place (pitch only).
+static _Atomic(SGRTimePitch *) sg_pull, sg_inPlace;
+static atomic_bool sg_engaged, sg_busy;
+static pthread_mutex_t sg_buildLock = PTHREAD_MUTEX_INITIALIZER;
+
+// The output's format when Spotify started it, for the in place fallback.
+static atomic_uint_fast64_t sg_rateBits;
+static atomic_uint sg_formatFlags, sg_formatChannels, sg_formatBytes;
+
+static BOOL tapped(void) {
+ return atomic_load(&sg_source) != NULL;
+}
+
+static float loadFloat(atomic_uint *slot) {
+ uint32_t bits = atomic_load(slot);
+ float value;
+ memcpy(&value, &bits, sizeof value);
+ return value;
+}
+
+static void storeFloat(atomic_uint *slot, float value) {
+ uint32_t bits;
+ memcpy(&bits, &value, sizeof bits);
+ atomic_store(slot, bits);
+}
+
+#pragma mark - the render thread: the chain
+
+// The source's next `frames` frames into `data`, in chunks of at most sg_chunk frames, each with a sample
+// time of its own, so Spotify's units never see a time twice (an AU renders a time it has seen from cache).
+static OSStatus pullSource(AudioUnit source, const AudioTimeStamp *outputTime, UInt32 frames, AudioBufferList *data) {
+ enum { kMaxBuffers = 8 };
+ UInt32 chunk = atomic_load_explicit(&sg_chunk, memory_order_relaxed);
+ if (data->mNumberBuffers > kMaxBuffers) chunk = frames;
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) if (!data->mBuffers[b].mData) chunk = frames;
+ UInt32 bytesPerFrame[kMaxBuffers];
+ for (UInt32 b = 0; b < data->mNumberBuffers && b < kMaxBuffers; b++) bytesPerFrame[b] = data->mBuffers[b].mDataByteSize / frames;
+
+ for (UInt32 done = 0; done < frames;) {
+ UInt32 count = MIN(chunk, frames - done);
+ struct { AudioBufferList list; AudioBuffer more[kMaxBuffers - 1]; } part;
+ AudioBufferList *target = data;
+ if (count != frames) {
+ part.list.mNumberBuffers = data->mNumberBuffers;
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) {
+ part.list.mBuffers[b] = (AudioBuffer){data->mBuffers[b].mNumberChannels, count * bytesPerFrame[b],
+ (char *)data->mBuffers[b].mData + done * bytesPerFrame[b]};
+ }
+ target = &part.list;
+ }
+ AudioTimeStamp time = outputTime ? *outputTime : (AudioTimeStamp){0};
+ time.mSampleTime = sg_sourceTime;
+ time.mFlags |= kAudioTimeStampSampleTimeValid;
+ AudioUnitRenderActionFlags flags = 0;
+ OSStatus status = AudioUnitRender(source, &flags, &time, sg_sourceBus, count, target);
+ sg_sourceTime += count;
+ if (status != noErr) return status;
+ done += count;
+ }
+ return noErr;
+}
+
+static OSStatus pullForUnit(void *context, UInt32 frames, AudioBufferList *data) {
+ AudioUnit source = atomic_load(&sg_source);
+ if (!source) {
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) if (data->mBuffers[b].mData) memset(data->mBuffers[b].mData, 0, data->mBuffers[b].mDataByteSize);
+ return noErr;
+ }
+ return pullSource(source, NULL, frames, data);
+}
+
+static BOOL fitsUnit(const AudioBufferList *data, UInt32 frames, SGRTimePitch *unit) {
+ if (data->mNumberBuffers != SGRTimePitchChannels(unit) || frames > kSGRTimePitchMaxFrames) return NO;
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) {
+ if (!data->mBuffers[b].mData || data->mBuffers[b].mDataByteSize < frames * sizeof(float)) return NO;
+ }
+ return YES;
+}
+
+// The RemoteIO unit's input, in place of Spotify's connection from the mixer.
+static OSStatus feed(void *refCon, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *timestamp, UInt32 bus,
+ UInt32 frames, AudioBufferList *data) {
+ AudioUnit source = atomic_load(&sg_source);
+ if (!source) {
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) if (data->mBuffers[b].mData) memset(data->mBuffers[b].mData, 0, data->mBuffers[b].mDataByteSize);
+ *flags |= kAudioUnitRenderAction_OutputIsSilence;
+ return noErr;
+ }
+ atomic_store(&sg_busy, true);
+ OSStatus status = -1;
+ SGRTimePitch *unit = atomic_load(&sg_pull);
+ if (atomic_load(&sg_engaged) && unit && fitsUnit(data, frames, unit)) status = SGRTimePitchRender(unit, frames, data);
+ if (status != noErr) status = pullSource(source, timestamp, frames, data);
+ atomic_store(&sg_busy, false);
+ return status;
+}
+
+#pragma mark - the render thread: in place, the fallback
+
+static float sg_scratch[kSGRTimePitchMaxChannels][kSGRTimePitchMaxFrames];
+
+static inline float readSample(const void *data, UInt32 index, UInt32 bytes, BOOL isFloat, UInt32 fraction) {
+ if (bytes == 4) {
+ if (isFloat) return ((const float *)data)[index];
+ int32_t value = ((const int32_t *)data)[index];
+ return fraction ? (float)((double)value / (double)(1u << fraction)) : (float)(value / 2147483648.0);
+ }
+ return ((const int16_t *)data)[index] / 32768.0f;
+}
+
+static inline void writeSample(void *data, UInt32 index, float value, UInt32 bytes, BOOL isFloat, UInt32 fraction) {
+ if (bytes == 4 && isFloat) {
+ ((float *)data)[index] = value;
+ return;
+ }
+ value = fmaxf(-1, fminf(value, 1));
+ if (bytes == 4) {
+ double scale = fraction ? (double)(1u << fraction) : 2147483647.0;
+ ((int32_t *)data)[index] = (int32_t)(value * scale);
+ } else {
+ ((int16_t *)data)[index] = (int16_t)(value * 32767);
+ }
+}
+
+static void shiftInPlace(SGRTimePitch *unit, AudioUnitRenderActionFlags *flags, UInt32 frames, AudioBufferList *data) {
+ UInt32 formatFlags = atomic_load_explicit(&sg_formatFlags, memory_order_relaxed);
+ UInt32 bytes = atomic_load_explicit(&sg_formatBytes, memory_order_relaxed);
+ UInt32 channels = SGRTimePitchChannels(unit);
+ BOOL isFloat = (formatFlags & kAudioFormatFlagIsFloat) != 0;
+ BOOL split = (formatFlags & kAudioFormatFlagIsNonInterleaved) != 0;
+ UInt32 fraction = (formatFlags & kLinearPCMFormatFlagsSampleFractionMask) >> kLinearPCMFormatFlagsSampleFractionShift;
+ if (frames > kSGRTimePitchMaxFrames || (bytes != 2 && bytes != 4)) return;
+ if (split ? data->mNumberBuffers != channels : data->mNumberBuffers != 1 || data->mBuffers[0].mNumberChannels != channels) return;
+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) {
+ if (!data->mBuffers[b].mData || data->mBuffers[b].mDataByteSize < frames * bytes * (split ? 1 : channels)) return;
+ }
+ // Silence still goes through, so the sound the unit holds plays out rather than coming back later.
+ BOOL silent = (*flags & kAudioUnitRenderAction_OutputIsSilence) != 0;
+
+ float *lanes[kSGRTimePitchMaxChannels];
+ BOOL direct = split && isFloat && bytes == 4 && !silent;
+ for (UInt32 c = 0; c < channels; c++) {
+ if (direct) {
+ lanes[c] = data->mBuffers[c].mData;
+ continue;
+ }
+ lanes[c] = sg_scratch[c];
+ const void *source = data->mBuffers[split ? c : 0].mData;
+ for (UInt32 i = 0; i < frames; i++) {
+ lanes[c][i] = silent ? 0 : readSample(source, split ? i : i * channels + c, bytes, isFloat, fraction);
+ }
+ }
+ if (!SGRTimePitchProcess(unit, lanes, frames) || direct) return;
+ for (UInt32 c = 0; c < channels; c++) {
+ void *target = data->mBuffers[split ? c : 0].mData;
+ for (UInt32 i = 0; i < frames; i++) writeSample(target, split ? i : i * channels + c, lanes[c][i], bytes, isFloat, fraction);
+ }
+ *flags &= ~kAudioUnitRenderAction_OutputIsSilence;
+}
+
+static OSStatus rendered(void *refCon, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *timestamp, UInt32 bus,
+ UInt32 frames, AudioBufferList *data) {
+ if (!(*flags & kAudioUnitRenderAction_PostRender) || bus != 0 || !data || !data->mNumberBuffers || !frames) return noErr;
+ if (tapped() || !atomic_load(&sg_engaged)) return noErr;
+ atomic_store(&sg_busy, true);
+ SGRTimePitch *unit = atomic_load(&sg_inPlace);
+ if (atomic_load(&sg_engaged) && unit) shiftInPlace(unit, flags, frames, data);
+ atomic_store(&sg_busy, false);
+ return noErr;
+}
+
+#pragma mark - Spotify's audio thread
+
+static BOOL isRemoteIO(AudioUnit unit) {
+ AudioComponentDescription description = {0};
+ if (!unit || AudioComponentGetDescription(AudioComponentInstanceGetComponent(unit), &description) != noErr) return NO;
+ return description.componentType == kAudioUnitType_Output && description.componentSubType == kAudioUnitSubType_RemoteIO;
+}
+
+static OSStatus (*sg_setProperty)(AudioUnit, AudioUnitPropertyID, AudioUnitScope, AudioUnitElement, const void *, UInt32);
+
+static OSStatus setProperty(AudioUnit unit, AudioUnitPropertyID property, AudioUnitScope scope, AudioUnitElement element,
+ const void *data, UInt32 size) {
+ if (property == kAudioUnitProperty_MaximumFramesPerSlice && data && size >= sizeof(UInt32)) {
+ UInt32 frames = *(const UInt32 *)data;
+ if (frames >= 256) atomic_store(&sg_chunk, frames);
+ }
+ if (property != kAudioUnitProperty_MakeConnection || scope != kAudioUnitScope_Input || element != 0 || !data
+ || size < sizeof(AudioUnitConnection) || !isRemoteIO(unit)) {
+ return sg_setProperty(unit, property, scope, element, data, size);
+ }
+ const AudioUnitConnection *connection = data;
+ if (!connection->sourceAudioUnit) {
+ atomic_store(&sg_source, NULL);
+ AURenderCallbackStruct none = {0};
+ sg_setProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &none, sizeof none);
+ SGLog(@"redesign speed: Spotify disconnected its output");
+ return sg_setProperty(unit, property, scope, element, data, size);
+ }
+ sg_sourceBus = connection->sourceOutputNumber;
+ atomic_store(&sg_source, connection->sourceAudioUnit);
+ AURenderCallbackStruct callback = {feed, NULL};
+ OSStatus status = sg_setProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, sizeof callback);
+ SGLog(@"redesign speed: Spotify's mixer feeds its output through the menu's unit now (status %d)", (int)status);
+ if (status == noErr) return noErr;
+ atomic_store(&sg_source, NULL);
+ return sg_setProperty(unit, property, scope, element, data, size);
+}
+
+static OSStatus (*sg_startOutput)(AudioUnit unit);
+
+static void readFormat(AudioUnit unit) {
+ AudioStreamBasicDescription format = {0};
+ UInt32 size = sizeof format;
+ OSStatus status = AudioUnitGetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, &size);
+ static int logged;
+ if (status != noErr || format.mFormatID != kAudioFormatLinearPCM || format.mSampleRate <= 0) {
+ if (logged++ < 6) SGLog(@"redesign speed: the output is not linear PCM (%d)", (int)status);
+ return;
+ }
+ uint64_t bits;
+ memcpy(&bits, &format.mSampleRate, sizeof bits);
+ atomic_store(&sg_rateBits, bits);
+ atomic_store(&sg_formatFlags, format.mFormatFlags);
+ atomic_store(&sg_formatChannels, format.mChannelsPerFrame);
+ atomic_store(&sg_formatBytes, format.mBitsPerChannel / 8);
+ if (logged++ < 6) SGLog(@"redesign speed: Spotify's output is %.0f Hz, %u channels, %u bits, flags 0x%x, slices of %u, %@",
+ format.mSampleRate, (unsigned)format.mChannelsPerFrame, (unsigned)format.mBitsPerChannel,
+ (unsigned)format.mFormatFlags, atomic_load(&sg_chunk), tapped() ? @"fed through the menu's unit" : @"not taken over");
+}
+
+static void apply(void);
+
+static OSStatus startOutput(AudioUnit unit) {
+ if (unit && isRemoteIO(unit)) {
+ readFormat(unit);
+ AudioUnitRemoveRenderNotify(unit, rendered, NULL);
+ AudioUnitAddRenderNotify(unit, rendered, NULL);
+ // A speed or pitch chosen before the output started, or kept across a new format, applies now.
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (sg_speed != 1 || sg_semitones != 0) apply();
+ });
+ }
+ return sg_startOutput(unit);
+}
+
+#pragma mark - engaging the unit
+
+// Stops the render thread using a unit, and returns once it no longer is.
+static void disengage(void) {
+ atomic_store(&sg_engaged, false);
+ for (int i = 0; i < 200 && atomic_load(&sg_busy); i++) usleep(250);
+}
+
+// The unit for the output's format and the way in use, made when there is none or the format changed. A
+// replaced one is never freed: the render thread may still hold it, and a format change is rare.
+static SGRTimePitch *unitForFormat(void) {
+ double rate;
+ uint64_t bits = atomic_load(&sg_rateBits);
+ memcpy(&rate, &bits, sizeof rate);
+ UInt32 channels = atomic_load(&sg_formatChannels);
+ BOOL pull = tapped();
+ if (pull) {
+ // In the chain the unit hands its buffers to the RemoteIO unit as they are: float, one per channel.
+ UInt32 flags = atomic_load(&sg_formatFlags);
+ BOOL canonical = (flags & kAudioFormatFlagIsFloat) && (flags & kAudioFormatFlagIsNonInterleaved) && atomic_load(&sg_formatBytes) == 4;
+ if (!canonical) {
+ static int logged;
+ if (logged++ < 3) SGLog(@"redesign speed: the output is not float per channel (flags 0x%x), speed cannot apply", (unsigned)flags);
+ return NULL;
+ }
+ }
+ if (rate <= 0 || channels < 1 || channels > kSGRTimePitchMaxChannels) return NULL;
+ _Atomic(SGRTimePitch *) *slot = pull ? &sg_pull : &sg_inPlace;
+ pthread_mutex_lock(&sg_buildLock);
+ SGRTimePitch *unit = atomic_load(slot);
+ if (!unit || SGRTimePitchSampleRate(unit) != rate || SGRTimePitchChannels(unit) != channels) {
+ BOOL wasEngaged = atomic_load(&sg_engaged);
+ disengage();
+ unit = SGRTimePitchCreate(rate, channels, pull ? pullForUnit : NULL, NULL);
+ if (unit) {
+ SGRTimePitchSetRate(unit, pull ? sg_speed : 1);
+ SGRTimePitchSetSemitones(unit, sg_semitones);
+ }
+ atomic_store(slot, unit);
+ SGLog(@"redesign speed: %@ %@ for %.0f Hz, %u channels", unit ? @"a unit" : @"no unit", pull ? @"in the chain" : @"in place", rate, (unsigned)channels);
+ if (unit && wasEngaged) atomic_store(&sg_engaged, true);
+ }
+ pthread_mutex_unlock(&sg_buildLock);
+ return unit;
+}
+
+static void report(void) {
+ SGRTimePitch *unit = atomic_load(tapped() ? &sg_pull : &sg_inPlace);
+ if (!unit) return;
+ SGLog(@"redesign speed: %.2fx, %+.0f st, %u underruns, %u failures, largest pull %u, %.1f s of input", sg_speed, sg_semitones,
+ SGRTimePitchUnderruns(unit), SGRTimePitchFailures(unit), SGRTimePitchLargestPull(unit),
+ SGRTimePitchConsumed(unit) / SGRTimePitchSampleRate(unit));
+}
+
+// Puts the unit in or takes it out for the current speed and pitch.
+static void apply(void) {
+ BOOL normal = sg_speed == 1 && sg_semitones == 0;
+ static NSUInteger change;
+ NSUInteger thisChange = ++change;
+ if (normal) {
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kOffAfter * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ if (thisChange != change || sg_speed != 1 || sg_semitones != 0 || !atomic_load(&sg_engaged)) return;
+ report();
+ disengage();
+ });
+ }
+ SGRTimePitch *unit = unitForFormat();
+ if (!unit) {
+ static int logged;
+ if (!normal && logged++ < 3) SGLog(@"redesign speed: Spotify's output has not started, nothing to change yet");
+ return;
+ }
+ SGRTimePitchSetRate(unit, tapped() ? sg_speed : 1);
+ SGRTimePitchSetSemitones(unit, sg_semitones);
+ if (!normal && !atomic_load(&sg_engaged)) {
+ disengage();
+ SGRTimePitchReset(unit);
+ atomic_store(&sg_engaged, true);
+ }
+}
+
+#pragma mark - the menu's calls
+
+double SGRPlayerSpeed(void) {
+ return sg_speed;
+}
+
+BOOL SGRPlayerSpeedAllowed(void) {
+ return tapped();
+}
+
+void SGRSetPlayerSpeed(double speed) {
+ if (!tapped()) return;
+ sg_speed = (float)speed;
+ storeFloat(&sg_speedBits, sg_speed);
+ apply();
+}
+
+float SGRPlayerPitch(void) {
+ return sg_semitones;
+}
+
+void SGRSetPlayerPitch(float semitones) {
+ if (!sg_startOutput && !tapped()) return;
+ sg_semitones = semitones;
+ apply();
+}
+
+BOOL SGRPlayerPitchAvailable(void) {
+ return sg_startOutput != NULL || tapped();
+}
+
+#pragma mark - Spotify's clock
+
+%hook SPTPlayerState
+- (double)playbackSpeed {
+ double speed = %orig;
+ float ours = loadFloat(&sg_speedBits);
+ return ours > 0 && ours != 1 ? speed * ours : speed;
+}
+%end
+
+%ctor {
+ if (!SGRedesignedUI()) return;
+ storeFloat(&sg_speedBits, 1);
+ if (!SGRebindImport("AudioUnitSetProperty", setProperty, (void **)&sg_setProperty) || !sg_setProperty) {
+ sg_setProperty = NULL;
+ SGLog(@"redesign speed: Spotify does not import AudioUnitSetProperty, the menu offers pitch only");
+ }
+ if (!SGRebindImport("AudioOutputUnitStart", startOutput, (void **)&sg_startOutput) || !sg_startOutput) {
+ sg_startOutput = NULL;
+ SGLog(@"redesign speed: Spotify does not import AudioOutputUnitStart");
+ }
+ %init;
+ SGRequireClasses(@[@"SPTPlayerState"]);
+}
diff --git a/tweak/Sources/Redesigned/Player/SGRTimePitch.h b/tweak/Sources/Redesigned/Player/SGRTimePitch.h
new file mode 100644
index 00000000..2f9dbe37
--- /dev/null
+++ b/tweak/Sources/Redesigned/Player/SGRTimePitch.h
@@ -0,0 +1,55 @@
+// The player menu's Speed and Pitch applied to sound: Apple's own time and pitch unit (AUNewTimePitch,
+// the one behind AVAudioUnitTimePitch), which plays its input faster or slower at the same pitch (rate),
+// and higher or lower at the same speed (semitones).
+//
+// It takes its input one of two ways:
+//
+// - Pulled from a source: the unit asks the source for about rate times the frames it hands back, so a
+// rate over 1 consumes the music faster. This is how speed works, the source being Spotify's mixer.
+// - In place: the buffer is pushed into a FIFO and the unit pulled for as many frames as came in, rate 1
+// only. This is pitch alone on a finished buffer, when there is no source to pull.
+//
+// Measured on the Mac (harness/pitch): the pitch exact to 0.01% at any rate, the rate exact in frames
+// consumed, about 0.2% of a core, and the sound 4096 frames (93 ms at 44.1 kHz) later than it went in.
+//
+// Plain C over audio units: SGRTimePitchRender and SGRTimePitchProcess run on Core Audio's render
+// thread, so they never allocate, lock, log or send a message. The rest is for any other thread.
+// Compiles on the Mac as is.
+#import <AudioToolbox/AudioToolbox.h>
+#import <stdbool.h>
+
+enum { kSGRTimePitchMaxChannels = 2, kSGRTimePitchMaxFrames = 4096 };
+
+typedef struct SGRTimePitch SGRTimePitch;
+
+// Fills `data` (the unit's buffers, one per channel) with the next `frames` frames of input.
+typedef OSStatus (*SGRTimePitchSource)(void *context, UInt32 frames, AudioBufferList *data);
+
+// Float, non-interleaved, `channels` of them, at `sampleRate`, in buffers of up to kSGRTimePitchMaxFrames
+// frames. With a source it pulls from it, without one it works in place. NULL when the unit could not be
+// made. Not on the render thread.
+SGRTimePitch *SGRTimePitchCreate(double sampleRate, UInt32 channels, SGRTimePitchSource source, void *context);
+double SGRTimePitchSampleRate(const SGRTimePitch *unit);
+UInt32 SGRTimePitchChannels(const SGRTimePitch *unit);
+
+// Any thread, taking effect with the next buffer. Rate 1 is normal speed (0.25...4); semitones up or down.
+void SGRTimePitchSetRate(SGRTimePitch *unit, float rate);
+void SGRTimePitchSetSemitones(SGRTimePitch *unit, float semitones);
+
+// Forgets the sound held, so the next buffer starts over from silence. Only while nothing renders.
+void SGRTimePitchReset(SGRTimePitch *unit);
+
+// Pull mode: `frames` frames of output into `data`, float non-interleaved buffers of the unit's channels.
+OSStatus SGRTimePitchRender(SGRTimePitch *unit, UInt32 frames, AudioBufferList *data);
+
+// In place mode: replaces `frames` samples of each channel with the same sound moved in pitch, the unit's
+// latency later. Returns false, leaving them as they were, when it cannot (too many frames, the unit failed).
+bool SGRTimePitchProcess(SGRTimePitch *unit, float *const *channels, UInt32 frames);
+
+// Diagnostics, read from any thread: pulls the FIFO could not fully answer, render errors, the unit's own
+// delay in seconds, the most frames it asked its input for at once, and the input frames it consumed.
+UInt32 SGRTimePitchUnderruns(const SGRTimePitch *unit);
+UInt32 SGRTimePitchFailures(const SGRTimePitch *unit);
+double SGRTimePitchLatency(const SGRTimePitch *unit);
+UInt32 SGRTimePitchLargestPull(const SGRTimePitch *unit);
+uint64_t SGRTimePitchConsumed(const SGRTimePitch *unit);
diff --git a/tweak/Sources/Redesigned/Player/SGRTimePitch.m b/tweak/Sources/Redesigned/Player/SGRTimePitch.m
new file mode 100644
index 00000000..ed9f42da
--- /dev/null
+++ b/tweak/Sources/Redesigned/Player/SGRTimePitch.m
@@ -0,0 +1,181 @@
+#import "SGRTimePitch.h"
+#import <stdatomic.h>
+#import <stdlib.h>
+#import <string.h>
+
+// The FIFO of in place mode, a power of two with room for a few of the largest buffers.
+enum { kFifoFrames = 16384 };
+
+struct SGRTimePitch {
+ AudioUnit unit;
+ double sampleRate;
+ UInt32 channels;
+ SGRTimePitchSource source;
+ void *context;
+ float *fifo[kSGRTimePitchMaxChannels];
+ uint64_t written, read; // render thread only
+ Float64 sampleTime;
+ struct {
+ AudioBufferList list;
+ AudioBuffer more[kSGRTimePitchMaxChannels - 1];
+ } output;
+ float *outputData[kSGRTimePitchMaxChannels];
+ atomic_uint underruns, failures, largestPull;
+ atomic_uint_fast64_t consumed;
+ double latency;
+};
+
+static OSStatus input(void *refCon, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *timestamp, UInt32 bus,
+ UInt32 frames, AudioBufferList *data) {
+ SGRTimePitch *unit = refCon;
+ if (frames > atomic_load_explicit(&unit->largestPull, memory_order_relaxed)) {
+ atomic_store_explicit(&unit->largestPull, frames, memory_order_relaxed);
+ }
+ atomic_fetch_add_explicit(&unit->consumed, frames, memory_order_relaxed);
+ if (unit->source) return unit->source(unit->context, frames, data);
+
+ uint64_t available = unit->written - unit->read;
+ UInt32 served = (UInt32)(available < frames ? available : frames);
+ if (served < frames) atomic_fetch_add_explicit(&unit->underruns, 1, memory_order_relaxed);
+ for (UInt32 c = 0; c < data->mNumberBuffers && c < unit->channels; c++) {
+ float *out = data->mBuffers[c].mData;
+ if (!out) continue;
+ for (UInt32 i = 0; i < served; i++) out[i] = unit->fifo[c][(unit->read + i) & (kFifoFrames - 1)];
+ if (served < frames) memset(out + served, 0, (frames - served) * sizeof(float));
+ data->mBuffers[c].mDataByteSize = frames * sizeof(float);
+ }
+ unit->read += served;
+ return noErr;
+}
+
+SGRTimePitch *SGRTimePitchCreate(double sampleRate, UInt32 channels, SGRTimePitchSource source, void *context) {
+ if (sampleRate <= 0 || channels < 1 || channels > kSGRTimePitchMaxChannels) return NULL;
+ AudioComponentDescription description = {
+ .componentType = kAudioUnitType_FormatConverter,
+ .componentSubType = kAudioUnitSubType_NewTimePitch,
+ .componentManufacturer = kAudioUnitManufacturer_Apple,
+ };
+ AudioComponent component = AudioComponentFindNext(NULL, &description);
+ if (!component) return NULL;
+ SGRTimePitch *unit = calloc(1, sizeof *unit);
+ if (!unit) return NULL;
+ unit->sampleRate = sampleRate;
+ unit->channels = channels;
+ unit->source = source;
+ unit->context = context;
+ if (AudioComponentInstanceNew(component, &unit->unit) != noErr) {
+ free(unit);
+ return NULL;
+ }
+ AudioStreamBasicDescription format = {
+ .mSampleRate = sampleRate,
+ .mFormatID = kAudioFormatLinearPCM,
+ .mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved,
+ .mBytesPerPacket = sizeof(float),
+ .mFramesPerPacket = 1,
+ .mBytesPerFrame = sizeof(float),
+ .mChannelsPerFrame = channels,
+ .mBitsPerChannel = 32,
+ };
+ UInt32 maxFrames = kSGRTimePitchMaxFrames;
+ AURenderCallbackStruct callback = {input, unit};
+ OSStatus status = AudioUnitSetProperty(unit->unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, sizeof format);
+ if (!status) status = AudioUnitSetProperty(unit->unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &format, sizeof format);
+ if (!status) status = AudioUnitSetProperty(unit->unit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFrames, sizeof maxFrames);
+ if (!status) status = AudioUnitSetProperty(unit->unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, sizeof callback);
+ if (!status) status = AudioUnitSetParameter(unit->unit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, 1, 0);
+ if (!status) status = AudioUnitInitialize(unit->unit);
+ if (status) {
+ AudioComponentInstanceDispose(unit->unit);
+ free(unit);
+ return NULL;
+ }
+ Float64 latency = 0;
+ UInt32 size = sizeof latency;
+ if (AudioUnitGetProperty(unit->unit, kAudioUnitProperty_Latency, kAudioUnitScope_Global, 0, &latency, &size) == noErr) {
+ unit->latency = latency;
+ }
+ unit->output.list.mNumberBuffers = channels;
+ for (UInt32 c = 0; c < channels; c++) {
+ if (!source) unit->fifo[c] = calloc(kFifoFrames, sizeof(float));
+ unit->outputData[c] = calloc(kSGRTimePitchMaxFrames, sizeof(float));
+ }
+ return unit;
+}
+
+double SGRTimePitchSampleRate(const SGRTimePitch *unit) {
+ return unit->sampleRate;
+}
+
+UInt32 SGRTimePitchChannels(const SGRTimePitch *unit) {
+ return unit->channels;
+}
+
+void SGRTimePitchSetRate(SGRTimePitch *unit, float rate) {
+ AudioUnitSetParameter(unit->unit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, rate, 0);
+}
+
+void SGRTimePitchSetSemitones(SGRTimePitch *unit, float semitones) {
+ AudioUnitSetParameter(unit->unit, kNewTimePitchParam_Pitch, kAudioUnitScope_Global, 0, semitones * 100, 0);
+}
+
+void SGRTimePitchReset(SGRTimePitch *unit) {
+ AudioUnitReset(unit->unit, kAudioUnitScope_Global, 0);
+ unit->written = unit->read = 0;
+ unit->sampleTime = 0;
+}
+
+OSStatus SGRTimePitchRender(SGRTimePitch *unit, UInt32 frames, AudioBufferList *data) {
+ if (!frames || frames > kSGRTimePitchMaxFrames) return kAudioUnitErr_TooManyFramesToProcess;
+ AudioTimeStamp timestamp = {.mSampleTime = unit->sampleTime, .mFlags = kAudioTimeStampSampleTimeValid};
+ AudioUnitRenderActionFlags flags = 0;
+ OSStatus status = AudioUnitRender(unit->unit, &flags, &timestamp, 0, frames, data);
+ unit->sampleTime += frames;
+ if (status != noErr) atomic_fetch_add_explicit(&unit->failures, 1, memory_order_relaxed);
+ return status;
+}
+
+bool SGRTimePitchProcess(SGRTimePitch *unit, float *const *channels, UInt32 frames) {
+ if (unit->source || !frames || frames > kSGRTimePitchMaxFrames) return false;
+ // A FIFO that would overflow (the unit stopped pulling) starts over rather than wrap onto itself.
+ if (unit->written - unit->read + frames > kFifoFrames) {
+ unit->written = unit->read = 0;
+ atomic_fetch_add_explicit(&unit->failures, 1, memory_order_relaxed);
+ return false;
+ }
+ for (UInt32 c = 0; c < unit->channels; c++) {
+ const float *in = channels[c];
+ for (UInt32 i = 0; i < frames; i++) unit->fifo[c][(unit->written + i) & (kFifoFrames - 1)] = in[i];
+ }
+ unit->written += frames;
+
+ for (UInt32 c = 0; c < unit->channels; c++) {
+ unit->output.list.mBuffers[c] = (AudioBuffer){1, frames * (UInt32)sizeof(float), unit->outputData[c]};
+ }
+ if (SGRTimePitchRender(unit, frames, &unit->output.list) != noErr) return false;
+ for (UInt32 c = 0; c < unit->channels; c++) {
+ const float *out = unit->output.list.mBuffers[c].mData;
+ if (out) memcpy(channels[c], out, frames * sizeof(float));
+ }
+ return true;
+}
+
+UInt32 SGRTimePitchUnderruns(const SGRTimePitch *unit) {
+ return atomic_load((atomic_uint *)&unit->underruns);
+}
+
+UInt32 SGRTimePitchFailures(const SGRTimePitch *unit) {
+ return atomic_load((atomic_uint *)&unit->failures);
+}
+
+double SGRTimePitchLatency(const SGRTimePitch *unit) {
+ return unit->latency;
+}
+
+UInt32 SGRTimePitchLargestPull(const SGRTimePitch *unit) {
+ return atomic_load((atomic_uint *)&unit->largestPull);
+}
+
+uint64_t SGRTimePitchConsumed(const SGRTimePitch *unit) {
+ return atomic_load((atomic_uint_fast64_t *)&unit->consumed);
+}