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: Lyrics Timing delays lines for Bluetooth headphones

My-Name-Is-Jeffc8f92cb5004b
diff --git a/docs/tweaks.md b/docs/tweaks.md
index b2268706..505bbee5 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -92,7 +92,12 @@ Shared:
for the lock screen and the Live Activity), a line in another alphabet in Latin letters, offline by Apple's
transforms, kanji read the Japanese way in a song with kana (Romanise.m; checked on the simulator by
harness/lyrics' -romanise), and the Lyrics page's parts. Lines are kept in time order
- whatever order a source lists them in. With EeveeSpotify injected (SGEeveeSpotifyInjected), its lyrics
+ whatever order a source lists them in. The Lyrics page's Delay (Timing, 0 to 1000 ms, either look)
+ shows every line that much later than the song, for Bluetooth headphones: whatever times lines
+ against the position takes it off first (the redesign's lyrics view and its word sweep, the lock
+ screen's line and artwork, the Live Activity, the native look's lyrics page for local files), and a
+ tap on a line seeks to its start plus the delay. Karaoke's audio, the scrubber and the lyrics preview
+ are left alone. It is read at every use, so a change applies at once (checked by harness/lyrics' -check). With EeveeSpotify injected (SGEeveeSpotifyInjected), its lyrics
answer Spotify's requests and the mod's sources and its own requests stand aside
LyricsSources/ the sources lyrics come from, asked in the order the Lyrics page puts them in and merged into the
best answer (LyricsSources.m, the list to drag in LyricsSourcesPage.m): Apple Music's TTML from
diff --git a/harness/local-files/lyrics/main.m b/harness/local-files/lyrics/main.m
index 80d29197..f46bee0a 100644
--- a/harness/local-files/lyrics/main.m
+++ b/harness/local-files/lyrics/main.m
@@ -48,6 +48,7 @@ void SGLyricsPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **st
*texts = @[];
}
SPTPlayerTrack *SGKaraokeTrackFor(NSString *trackID) { return nil; }
+NSInteger SGKaraokeDelayMs(void) { return [NSUserDefaults.standardUserDefaults integerForKey:SGKeyLyricsDelay]; }
#pragma mark - the run
diff --git a/harness/lyrics/main.m b/harness/lyrics/main.m
index b6940c92..4f52f849 100644
--- a/harness/lyrics/main.m
+++ b/harness/lyrics/main.m
@@ -348,6 +348,57 @@ static void checkGlide(SGRKaraokeView *karaoke, dispatch_block_t then) {
});
}
+// A tap as the view's recognizer reports it, at a point in the window.
+@interface SGHarnessTap : UITapGestureRecognizer
+@property (nonatomic) CGPoint point;
+@end
+
+@implementation SGHarnessTap
+- (CGPoint)locationInView:(UIView *)view {
+ return [view convertPoint:self.point fromView:nil];
+}
+@end
+
+// The Lyrics page's Delay of 500 ms: the view times its lines against the song's position less it, and a
+// tap on a line three ahead seeks to that line's start plus it, so once the seek lands the line tapped is
+// the one sung, on the delayed clock.
+static void checkDelay(SGRKaraokeView *karaoke, dispatch_block_t then) {
+ const NSInteger delay = 500;
+ [NSUserDefaults.standardUserDefaults setInteger:delay forKey:SGKeyLyricsDelay];
+ NSArray<SGKaraokeLine *> *lines = [karaoke valueForKey:@"lines"];
+ NSInteger before = SGKaraokePositionMs();
+ NSInteger shown = ((NSInteger (*)(id, SEL))objc_msgSend)(karaoke, NSSelectorFromString(@"positionMs"));
+ NSInteger later = SGKaraokePositionMs();
+ expect(shown >= before - delay && shown <= later - delay,
+ [NSString stringWithFormat:@"with 500 ms of delay the lines are timed at %ld ms, the song at %ld ms", (long)shown, (long)before]);
+ // Two lines, at 1 s and 3 s: at 3.2 s into the song the line shown is still the first.
+ NSArray<SGKaraokeLine *> *pair = @[timed(1000, @"a", nil), timed(3000, @"b", nil)];
+ expect(SGKaraokeLeadLine(pair, 3200 - delay) == 0 && SGKaraokeLeadLine(pair, 3600 - delay) == 1,
+ @"at 3.2 s the line from 1 s shows, at 3.6 s the one from 3 s");
+
+ NSInteger focus = [[karaoke valueForKey:@"focus"] integerValue], target = focus + 3;
+ UIView *view = [karaoke valueForKey:@"shown"][@(target)];
+ if (!view || target >= (NSInteger)lines.count) {
+ expect(NO, @"a line three ahead has a view to tap");
+ then();
+ return;
+ }
+ SGHarnessTap *tap = [SGHarnessTap new];
+ tap.point = [view convertPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)) toView:nil];
+ ((void (*)(id, SEL, id))objc_msgSend)(karaoke, NSSelectorFromString(@"tapped:"), tap);
+ NSInteger start = lines[(NSUInteger)target].start;
+ after(0.8, ^{
+ // The player reports the seek -seekLag (0.4 s) after it, so it has run on about 0.4 s since.
+ NSInteger song = SGKaraokePositionMs();
+ expect(song >= start + delay && song <= start + delay + 600,
+ [NSString stringWithFormat:@"a tap on the line at %ld ms seeks the song to %ld ms (now at %ld ms)", (long)start, (long)(start + delay), (long)song]);
+ NSInteger sung = [[karaoke valueForKey:@"focus"] integerValue];
+ expect(sung > focus && sung <= target, [NSString stringWithFormat:@"and the line tapped (or one sung over it) is the one sung (line %ld for %ld)", (long)sung, (long)target]);
+ [NSUserDefaults.standardUserDefaults removeObjectForKey:SGKeyLyricsDelay];
+ then();
+ });
+}
+
// Scrolled by hand, the page's room grows (the controls go) and the scroll goes on up: the lines made for
// the view meanwhile keep the room between them that the lines already there have.
static void checkBrowse(SGRKaraokeView *karaoke, UIView *host, dispatch_block_t then) {
@@ -461,9 +512,11 @@ static void runChecks(SGRKaraokeView *karaoke, UIView *host) {
after(0.5, ^{
checkBrowse(karaoke, host, ^{
checkGlide(karaoke, ^{
- printf("%d failed\n", sg_failures);
- fflush(stdout);
- exit(sg_failures);
+ checkDelay(karaoke, ^{
+ printf("%d failed\n", sg_failures);
+ fflush(stdout);
+ exit(sg_failures);
+ });
});
});
});
diff --git a/harness/lyrics/stubs.m b/harness/lyrics/stubs.m
index a68df503..e470e321 100644
--- a/harness/lyrics/stubs.m
+++ b/harness/lyrics/stubs.m
@@ -95,6 +95,8 @@ NSString *SGKaraokePlayingTrack(void) { return @"harness"; }
NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID) { return sg_lines; }
void SGKaraokeKeepLines(NSString *trackID, NSArray<SGKaraokeLine *> *lines) { sg_lines = lines; }
+NSInteger SGKaraokeDelayMs(void) { return [NSUserDefaults.standardUserDefaults integerForKey:SGKeyLyricsDelay]; }
+
NSInteger SGKaraokePositionMs(void) {
if (sg_from < 0) return -1;
CFTimeInterval now = CACurrentMediaTime();
diff --git a/harness/mod-settings/stubs.m b/harness/mod-settings/stubs.m
index 691e8675..062c882f 100644
--- a/harness/mod-settings/stubs.m
+++ b/harness/mod-settings/stubs.m
@@ -21,7 +21,7 @@ UIViewController *SGRLyricsSettingsPage(NSString *title, NSString *intro, NSArra
#define ROW(name) SGModRow *name(void) { return SGPageRow(@#name, ^UIViewController *{ return nil; }); }
ROW(SGAppIconRow) ROW(SGGeminiKeyRow) ROW(SGGlassLyricsRow) ROW(SGLockScreenLyricsRow) ROW(SGLyricsMeaningsRow)
-ROW(SGLyricsTranslationLanguageRow) ROW(SGLyricsWordTimingRow) ROW(SGRLyricsTextSizesRow)
+ROW(SGLyricsTranslationLanguageRow) ROW(SGLyricsWordTimingRow) ROW(SGRLyricsTextSizesRow) ROW(SGLyricsDelayRow) ROW(SGLyricsRomanisedRow)
NSArray<SGModRow *> *SGAppFontRows(void) { return @[]; }
NSArray<SGModRow *> *SGNativeAppearanceRows(void) { return @[]; }
NSArray<SGModRow *> *SGRAppearanceRows(void) { return @[]; }
diff --git a/harness/now-playing/main.m b/harness/now-playing/main.m
index 4247cf03..899a82b8 100644
--- a/harness/now-playing/main.m
+++ b/harness/now-playing/main.m
@@ -61,6 +61,7 @@ id SGKaraokePlayer(void) { return sg_player; }
NSString *SGKaraokePlayingTrack(void) { return @"track"; }
NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID) { return sg_lines; }
void SGKaraokeRequestLyrics(NSString *trackID) {}
+NSInteger SGKaraokeDelayMs(void) { return [NSUserDefaults.standardUserDefaults integerForKey:SGKeyLyricsDelay]; }
static SGKaraokeLine *lineOf(NSString *text, NSInteger start, NSInteger end) {
SGKaraokeWord *word = [SGKaraokeWord new];
diff --git a/harness/player/stubs.m b/harness/player/stubs.m
index 5358313d..dd9eb425 100644
--- a/harness/player/stubs.m
+++ b/harness/player/stubs.m
@@ -108,6 +108,8 @@ SPTPlayerTrack *SGKaraokeTrackFor(NSString *trackID) { return nil; }
void SGKaraokeRememberTrack(SPTPlayerTrack *track) {}
// The song runs on from the moment the harness started it, so the sweep is alive in a screenshot.
+NSInteger SGKaraokeDelayMs(void) { return [NSUserDefaults.standardUserDefaults integerForKey:SGKeyLyricsDelay]; }
+
NSInteger SGKaraokePositionMs(void) {
if (!sg_started) return sg_position;
return sg_position + (NSInteger)((CACurrentMediaTime() - sg_started) * 1000);
diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.m
index 8c9624ac..30b86862 100644
--- a/tweak/Sources/App/Pages.m
+++ b/tweak/Sources/App/Pages.m
@@ -111,14 +111,19 @@ static UIViewController *lyricsPage(void) {
sing.value = ^NSString *{ return SGSingSummary(); };
NSMutableArray<SGModSection *> *sections = [NSMutableArray arrayWithObjects:SGSection(nil, @[SGWithTile(sing, @"music.mic", UIColor.systemRedColor)]),
SGLyricsSourcesSection(redesigned), nil];
+ SGModSection *timing = SGNotedSection(@"Timing", @[SGLyricsDelayRow()],
+ @"Every line shows this much later than the song, for Bluetooth headphones that play a little behind the lyrics. Applies at once.");
if (redesigned) {
[sections addObjectsFromArray:@[
SGSection(@"Display", @[SGRLyricsTextSizesRow(), SGLyricsWordTimingRow(), SGLyricsRomanisedRow(),
SGSwitchRow(@"Hide the controls", @"A few seconds after the last touch, the lyrics take the whole player", SGRKeyLyricsAutoHide),
SGSwitchRow(@"Landscape lyrics", @"Turn the phone with the lyrics open", SGRKeyLyricsLandscape)]),
+ timing,
SGSection(@"Translation", @[SGLyricsTranslationLanguageRow(), SGGeminiKeyRow()]),
SGSection(nil, @[SGLyricsMeaningsRow()]),
]];
+ } else {
+ [sections addObject:timing];
}
[sections addObject:SGSection(nil, more)];
// The redesign's page leads with its lyrics playing in the look the page sets, and that look's presets.
diff --git a/tweak/Sources/Native/LocalFiles/LocalLyricsPage.m b/tweak/Sources/Native/LocalFiles/LocalLyricsPage.m
index f2c37f04..15bd844d 100644
--- a/tweak/Sources/Native/LocalFiles/LocalLyricsPage.m
+++ b/tweak/Sources/Native/LocalFiles/LocalLyricsPage.m
@@ -213,7 +213,7 @@ static UILabel *label(UIFontTextStyle style, UIFontWeight weight, CGFloat size,
NSString *uri = SGURIString(state.track.URI);
if (uri && ![uri isEqualToString:_uri]) [self load:state uri:uri];
if (!_timed || !_labels.count) return;
- NSInteger ms = (NSInteger)((state.isPaused ? state.positionAsOfTimestamp : state.position) * 1000);
+ NSInteger ms = (NSInteger)((state.isPaused ? state.positionAsOfTimestamp : state.position) * 1000) - SGKaraokeDelayMs();
NSInteger current = SGKaraokeLeadLine(_lines, ms);
if (current != _current && current < (NSInteger)_labels.count) [self light:current];
}
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 2e86a78c..d4a7f886 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1356,9 +1356,13 @@ typedef struct {
[self restyle];
}
-// The preview's lines on its own clock, round and round; everything else the player's.
+// The preview's lines on its own clock, round and round; everything else the player's, less the
+// Lyrics page's Delay (below 0 for the first moments of a song with one).
- (NSInteger)positionMs {
- if (!_sample) return SGKaraokePositionMs();
+ if (!_sample) {
+ NSInteger ms = SGKaraokePositionMs();
+ return ms < 0 ? ms : ms - SGKaraokeDelayMs();
+ }
return (NSInteger)fmod((CACurrentMediaTime() - _sampleEpoch) * 1000, _sampleLength);
}
@@ -1384,8 +1388,10 @@ typedef struct {
if (_plain) return; // a line with no time has nowhere to seek to
for (SGRKaraokeLineView *view in _shown.allValues) {
if (!CGRectContainsPoint(CGRectInset(view.frame, -_margin, -_lineGap / 2), point)) continue;
- SGLog(@"lyrics: tapped the line at %ld-%ld ms, the clock at %.0f ms", (long)view.line.start, (long)view.line.end, _clock);
- SGKaraokeSeek(view.line.start);
+ // The line shows Delay later than the song, so its sound is that much past its start.
+ NSInteger delay = SGKaraokeDelayMs();
+ SGLog(@"lyrics: tapped the line at %ld-%ld ms, the clock at %.0f ms, %ld ms of delay", (long)view.line.start, (long)view.line.end, _clock, (long)delay);
+ SGKaraokeSeek(view.line.start + delay);
SGPlayFeedback(SGFeedbackSkip);
[self glideTo:view.line.start];
return;
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.x b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
index 1050d3aa..c0304abd 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.x
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
@@ -140,6 +140,7 @@ static NSString *lyricsLine(NSString *trackID, NSString **next, NSString **trans
if (lines && SGKaraokeLinesTiming(lines) == SGKaraokeTimingNone) lines = nil;
NSInteger position = SGKaraokePositionMs();
if (position >= 0 && !SGPlayerState().isPaused) position += kRenderLeadMs;
+ if (position >= 0) position -= SGKaraokeDelayMs();
// With two voices at once, the one that came in first, and the one singing over it as the next.
NSInteger index = SGKaraokeLeadLine(lines, position);
*next = index + 1 < (NSInteger)lines.count ? SGKaraokeLineText(lines[index + 1]) : @"";
diff --git a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
index 4add156c..564d00f0 100644
--- a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
+++ b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
@@ -73,7 +73,7 @@ static NSString *lineFor(NSDictionary *info, double elapsed) {
}
// Plain text has no line being sung to show.
if (SGKaraokeLinesTiming(lines) == SGKaraokeTimingNone) return nil;
- NSInteger position = (NSInteger)(elapsed * 1000);
+ NSInteger position = (NSInteger)(elapsed * 1000) - SGKaraokeDelayMs();
NSInteger index = SGKaraokeLeadLine(lines, position);
if (index < 0) return nil;
BOOL nextFarOff = index + 1 == (NSInteger)lines.count || lines[index + 1].start - position > kBreakMs;
diff --git a/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x b/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x
index 19e4b09a..3c04eb0f 100644
--- a/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x
+++ b/tweak/Sources/Shared/LockScreenLyrics/LyricsArtwork.x
@@ -117,6 +117,7 @@ static void tick(void) {
}
NSInteger position = SGKaraokePositionMs();
if (position < 0) return;
+ position -= SGKaraokeDelayMs();
NSInteger index = SGKaraokeLeadLine(lines, position);
BOOL nextFarOff = index + 1 == (NSInteger)lines.count || lines[index + 1].start - position > kBreakMs;
BOOL resting = index < 0 || (position > SGKaraokeSungEnd(lines[index]) + kBreakMs && nextFarOff);
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
index af89bcc1..5ce9fdf3 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x
+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
@@ -400,6 +400,10 @@ NSInteger SGKaraokePositionMs(void) {
return (NSInteger)((state.isPaused ? state.positionAsOfTimestamp : state.position) * 1000);
}
+NSInteger SGKaraokeDelayMs(void) {
+ return SGInt(SGKeyLyricsDelay, 0);
+}
+
void SGKaraokeSeek(NSInteger ms) {
id player = sg_player;
if (![player respondsToSelector:@selector(seekTo:)]) {
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index c24c90c1..f91448af 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -11,12 +11,13 @@
// LyricsSettings.m: the Lyrics page's parts (App/Pages.m puts the page together): where lyrics come
// from, naming the source (read by the redesign's lyrics view only), the lock screen, which
// language a line's translation is taken in, of those the lyrics come with, and the one Musixmatch's
-// community translations are asked in (the redesign's lyrics being where translations show), whether lines timed only by the line are swept word by word, and whether lines in other alphabets show again in Latin letters.
+// community translations are asked in (the redesign's lyrics being where translations show), whether lines timed only by the line are swept word by word, whether lines in other alphabets show again in Latin letters, and how much later than the song every line shows.
SGModSection *SGLyricsSourcesSection(BOOL namingSource);
SGModRow *SGLockScreenLyricsRow(void);
SGModRow *SGLyricsTranslationLanguageRow(void);
SGModRow *SGLyricsWordTimingRow(void);
SGModRow *SGLyricsRomanisedRow(void);
+SGModRow *SGLyricsDelayRow(void);
// Shows a line written in another alphabet (Japanese, Korean, Chinese, Cyrillic, Greek, Thai, Arabic,
// Hebrew, Devanagari...) again under itself in Latin letters, before its translation. Off by default.
@@ -145,6 +146,11 @@ BOOL SGLyricsStandAsideForEevee(void);
// The base62 id, a local file's lyrics key (Shared/LocalFiles), nil before the player reported.
NSString *SGKaraokePlayingTrack(void);
NSInteger SGKaraokePositionMs(void); // negative when unknown
+// The Lyrics page's Delay: every line shows this many ms later than the song, for headphones that play
+// behind it. Whatever times lines against the position compares them with the position less this, and a
+// tap on a line seeks to its start plus this. Read at every call, so a change applies at once.
+#define SGKeyLyricsDelay @"spotifyglass.lyricsDelay"
+NSInteger SGKaraokeDelayMs(void);
void SGKaraokeSeek(NSInteger ms);
id SGKaraokePlayer(void); // SPTEsperantoPlayer, nil before the app asked it for its state
// A track the player has reported, by its base62 id; nil for one it has not played this session.
diff --git a/tweak/Sources/Shared/Lyrics/LyricsSettings.m b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
index 6039cefe..6765a024 100644
--- a/tweak/Sources/Shared/Lyrics/LyricsSettings.m
+++ b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
@@ -56,3 +56,10 @@ SGModRow *SGLyricsRomanisedRow(void) {
SGModRow *SGLyricsWordTimingRow(void) {
return SGOptionRow(@"Simulate word timing", nil, SGKeyLyricsSimulateWords);
}
+
+// Read wherever lines are timed, so it applies as the thumb moves.
+SGModRow *SGLyricsDelayRow(void) {
+ return SGSliderRow(@"Delay", nil, 0, 1000, 25, ^double { return SGKaraokeDelayMs(); },
+ ^(double value) { SGSetInt(SGKeyLyricsDelay, lround(value)); },
+ ^NSString *(double value) { return [NSString stringWithFormat:@"%ld ms", lround(value)]; });
+}