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

fix: synced lyrics whose lines go back in time are read in time order

My-Name-Is-Jeffffd703ebc360
diff --git a/harness/lyrics/main.m b/harness/lyrics/main.m
index bc57ef74..2a1599a8 100644
--- a/harness/lyrics/main.m
+++ b/harness/lyrics/main.m
@@ -214,7 +214,22 @@ static CGFloat anchorMiss(SGRKaraokeView *karaoke) {
return fabs(view.center.y - view.bounds.size.height / 2 - karaoke.bounds.size.height * 0.28);
}
+static NSString *textsOf(NSArray<SGKaraokeLine *> *lines) {
+ NSMutableArray<NSString *> *texts = [NSMutableArray array];
+ for (SGKaraokeLine *line in lines) [texts addObject:SGKaraokeLineText(line)];
+ return [texts componentsJoinedByString:@" "];
+}
+
static void runChecks(SGRKaraokeView *karaoke, UIView *host) {
+ // Lines listed out of time order are estimated up to the line sung after it, and kept in time order.
+ NSArray<SGKaraokeLine *> *estimated = SGKaraokeInTimeOrder(SGKaraokeEstimatedLines(@[@1000, @8000, @6000, @3000, @6000], @[@"a", @"b", @"c", @"d", @"e"]));
+ expect([textsOf(estimated) isEqualToString:@"a d c e b"] && estimated[1].end <= 6000,
+ [NSString stringWithFormat:@"lines going back in time are read in time order (%@)", textsOf(estimated)]);
+ NSArray<SGKaraokeLine *> *listed = @[timed(5000, @"x", nil), timed(2000, @"y", nil), timed(5000, @"z", nil), timed(1000, @"w", nil)];
+ NSArray<SGKaraokeLine *> *ordered = SGKaraokeInTimeOrder(listed);
+ expect([textsOf(ordered) isEqualToString:@"w y x z"] && SGKaraokeInTimeOrder(ordered) == ordered,
+ [NSString stringWithFormat:@"kept lines are put in time order, the same array once they are (%@)", textsOf(ordered)]);
+
// A held last word keeps its glow as its line goes dim, and loses it once the line has.
SGKaraokeLine *line = timed(0, @"hold this", @"v1");
line.words.lastObject.end = line.words.lastObject.start + 3000;
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
index d794bc15..903394c3 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x
+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
@@ -141,7 +141,7 @@ static void keep(NSString *track, NSArray<SGKaraokeLine *> *lines) {
[sg_requested removeObject:kept];
}
}
- sg_lyrics[track] = lines;
+ sg_lyrics[track] = SGKaraokeInTimeOrder(lines);
if (lines.count) translate(track);
announce(track);
}
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
index bdf55bc7..8ac5da8b 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
+++ b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
@@ -219,12 +219,19 @@ static SGKaraokeLine *timedLine(NSString *text, NSInteger start, NSInteger gap)
}
NSArray<SGKaraokeLine *> *SGKaraokeEstimatedLines(NSArray<NSNumber *> *starts, NSArray<NSString *> *texts) {
+ // A line listed out of time order is given the gap to the line sung after it, not the one listed after
+ // it. The lines stay in the order they were listed: a source pairs them up with its own by position, and
+ // the lines are put in time order where they are kept (SGKaraokeInTimeOrder).
+ NSUInteger count = MIN(texts.count, starts.count);
NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];
- for (NSUInteger i = 0; i < texts.count; i++) {
+ for (NSUInteger i = 0; i < count; i++) {
if (isBreak(texts[i])) continue;
- NSInteger start = starts[i].integerValue;
- NSInteger gap = i + 1 < starts.count ? starts[i + 1].integerValue - start : 0;
- [lines addObject:timedLine(texts[i], start, MAX(gap, 0))];
+ NSInteger start = starts[i].integerValue, gap = 0;
+ for (NSUInteger j = 0; j < count; j++) { // ponytail: quadratic, a song's few hundred lines at most
+ NSInteger after = starts[j].integerValue - start;
+ if (after > 0 && (!gap || after < gap)) gap = after;
+ }
+ [lines addObject:timedLine(texts[i], start, gap)];
}
return lines.count ? lines : nil;
}
@@ -249,6 +256,18 @@ SGKaraokeTiming SGKaraokeLinesTiming(NSArray<SGKaraokeLine *> *lines) {
return finest;
}
+// Some sources list a line out of its place in time: a chorus written once with its repeats after it,
+// or one voice's lines listed after the other's. Everything that reads lines takes them in time order.
+NSArray<SGKaraokeLine *> *SGKaraokeInTimeOrder(NSArray<SGKaraokeLine *> *lines) {
+ for (NSUInteger i = 1; i < lines.count; i++) {
+ if (lines[i].start >= lines[i - 1].start) continue;
+ return [lines sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(SGKaraokeLine *a, SGKaraokeLine *b) {
+ return a.start < b.start ? NSOrderedAscending : a.start > b.start ? NSOrderedDescending : NSOrderedSame;
+ }];
+ }
+ return lines;
+}
+
#pragma mark - Spotify's own bodies
// Lyrics { 1 data: { 1 time_synchronized, 2 repeated line: { 1 offset_ms, 2 content } }, 2 colors }
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index 10a5c557..e9944e48 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -94,6 +94,9 @@ NSArray<SGKaraokeLine *> *SGKaraokeEstimatedLines(NSArray<NSNumber *> *starts, N
NSArray<SGKaraokeLine *> *SGKaraokeStaticLines(NSArray<NSString *> *texts);
// The finest timing any of the lines has; SGKaraokeTimingNone for none at all.
SGKaraokeTiming SGKaraokeLinesTiming(NSArray<SGKaraokeLine *> *lines);
+// The lines in the order they start, lines starting together kept as they came; the array itself when
+// it is in order already, which it nearly always is.
+NSArray<SGKaraokeLine *> *SGKaraokeInTimeOrder(NSArray<SGKaraokeLine *> *lines);
NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID); // nil until the lyrics came
void SGKaraokeKeepLines(NSString *trackID, NSArray<SGKaraokeLine *> *lines);
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsHook.x b/tweak/Sources/Shared/LyricsSources/LyricsHook.x
index 6ec368ff..24d97e15 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsHook.x
+++ b/tweak/Sources/Shared/LyricsSources/LyricsHook.x
@@ -131,12 +131,23 @@ static NSData *pageBody(SGLyricsResult *chain, NSData *colours) {
NSMutableArray<SGPBField *> *lyrics = [NSMutableArray array];
if (chain.synced) [lyrics addObject:SGPBVarint(1, 1)];
NSArray<NSNumber *> *starts = chain.starts;
- [chain.texts enumerateObjectsUsingBlock:^(NSString *text, NSUInteger i, BOOL *stop) {
- NSInteger start = i < starts.count ? [starts[i] integerValue] : 0;
- NSData *line = SGPBSerialize(@[SGPBVarint(1, (uint64_t)MAX(start, 0)),
+ NSInteger (^startOf)(NSUInteger) = ^NSInteger(NSUInteger i) { return i < starts.count ? MAX([starts[i] integerValue], 0) : 0; };
+ // Spotify's page takes its lines in time order, and a source can list one out of it (a chorus written
+ // once with its repeats after it): they go in by their starts, lines starting together as they came.
+ NSMutableArray<NSNumber *> *order = [NSMutableArray arrayWithCapacity:chain.texts.count];
+ for (NSUInteger i = 0; i < chain.texts.count; i++) [order addObject:@(i)];
+ if (chain.synced) {
+ [order sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(NSNumber *a, NSNumber *b) {
+ NSInteger x = startOf(a.unsignedIntegerValue), y = startOf(b.unsignedIntegerValue);
+ return x < y ? NSOrderedAscending : x > y ? NSOrderedDescending : NSOrderedSame;
+ }];
+ }
+ for (NSNumber *index in order) {
+ NSString *text = chain.texts[index.unsignedIntegerValue];
+ NSData *line = SGPBSerialize(@[SGPBVarint(1, (uint64_t)startOf(index.unsignedIntegerValue)),
SGPBString(2, [text isKindOfClass:NSString.class] ? text : @"")]);
[lyrics addObject:SGPBBytes(2, line)];
- }];
+ }
[lyrics addObject:SGPBString(5, chain.provider.length ? chain.provider : kUnnamedProvider)];
return SGPBSerialize(@[SGPBBytes(1, SGPBSerialize(lyrics)), SGPBBytes(2, colours ?: defaultColours())]);
}