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: Vibrations in the redesign, taps for the player's controls and Music Haptics played along with the song

Vojtěch Škopek387533dcdc60
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 7644df8e..65e71952 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -4,7 +4,8 @@
tweak/ the Theos project: Makefile, control, the bundle filter plist
tweak/Sources/Core/ what every file builds on: logging, preferences, view-tree walking, glass panes, the
- look this launch runs (SGUIMode.h) and the forced-flag registry (SGFlagForce.h)
+ look this launch runs (SGUIMode.h), the forced-flag registry (SGFlagForce.h) and C
+ functions Spotify imports hooked by rebinding its import slots (SGRebind.h)
tweak/Sources/Headers/ reverse-engineered Spotify classes, one header each, only the selectors used
tweak/Sources/Settings/ the Mod Settings framework: SGPage (a page on Spotify's stack), SGModPage (sections
of rows), SGPageStyle (Spotify's list look), SGGlowSwitch
@@ -113,6 +114,13 @@ Redesigned:
edge, the shelves' headings at the Music app's size, each shortcut tile's cover run across it blurred
(SGRPalette's extension), continuous corners on the covers, and in FLEX builds a meter of each scroll's
frames and the hooks' time (Home.h lists its files)
+ Haptics/ Vibrations (Haptics.h lists its files): a tap of UIKit's feedback generators for the player's and the now
+ playing bar's controls, the scrubber's tenths and ends, cover swipes, gestures and the lyrics page's tap to
+ seek (ControlHaptics.x, SGRFeedback.m); and Music Haptics, Core Haptics playing along with the song:
+ Spotify's import of AudioOutputUnitStart is rebound so its RemoteIO output unit gets a render notify, the
+ samples go through a drum and bass analyzer on the render thread (SGRMusicAnalyzer.m, plain C), and a
+ thread of its own schedules the taps and the rumble for when the sound is heard (MusicHaptics.x). Both
+ switches apply at once; nothing plays while Spotify is not the active app
App:
@@ -127,7 +135,8 @@ which makes every unset switch read off, so a reset is stock Spotify whatever sw
A hook reads its switch when it runs (`SGEnabled`, `SGHidden`, `SGFlag` from Core/SGPrefs.h), so a
change shows after Spotify restarts; the tab editor on the Navbar page is the exception and applies as soon as the bar lays
-out again, as are the Home gradient's colour, strength and height, but not the switch that turns it on. The root page in `App/ModSettings.x` holds the Appearance card and links the page of each part of Spotify, and only the stored look's.
+out again, as are the Home gradient's colour, strength and height, but not the switch that turns it on, and the redesign's
+Vibrations. The root page in `App/ModSettings.x` holds the Appearance card and links the page of each part of Spotify, and only the stored look's.
## Make targets
@@ -158,7 +167,9 @@ naming the source in the redesign, the lock screen, and glass lyrics in the nati
in the native look also Now playing bar (its device button and its flags), Queue & devices, and
Spotify's own player screen (artwork background, glass header buttons, Disable Canvas and the sheet,
header, slider and sticky header flags, the cards under the player and the lyrics preview and player
-buttons to hide). Home & Library, in the native look only:
+buttons to hide); in the redesign instead Vibrations, Controls (on until switched off) and Music Haptics
+(off until switched on, with an ⓘ saying it follows the sound this iPhone plays while Spotify is open), both
+applying straight away. Home & Library, in the native look only:
the Gradient page (the wash behind the top of Home in one of eight colours, at three strengths and
four heights) and the Home flags, the parts of Home to hide including the DJ button and badge, the
playlist header, buttons and pills to hide, and the Library flags. Then Premium, ads & privacy
diff --git a/tweak/Makefile b/tweak/Makefile
index 8c81efdf..3e27c340 100644
--- a/tweak/Makefile
+++ b/tweak/Makefile
@@ -12,7 +12,7 @@ SG_VERSION := $(shell sed -n 's/^Version: //p' control)
# is generated from the IPA by scripts/extract-flags.py.
spotifyglass_FILES := $(shell find Sources -name '*.m' -o -name '*.x' -o -name '*.swift' | sort)
spotifyglass_CFLAGS := -fobjc-arc -ISources -DSG_VERSION=\"$(SG_VERSION)\"
-spotifyglass_FRAMEWORKS := UIKit QuartzCore UniformTypeIdentifiers MediaPlayer CoreImage
+spotifyglass_FRAMEWORKS := UIKit QuartzCore UniformTypeIdentifiers MediaPlayer CoreImage AudioToolbox AVFoundation CoreHaptics
# Runtime swizzling instead of Substrate so the dylib has no extra dependency inside the IPA.
spotifyglass_LOGOS_DEFAULT_GENERATOR := internal
diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.m
index 2ddeb93e..d38236d3 100644
--- a/tweak/Sources/App/Pages.m
+++ b/tweak/Sources/App/Pages.m
@@ -10,6 +10,7 @@
#import "Native/Navbar/Navbar.h"
#import "Native/NowPlayingBar/NowPlayingBar.h"
#import "Native/Player/NowPlaying.h"
+#import "Redesigned/Haptics/Haptics.h"
#import "Redesigned/Navbar/Navbar.h"
#import "Redesigned/Kit/SGRAccent.h"
@@ -77,9 +78,9 @@ UIViewController *SGPlayerSettingsPage(void) {
[pages addObject:SGWithSymbol(SGPageRow(@"Lock screen widget", ^UIViewController *{ return SGLockScreenWidgetPage(); }), @"lock")];
[sections addObject:SGSection(nil, pages)];
if (native) [sections addObjectsFromArray:SGNativePlayerScreenSections()];
+ else [sections addObject:SGRVibrationsSection()];
- return [[SGModPage alloc] initWithTitle:@"Player"
- intro:@"Changes apply after you restart Spotify. Gestures and Blocked artists apply straight away."
- sections:sections
- footer:nil];
+ NSString *intro = native ? @"Changes apply after you restart Spotify. Gestures and Blocked artists apply straight away."
+ : @"Changes apply after you restart Spotify. Gestures, Blocked artists and Vibrations apply straight away.";
+ return [[SGModPage alloc] initWithTitle:@"Player" intro:intro sections:sections footer:nil];
}
diff --git a/tweak/Sources/Core/SGRebind.h b/tweak/Sources/Core/SGRebind.h
new file mode 100644
index 00000000..4539a4a1
--- /dev/null
+++ b/tweak/Sources/Core/SGRebind.h
@@ -0,0 +1,12 @@
+// Hooks a C function Spotify's executable imports, by pointing the executable's own import slot for it
+// (its __got, or __la_symbol_ptr in an older build) at a replacement. Only calls Spotify's own code
+// makes go through the new function; the system's frameworks keep calling the original. This is the
+// fishhook technique, kept to the main executable, for the few places no Objective-C method stands
+// between Spotify and a system call (the audio output unit Music Haptics listens to).
+//
+// Threading: call from a %ctor, before Spotify has run; the slots are written without a lock.
+#import <Foundation/Foundation.h>
+
+// Returns YES when at least one slot named `symbol` (without the leading underscore) was repointed.
+// `original` receives what the first slot held, which is what the replacement calls on.
+BOOL SGRebindImport(const char *symbol, void *replacement, void **original);
diff --git a/tweak/Sources/Core/SGRebind.m b/tweak/Sources/Core/SGRebind.m
new file mode 100644
index 00000000..05c87279
--- /dev/null
+++ b/tweak/Sources/Core/SGRebind.m
@@ -0,0 +1,96 @@
+#import <mach/mach.h>
+#import <mach-o/dyld.h>
+#import <mach-o/loader.h>
+#import <mach-o/nlist.h>
+#import <string.h>
+#import "SGRebind.h"
+#import "SGLog.h"
+
+static const struct mach_header_64 *mainExecutable(intptr_t *slide) {
+ for (uint32_t i = 0; i < _dyld_image_count(); i++) {
+ const struct mach_header *header = _dyld_get_image_header(i);
+ if (header->magic != MH_MAGIC_64 || header->filetype != MH_EXECUTE) continue;
+ *slide = _dyld_get_image_vmaddr_slide(i);
+ return (const struct mach_header_64 *)header;
+ }
+ return NULL;
+}
+
+// Writes one pointer, making its page writable first; a page dyld made read only after binding
+// (__DATA_CONST, __AUTH_CONST) is copied and made read only again.
+static BOOL writeSlot(void **slot, void *value, BOOL readOnly) {
+ vm_size_t page = vm_page_size;
+ vm_address_t start = (vm_address_t)slot & ~(page - 1);
+ kern_return_t result = vm_protect(mach_task_self(), start, page, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
+ if (result != KERN_SUCCESS) {
+ SGLog(@"rebind: vm_protect failed (%d)", result);
+ return NO;
+ }
+ *slot = value;
+ if (readOnly) vm_protect(mach_task_self(), start, page, FALSE, VM_PROT_READ);
+ return YES;
+}
+
+BOOL SGRebindImport(const char *symbol, void *replacement, void **original) {
+ intptr_t slide = 0;
+ const struct mach_header_64 *header = mainExecutable(&slide);
+ if (!header) return NO;
+
+ const struct segment_command_64 *linkedit = NULL;
+ const struct symtab_command *symtab = NULL;
+ const struct dysymtab_command *dysymtab = NULL;
+ const struct load_command *command = (const struct load_command *)(header + 1);
+ for (uint32_t i = 0; i < header->ncmds; i++, command = (const struct load_command *)((const char *)command + command->cmdsize)) {
+ if (command->cmd == LC_SEGMENT_64 && strcmp(((const struct segment_command_64 *)command)->segname, SEG_LINKEDIT) == 0) {
+ linkedit = (const struct segment_command_64 *)command;
+ } else if (command->cmd == LC_SYMTAB) {
+ symtab = (const struct symtab_command *)command;
+ } else if (command->cmd == LC_DYSYMTAB) {
+ dysymtab = (const struct dysymtab_command *)command;
+ }
+ }
+ if (!linkedit || !symtab || !dysymtab || !dysymtab->nindirectsyms) return NO;
+
+ uintptr_t base = (uintptr_t)slide + (uintptr_t)(linkedit->vmaddr - linkedit->fileoff);
+ const struct nlist_64 *symbols = (const struct nlist_64 *)(base + symtab->symoff);
+ const char *strings = (const char *)(base + symtab->stroff);
+ const uint32_t *indirect = (const uint32_t *)(base + dysymtab->indirectsymoff);
+
+ // The symbol's index once, among the undefined ones, rather than a name compared per slot.
+ uint32_t wanted = UINT32_MAX;
+ for (uint32_t i = dysymtab->iundefsym; i < dysymtab->iundefsym + dysymtab->nundefsym && i < symtab->nsyms; i++) {
+ uint32_t offset = symbols[i].n_un.n_strx;
+ if (offset >= symtab->strsize) continue;
+ const char *name = strings + offset;
+ if (name[0] == '_' && strcmp(name + 1, symbol) == 0) {
+ wanted = i;
+ break;
+ }
+ }
+ if (wanted == UINT32_MAX) return NO;
+
+ BOOL rebound = NO;
+ command = (const struct load_command *)(header + 1);
+ for (uint32_t i = 0; i < header->ncmds; i++, command = (const struct load_command *)((const char *)command + command->cmdsize)) {
+ if (command->cmd != LC_SEGMENT_64) continue;
+ const struct segment_command_64 *segment = (const struct segment_command_64 *)command;
+ BOOL readOnly = strcmp(segment->segname, "__DATA_CONST") == 0 || strcmp(segment->segname, "__AUTH_CONST") == 0;
+ const struct section_64 *section = (const struct section_64 *)(segment + 1);
+ for (uint32_t s = 0; s < segment->nsects; s++, section++) {
+ uint32_t type = section->flags & SECTION_TYPE;
+ if (type != S_NON_LAZY_SYMBOL_POINTERS && type != S_LAZY_SYMBOL_POINTERS) continue;
+ void **slots = (void **)((uintptr_t)slide + section->addr);
+ uint64_t count = section->size / sizeof(void *);
+ for (uint64_t k = 0; k < count; k++) {
+ uint64_t entry = (uint64_t)section->reserved1 + k;
+ if (entry >= dysymtab->nindirectsyms || indirect[entry] != wanted) continue;
+ if (slots[k] == replacement) continue;
+ void *held = slots[k];
+ if (!writeSlot(&slots[k], replacement, readOnly)) continue;
+ if (original && !*original) *original = held;
+ rebound = YES;
+ }
+ }
+ }
+ return rebound;
+}
diff --git a/tweak/Sources/Redesigned/Haptics/ControlHaptics.x b/tweak/Sources/Redesigned/Haptics/ControlHaptics.x
new file mode 100644
index 00000000..c12f9e48
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/ControlHaptics.x
@@ -0,0 +1,243 @@
+// Vibrations > Controls: a tap for what a finger does to playback, in the full screen player and the
+// now playing bar (SGRFeedback.m holds which tap each gets).
+//
+// Buttons: every control's action passes -[UIControl sendAction:to:forEvent:] (target and action) or
+// -[UIControl sendAction:] (a UIAction), so the controls are told apart there by accessibility
+// identifier, after they have acted. Tree (trees/clean/player/01.txt): id=SPTNowPlayingPlayButton is the
+// PlayButtonView (:266, and the sticky header's :492) around the UIButton that acts (CondensedButton, :272),
+// in the bar too (trees/clean/home/01.txt:1281); id=SPTNowPlayingPreviousTrackButton and ...NextTrackButton
+// are Encore buttons (:257, :282); id=Components.UI.ShuffleButton and Nowplaying-RepeatButton are
+// EncoreButtons, UIButtons (:247, :290); id=Components.UI.AddToButton is a UIButton (:205, :425 on a card,
+// home/01.txt:1296 in the bar).
+// Play or pause is read off the player's state before the button acts, since the tap turns it.
+//
+// Scrubbing: the position slider is NowPlaying_ECMKit.ProgressBar.Slider, a UISlider
+// (id=SPTNowPlayingSliderV2, :216) that overrides UIControl's begin, continue and endTracking
+// (objc-methods.txt:83559); a tap when it is taken, a tick at every tenth of the song it passes, a firmer one at
+// either end, a soft one when it lets go.
+//
+// Cover swipes: the player's covers are a list of pages scrolled sideways (AccessibleCollectionView,
+// :28) and so are the bar's titles (NowPlaying_BarImpl.InformationCollectionView, home/01.txt:1104);
+// a tick when a drag or its fling carries the list past halfway to the next page, where letting go skips.
+//
+// Gestures (Shared/Gestures) tell of each action a double tap performs; the lyrics page's tap to seek
+// plays its tap in Redesigned/Lyrics/SGRKaraokeView.m.
+#import "Core/SGCore.h"
+#import "Redesigned/Kit/SGRKit.h"
+#import "Shared/Gestures/Gestures.h"
+#import "Haptics.h"
+
+// How close to either end the scrubber is at it.
+static const float kScrubEdge = 0.005f;
+// A tenth is only passed once the scrubber is this far past it, so a finger resting on one ticks once.
+static const float kScrubHysteresis = 0.004f;
+// The share of a page past halfway a swipe has to reach before it ticks.
+static const CGFloat kPageHysteresis = 0.02;
+// The same control acting twice this quickly (a target and a UIAction, or two targets) is one tap.
+static const CFTimeInterval kSameTap = 0.1;
+
+#pragma mark - buttons
+
+typedef NS_ENUM(NSInteger, ControlKind) {
+ ControlNone,
+ ControlPlay,
+ ControlSkip,
+ ControlToggle,
+ ControlAdd,
+};
+
+static ControlKind kindOf(UIControl *control) {
+ static NSDictionary<NSString *, NSNumber *> *kinds;
+ if (!kinds) {
+ kinds = @{
+ @"SPTNowPlayingPlayButton": @(ControlPlay),
+ @"SPTNowPlayingPreviousTrackButton": @(ControlSkip),
+ @"SPTNowPlayingNextTrackButton": @(ControlSkip),
+ @"Components.UI.ShuffleButton": @(ControlToggle),
+ @"Nowplaying-RepeatButton": @(ControlToggle),
+ @"Components.UI.AddToButton": @(ControlAdd),
+ };
+ }
+ NSString *identifier = control.accessibilityIdentifier;
+ NSNumber *kind = identifier.length ? kinds[identifier] : nil;
+ // The play button's identifier is on the PlayButtonView around the UIButton that acts.
+ if (!kind && !identifier.length) kind = kinds[control.superview.accessibilityIdentifier ?: @""];
+ return (ControlKind)kind.integerValue;
+}
+
+// A touch that has not ended is a control acting on touch down or while dragged, not a tap.
+static BOOL isTap(UIEvent *event) {
+ NSSet<UITouch *> *touches = event.allTouches;
+ if (!touches.count) return YES;
+ for (UITouch *touch in touches) {
+ if (touch.phase == UITouchPhaseEnded) return YES;
+ }
+ return NO;
+}
+
+static void controlActed(UIControl *control, ControlKind kind, BOOL wasPaused) {
+ static __weak UIControl *lastControl;
+ static CFTimeInterval lastTime;
+ CFTimeInterval now = CACurrentMediaTime();
+ if (control == lastControl && now - lastTime < kSameTap) return;
+ lastControl = control;
+ lastTime = now;
+ switch (kind) {
+ case ControlNone: return;
+ case ControlPlay: SGRPlayFeedback(wasPaused ? SGRFeedbackPlay : SGRFeedbackPause); break;
+ case ControlSkip: SGRPlayFeedback(SGRFeedbackSkip); break;
+ case ControlToggle: SGRPlayFeedback(SGRFeedbackToggle); break;
+ case ControlAdd: SGRPlayFeedback(SGRFeedbackAdd); break;
+ }
+ static NSMutableSet<NSNumber *> *logged;
+ if (!logged) logged = [NSMutableSet set];
+ if ([logged containsObject:@(kind)]) return;
+ [logged addObject:@(kind)];
+ SGLog(@"redesign haptics: %@ (%@) acted, kind %ld", control.accessibilityIdentifier ?: control.superview.accessibilityIdentifier, NSStringFromClass(control.class), (long)kind);
+}
+
+%hook UIControl
+- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
+ ControlKind kind = kindOf(self);
+ if (kind == ControlNone || !isTap(event)) {
+ %orig;
+ return;
+ }
+ BOOL wasPaused = SGRPlayerState().isPaused;
+ %orig;
+ controlActed(self, kind, wasPaused);
+}
+
+- (void)sendAction:(UIAction *)action {
+ ControlKind kind = kindOf(self);
+ if (kind == ControlNone) {
+ %orig;
+ return;
+ }
+ BOOL wasPaused = SGRPlayerState().isPaused;
+ %orig;
+ controlActed(self, kind, wasPaused);
+}
+%end
+
+#pragma mark - scrubbing
+
+static int sg_scrubTenth;
+static BOOL sg_scrubAtEdge;
+static NSUInteger sg_scrubTicks;
+
+static float positionOf(UISlider *slider) {
+ float span = slider.maximumValue - slider.minimumValue;
+ return span > 0 ? (slider.value - slider.minimumValue) / span : 0;
+}
+
+static BOOL atEdge(float position) {
+ return position <= kScrubEdge || position >= 1 - kScrubEdge;
+}
+
+static void scrubBegan(UISlider *slider) {
+ float position = positionOf(slider);
+ sg_scrubTenth = (int)floor(MIN(position, 0.9999f) * 10);
+ sg_scrubAtEdge = atEdge(position);
+ sg_scrubTicks = 0;
+ SGRPlayFeedback(SGRFeedbackGrab);
+ SGRPrepareFeedback(SGRFeedbackDetent);
+}
+
+static void scrubMoved(UISlider *slider) {
+ float position = positionOf(slider);
+ BOOL edge = atEdge(position);
+ if (edge) {
+ if (!sg_scrubAtEdge) SGRPlayFeedback(SGRFeedbackEdge);
+ sg_scrubAtEdge = YES;
+ sg_scrubTenth = (int)floor(MIN(position, 0.9999f) * 10);
+ return;
+ }
+ sg_scrubAtEdge = NO;
+ int tenth = (int)floor(position * 10);
+ if (tenth == sg_scrubTenth) return;
+ float boundary = (tenth > sg_scrubTenth ? tenth : sg_scrubTenth) / 10.0f;
+ if (fabsf(position - boundary) < kScrubHysteresis) return;
+ sg_scrubTenth = tenth;
+ sg_scrubTicks++;
+ SGRPlayFeedback(SGRFeedbackDetent);
+}
+
+%hook _TtCO17NowPlaying_ECMKit11ProgressBar6Slider
+- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
+ BOOL tracking = %orig;
+ if (tracking) scrubBegan((UISlider *)self);
+ return tracking;
+}
+
+- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
+ BOOL tracking = %orig;
+ scrubMoved((UISlider *)self);
+ return tracking;
+}
+
+- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
+ %orig;
+ SGRPlayFeedback(SGRFeedbackRelease);
+ static NSUInteger logged;
+ if (logged++ < 3) SGLog(@"redesign haptics: scrubbed to %.2f of the song, %lu ticks", positionOf((UISlider *)self), (unsigned long)sg_scrubTicks);
+}
+%end
+
+#pragma mark - cover swipes
+
+static void pageDetent(UIScrollView *list, NSInteger *lastPage) {
+ CGFloat width = list.bounds.size.width;
+ if (width < 1) return;
+ CGFloat position = list.contentOffset.x / width;
+ NSInteger page = lround(position);
+ if (!list.isTracking && !list.isDecelerating) {
+ *lastPage = page;
+ return;
+ }
+ if (page == *lastPage || fabs(position - *lastPage) < 0.5 + kPageHysteresis) return;
+ *lastPage = page;
+ SGRPlayFeedback(SGRFeedbackDetent);
+}
+
+%hook _TtC35NowPlaying_ContentLayerPlatformImpl24AccessibleCollectionView
+- (void)setContentOffset:(CGPoint)offset {
+ %orig;
+ static NSInteger lastPage;
+ pageDetent((UIScrollView *)self, &lastPage);
+}
+%end
+
+%hook _TtC18NowPlaying_BarImpl25InformationCollectionView
+- (void)setContentOffset:(CGPoint)offset {
+ %orig;
+ static NSInteger lastPage;
+ pageDetent((UIScrollView *)self, &lastPage);
+}
+%end
+
+#pragma mark - gestures
+
+static void gesturePerformed(SGGestureAction action) {
+ switch (action) {
+ case SGGestureNothing: break;
+ case SGGesturePlayPause: SGRPlayFeedback(SGRPlayerState().isPaused ? SGRFeedbackPlay : SGRFeedbackPause); break;
+ case SGGestureSeekBack:
+ case SGGestureSeekForward:
+ case SGGestureNextTrack:
+ case SGGesturePreviousTrack: SGRPlayFeedback(SGRFeedbackSkip); break;
+ case SGGestureShuffle:
+ case SGGestureRepeat: SGRPlayFeedback(SGRFeedbackToggle); break;
+ }
+}
+
+%ctor {
+ if (!SGRedesignedUI()) return;
+ %init;
+ SGGestureSetObserver(^(SGGestureAction action) { gesturePerformed(action); });
+ SGRequireClasses(@[
+ @"_TtCO17NowPlaying_ECMKit11ProgressBar6Slider",
+ @"_TtC35NowPlaying_ContentLayerPlatformImpl24AccessibleCollectionView",
+ @"_TtC18NowPlaying_BarImpl25InformationCollectionView",
+ ]);
+}
diff --git a/tweak/Sources/Redesigned/Haptics/Haptics.h b/tweak/Sources/Redesigned/Haptics/Haptics.h
new file mode 100644
index 00000000..83fac62a
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/Haptics.h
@@ -0,0 +1,40 @@
+// Vibrations, the redesign's haptics (Mod Settings > Player > Vibrations): the Taptic Engine answering
+// what a finger does to playback (Controls), and playing along with the music (Music Haptics).
+//
+// SGRFeedback.m which tap each kind of control gets, played while Controls is on
+// ControlHaptics.x the player's and the now playing bar's controls, the scrubber, the cover swipes, the gestures
+// MusicHaptics.x Spotify's audio output listened to, and Core Haptics played along with it
+// SGRMusicAnalyzer.m the listening: taps and a rumble out of the samples
+// HapticsSettings.m the Vibrations section
+//
+// Both switches apply at once, without a restart. The lyrics page's tap to seek plays its feedback from
+// Redesigned/Lyrics/SGRKaraokeView.m.
+// Threading: main thread only.
+#import <UIKit/UIKit.h>
+
+#define SGRKeyControlHaptics @"spotifyglass.redesign.haptics.controls"
+#define SGRKeyMusicHaptics @"spotifyglass.redesign.haptics.music"
+
+typedef NS_ENUM(NSInteger, SGRFeedback) {
+ SGRFeedbackPlay, // playback starts
+ SGRFeedbackPause, // playback stops
+ SGRFeedbackSkip, // previous, next, a jump in the song (a double tap, a lyric line)
+ SGRFeedbackToggle, // shuffle, repeat
+ SGRFeedbackAdd, // the add button: liked songs, a playlist
+ SGRFeedbackGrab, // a finger takes the scrubber
+ SGRFeedbackDetent, // the scrubber passing a tenth of the song, a cover swipe passing halfway
+ SGRFeedbackEdge, // the scrubber reaching the start or the end
+ SGRFeedbackRelease, // the scrubber let go
+};
+
+// Plays `feedback` while Controls is on.
+void SGRPlayFeedback(SGRFeedback feedback);
+// Wakes the Taptic Engine for feedback about to follow quickly (a finger on the scrubber).
+void SGRPrepareFeedback(SGRFeedback feedback);
+
+// From the Music Haptics switch: starts or stops listening at once.
+void SGRSetMusicHapticsEnabled(BOOL on);
+
+@class SGModSection;
+// The Vibrations section of the Player page.
+SGModSection *SGRVibrationsSection(void);
diff --git a/tweak/Sources/Redesigned/Haptics/HapticsSettings.m b/tweak/Sources/Redesigned/Haptics/HapticsSettings.m
new file mode 100644
index 00000000..9c5ee983
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/HapticsSettings.m
@@ -0,0 +1,17 @@
+// The Vibrations section of the Player page, in the redesign only (App/Pages.m puts it there).
+#import "Core/SGCore.h"
+#import "Settings/SGModPage.h"
+#import "Haptics.h"
+
+static NSString *const kMusicHapticsInfo = @"The iPhone taps along with the drums and rumbles under the bass of whatever Spotify is playing, worked out from the sound as it plays, much like Music Haptics in Apple Music.\n\nIt follows the sound this iPhone plays, through its speaker or headphones, while Spotify is open: iOS plays no haptics for an app in the background, and a song playing on another device through Connect has no sound here to follow.";
+
+SGModSection *SGRVibrationsSection(void) {
+ SGModRow *controls = SGSwitchRow(@"Controls", @"Play, pause, skipping, scrubbing, shuffle, repeat and adding a song", SGRKeyControlHaptics);
+ SGModRow *music = SGOptionRow(@"Music Haptics", @"Taps and rumbles along with the music", SGRKeyMusicHaptics);
+ music.info = kMusicHapticsInfo;
+ music.changed = ^(BOOL on) { SGRSetMusicHapticsEnabled(on); };
+ return SGSection(@"Vibrations", @[
+ SGWithSymbol(controls, @"hand.tap"),
+ SGWithSymbol(music, @"waveform"),
+ ]);
+}
diff --git a/tweak/Sources/Redesigned/Haptics/MusicHaptics.x b/tweak/Sources/Redesigned/Haptics/MusicHaptics.x
new file mode 100644
index 00000000..2b90ba66
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/MusicHaptics.x
@@ -0,0 +1,485 @@
+// Vibrations > Music Haptics: the Taptic Engine playing along with the song, taps on the drums and a
+// rumble under the bass, the way Apple Music's Music Haptics feels, worked out live from the sound.
+//
+// Spotify plays through Core Audio units of its own (AudioUnitDriver2 in the binary: converter, EQ, mixer
+// and a RemoteIO output unit, started with AudioOutputUnitStart(_outputUnit)), with no Objective-C
+// method between it and the unit. So Spotify's import of AudioOutputUnitStart is rebound
+// (Core/SGRebind.h), and every RemoteIO unit it starts gets a render notify: after each render, the
+// buffer bound for the speaker is mixed to mono and handed to the analyzer (SGRMusicAnalyzer.h) on
+// the render thread, which puts what it hears into a ring of events. The render timestamp says when
+// that buffer reaches the output; AVAudioSession's output latency (large over Bluetooth) is added, so
+// a tap lands when its drum is heard. A thread of this file's own takes the events off the ring and
+// schedules them with Core Haptics at those times: each hit a transient, the rumble one looping
+// continuous event whose intensity follows the level.
+//
+// The rebinding and the notify are in place while Redesigned UI is on, so the switch works at once;
+// with the switch off the notify returns straight away. Nothing listens while Spotify is not the active
+// app, since iOS plays no haptics for an app in the background. Sound that is not Spotify's own
+// output (Connect, AirPlay to another device, video) never passes the unit, and plays no haptics.
+//
+// Threading: the notify runs on the render thread and only touches atomics, the analyzer and the
+// ring; the Core Haptics objects belong to the player thread; the switch and the app's state are set
+// on the main thread.
+#import <AudioToolbox/AudioToolbox.h>
+#import <AVFoundation/AVFoundation.h>
+#import <CoreHaptics/CoreHaptics.h>
+#import <mach/mach_time.h>
+#import <pthread.h>
+#import <stdatomic.h>
+#import "Core/SGCore.h"
+#import "Core/SGRebind.h"
+#import "Haptics.h"
+#import "SGRMusicAnalyzer.h"
+
+// Core Haptics takes about this long from a scheduled time to the Taptic Engine's peak.
+static const double kHapticLead = 0.012;
+// A tap this late is left out rather than played off the beat.
+static const double kLatestTap = 0.05;
+// Taps are this strong at most, and the rumble this strong at its level's most.
+static const float kTapGain = 1, kRumbleGain = 1;
+// The rumble starts over this level and stops once it has stayed under the other this long.
+static const float kRumbleStart = 0.05f, kRumbleStop = 0.02f;
+static const double kRumbleStopAfter = 0.3;
+// A level closer than this to the last one sent is skipped, unless it moved by more than kRumbleStep.
+static const double kRumbleInterval = 0.025;
+static const float kRumbleStep = 0.08f;
+static const NSTimeInterval kRumbleLength = 30;
+// Without events for this long, or for this many IO buffers if that is longer, the rumble goes quiet (the
+// output stopped rendering), and after the longer wait the engine is stopped.
+static const double kQuietAfter = 0.1, kQuietAfterBuffers = 2.5, kEngineStopAfter = 5;
+// An engine that would not start is not asked again for this long.
+static const double kStartRetryAfter = 2;
+
+enum { kRingSize = 1024, kMonoFrames = 4096 };
+
+#pragma mark - shared between the threads
+
+static atomic_bool sg_enabled, sg_active, sg_listening;
+static atomic_uint sg_generation;
+static atomic_uint_fast64_t sg_latencyBits;
+static atomic_uint sg_formatFlags, sg_lastFrames;
+static atomic_uint_fast64_t sg_sampleRateBits;
+
+static SGRMusicEvent sg_ring[kRingSize];
+static atomic_uint sg_head, sg_tail; // head moved by the render thread, tail by the player thread
+static dispatch_semaphore_t sg_wake;
+
+static double sg_secondsPerTick;
+
+static void storeDouble(atomic_uint_fast64_t *slot, double value) {
+ uint64_t bits;
+ memcpy(&bits, &value, sizeof bits);
+ atomic_store(slot, bits);
+}
+
+static double loadDouble(atomic_uint_fast64_t *slot) {
+ uint64_t bits = atomic_load(slot);
+ double value;
+ memcpy(&value, &bits, sizeof value);
+ return value;
+}
+
+#pragma mark - the render thread
+
+static SGRMusicAnalyzer sg_analyzer;
+static float sg_mono[kMonoFrames];
+
+static void pushEvent(const SGRMusicEvent *event, void *context) {
+ unsigned head = atomic_load_explicit(&sg_head, memory_order_relaxed);
+ unsigned tail = atomic_load_explicit(&sg_tail, memory_order_relaxed);
+ if (head - tail >= kRingSize) return;
+ sg_ring[head & (kRingSize - 1)] = *event;
+ atomic_store_explicit(&sg_head, head + 1, memory_order_relaxed);
+}
+
+static inline float sampleAt(const AudioBuffer *buffer, UInt32 index, UInt32 bytes, BOOL isFloat, UInt32 fraction) {
+ const void *data = buffer->mData;
+ 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);
+ }
+ if (bytes == 2) return ((const int16_t *)data)[index] / 32768.0f;
+ if (bytes == 8 && isFloat) return (float)((const double *)data)[index];
+ return 0;
+}
+
+// Spotify's own format is float, non-interleaved stereo coming out of its EQ, but the unit's format is
+// read rather than assumed: what the buffers look like decides how they are read.
+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 (!atomic_load_explicit(&sg_listening, memory_order_relaxed)) return noErr;
+ if (*flags & kAudioUnitRenderAction_OutputIsSilence) return noErr;
+
+ double sampleRate = loadDouble(&sg_sampleRateBits);
+ static double analyzedRate;
+ static unsigned analyzedGeneration;
+ unsigned generation = atomic_load_explicit(&sg_generation, memory_order_relaxed);
+ if (sampleRate <= 0) return noErr;
+ if (sampleRate != analyzedRate || generation != analyzedGeneration) {
+ SGRMusicAnalyzerReset(&sg_analyzer, sampleRate, 1 / sg_secondsPerTick);
+ analyzedRate = sampleRate;
+ analyzedGeneration = generation;
+ }
+ atomic_store_explicit(&sg_lastFrames, frames, memory_order_relaxed);
+
+ UInt32 formatFlags = atomic_load_explicit(&sg_formatFlags, memory_order_relaxed);
+ BOOL isFloat = (formatFlags & kAudioFormatFlagIsFloat) != 0;
+ UInt32 fraction = (formatFlags & kLinearPCMFormatFlagsSampleFractionMask) >> kLinearPCMFormatFlagsSampleFractionShift;
+ const AudioBuffer *first = &data->mBuffers[0];
+ BOOL split = data->mNumberBuffers > 1;
+ UInt32 channels = split ? 1 : MAX(first->mNumberChannels, 1u);
+ if (!first->mData || first->mDataByteSize < frames * channels) return noErr;
+ UInt32 bytes = first->mDataByteSize / (frames * channels);
+ const AudioBuffer *second = split && data->mBuffers[1].mData && data->mBuffers[1].mDataByteSize >= frames * bytes ? &data->mBuffers[1] : NULL;
+
+ uint64_t hostTime = (timestamp->mFlags & kAudioTimeStampHostTimeValid) ? timestamp->mHostTime : mach_absolute_time();
+ unsigned headBefore = atomic_load_explicit(&sg_head, memory_order_relaxed);
+ for (UInt32 done = 0; done < frames;) {
+ UInt32 count = MIN(frames - done, (UInt32)kMonoFrames);
+ for (UInt32 i = 0; i < count; i++) {
+ UInt32 frame = done + i;
+ float left, right;
+ if (split) {
+ left = sampleAt(first, frame, bytes, isFloat, fraction);
+ right = second ? sampleAt(second, frame, bytes, isFloat, fraction) : left;
+ } else {
+ left = sampleAt(first, frame * channels, bytes, isFloat, fraction);
+ right = channels > 1 ? sampleAt(first, frame * channels + 1, bytes, isFloat, fraction) : left;
+ }
+ sg_mono[i] = 0.5f * (left + right);
+ }
+ uint64_t chunkTime = hostTime + (uint64_t)(done / sampleRate / sg_secondsPerTick);
+ SGRMusicAnalyzerProcess(&sg_analyzer, sg_mono, count, chunkTime, pushEvent, NULL);
+ done += count;
+ }
+ if (atomic_load_explicit(&sg_head, memory_order_relaxed) != headBefore) dispatch_semaphore_signal(sg_wake);
+ return noErr;
+}
+
+#pragma mark - the output unit
+
+static OSStatus (*sg_startOutput)(AudioUnit unit);
+
+static NSString *fourCC(UInt32 code) {
+ char text[5] = {(char)(code >> 24), (char)(code >> 16), (char)(code >> 8), (char)code, 0};
+ return @(text);
+}
+
+static void listenTo(AudioUnit unit) {
+ AudioComponentDescription description = {0};
+ if (AudioComponentGetDescription(AudioComponentInstanceGetComponent(unit), &description) != noErr) return;
+ static int logged;
+ if (description.componentType != kAudioUnitType_Output || description.componentSubType != kAudioUnitSubType_RemoteIO) {
+ if (logged++ < 8) SGLog(@"music haptics: a started unit is not RemoteIO ('%@' '%@'), not listened to", fourCC(description.componentType), fourCC(description.componentSubType));
+ return;
+ }
+ AudioStreamBasicDescription format = {0};
+ UInt32 size = sizeof(format);
+ OSStatus status = AudioUnitGetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, &size);
+ if (status != noErr || format.mFormatID != kAudioFormatLinearPCM || format.mSampleRate <= 0) {
+ if (logged++ < 8) SGLog(@"music haptics: the output's format is not linear PCM (%d, '%@'), not listened to", (int)status, fourCC(format.mFormatID));
+ return;
+ }
+ atomic_store(&sg_formatFlags, format.mFormatFlags);
+ storeDouble(&sg_sampleRateBits, format.mSampleRate);
+ AudioUnitRemoveRenderNotify(unit, rendered, NULL);
+ status = AudioUnitAddRenderNotify(unit, rendered, NULL);
+ if (logged++ < 8) SGLog(@"music haptics: listening to Spotify's output, %.0f Hz, %u channels, %u bits, flags 0x%x (status %d)", format.mSampleRate, (unsigned)format.mChannelsPerFrame, (unsigned)format.mBitsPerChannel, (unsigned)format.mFormatFlags, (int)status);
+}
+
+static OSStatus startOutput(AudioUnit unit) {
+ if (unit) listenTo(unit);
+ return sg_startOutput(unit);
+}
+
+#pragma mark - the player thread
+
+static CHHapticEngine *sg_engine;
+static BOOL sg_engineRunning;
+static atomic_bool sg_engineStopped;
+static id<CHHapticAdvancedPatternPlayer> sg_rumble;
+static BOOL sg_rumbling;
+static double sg_rumbleSentAt, sg_quietSince;
+static float sg_rumbleSent;
+
+typedef struct {
+ NSUInteger taps, late, levels;
+ double leadSum, leadMin;
+} Stats;
+static Stats sg_stats;
+
+static double hostSeconds(uint64_t ticks) {
+ return ticks * sg_secondsPerTick;
+}
+
+static BOOL startEngine(void) {
+ static BOOL supported, checked;
+ static double retryAt;
+ if (!checked) {
+ checked = YES;
+ supported = CHHapticEngine.capabilitiesForHardware.supportsHaptics;
+ if (!supported) SGLog(@"music haptics: this device has no Taptic Engine for Core Haptics");
+ }
+ if (!supported) return NO;
+ NSError *error = nil;
+ if (!sg_engine) {
+ // Spotify's own session rather than a new one, and haptics only, so nothing touches its audio.
+ sg_engine = [[CHHapticEngine alloc] initWithAudioSession:AVAudioSession.sharedInstance error:&error];
+ if (!sg_engine) {
+ SGLog(@"music haptics: no haptic engine: %@", error);
+ return NO;
+ }
+ sg_engine.playsHapticsOnly = YES;
+ sg_engine.autoShutdownEnabled = NO;
+ sg_engine.stoppedHandler = ^(CHHapticEngineStoppedReason reason) {
+ atomic_store(&sg_engineStopped, true);
+ SGLog(@"music haptics: the engine stopped (reason %ld)", (long)reason);
+ };
+ sg_engine.resetHandler = ^{
+ atomic_store(&sg_engineStopped, true);
+ SGLog(@"music haptics: the engine was reset");
+ };
+ }
+ if (atomic_exchange(&sg_engineStopped, false)) {
+ sg_engineRunning = NO;
+ sg_rumble = nil;
+ sg_rumbling = NO;
+ }
+ if (sg_engineRunning) return YES;
+ double now = hostSeconds(mach_absolute_time());
+ if (now < retryAt) return NO;
+ if (![sg_engine startAndReturnError:&error]) {
+ retryAt = now + kStartRetryAfter;
+ static int logged;
+ if (logged++ < 3) SGLog(@"music haptics: the engine did not start: %@", error);
+ return NO;
+ }
+ sg_engineRunning = YES;
+ return YES;
+}
+
+static void stopEngine(void) {
+ [sg_rumble stopAtTime:CHHapticTimeImmediate error:nil];
+ sg_rumble = nil;
+ sg_rumbling = NO;
+ if (sg_engineRunning) [sg_engine stopWithCompletionHandler:nil];
+ sg_engineRunning = NO;
+}
+
+// When `hostTime`'s sound is heard, in the engine's clock; `lead` is how far ahead of now that is.
+static NSTimeInterval engineTime(uint64_t hostTime, double *lead) {
+ double now = hostSeconds(mach_absolute_time());
+ double heard = hostSeconds(hostTime) + loadDouble(&sg_latencyBits) - kHapticLead;
+ *lead = heard - now;
+ return sg_engine.currentTime + MAX(0, *lead);
+}
+
+static CHHapticEventParameter *parameter(CHHapticEventParameterID identifier, float value) {
+ return [[CHHapticEventParameter alloc] initWithParameterID:identifier value:value];
+}
+
+static void playTap(const SGRMusicEvent *event) {
+ double lead;
+ NSTimeInterval at = engineTime(event->hostTime, &lead);
+ if (lead < -kLatestTap) {
+ sg_stats.late++;
+ return;
+ }
+ CHHapticEvent *hit = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticTransient parameters:@[
+ parameter(CHHapticEventParameterIDHapticIntensity, MIN(1, event->intensity * kTapGain)),
+ parameter(CHHapticEventParameterIDHapticSharpness, event->sharpness),
+ ] relativeTime:0];
+ NSError *error = nil;
+ CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:@[hit] parameters:@[] error:&error];
+ id<CHHapticPatternPlayer> player = pattern ? [sg_engine createPlayerWithPattern:pattern error:&error] : nil;
+ if (!player || ![player startAtTime:at error:&error]) {
+ static int logged;
+ if (logged++ < 3) SGLog(@"music haptics: a tap did not play: %@", error);
+ if (error.code == CHHapticErrorCodeEngineNotRunning || error.code == CHHapticErrorCodeServerInterrupted) atomic_store(&sg_engineStopped, true);
+ return;
+ }
+ sg_stats.taps++;
+ sg_stats.leadSum += lead;
+ sg_stats.leadMin = sg_stats.taps == 1 ? lead : MIN(sg_stats.leadMin, lead);
+}
+
+static void stopRumble(NSTimeInterval at) {
+ if (!sg_rumbling) return;
+ [sg_rumble stopAtTime:at error:nil];
+ sg_rumbling = NO;
+}
+
+static void playLevel(const SGRMusicEvent *event) {
+ sg_stats.levels++;
+ double lead;
+ NSTimeInterval at = engineTime(event->hostTime, &lead);
+ double heard = hostSeconds(event->hostTime);
+ float level = MIN(1, event->intensity * kRumbleGain);
+
+ if (level < kRumbleStop) {
+ if (!sg_quietSince) sg_quietSince = heard;
+ if (heard - sg_quietSince >= kRumbleStopAfter) stopRumble(at);
+ } else {
+ sg_quietSince = 0;
+ }
+ if (!sg_rumbling && level < kRumbleStart) return;
+ if (sg_rumbling && heard - sg_rumbleSentAt < kRumbleInterval && fabsf(level - sg_rumbleSent) < kRumbleStep) return;
+
+ NSError *error = nil;
+ if (!sg_rumble) {
+ // Sharpness 0 under a control that adds the level's own.
+ CHHapticEvent *hum = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:@[
+ parameter(CHHapticEventParameterIDHapticIntensity, 1),
+ parameter(CHHapticEventParameterIDHapticSharpness, 0),
+ ] relativeTime:0 duration:kRumbleLength];
+ CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:@[hum] parameters:@[] error:&error];
+ sg_rumble = pattern ? [sg_engine createAdvancedPlayerWithPattern:pattern error:&error] : nil;
+ sg_rumble.loopEnabled = YES;
+ if (!sg_rumble) {
+ static int logged;
+ if (logged++ < 3) SGLog(@"music haptics: no rumble: %@", error);
+ return;
+ }
+ }
+ NSArray<CHHapticDynamicParameter *> *controls = @[
+ [[CHHapticDynamicParameter alloc] initWithParameterID:CHHapticDynamicParameterIDHapticIntensityControl value:level relativeTime:0],
+ [[CHHapticDynamicParameter alloc] initWithParameterID:CHHapticDynamicParameterIDHapticSharpnessControl value:event->sharpness relativeTime:0],
+ ];
+ if (!sg_rumbling) {
+ [sg_rumble sendParameters:controls atTime:CHHapticTimeImmediate error:nil];
+ if (![sg_rumble startAtTime:at error:&error]) {
+ static int logged;
+ if (logged++ < 3) SGLog(@"music haptics: the rumble did not start: %@", error);
+ sg_rumble = nil;
+ return;
+ }
+ sg_rumbling = YES;
+ } else {
+ [sg_rumble sendParameters:controls atTime:at error:nil];
+ }
+ sg_rumbleSentAt = heard;
+ sg_rumbleSent = level;
+}
+
+static void report(void) {
+ static double lastReport;
+ static int reports;
+ double now = hostSeconds(mach_absolute_time());
+ if (!lastReport) lastReport = now;
+ if (now - lastReport < 30 || reports >= 4 || !sg_stats.levels) return;
+ lastReport = now;
+ reports++;
+ SGLog(@"music haptics: %lu taps (%lu too late), lead %.0f ms on average and %.0f ms at the least, %lu levels, rumble %@, buffers of %u frames, output latency %.0f ms",
+ (unsigned long)sg_stats.taps, (unsigned long)sg_stats.late, sg_stats.taps ? sg_stats.leadSum / sg_stats.taps * 1000 : 0, sg_stats.leadMin * 1000,
+ (unsigned long)sg_stats.levels, sg_rumbling ? @"on" : @"off", atomic_load(&sg_lastFrames), loadDouble(&sg_latencyBits) * 1000);
+ sg_stats = (Stats){0};
+}
+
+static double quietAfter(void) {
+ double rate = loadDouble(&sg_sampleRateBits);
+ double buffer = rate > 0 ? atomic_load(&sg_lastFrames) / rate : 0;
+ return MAX(kQuietAfter, kQuietAfterBuffers * buffer);
+}
+
+static void *playerLoop(void *unused) {
+ pthread_setname_np("spotifyglass.music-haptics");
+ double lastEvent = 0;
+ BOOL idle = YES;
+ for (;;) {
+ dispatch_time_t wait = idle ? DISPATCH_TIME_FOREVER : dispatch_time(DISPATCH_TIME_NOW, (int64_t)(quietAfter() * NSEC_PER_SEC));
+ dispatch_semaphore_wait(sg_wake, wait);
+ @autoreleasepool {
+ double now = hostSeconds(mach_absolute_time());
+ if (!atomic_load(&sg_listening)) {
+ atomic_store(&sg_tail, atomic_load(&sg_head));
+ stopEngine();
+ idle = YES;
+ continue;
+ }
+ BOOL any = NO;
+ unsigned head = atomic_load(&sg_head), tail = atomic_load(&sg_tail);
+ if (tail != head && startEngine()) {
+ for (; tail != head; tail++) {
+ SGRMusicEvent event = sg_ring[tail & (kRingSize - 1)];
+ if (event.kind == SGRMusicEventTap) playTap(&event);
+ else playLevel(&event);
+ }
+ any = YES;
+ }
+ atomic_store(&sg_tail, head);
+ if (any) {
+ lastEvent = now;
+ idle = NO;
+ report();
+ continue;
+ }
+ if (now - lastEvent >= quietAfter()) stopRumble(CHHapticTimeImmediate);
+ if (now - lastEvent >= kEngineStopAfter) {
+ stopEngine();
+ idle = YES;
+ }
+ }
+ }
+ return NULL;
+}
+
+#pragma mark - the switch and the app
+
+static void readLatency(void) {
+ AVAudioSession *session = AVAudioSession.sharedInstance;
+ storeDouble(&sg_latencyBits, session.outputLatency);
+ static int logged;
+ if (logged++ < 6) SGLog(@"music haptics: output latency %.0f ms, IO buffer %.0f ms, route %@", session.outputLatency * 1000, session.IOBufferDuration * 1000, session.currentRoute.outputs.firstObject.portType);
+}
+
+static void updateListening(void) {
+ BOOL listening = atomic_load(&sg_enabled) && atomic_load(&sg_active);
+ if (atomic_exchange(&sg_listening, listening) == listening) return;
+ if (listening) {
+ atomic_fetch_add(&sg_generation, 1);
+ readLatency();
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ pthread_attr_t attributes;
+ pthread_attr_init(&attributes);
+ pthread_attr_set_qos_class_np(&attributes, QOS_CLASS_USER_INTERACTIVE, 0);
+ pthread_t thread;
+ pthread_create(&thread, &attributes, playerLoop, NULL);
+ pthread_attr_destroy(&attributes);
+ });
+ }
+ dispatch_semaphore_signal(sg_wake);
+}
+
+void SGRSetMusicHapticsEnabled(BOOL on) {
+ if (!SGRedesignedUI() || !sg_wake) return;
+ atomic_store(&sg_enabled, on);
+ updateListening();
+}
+
+%ctor {
+ if (!SGRedesignedUI()) return;
+ mach_timebase_info_data_t timebase;
+ mach_timebase_info(&timebase);
+ sg_secondsPerTick = (double)timebase.numer / timebase.denom / 1e9;
+ if (!SGRebindImport("AudioOutputUnitStart", startOutput, (void **)&sg_startOutput) || !sg_startOutput) {
+ SGLog(@"music haptics: Spotify does not import AudioOutputUnitStart, Music Haptics is inactive");
+ return;
+ }
+ sg_wake = dispatch_semaphore_create(0);
+ atomic_store(&sg_enabled, SGFlag(SGRKeyMusicHaptics, NO));
+ NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
+ [center addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {
+ atomic_store(&sg_active, true);
+ updateListening();
+ }];
+ [center addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {
+ atomic_store(&sg_active, false);
+ updateListening();
+ }];
+ [center addObserverForName:AVAudioSessionRouteChangeNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {
+ if (atomic_load(&sg_listening)) readLatency();
+ }];
+}
diff --git a/tweak/Sources/Redesigned/Haptics/SGRFeedback.m b/tweak/Sources/Redesigned/Haptics/SGRFeedback.m
new file mode 100644
index 00000000..1cca3236
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/SGRFeedback.m
@@ -0,0 +1,53 @@
+// The taps of Vibrations > Controls, one per kind of control, from UIKit's feedback generators, so they
+// feel like the rest of iOS: impacts for a press that does something, selection ticks for a value moving
+// under the finger (Apple's Playing haptics: impact complements a physical action, selection a changing
+// value). Starting playback is a crisp tap and stopping it a soft one, so the two can be told apart
+// without looking.
+#import "Core/SGCore.h"
+#import "Haptics.h"
+
+typedef struct {
+ NSInteger style; // a UIImpactFeedbackStyle, or kSelection
+ CGFloat intensity;
+} Tap;
+
+static const NSInteger kSelection = -1;
+
+static Tap tapFor(SGRFeedback feedback) {
+ switch (feedback) {
+ case SGRFeedbackPlay: return (Tap){UIImpactFeedbackStyleRigid, 0.8};
+ case SGRFeedbackPause: return (Tap){UIImpactFeedbackStyleSoft, 1};
+ case SGRFeedbackSkip: return (Tap){UIImpactFeedbackStyleLight, 1};
+ case SGRFeedbackToggle: return (Tap){kSelection, 1};
+ case SGRFeedbackAdd: return (Tap){UIImpactFeedbackStyleMedium, 0.8};
+ case SGRFeedbackGrab: return (Tap){UIImpactFeedbackStyleLight, 0.7};
+ case SGRFeedbackDetent: return (Tap){kSelection, 1};
+ case SGRFeedbackEdge: return (Tap){UIImpactFeedbackStyleRigid, 0.6};
+ case SGRFeedbackRelease: return (Tap){UIImpactFeedbackStyleSoft, 0.8};
+ }
+ return (Tap){kSelection, 1};
+}
+
+static UIFeedbackGenerator *generatorFor(NSInteger style) {
+ static NSMutableDictionary<NSNumber *, UIFeedbackGenerator *> *generators;
+ if (!generators) generators = [NSMutableDictionary dictionary];
+ UIFeedbackGenerator *generator = generators[@(style)];
+ if (!generator) {
+ generator = style == kSelection ? [UISelectionFeedbackGenerator new] : [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyle)style];
+ generators[@(style)] = generator;
+ }
+ return generator;
+}
+
+void SGRPlayFeedback(SGRFeedback feedback) {
+ if (!SGEnabled(SGRKeyControlHaptics)) return;
+ Tap tap = tapFor(feedback);
+ UIFeedbackGenerator *generator = generatorFor(tap.style);
+ if (tap.style == kSelection) [(UISelectionFeedbackGenerator *)generator selectionChanged];
+ else [(UIImpactFeedbackGenerator *)generator impactOccurredWithIntensity:tap.intensity];
+}
+
+void SGRPrepareFeedback(SGRFeedback feedback) {
+ if (!SGEnabled(SGRKeyControlHaptics)) return;
+ [generatorFor(tapFor(feedback).style) prepare];
+}
diff --git a/tweak/Sources/Redesigned/Haptics/SGRMusicAnalyzer.h b/tweak/Sources/Redesigned/Haptics/SGRMusicAnalyzer.h
new file mode 100644
index 00000000..2fd11cee
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/SGRMusicAnalyzer.h
@@ -0,0 +1,80 @@
+// Music Haptics' ear: the samples Spotify sends to the speaker turned into what the Taptic Engine plays,
+// a tap where a drum hits and a level for the rumble under the bass.
+//
+// Two bands are listened to, the bass under 110 Hz (kicks) and the top over 2 kHz (snares, claps). A band
+// hits when its energy over the last few ms (25 for the bass, a cycle of its lowest notes) rises well
+// above its own average of the last second, and may hit again once it has fallen back under that
+// average; the top also needs the middle of the sound (250 Hz to 2 kHz) to lift with it, which a snare
+// or a clap does and a hi-hat does not. A tap's intensity is how far over the average the hit went,
+// scaled by how loud the band is against its recent peak, and its sharpness is how bright the sound is.
+// The level follows the bass envelope against the same peak, so quiet passages rumble less.
+//
+// Tuned on the Mac against librosa's onsets for a 172 BPM track (precision 0.88, recall 0.82, 3.3 taps a
+// second) and a synthetic 124 BPM beat over a held bass (57 of 58 kicks and all 29 snares, no hi-hats).
+//
+// Plain C over a struct the caller owns: it runs on Core Audio's render thread, so nothing here
+// allocates, locks, logs or sends a message. Compiles on the Mac as is, for tuning against a file.
+#import <Foundation/Foundation.h>
+
+typedef NS_ENUM(uint8_t, SGRMusicEventKind) {
+ SGRMusicEventTap, // a hit: one transient
+ SGRMusicEventLevel, // the continuous vibration from here on, sent about 60 times a second
+};
+
+typedef struct {
+ uint64_t hostTime; // mach absolute time of the sound, as the render timestamp gave it
+ float intensity; // 0...1
+ float sharpness; // 0...1
+ SGRMusicEventKind kind;
+} SGRMusicEvent;
+
+typedef void (*SGRMusicEmit)(const SGRMusicEvent *event, void *context);
+
+enum { SGRMusicHistoryLength = 256, SGRMusicAttackHops = 4, SGRMusicWindowHops = 4 };
+
+typedef struct {
+ double b0, b1, b2, a1, a2;
+ double z1, z2;
+} SGRMusicBiquad;
+
+typedef struct {
+ float history[SGRMusicHistoryLength]; // the energy of each hop of the last second
+ int count, next;
+ double sum;
+ float window[SGRMusicWindowHops]; // the energy of the last hops
+ int windowNext;
+ float lastInstant;
+ float peakDb; // the loudest this band has been lately, falling slowly
+ float recentDb[SGRMusicAttackHops];
+ int recentNext;
+ int rest; // hops before the band may hit again
+ int pending; // hops a hit has been rising for, 0 when none
+ float pendingRatio; // the most the rising hit went over the average
+ float pendingEnergy; // its energy then
+ float lastHit; // the energy of the last hit taken
+ int sinceHit; // hops since then
+ float pendingDb;
+ float pendingBright;
+ uint64_t pendingTime;
+ int armed;
+} SGRMusicBand;
+
+typedef struct {
+ double sampleRate;
+ double ticksPerSecond;
+ int hopFrames, hopFilled;
+ int historyHops;
+ uint64_t hopTime;
+ double lowSum, highSum, midSum, fullSum;
+ SGRMusicBiquad lowPass[2], highPass[2], midPass[2];
+ SGRMusicBand kick, snap, mid; // the bass, the top, and the middle (only its average is read)
+ double bassEnvelope, brightness;
+ int levelCountdown;
+ int sinceKick; // hops since the bass last hit
+} SGRMusicAnalyzer;
+
+// Before the first buffer, and again when the sample rate changes or the sound jumps (a seek).
+void SGRMusicAnalyzerReset(SGRMusicAnalyzer *analyzer, double sampleRate, double ticksPerSecond);
+// `mono` holds `frames` samples in -1...1, the first reaching the output at `hostTime`.
+void SGRMusicAnalyzerProcess(SGRMusicAnalyzer *analyzer, const float *mono, uint32_t frames, uint64_t hostTime,
+ SGRMusicEmit emit, void *context);
diff --git a/tweak/Sources/Redesigned/Haptics/SGRMusicAnalyzer.m b/tweak/Sources/Redesigned/Haptics/SGRMusicAnalyzer.m
new file mode 100644
index 00000000..a0f99e0e
--- /dev/null
+++ b/tweak/Sources/Redesigned/Haptics/SGRMusicAnalyzer.m
@@ -0,0 +1,263 @@
+#import <limits.h>
+#import <math.h>
+#import <string.h>
+#import "SGRMusicAnalyzer.h"
+
+static const double kHopSeconds = 0.0058; // 256 frames at 44.1 kHz
+static const double kAverageSeconds = 1.0; // what a hit is measured against
+static const double kLowCutoff = 110, kHighCutoff = 2000, kMidLowCutoff = 250;
+static const double kWarmupSeconds = 0.12;
+static const float kFloorDb = -62; // quieter than this is silence
+static const float kPeakFallDbPerSecond = 0.5;
+
+// How far over its average a band's energy has to go to hit, how far under it to hit again, how much it
+// has to have risen over the last SGRMusicAttackHops (a swell rises too slowly), the rest after a hit,
+// the dB over the threshold that makes a tap as strong as it gets, how quiet the band may be and still
+// hit, and the level a band is called loud at even when it has not been louder lately. The energy a
+// band is judged by is the mean of its last `windowHops`, which for the bass has to span a cycle of a
+// 40 Hz note (25 ms), or the note's own ripple reads as hits. `midRatio` is how far over its own average
+// the middle of the sound (250 Hz to 2 kHz, over the bass that fills the whole) has to go at the same
+// time: a snare or a clap lifts it, a hi-hat hardly does.
+typedef struct {
+ float ratio, rearm, attackDb;
+ double restSeconds;
+ float rangeDb, floorDb, loudDb;
+ int windowHops;
+ float midRatio;
+} BandTuning;
+static const BandTuning kKickTuning = {1.6f, 1.05f, 3, 0.1, 9, -42, -16, 4, 0};
+static const BandTuning kSnapTuning = {2.2f, 1.0f, 6, 0.18, 9, -52, -26, 2, 1.6f};
+// A hit keeps rising for at most this many hops before it is taken at its highest. Longer finds no
+// more hits (a 172 BPM track gave 3.2 taps a second at 2, 3 and 5) and only makes every tap later.
+static const int kMaxRisingHops = 3;
+// Quieter than the band's loud level by this much, a hit is as weak as a tap gets.
+static const float kLoudRangeDb = 24;
+// A hit this much louder than the last one is taken even while the band rests, once this long has passed:
+// a soft rise just before a big one would otherwise hold the big one back.
+static const float kLouderRatio = 4;
+static const double kLouderAfterSeconds = 0.05;
+// A snap this near a kick is felt as the same hit.
+static const double kSnapAfterKickSeconds = 0.06;
+
+static const float kLevelMax = 0.35f;
+static const float kLevelRangeDb = 15;
+static const double kBassAttackSeconds = 0.01, kBassReleaseSeconds = 0.15, kBrightnessSeconds = 0.2;
+static const int kLevelEveryHops = 3;
+
+static inline float clamp01(float value) {
+ return value < 0 ? 0 : value > 1 ? 1 : value;
+}
+
+static inline float decibels(double energy) {
+ return (float)(10 * log10(energy + 1e-12));
+}
+
+// RBJ's cookbook, Q 0.707; two in a row make 24 dB an octave.
+static void setBiquad(SGRMusicBiquad *filter, double sampleRate, double cutoff, int highPass) {
+ double w = 2 * M_PI * cutoff / sampleRate;
+ double cosw = cos(w), alpha = sin(w) / (2 * M_SQRT1_2);
+ double a0 = 1 + alpha;
+ double b1 = highPass ? -(1 + cosw) : 1 - cosw;
+ double b0 = highPass ? (1 + cosw) / 2 : (1 - cosw) / 2;
+ filter->b0 = b0 / a0;
+ filter->b1 = b1 / a0;
+ filter->b2 = b0 / a0;
+ filter->a1 = -2 * cosw / a0;
+ filter->a2 = (1 - alpha) / a0;
+ filter->z1 = filter->z2 = 0;
+}
+
+static inline double runBiquad(SGRMusicBiquad *filter, double x) {
+ double y = filter->b0 * x + filter->z1;
+ filter->z1 = filter->b1 * x - filter->a1 * y + filter->z2;
+ filter->z2 = filter->b2 * x - filter->a2 * y;
+ return y;
+}
+
+static void resetBand(SGRMusicBand *band) {
+ memset(band, 0, sizeof(*band));
+ band->peakDb = kFloorDb;
+ for (int i = 0; i < SGRMusicAttackHops; i++) band->recentDb[i] = kFloorDb;
+ band->armed = 1;
+}
+
+// A band is loud at its recent peak, or at the tuning's loud level when it has been quieter than that.
+static inline float loudDbOf(const SGRMusicBand *band, const BandTuning *tuning) {
+ return band->peakDb > tuning->loudDb ? band->peakDb : tuning->loudDb;
+}
+
+void SGRMusicAnalyzerReset(SGRMusicAnalyzer *analyzer, double sampleRate, double ticksPerSecond) {
+ memset(analyzer, 0, sizeof(*analyzer));
+ analyzer->sampleRate = sampleRate;
+ analyzer->ticksPerSecond = ticksPerSecond;
+ analyzer->hopFrames = (int)lround(sampleRate * kHopSeconds);
+ if (analyzer->hopFrames < 64) analyzer->hopFrames = 64;
+ int hops = (int)lround(kAverageSeconds * sampleRate / analyzer->hopFrames);
+ analyzer->historyHops = hops < 8 ? 8 : hops > SGRMusicHistoryLength ? SGRMusicHistoryLength : hops;
+ for (int i = 0; i < 2; i++) {
+ setBiquad(&analyzer->lowPass[i], sampleRate, kLowCutoff, 0);
+ setBiquad(&analyzer->highPass[i], sampleRate, kHighCutoff, 1);
+ }
+ setBiquad(&analyzer->midPass[0], sampleRate, kMidLowCutoff, 1);
+ setBiquad(&analyzer->midPass[1], sampleRate, kHighCutoff, 0);
+ resetBand(&analyzer->kick);
+ resetBand(&analyzer->snap);
+ resetBand(&analyzer->mid);
+ analyzer->sinceKick = 1 << 20;
+}
+
+static double hopDuration(const SGRMusicAnalyzer *analyzer) {
+ return analyzer->hopFrames / analyzer->sampleRate;
+}
+
+// Moves a band on by one hop's energy: the energy of its last `windowHops` hops, and the average of the
+// last second before this hop in `average`.
+static float track(SGRMusicAnalyzer *analyzer, SGRMusicBand *band, int windowHops, double energy, double *average) {
+ band->window[band->windowNext] = (float)energy;
+ band->windowNext = (band->windowNext + 1) % SGRMusicWindowHops;
+ double sum = 0;
+ for (int i = 1; i <= windowHops; i++) sum += band->window[(band->windowNext - i + SGRMusicWindowHops) % SGRMusicWindowHops];
+ float instant = (float)(sum / windowHops);
+ *average = band->count ? band->sum / band->count : instant;
+
+ if (band->count == analyzer->historyHops) band->sum -= band->history[band->next];
+ else band->count++;
+ band->history[band->next] = (float)energy;
+ band->sum += energy;
+ if (band->sum < 0) band->sum = 0;
+ band->next = (band->next + 1) % analyzer->historyHops;
+ return instant;
+}
+
+// Takes one hop's energy; returns 1 and fills `event` when a hit that was rising has peaked.
+static int listen(SGRMusicAnalyzer *analyzer, SGRMusicBand *band, const BandTuning *tuning, double energy,
+ uint64_t time, float brightness, float midRatio, SGRMusicEvent *event) {
+ double hop = hopDuration(analyzer);
+ double average;
+ float instant = track(analyzer, band, tuning->windowHops, energy, &average);
+
+ float db = decibels(instant);
+ band->peakDb = fmax(db, band->peakDb - kPeakFallDbPerSecond * hop);
+ if (band->peakDb < kFloorDb) band->peakDb = kFloorDb;
+ float attack = db - band->recentDb[band->recentNext];
+ band->recentDb[band->recentNext] = db;
+ band->recentNext = (band->recentNext + 1) % SGRMusicAttackHops;
+ if (band->rest > 0) band->rest--;
+ if (band->sinceHit < INT_MAX) band->sinceHit++;
+
+ int fired = 0;
+ float ratio = average > 0 ? (float)(instant / average) : 0;
+ if (band->pending) {
+ if (ratio > band->pendingRatio) {
+ band->pendingRatio = ratio;
+ band->pendingEnergy = instant;
+ band->pendingDb = db;
+ band->pendingBright = brightness;
+ }
+ if (instant < band->lastInstant || band->pending >= kMaxRisingHops) {
+ float over = 10 * log10(band->pendingRatio / tuning->ratio);
+ float strength = clamp01(over / tuning->rangeDb);
+ float loud = clamp01((band->pendingDb - (loudDbOf(band, tuning) - kLoudRangeDb)) / kLoudRangeDb);
+ event->kind = SGRMusicEventTap;
+ event->hostTime = band->pendingTime;
+ event->intensity = (0.45f + 0.55f * strength) * (0.3f + 0.7f * loud);
+ event->sharpness = band->pendingBright;
+ band->lastHit = band->pendingEnergy;
+ band->sinceHit = 0;
+ band->pending = 0;
+ band->armed = 0;
+ band->rest = (int)ceil(tuning->restSeconds / hop);
+ fired = 1;
+ } else {
+ band->pending++;
+ }
+ } else {
+ if (!band->armed && instant < tuning->rearm * average) band->armed = 1;
+ int warm = band->count * hop >= kWarmupSeconds;
+ int free = (band->armed && !band->rest)
+ || (instant > kLouderRatio * band->lastHit && band->sinceHit * hop >= kLouderAfterSeconds);
+ if (warm && free && db > tuning->floorDb && ratio > tuning->ratio && attack > tuning->attackDb && instant > band->lastInstant
+ && midRatio >= tuning->midRatio) {
+ band->pending = 1;
+ band->pendingRatio = ratio;
+ band->pendingEnergy = instant;
+ band->pendingDb = db;
+ band->pendingBright = brightness;
+ // The window crosses the threshold about when half of it holds the hit, so the hit began half a
+ // window back; a haptic lands a touch after its sound rather than before.
+ band->pendingTime = time - (uint64_t)(0.5 * tuning->windowHops * hop * analyzer->ticksPerSecond);
+ }
+ }
+ band->lastInstant = instant;
+ return fired;
+}
+
+static void endHop(SGRMusicAnalyzer *analyzer, SGRMusicEmit emit, void *context) {
+ double n = analyzer->hopFrames;
+ double low = analyzer->lowSum / n, high = analyzer->highSum / n, full = analyzer->fullSum / n;
+ analyzer->lowSum = analyzer->highSum = analyzer->fullSum = 0;
+ double hop = hopDuration(analyzer);
+ uint64_t time = analyzer->hopTime;
+
+ // Brightness: how much of the sound is over 2 kHz, where 0.25 of the energy is already very bright.
+ float share = full > 1e-12 ? (float)(high / full) : 0;
+ float bright = clamp01(share / 0.25f);
+ analyzer->brightness += (bright - analyzer->brightness) * (1 - exp(-hop / kBrightnessSeconds));
+
+ double midAverage;
+ float midInstant = track(analyzer, &analyzer->mid, kSnapTuning.windowHops, analyzer->midSum / n, &midAverage);
+ float midRatio = midAverage > 0 ? (float)(midInstant / midAverage) : 0;
+ analyzer->midSum = 0;
+
+ SGRMusicEvent event;
+ analyzer->sinceKick++;
+ if (listen(analyzer, &analyzer->kick, &kKickTuning, low, time, bright, midRatio, &event)) {
+ event.sharpness = 0.15f + 0.3f * event.sharpness;
+ analyzer->sinceKick = 0;
+ emit(&event, context);
+ }
+ if (listen(analyzer, &analyzer->snap, &kSnapTuning, high, time, bright, midRatio, &event)) {
+ if (analyzer->sinceKick * hop > kSnapAfterKickSeconds) {
+ event.intensity *= 0.8f;
+ event.sharpness = 0.55f + 0.4f * event.sharpness;
+ emit(&event, context);
+ }
+ }
+
+ double coefficient = low > analyzer->bassEnvelope ? kBassAttackSeconds : kBassReleaseSeconds;
+ analyzer->bassEnvelope += (low - analyzer->bassEnvelope) * (1 - exp(-hop / coefficient));
+ if (--analyzer->levelCountdown <= 0) {
+ analyzer->levelCountdown = kLevelEveryHops;
+ float db = decibels(analyzer->bassEnvelope);
+ float loud = loudDbOf(&analyzer->kick, &kKickTuning);
+ float level = decibels(full) < kFloorDb ? 0 : clamp01((db - (loud - kLevelRangeDb)) / kLevelRangeDb);
+ SGRMusicEvent rumble = {
+ .kind = SGRMusicEventLevel,
+ .hostTime = time,
+ .intensity = level * level * kLevelMax,
+ .sharpness = 0.1f + 0.25f * (float)analyzer->brightness,
+ };
+ emit(&rumble, context);
+ }
+}
+
+void SGRMusicAnalyzerProcess(SGRMusicAnalyzer *analyzer, const float *mono, uint32_t frames, uint64_t hostTime,
+ SGRMusicEmit emit, void *context) {
+ if (analyzer->sampleRate <= 0) return;
+ double ticksPerFrame = analyzer->ticksPerSecond / analyzer->sampleRate;
+ for (uint32_t i = 0; i < frames; i++) {
+ if (analyzer->hopFilled == 0) analyzer->hopTime = hostTime + (uint64_t)(i * ticksPerFrame);
+ double x = mono[i] + 1e-18;
+ double low = runBiquad(&analyzer->lowPass[1], runBiquad(&analyzer->lowPass[0], x));
+ double high = runBiquad(&analyzer->highPass[1], runBiquad(&analyzer->highPass[0], x));
+ double mid = runBiquad(&analyzer->midPass[1], runBiquad(&analyzer->midPass[0], x));
+ analyzer->lowSum += low * low;
+ analyzer->highSum += high * high;
+ analyzer->midSum += mid * mid;
+ analyzer->fullSum += x * x;
+ if (++analyzer->hopFilled == analyzer->hopFrames) {
+ analyzer->hopFilled = 0;
+ endHop(analyzer, emit, context);
+ }
+ }
+}
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index d1c60cf7..16e3a9b1 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -4,6 +4,7 @@
#import "SGRKaraokeView.h"
#import "Shared/LyricsSources/LyricsSources.h"
#import "Shared/Player/PlayerEvents.h"
+#import "Redesigned/Haptics/Haptics.h"
static const CGFloat kFontSize = 30, kMargin = 24, kLineGap = 24, kRowTighten = 2;
// The card under the player is a seventh of the page's height, so it gets Spotify's own card type
@@ -430,6 +431,7 @@ static double secant(SGSweepKnot *knots, NSUInteger i) {
for (SGRKaraokeLineView *view in _shown.allValues) {
if (!CGRectContainsPoint(CGRectInset(view.frame, -_margin, -_lineGap / 2), point)) continue;
SGKaraokeSeek(view.line.start);
+ SGRPlayFeedback(SGRFeedbackSkip);
[self followSong];
return;
}
diff --git a/tweak/Sources/Shared/Gestures/Gestures.h b/tweak/Sources/Shared/Gestures/Gestures.h
index e51d0ef0..203267e8 100644
--- a/tweak/Sources/Shared/Gestures/Gestures.h
+++ b/tweak/Sources/Shared/Gestures/Gestures.h
@@ -38,6 +38,10 @@ NSInteger SGGestureCellAt(CGPoint point, CGSize size);
UIViewController *SGGesturesSettingsPage(void);
+// Told of each action a double tap is about to perform, before the player has acted on it; one observer,
+// for a look that answers the gesture (the redesign's haptics). Main thread.
+void SGGestureSetObserver(void (^observer)(SGGestureAction action));
+
// Puts the double tap on `host`, the view the player's grid covers, while the switch is on; again on
// every layout pass of the host, so taps Spotify adds later still wait for it. Main thread.
void SGGestureAttach(UIView *host);
diff --git a/tweak/Sources/Shared/Gestures/Gestures.x b/tweak/Sources/Shared/Gestures/Gestures.x
index 5ecb5757..163709fe 100644
--- a/tweak/Sources/Shared/Gestures/Gestures.x
+++ b/tweak/Sources/Shared/Gestures/Gestures.x
@@ -9,12 +9,18 @@
#import "Headers/SPTNowPlayingPlaybackController.h"
static __weak SPTNowPlayingPlaybackControllerImplementation *sg_player;
+static void (^sg_observer)(SGGestureAction action);
+
+void SGGestureSetObserver(void (^observer)(SGGestureAction action)) {
+ sg_observer = [observer copy];
+}
#pragma mark - what a cell does
static void perform(SGGestureAction action) {
SPTNowPlayingPlaybackControllerImplementation *player = sg_player;
if (!player) return;
+ if (action != SGGestureNothing && sg_observer) sg_observer(action);
switch (action) {
case SGGestureNothing:
break;