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: LRCLIB reads a Lyricsfile off the main queue

My-Name-Is-Jeff95146b90f757
diff --git a/harness/lyrics-sources/README.md b/harness/lyrics-sources/README.md
index bd84ffa4..02e9cd3c 100644
--- a/harness/lyrics-sources/README.md
+++ b/harness/lyrics-sources/README.md
@@ -12,7 +12,8 @@
- 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.
+ over a closer one without. 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 e7c48d4b..124f64f5 100644
--- a/harness/lyrics-sources/check.m
+++ b/harness/lyrics-sources/check.m
@@ -38,6 +38,23 @@ static id (^sg_answer)(NSURL *url, id body);
void SGLyricsGetJSON(NSURL *url, NSDictionary *headers, void (^done)(id root)) { done(sg_answer(url, nil)); }
void SGLyricsPostJSON(NSURL *url, NSDictionary *headers, id body, void (^done)(id root)) { done(sg_answer(url, body)); }
+// LRCLIB reads a Lyricsfile off the main queue and answers back on it, so this runs the main queue
+// until the answer comes. answeredAtOnce says whether it came before the ask returned.
+static SGLyricsResult *lrclibAnswer(SGLyricsQuery *query, BOOL *answeredAtOnce) {
+ __block SGLyricsResult *got = nil;
+ __block BOOL answered = NO;
+ SGLrcLibAsk(query, ^(SGLyricsResult *result) {
+ CHECK(NSThread.isMainThread);
+ got = result;
+ answered = YES;
+ });
+ if (answeredAtOnce) *answeredAtOnce = answered;
+ NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:5];
+ while (!answered && deadline.timeIntervalSinceNow > 0) [NSRunLoop.mainRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
+ CHECK(answered);
+ return got;
+}
+
static NSString *param(NSURL *url, NSString *name) {
for (NSURLQueryItem *item in [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO].queryItems) {
if ([item.name isEqualToString:name]) return item.value;
@@ -306,25 +323,29 @@ int main(int argc, char **argv) {
query.title = @"a";
query.artist = @"b";
query.seconds = 200;
- got = nil;
- SGLrcLibAsk(query, ^(SGLyricsResult *result) { got = result; });
- CHECK(got.wordTimed && got.synced && got.karaokeLines.count == 1 && got.texts.count);
+ // A Lyricsfile is read off the main queue, so its answer comes later; one without words at once.
+ BOOL atOnce = YES;
+ got = lrclibAnswer(query, &atOnce);
+ CHECK(got.wordTimed && got.synced && got.karaokeLines.count == 1 && got.texts.count && !atOnce);
record = @{@"id": @1, @"duration": @200, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": @"version: '9'"};
- SGLrcLibAsk(query, ^(SGLyricsResult *result) { got = result; });
+ got = lrclibAnswer(query, nil);
CHECK(!got.wordTimed && got.synced && got.karaokeLines.count == 2);
record = @{@"id": @1, @"duration": @200, @"hasWordSync": @NO, @"syncedLyrics": twoLines, @"lyricsfile": wordFile};
- SGLrcLibAsk(query, ^(SGLyricsResult *result) { got = result; });
- CHECK(!got.wordTimed && got.synced && got.karaokeLines.count == 2);
+ got = lrclibAnswer(query, &atOnce);
+ CHECK(!got.wordTimed && got.synced && got.karaokeLines.count == 2 && atOnce);
+ // An instrumental is answered as one, its Lyricsfile not read.
+ record = @{@"id": @1, @"duration": @200, @"instrumental": @YES, @"hasWordSync": @YES, @"lyricsfile": wordFile};
+ got = lrclibAnswer(query, &atOnce);
+ CHECK(got.instrumental && atOnce);
record = nil;
found = @[@{@"id": @2, @"duration": @200, @"syncedLyrics": twoLines},
@{@"id": @3, @"duration": @202, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": wordFile}];
- SGLrcLibAsk(query, ^(SGLyricsResult *result) { got = result; });
+ got = lrclibAnswer(query, nil);
CHECK(got.wordTimed && got.karaokeLines.count == 1);
// 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, @"syncedLyrics": NSNull.null, @"plainLyrics": @"x"}, @{@"id": @5, @"syncedLyrics": @"", @"plainLyrics": @"y\nz"}];
- got = nil;
- SGLrcLibAsk(query, ^(SGLyricsResult *result) { got = result; });
+ got = lrclibAnswer(query, nil);
CHECK(got && !got.synced);
for (int i = 1; i < argc; i++) checkLive(@(argv[i]));
diff --git a/tweak/Sources/Shared/LyricsSources/LrcLib.m b/tweak/Sources/Shared/LyricsSources/LrcLib.m
index aea0fbb2..7fca41de 100644
--- a/tweak/Sources/Shared/LyricsSources/LrcLib.m
+++ b/tweak/Sources/Shared/LyricsSources/LrcLib.m
@@ -65,6 +65,11 @@ static NSArray<SGKaraokeLine *> *linesFromLRC(NSString *lrc) {
return SGKaraokeEstimatedLines(starts, texts);
}
+// Without word sync a record's Lyricsfile only repeats its LRC's line times, so it is not read.
+static BOOL timesWords(NSDictionary *record) {
+ return [record isKindOfClass:NSDictionary.class] && [record[@"hasWordSync"] isEqual:@YES];
+}
+
static SGLyricsResult *syncedResult(NSArray<SGKaraokeLine *> *lines) {
if (!lines) return nil;
SGLyricsResult *result = [SGLyricsResult new];
@@ -82,15 +87,15 @@ SGLyricsResult *SGLyricsLRCResult(NSString *lrc) {
return syncedResult([lrc isKindOfClass:NSString.class] ? linesFromLRC(lrc) : nil);
}
-static SGLyricsResult *resultFrom(NSDictionary *record) {
+// wordLines: the record's Lyricsfile as lines, nil when it has none this 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"];
- // Without word sync the record's Lyricsfile only repeats its LRC's line times, so it is not read.
- if ([record[@"hasWordSync"] isEqual:@YES]) {
- SGLyricsResult *worded = syncedResult(SGLyricsfileLines(record[@"lyricsfile"]));
+ if (timesWords(record)) {
+ SGLyricsResult *worded = syncedResult(wordLines);
if (worded) {
worded.wordTimed = YES;
return worded;
@@ -113,6 +118,20 @@ static SGLyricsResult *resultFrom(NSDictionary *record) {
return result;
}
+// 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
@@ -159,13 +178,13 @@ SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
void (^bySearch)(void) = ^{
SGLyricsGetJSON(SGLyricsURL(kSearch, search), headers(), ^(id found) {
- NSDictionary *record = bestOf(found, query.seconds);
- SGLyricsResult *result = resultFrom(record);
- 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"]
- : [NSString stringWithFormat:@"%lu untimed lines", (unsigned long)result.texts.count]);
- done(result);
+ resultFor(bestOf(found, 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"]
+ : [NSString stringWithFormat:@"%lu untimed lines", (unsigned long)result.texts.count]);
+ done(result);
+ });
});
};
@@ -177,13 +196,14 @@ 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) {
- SGLyricsResult *result = resultFrom(record);
- if (!result) {
- bySearch();
- return;
- }
- SGLog(@"lrclib: %@ by %@ matched exactly, %@", query.title, query.artist,
- result.instrumental ? @"instrumental" : result.wordTimed ? @"word timed" : result.synced ? @"timed" : @"untimed");
- done(result);
+ resultFor(record, ^(SGLyricsResult *result) {
+ if (!result) {
+ bySearch();
+ return;
+ }
+ SGLog(@"lrclib: %@ by %@ matched exactly, %@", query.title, query.artist,
+ result.instrumental ? @"instrumental" : result.wordTimed ? @"word timed" : result.synced ? @"timed" : @"untimed");
+ done(result);
+ });
});
};
diff --git a/tweak/Sources/Shared/LyricsSources/Lyricsfile.m b/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
index 40d01a60..b4656986 100644
--- a/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
+++ b/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
@@ -283,8 +283,8 @@ static id parseYAML(NSString *yaml) {
#pragma mark - the lines
-// A whole song timed by the word is tens of kilobytes; anything past this is no Lyricsfile. It is read on
-// the main queue, where a megabyte took a third of a second on a Mac.
+// A whole song timed by the word is tens of kilobytes; anything past this is no Lyricsfile. A megabyte
+// took a third of a second to read on a Mac.
static const NSUInteger kMaxLength = 1 << 18;
// A time in milliseconds: ASCII digits only, none negative, and at most nine of them (over a hundred