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: an LRCLIB record's words count only once its Lyricsfile reads

My-Name-Is-Jeff5175599150f3
diff --git a/harness/lyrics-sources/README.md b/harness/lyrics-sources/README.md
index ce5d2420..fca931b1 100644
--- a/harness/lyrics-sources/README.md
+++ b/harness/lyrics-sources/README.md
@@ -15,8 +15,9 @@
- LRCLIB's Lyricsfile (`Lyricsfile.m`): the spec's example and LRCLIB's own way of writing it read as word
timed lines, segments run together into words, a file timed only by the line left to the LRC, a broken
or unknown file refused whole, and the LRCLIB ask taking the words over the LRC and a record with words
- over a closer one without. The ask reads a Lyricsfile off the main queue and answers on it, later than
- a record without one.
+ over a closer one without, passing over a search record that is not the track (another song, another
+ artist), and counting a record's words only once its Lyricsfile reads. The ask reads a Lyricsfile off the
+ main queue and answers on it, later than a record without one.
`./build.sh krc.json qq.json lrclib.json` also reads real replies saved from `lyrics.kugou.com/download`,
QQ's `GetPlayLyricInfo` and LRCLIB's `/api/get/<id>`, and prints how many lines each has; an LRCLIB record
diff --git a/harness/lyrics-sources/check.m b/harness/lyrics-sources/check.m
index b5dcd29a..04293ee7 100644
--- a/harness/lyrics-sources/check.m
+++ b/harness/lyrics-sources/check.m
@@ -403,6 +403,15 @@ int main(int argc, char **argv) {
found = @[@{@"id": @8, @"trackName": @"a - Live", @"artistName": @"b, x", @"duration": @200, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": wordFile}];
got = lrclibAnswer(query, nil);
CHECK(got.wordTimed && got.karaokeLines.count == 1);
+ // A record's words count once its Lyricsfile reads: one whose file cannot be read is weighed on its LRC,
+ // so the words of a record further off win, and with no file that reads the closer LRC does.
+ NSDictionary *unreadable = @{@"id": @9, @"trackName": @"a", @"artistName": @"b", @"duration": @202, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": @"version: '9'"};
+ found = @[unreadable, @{@"id": @10, @"trackName": @"a", @"artistName": @"b", @"duration": @204, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": wordFile}];
+ got = lrclibAnswer(query, nil);
+ CHECK(got.wordTimed && got.karaokeLines.count == 1);
+ found = @[unreadable, @{@"id": @11, @"trackName": @"a", @"artistName": @"b", @"duration": @200, @"syncedLyrics": @"[00:01.00]a\n[00:02.00]b\n[00:03.00]c\n"}];
+ got = lrclibAnswer(query, nil);
+ CHECK(!got.wordTimed && got.synced && got.karaokeLines.count == 3);
// With no length to go on, a record whose LRC is null against one whose LRC is empty: the plain text wins.
query.seconds = 0;
found = @[@{@"id": @4, @"trackName": @"a", @"artistName": @"b", @"syncedLyrics": NSNull.null, @"plainLyrics": @"x"},
diff --git a/tweak/Sources/Shared/LyricsSources/LrcLib.m b/tweak/Sources/Shared/LyricsSources/LrcLib.m
index 6cc34fd0..7acfac6f 100644
--- a/tweak/Sources/Shared/LyricsSources/LrcLib.m
+++ b/tweak/Sources/Shared/LyricsSources/LrcLib.m
@@ -101,20 +101,17 @@ SGLyricsResult *SGLyricsPlainResult(NSArray<NSString *> *rows) {
return result;
}
-// wordLines: the record's Lyricsfile as lines, nil when it has none this reads.
+// wordLines: the record's Lyricsfile as lines, nil when it has none that reads.
static SGLyricsResult *resultFrom(NSDictionary *record, NSArray<SGKaraokeLine *> *wordLines) {
if (![record isKindOfClass:NSDictionary.class]) return nil;
SGLyricsResult *result = [SGLyricsResult new];
result.instrumental = [record[@"instrumental"] boolValue];
if (result.instrumental) return result;
id synced = record[@"syncedLyrics"], plain = record[@"plainLyrics"];
- if (timesWords(record)) {
- SGLyricsResult *worded = syncedResult(wordLines);
- if (worded) {
- worded.wordTimed = YES;
- return worded;
- }
- SGLog(@"lrclib: record %@ times words in a Lyricsfile this cannot read", record[@"id"]);
+ SGLyricsResult *worded = syncedResult(wordLines);
+ if (worded) {
+ worded.wordTimed = YES;
+ return worded;
}
SGLyricsResult *timed = SGLyricsLRCResult(synced);
if (timed) return timed;
@@ -123,20 +120,6 @@ static SGLyricsResult *resultFrom(NSDictionary *record, NSArray<SGKaraokeLine *>
return SGLyricsPlainResult([plain componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]);
}
-// Calls back on the main queue, where it is called. A Lyricsfile is read on another queue: a long one
-// takes a good part of a second to read, and the main queue would stall for all of it.
-static void resultFor(NSDictionary *record, void (^done)(SGLyricsResult *result)) {
- if (!timesWords(record) || [record[@"instrumental"] boolValue]) {
- done(resultFrom(record, nil));
- return;
- }
- id yaml = record[@"lyricsfile"];
- dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
- NSArray<SGKaraokeLine *> *worded = SGLyricsfileLines(yaml);
- dispatch_async(dispatch_get_main_queue(), ^{ done(resultFrom(record, worded)); });
- });
-}
-
// How good a recording is for the track: words timed beat lines timed, which beat untimed, then the
// closest length. With no length to go on, the fullest sheet wins — the search will happily rank a
// seventeen second stub of a song above the song, and a stub is the one thing a length would have
@@ -161,19 +144,55 @@ static BOOL betterRecord(NSDictionary *record, NSDictionary *best, NSInteger sec
// The search ranks by its own lights, and its first hit is often another song of the artist's or the
// song by someone else, so a record has to be the track (SongMatch.m) before its timing counts. The
// artists are written as one name, "A, B" for several.
-static NSDictionary *bestOf(id found, SGLyricsQuery *query) {
- NSDictionary *best = nil;
+static NSArray<NSDictionary *> *matching(id found, SGLyricsQuery *query) {
+ NSMutableArray<NSDictionary *> *records = [NSMutableArray array];
for (NSDictionary *record in [found isKindOfClass:NSArray.class] ? found : @[]) {
if (![record isKindOfClass:NSDictionary.class]) continue;
NSInteger length = (NSInteger)[record[@"duration"] doubleValue];
if (query.seconds > 0 && length > 0 && labs(length - query.seconds) > kLengthSlack) continue;
NSString *artists = [record[@"artistName"] isKindOfClass:NSString.class] ? record[@"artistName"] : @"";
if (!SGLyricsSongRank(query.title, query.artist, record[@"trackName"], [artists componentsSeparatedByString:@", "])) continue;
- if (betterRecord(record, best, query.seconds)) best = record;
+ [records addObject:record];
+ }
+ return records;
+}
+
+static NSDictionary *bestOf(NSArray<NSDictionary *> *records, NSInteger seconds) {
+ NSDictionary *best = nil;
+ for (NSDictionary *record in records) {
+ if (betterRecord(record, best, seconds)) best = record;
}
return best;
}
+// The best of the records as a result, on the main queue, where it is called. A record's words count only once
+// its Lyricsfile reads, so the best that says it times words is read first, and one whose file cannot be read is
+// weighed again on its LRC alone, until the best is one with words read or one without. A Lyricsfile is read on
+// another queue: a long one takes a good part of a second to read, and the main queue would stall for all of it.
+static void resultFor(NSArray<NSDictionary *> *records, NSInteger seconds, void (^done)(SGLyricsResult *result)) {
+ NSDictionary *best = bestOf(records, seconds);
+ if (!timesWords(best) || [best[@"instrumental"] boolValue]) {
+ done(resultFrom(best, nil));
+ return;
+ }
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
+ NSMutableArray<NSDictionary *> *left = [records mutableCopy];
+ NSDictionary *top = best;
+ NSArray<SGKaraokeLine *> *worded = nil;
+ while (timesWords(top) && ![top[@"instrumental"] boolValue]) {
+ worded = SGLyricsfileLines(top[@"lyricsfile"]);
+ if (worded) break;
+ SGLog(@"lrclib: record %@ times words in a Lyricsfile this cannot read", top[@"id"]);
+ NSMutableDictionary *onLines = [top mutableCopy];
+ [onLines removeObjectForKey:@"hasWordSync"];
+ left[[left indexOfObjectIdenticalTo:top]] = onLines;
+ top = bestOf(left, seconds);
+ }
+ NSDictionary *chosen = top;
+ dispatch_async(dispatch_get_main_queue(), ^{ done(resultFrom(chosen, worded)); });
+ });
+}
+
SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) {
if (!query.title.length || !query.artist.length) {
SGLog(@"lrclib: nothing to search with for %@", query.trackID);
@@ -188,7 +207,7 @@ SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
void (^bySearch)(void) = ^{
SGLyricsGetJSON(SGLyricsURL(kSearch, search), headers(), ^(id found) {
- resultFor(bestOf(found, query), ^(SGLyricsResult *result) {
+ resultFor(matching(found, query), query.seconds, ^(SGLyricsResult *result) {
SGLog(@"lrclib: search for %@ by %@ gave %@", query.title, query.artist,
!result ? @"nothing" : result.instrumental ? @"an instrumental"
: result.synced ? [NSString stringWithFormat:@"%lu %@ lines", (unsigned long)result.karaokeLines.count, result.wordTimed ? @"word timed" : @"timed"]
@@ -206,7 +225,7 @@ SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
NSMutableDictionary<NSString *, NSString *> *exact = [search mutableCopy];
exact[@"duration"] = @(query.seconds).stringValue;
SGLyricsGetJSON(SGLyricsURL(kGet, exact), headers(), ^(id record) {
- resultFor(record, ^(SGLyricsResult *result) {
+ resultFor([record isKindOfClass:NSDictionary.class] ? @[record] : @[], query.seconds, ^(SGLyricsResult *result) {
if (!result) {
bySearch();
return;