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: LRCLIB lyrics time each word when the record carries a Lyricsfile

My-Name-Is-Jeffe60b567c453d
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 3bc3ac7a..04d8b3e5 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -117,7 +117,7 @@ Shared:
pronunciation of a line, each keyed to the line by its itunes:key or lrc:key, the pronunciation
timed word by word (the translation taken in the Lyrics page's language); Musixmatch.m, matched by
Spotify's track id with an anonymous token, word timed where it has richsync; NetEase.m, word timing from yrc for what the others only line time; LrcLib.m, open and
- keyless and timed by the line, the floor under the rest. SpicyLyrics.m, Spicy Lyrics' Developer Platform
+ keyless and timed by the line, by the word where a record carries a Lyricsfile (Lyricsfile.m reads its YAML), the floor under the rest. SpicyLyrics.m, Spicy Lyrics' Developer Platform
by track id on the user's own publishable key (Keychain), syllable timed where it has a sync, with
its pronunciations and translations; its credit, which its terms require, shows whatever Show source
says and opens the uploader's and maker's pages from a tap (both looks), its answers and misses are
diff --git a/harness/lyrics-sources/README.md b/harness/lyrics-sources/README.md
index 41716eed..bd84ffa4 100644
--- a/harness/lyrics-sources/README.md
+++ b/harness/lyrics-sources/README.md
@@ -9,8 +9,13 @@
it, then unpacked and read, with the word times, the joining and the line ends checked;
- the QQ Music and KuGou asks against made-up search replies: titles, singers and lengths that do not
fit are passed over, the closest KuGou lengths are tried first and at most three, and QQ's LRC is read.
+- 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.
-`./build.sh krc.json qq.json` also reads real replies saved from `lyrics.kugou.com/download` and QQ's
-`GetPlayLyricInfo`, and prints how many lines each has. None are kept here, since they are real lyrics.
+`./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
+must read as word timed exactly when it says `hasWordSync`. None are kept here, since they are real lyrics.
It does not cover the requests themselves, the order on the Lyrics page, or the lines on the lyrics view.
diff --git a/harness/lyrics-sources/build.sh b/harness/lyrics-sources/build.sh
index 4b5a9d90..80949480 100755
--- a/harness/lyrics-sources/build.sh
+++ b/harness/lyrics-sources/build.sh
@@ -7,6 +7,6 @@ SRC=$(cd "$HERE/../../tweak/Sources" && pwd)
LS="$SRC/Shared/LyricsSources"
mkdir -p "$HERE/build"
xcrun -sdk macosx clang -target arm64-apple-ios17.0-macabi -isysroot "$(xcrun --sdk macosx --show-sdk-path)" -iframework "$(xcrun --sdk macosx --show-sdk-path)/System/iOSSupport/System/Library/Frameworks" -fobjc-arc -g -Wall -I"$SRC" -DSG_VERSION=\"0.0.0\" \
- "$HERE/check.m" "$LS/KuGou.m" "$LS/QQMusic.m" "$LS/NetEase.m" "$LS/LrcLib.m" "$SRC/Shared/Lyrics/KaraokeTiming.m" "$SRC/Shared/AdBlock/Protobuf.m" \
+ "$HERE/check.m" "$LS/KuGou.m" "$LS/QQMusic.m" "$LS/NetEase.m" "$LS/LrcLib.m" "$LS/Lyricsfile.m" "$SRC/Shared/Lyrics/KaraokeTiming.m" "$SRC/Shared/AdBlock/Protobuf.m" \
-framework UIKit -framework Foundation -lz -o "$HERE/build/check"
"$HERE/build/check" "$@"
diff --git a/harness/lyrics-sources/check.m b/harness/lyrics-sources/check.m
index 6d37bfc9..9799f0ee 100644
--- a/harness/lyrics-sources/check.m
+++ b/harness/lyrics-sources/check.m
@@ -77,8 +77,35 @@ static NSString *const kKRC = @"[id:$00000000]\r\n[ar:Someone]\r\n"
"[4000,1500]<0,400,0>你<400,400,0>好<800,0,0>啊\r\n"
"[7000,100]<0,300,0>late\r\n";
+// Every line's words read back as its text, the spaces aside, and start in order inside it.
+static void checkWordLines(NSArray<SGKaraokeLine *> *lines) {
+ for (SGKaraokeLine *line in lines) {
+ CHECK(line.timing != SGKaraokeTimingWords || line.words.count);
+ for (NSUInteger i = 0; i < line.words.count; i++) {
+ CHECK(line.words[i].start >= line.start && line.words[i].end > line.words[i].start && line.words[i].end <= line.end);
+ CHECK(!i || line.words[i].start >= line.words[i - 1].start);
+ }
+ }
+}
+
static void checkLive(NSString *path) {
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:0 error:nil];
+ if (root[@"lyricsfile"]) {
+ // An LRCLIB record: one that times words reads as word timed lines with the text it had.
+ NSArray<SGKaraokeLine *> *lines = SGLyricsfileLines(root[@"lyricsfile"]);
+ NSUInteger words = 0, timed = 0;
+ NSInteger startSum = 0;
+ for (SGKaraokeLine *line in lines) {
+ words += line.words.count;
+ timed += line.timing == SGKaraokeTimingWords;
+ startSum += line.start;
+ }
+ printf("%s: Lyricsfile, %lu lines (%lu word timed), %lu words, starts sum %ld\n", path.lastPathComponent.UTF8String,
+ (unsigned long)lines.count, (unsigned long)timed, (unsigned long)words, (long)startSum);
+ CHECK(!!lines == [root[@"hasWordSync"] boolValue]);
+ checkWordLines(lines);
+ return;
+ }
if (root[@"content"]) {
NSArray<SGKaraokeLine *> *lines = SGKuGouLines(root[@"content"]);
printf("%s: KRC, %lu word timed lines; first \"%s\" at %ld\n", path.lastPathComponent.UTF8String, (unsigned long)lines.count,
@@ -202,6 +229,73 @@ int main(int argc, char **argv) {
SGQQMusicAsk(query, ^(SGLyricsResult *result) { got = result; });
CHECK(!got && !asked);
+ // Lyricsfile: the spec's own example, two words timed to the end of each.
+ NSArray<SGKaraokeLine *> *lf = SGLyricsfileLines(@"version: '1.0'\n\nmetadata:\n title: 'Example Song'\n artist: 'Example Artist'\n"
+ " language: 'en'\n instrumental: false\n\nlines:\n - text: 'Hello world'\n start_ms: 1200\n end_ms: 2800\n words:\n"
+ " - text: 'Hello '\n start_ms: 1200\n end_ms: 1900\n - text: 'world'\n start_ms: 1900\n end_ms: 2800\n\n"
+ "plain: |\n Hello world\n");
+ CHECK(lf.count == 1 && lf[0].words.count == 2 && [SGKaraokeLineText(lf[0]) isEqualToString:@"Hello world"]);
+ CHECK(lf[0].start == 1200 && lf[0].end == 2800 && lf[0].timing == SGKaraokeTimingWords);
+ CHECK(lf[0].words[0].end == 1900 && lf[0].words[1].start == 1900 && !lf[0].words[1].joined);
+
+ // As LRCLIB writes it: keys in any order, words with no end of their own ending where the next
+ // starts and the last one with its line; a segment with no space after it runs into the next
+ // one, Japanese syllables stay apart and flush, and a line timed only as a whole is estimated.
+ // The rows a sequence's items start on are at its key's indent, quoted the way YAML quotes.
+ NSString *lrclib = @"version: \"1.0\"\nmetadata:\n title: ကမ္ဘာ\n artist: Someone # a comment\nlines:\n"
+ "- text: \"Hold on \"\n words:\n - text: \"Hold \"\n start_ms: 48712\n - text: \"on \"\n start_ms: 49109\n start_ms: 48712\n end_ms: 52187\n"
+ "- text: wanted now\n start_ms: 53000\n words:\n - text: wan\n start_ms: 53000\n - text: \"ted \"\n start_ms: 53200\n end_ms: 53500\n"
+ " - {text: now}\n";
+ CHECK(!SGLyricsfileLines(lrclib)); // a flow mapping is refused, and the whole file with it
+ lrclib = [lrclib stringByReplacingOccurrencesOfString:@" - {text: now}\n" withString:@" - text: now\n start_ms: 54000\n"];
+ lrclib = [lrclib stringByAppendingString:@"- text: '''Cause I''m-'\n start_ms: 60000\n end_ms: 62000\n- text: ''\n start_ms: 62000\n"
+ "- text: \"\\u65e5\\u672c語\"\n start_ms: 63000\n end_ms: 64000\n words:\n - text: 日本\n start_ms: 63000\n - text: 語\n start_ms: 63500\n"
+ "plain: |-\n Hold on\n\n wanted now\n"];
+ lf = SGLyricsfileLines(lrclib);
+ CHECK(lf.count == 4);
+ checkWordLines(lf);
+ CHECK([SGKaraokeLineText(lf[0]) isEqualToString:@"Hold on"] && lf[0].words[0].end == 49109 && lf[0].words[1].end == 52187);
+ CHECK(lf[1].words.count == 2 && [lf[1].words[0].text isEqualToString:@"wanted"] && lf[1].words[0].end == 53500);
+ CHECK([lf[1].words[1].text isEqualToString:@"now"] && !lf[1].words[1].joined && lf[1].end == 55000); // no end anywhere: a second past its last word
+ CHECK([SGKaraokeLineText(lf[2]) isEqualToString:@"'Cause I'm-"] && lf[2].timing == SGKaraokeTimingLine && lf[2].start == 60000);
+ CHECK(lf[3].words.count == 2 && lf[3].words[1].joined && [SGKaraokeLineText(lf[3]) isEqualToString:@"日本語"]);
+
+ // LRCLIB's Lyricsfile for a record timed by the line alone: nothing the LRC does not have.
+ CHECK(!SGLyricsfileLines(@"version: '1.0'\nmetadata:\n title: a\n artist: b\nlines:\n- text: Biatch!\n start_ms: 90\n end_ms: 960\n"));
+ // Refused whole: another version, a key twice, a tab for indent, a negative time, an end before its
+ // start, a quote never closed, words not a list, an alias, a stray row, not YAML at all.
+ NSString *wordFile = @"version: '1.0'\nlines:\n- text: a\n start_ms: 10\n words:\n - text: a\n start_ms: 10\n";
+ CHECK(SGLyricsfileLines(wordFile).count == 1);
+ for (NSArray<NSString *> *swap in @[@[@"'1.0'", @"'2.0'"], @[@" start_ms: 10\n words", @" start_ms: 10\n start_ms: 11\n words"],
+ @[@" start_ms: 10\n words", @"\tstart_ms: 10\n words"], @[@" start_ms: 10", @" start_ms: -10"],
+ @[@" start_ms: 10", @" start_ms: 10\n end_ms: 5"], @[@"- text: a", @"- text: \"a"],
+ @[@" - text: a\n start_ms: 10\n", @" a\n"], @[@"- text: a", @"- text: *a"],
+ @[@"lines:", @" stray\nlines:"]]) {
+ CHECK(!SGLyricsfileLines([wordFile stringByReplacingOccurrencesOfString:swap[0] withString:swap[1]]));
+ }
+ CHECK(!SGLyricsfileLines(@"[not, yaml") && !SGLyricsfileLines(@"") && !SGLyricsfileLines(nil) && !SGLyricsfileLines((NSString *)@3));
+
+ // LRCLIB: a record with words timed answers with them; one whose Lyricsfile cannot be read
+ // answers with its LRC; the search picks the record with words over a closer one without.
+ NSString *twoLines = @"[00:01.00]a\n[00:02.00]b\n";
+ __block NSDictionary *record = @{@"id": @1, @"duration": @200, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": wordFile};
+ __block NSArray *found = nil;
+ sg_answer = ^id(NSURL *url, id body) { return [url.path hasSuffix:@"/get"] ? record : found; };
+ 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);
+ record = @{@"id": @1, @"duration": @200, @"hasWordSync": @YES, @"syncedLyrics": twoLines, @"lyricsfile": @"version: '9'"};
+ SGLrcLibAsk(query, ^(SGLyricsResult *result) { got = result; });
+ CHECK(!got.wordTimed && got.synced && got.karaokeLines.count == 2);
+ 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; });
+ CHECK(got.wordTimed && got.karaokeLines.count == 1);
+
for (int i = 1; i < argc; i++) checkLive(@(argv[i]));
printf("lyrics-sources: all checks passed\n");
}
diff --git a/tweak/Sources/Shared/LyricsSources/LrcLib.m b/tweak/Sources/Shared/LyricsSources/LrcLib.m
index a6babc31..68eeac12 100644
--- a/tweak/Sources/Shared/LyricsSources/LrcLib.m
+++ b/tweak/Sources/Shared/LyricsSources/LrcLib.m
@@ -1,7 +1,8 @@
// LRCLIB, the floor of the order. It is an open library with no key and no catalog to license, so
// it holds what the rest cannot: the tracks Apple's word by word program never reached, and the
-// ones Musixmatch may show nobody. It times lines and not words, which is the whole point of putting
-// it last — the estimate inside a line is still a sweep, where Spotify's own lyrics only scroll.
+// ones Musixmatch may show nobody. It mostly times lines and not words, which is the whole point of
+// putting it last — the estimate inside a line is still a sweep, where Spotify's own lyrics only
+// scroll. A record someone has timed by the word carries a Lyricsfile too (Lyricsfile.m), read first.
//
// Its /api/get wants the length to agree almost exactly and answers 404 otherwise, so a miss falls
// back to /api/search, which answers with whole recordings to pick the closest of. LRCLIB asks that
@@ -64,8 +65,7 @@ static NSArray<SGKaraokeLine *> *linesFromLRC(NSString *lrc) {
return SGKaraokeEstimatedLines(starts, texts);
}
-SGLyricsResult *SGLyricsLRCResult(NSString *lrc) {
- NSArray<SGKaraokeLine *> *lines = [lrc isKindOfClass:NSString.class] ? linesFromLRC(lrc) : nil;
+static SGLyricsResult *syncedResult(NSArray<SGKaraokeLine *> *lines) {
if (!lines) return nil;
SGLyricsResult *result = [SGLyricsResult new];
result.synced = YES;
@@ -78,12 +78,22 @@ SGLyricsResult *SGLyricsLRCResult(NSString *lrc) {
return result;
}
+SGLyricsResult *SGLyricsLRCResult(NSString *lrc) {
+ return syncedResult([lrc isKindOfClass:NSString.class] ? linesFromLRC(lrc) : nil);
+}
+
static SGLyricsResult *resultFrom(NSDictionary *record) {
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"];
+ SGLyricsResult *worded = syncedResult(SGLyricsfileLines(record[@"lyricsfile"]));
+ if (worded) {
+ worded.wordTimed = YES;
+ return worded;
+ }
+ if ([record[@"hasWordSync"] isEqual:@YES]) SGLog(@"lrclib: record %@ times words in a Lyricsfile this cannot read", record[@"id"]);
SGLyricsResult *timed = SGLyricsLRCResult(synced);
if (timed) return timed;
// Nothing timed, but the words still beat an empty page when no other source has any.
@@ -100,11 +110,14 @@ static SGLyricsResult *resultFrom(NSDictionary *record) {
return result;
}
-// How good a recording is for the track: timed beats 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 caught.
+// 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
+// caught.
static BOOL betterRecord(NSDictionary *record, NSDictionary *best, NSInteger seconds) {
if (!best) return YES;
+ BOOL worded = [record[@"hasWordSync"] isEqual:@YES], wasWorded = [best[@"hasWordSync"] isEqual:@YES];
+ if (worded != wasWorded) return worded;
BOOL synced = [record[@"syncedLyrics"] isKindOfClass:NSString.class] && [record[@"syncedLyrics"] length];
BOOL wasSynced = [best[@"syncedLyrics"] isKindOfClass:NSString.class] && [best[@"syncedLyrics"] length];
if (synced != wasSynced) return synced;
@@ -147,7 +160,7 @@ SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
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 timed lines", (unsigned long)result.karaokeLines.count]
+ : 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);
});
@@ -167,7 +180,7 @@ SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
return;
}
SGLog(@"lrclib: %@ by %@ matched exactly, %@", query.title, query.artist,
- result.instrumental ? @"instrumental" : result.synced ? @"timed" : @"untimed");
+ result.instrumental ? @"instrumental" : result.wordTimed ? @"word timed" : result.synced ? @"timed" : @"untimed");
done(result);
});
};
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
index 89e6f203..e9acfa5e 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
@@ -169,6 +169,9 @@ extern SGLyricsAsk SGKuGouAsk;
// LrcLib.m. Timed LRC as a synced result, nil when no line in it is timed.
SGLyricsResult *SGLyricsLRCResult(NSString *lrc);
+// Lyricsfile.m. LRCLIB's Lyricsfile YAML as word timed lines; nil when it times no word, or is not
+// a Lyricsfile 1.0 this reads.
+NSArray<SGKaraokeLine *> *SGLyricsfileLines(NSString *yaml);
// NetEase.m. Word timed lines from NetEase's yrc, or with krc from KuGou's KRC text; nil for none.
NSArray<SGKaraokeLine *> *SGLyricsPieceLines(NSString *body, BOOL krc);
// QQMusic.m, for it and KuGou. A title as the two are matched by: lowercased, without spaces,
diff --git a/tweak/Sources/Shared/LyricsSources/Lyricsfile.m b/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
new file mode 100644
index 00000000..683292e4
--- /dev/null
+++ b/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
@@ -0,0 +1,357 @@
+// Lyricsfile, the YAML that LRCLIB serves under `lyricsfile` when someone has timed the words of a
+// record (https://github.com/tranxuanthang/lyricsfile/blob/main/SPECIFICATION.md):
+//
+// version: "1.0"
+// lines:
+// - text: "Hold on "
+// start_ms: 48712
+// end_ms: 52187
+// words:
+// - text: "Hold "
+// start_ms: 48712
+//
+// The spec allows mappings, sequences and scalars and nothing else, so this reads YAML's block style
+// and no more: no flow collections but an empty one, no anchors, aliases or tags, and no scalar
+// written over more than one row except a | or > block. A file outside that is refused whole, and
+// LRCLIB's LRC is read instead.
+#import "LyricsSources.h"
+
+#pragma mark - the YAML
+
+// The rows of the document and the one being read. A sequence item's "- " is blanked out in place, so
+// the mapping it opens reads like any other at the column its first key is in.
+@interface SGLFReader : NSObject {
+@public
+ NSMutableArray<NSString *> *rows;
+ NSUInteger at;
+ BOOL failed;
+}
+@end
+@implementation SGLFReader
+@end
+
+static NSUInteger indentOf(NSString *row) {
+ NSUInteger n = 0;
+ while (n < row.length && [row characterAtIndex:n] == ' ') n++;
+ return n;
+}
+
+static BOOL ignorable(NSString *row) {
+ NSString *trimmed = [row stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ return !trimmed.length || [trimmed hasPrefix:@"#"] || [trimmed isEqualToString:@"---"];
+}
+
+// The next row with something on it, nil at the end; a tab where the indent is fails the document.
+static NSString *nextRow(SGLFReader *r) {
+ while (r->at < r->rows.count && ignorable(r->rows[r->at])) r->at++;
+ if (r->at >= r->rows.count) return nil;
+ NSString *row = r->rows[r->at];
+ if ([row hasPrefix:@"..."]) return nil;
+ NSUInteger indent = indentOf(row);
+ if (indent < row.length && [row characterAtIndex:indent] == '\t') {
+ r->failed = YES;
+ return nil;
+ }
+ return row;
+}
+
+static BOOL isDash(NSString *row, NSUInteger indent) {
+ return indent < row.length && [row characterAtIndex:indent] == '-' && (indent + 1 == row.length || [row characterAtIndex:indent + 1] == ' ');
+}
+
+// What follows a scalar on its row: nothing, or a comment.
+static BOOL restIsEmpty(NSString *rest) {
+ NSString *trimmed = [rest stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ return !trimmed.length || [trimmed hasPrefix:@"#"];
+}
+
+static NSString *doubleQuoted(NSString *text, NSUInteger *end) {
+ NSMutableString *out = [NSMutableString string];
+ for (NSUInteger i = 1; i < text.length; i++) {
+ unichar c = [text characterAtIndex:i];
+ if (c == '"') {
+ *end = i + 1;
+ return out;
+ }
+ if (c != '\\') {
+ [out appendFormat:@"%C", c];
+ continue;
+ }
+ if (++i >= text.length) return nil;
+ unichar e = [text characterAtIndex:i];
+ NSUInteger digits = e == 'x' ? 2 : e == 'u' ? 4 : e == 'U' ? 8 : 0;
+ if (digits) {
+ if (i + digits >= text.length) return nil;
+ NSString *hex = [text substringWithRange:NSMakeRange(i + 1, digits)];
+ if ([hex rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"].invertedSet].location != NSNotFound) return nil;
+ UTF32Char point = (UTF32Char)strtoul(hex.UTF8String, NULL, 16);
+ NSString *ch = [[NSString alloc] initWithBytes:&point length:4 encoding:NSUTF32LittleEndianStringEncoding];
+ if (!ch) return nil;
+ [out appendString:ch];
+ i += digits;
+ continue;
+ }
+ switch (e) {
+ case 'n': [out appendString:@"\n"]; break;
+ case 't': [out appendString:@"\t"]; break;
+ case 'r': [out appendString:@"\r"]; break;
+ case ' ': case '"': case '/': case '\\': [out appendFormat:@"%C", e]; break;
+ case '_': [out appendString:@" "]; break;
+ default: return nil;
+ }
+ }
+ return nil; // the closing quote is on a later row, which this does not read
+}
+
+static NSString *singleQuoted(NSString *text, NSUInteger *end) {
+ NSMutableString *out = [NSMutableString string];
+ for (NSUInteger i = 1; i < text.length; i++) {
+ unichar c = [text characterAtIndex:i];
+ if (c == '\'') {
+ if (i + 1 < text.length && [text characterAtIndex:i + 1] == '\'') {
+ [out appendString:@"'"];
+ i++;
+ continue;
+ }
+ *end = i + 1;
+ return out;
+ }
+ [out appendFormat:@"%C", c];
+ }
+ return nil;
+}
+
+// A scalar written on one row, kept as text whatever it spells: the reader of each field decides what
+// it should be. nil for what this does not read.
+static id inlineScalar(NSString *text) {
+ text = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ if (!text.length) return nil;
+ unichar c = [text characterAtIndex:0];
+ NSUInteger end = 0;
+ if (c == '"' || c == '\'') {
+ NSString *value = c == '"' ? doubleQuoted(text, &end) : singleQuoted(text, &end);
+ return value && restIsEmpty([text substringFromIndex:end]) ? value : nil;
+ }
+ if ([text isEqualToString:@"[]"]) return @[];
+ if ([text isEqualToString:@"{}"]) return @{};
+ if (c < 128 && strchr("[]{}&*!|>%@`", (char)c)) return nil; // past ASCII, the cast to char could match
+ NSRange comment = [text rangeOfString:@" #"];
+ if (comment.location != NSNotFound) text = [text substringToIndex:comment.location];
+ return [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+}
+
+// A mapping key at the start of `text`, and where its value starts; NO when the text is no "key:".
+static BOOL splitKey(NSString *text, NSString **key, NSString **rest) {
+ unichar c = text.length ? [text characterAtIndex:0] : 0;
+ NSUInteger end = 0;
+ if (c == '"' || c == '\'') {
+ *key = c == '"' ? doubleQuoted(text, &end) : singleQuoted(text, &end);
+ if (!*key) return NO;
+ NSString *after = [[text substringFromIndex:end] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ if (![after hasPrefix:@":"]) return NO;
+ *rest = [after substringFromIndex:1];
+ return YES;
+ }
+ NSRange colon = [text rangeOfString:@": "];
+ if (colon.location == NSNotFound && [text hasSuffix:@":"]) colon = NSMakeRange(text.length - 1, 1);
+ if (colon.location == NSNotFound || colon.location == 0) return NO;
+ *key = [[text substringToIndex:colon.location] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ *rest = [text substringFromIndex:NSMaxRange(colon)];
+ return YES;
+}
+
+static id node(SGLFReader *r, NSUInteger indent);
+
+// The rows of a | or > block after the row that opens it, all indented deeper than `parent`.
+// shortcut: read as | with its chomping ignored; only `plain` is written this way, and nothing reads it.
+static NSString *blockScalar(SGLFReader *r, NSUInteger parent) {
+ NSMutableArray<NSString *> *lines = [NSMutableArray array];
+ NSUInteger indent = 0;
+ while (r->at < r->rows.count) {
+ NSString *row = r->rows[r->at];
+ BOOL blank = ![row stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet].length;
+ if (!blank) {
+ NSUInteger here = indentOf(row);
+ if (here <= parent || (indent && here < indent)) break;
+ if (!indent) indent = here;
+ }
+ [lines addObject:blank ? @"" : [row substringFromIndex:indent]];
+ r->at++;
+ }
+ return [lines componentsJoinedByString:@"\n"];
+}
+
+// The value after "key:" or "- ", the row it is on already passed: on the same row, a block under it,
+// or (after a key) a sequence at the key's own indent.
+static id valueAfter(SGLFReader *r, NSString *rest, NSUInteger indent, BOOL afterKey) {
+ NSString *trimmed = [rest stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ if ([trimmed hasPrefix:@"|"] || [trimmed hasPrefix:@">"]) return blockScalar(r, indent);
+ if (!restIsEmpty(trimmed)) {
+ id value = inlineScalar(trimmed);
+ if (!value) r->failed = YES;
+ return value;
+ }
+ NSString *row = nextRow(r);
+ if (!row) return NSNull.null;
+ NSUInteger here = indentOf(row);
+ if (here > indent || (afterKey && here == indent && isDash(row, here))) return node(r, here);
+ return NSNull.null;
+}
+
+static NSDictionary *mapping(SGLFReader *r, NSUInteger indent) {
+ NSMutableDictionary *map = [NSMutableDictionary dictionary];
+ NSString *row;
+ while (!r->failed && (row = nextRow(r))) {
+ NSUInteger here = indentOf(row);
+ if (here < indent || (here == indent && isDash(row, here))) break;
+ NSString *key, *rest;
+ if (here > indent || !splitKey([row substringFromIndex:here], &key, &rest) || map[key]) {
+ r->failed = YES; // a key given twice is refused, as the spec asks, rather than one picked
+ break;
+ }
+ r->at++;
+ id value = valueAfter(r, rest, indent, YES);
+ if (value) map[key] = value;
+ }
+ return map;
+}
+
+static NSArray *sequence(SGLFReader *r, NSUInteger indent) {
+ NSMutableArray *items = [NSMutableArray array];
+ NSString *row;
+ while (!r->failed && (row = nextRow(r))) {
+ NSUInteger here = indentOf(row);
+ if (here != indent || !isDash(row, here)) {
+ if (here > indent) r->failed = YES;
+ break;
+ }
+ NSString *content = [row substringFromIndex:here + 1];
+ NSUInteger column = here + 1 + indentOf(content);
+ NSString *key, *rest;
+ if (column < row.length && splitKey([row substringFromIndex:column], &key, &rest)) {
+ r->rows[r->at] = [[@"" stringByPaddingToLength:column withString:@" " startingAtIndex:0] stringByAppendingString:[row substringFromIndex:column]];
+ [items addObject:mapping(r, column)];
+ continue;
+ }
+ r->at++;
+ id value = valueAfter(r, content, indent, NO);
+ if (value) [items addObject:value];
+ }
+ return items;
+}
+
+static id node(SGLFReader *r, NSUInteger indent) {
+ NSString *row = nextRow(r);
+ if (!row) return NSNull.null;
+ return isDash(row, indent) ? sequence(r, indent) : mapping(r, indent);
+}
+
+// The document as dictionaries, arrays and strings; nil when it is not YAML this reads.
+static id parseYAML(NSString *yaml) {
+ SGLFReader *r = [SGLFReader new];
+ r->rows = [NSMutableArray array];
+ for (NSString *row in [yaml componentsSeparatedByString:@"\n"]) {
+ [r->rows addObject:[row hasSuffix:@"\r"] ? [row substringToIndex:row.length - 1] : row];
+ }
+ NSString *first = nextRow(r);
+ if (!first || indentOf(first) != 0) return nil;
+ id root = node(r, 0);
+ return r->failed || nextRow(r) ? nil : root;
+}
+
+#pragma mark - the lines
+
+// A time in milliseconds: digits only, none negative. -1 for anything else.
+static NSInteger msOf(id value) {
+ if (![value isKindOfClass:NSString.class] || ![value length]) return -1;
+ if ([value rangeOfCharacterFromSet:NSCharacterSet.decimalDigitCharacterSet.invertedSet].location != NSNotFound) return -1;
+ return [value integerValue];
+}
+
+// Where a line without an end of its own, or of its last word, is taken to stop.
+static const NSInteger kTailMs = 1000;
+
+// A word timed line, the segments of a spaced script run together into whole words as NetEase's are:
+// a segment the spec writes without the space after it ("wan", "ted ") goes on into the next one.
+static SGKaraokeLine *wordLine(NSArray *rawWords, NSInteger start, NSInteger end) {
+ NSInteger lineEnd = end;
+ if (lineEnd < 0) {
+ NSDictionary *last = rawWords.lastObject;
+ lineEnd = msOf(last[@"end_ms"]) >= 0 ? msOf(last[@"end_ms"]) : msOf(last[@"start_ms"]) + kTailMs;
+ }
+ NSMutableArray<SGKaraokeWord *> *words = [NSMutableArray array];
+ SGKaraokeWord *open = nil;
+ BOOL spaced = YES;
+ for (NSUInteger i = 0; i < rawWords.count; i++) {
+ NSDictionary *raw = rawWords[i];
+ NSString *said = raw[@"text"];
+ NSInteger wordStart = msOf(raw[@"start_ms"]), wordEnd = msOf(raw[@"end_ms"]);
+ if (![said isKindOfClass:NSString.class] || wordStart < 0) return nil;
+ if (raw[@"end_ms"] && wordEnd < wordStart) return nil;
+ if (wordEnd < 0) wordEnd = i + 1 < rawWords.count ? msOf(rawWords[i + 1][@"start_ms"]) : lineEnd;
+ wordEnd = MAX(wordEnd, wordStart + 1); // a word of no length still shows, for a moment
+ NSString *trimmed = [said stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
+ if (said.length && [NSCharacterSet.whitespaceAndNewlineCharacterSet characterIsMember:[said characterAtIndex:0]]) {
+ open = nil;
+ spaced = YES;
+ }
+ BOOL unspaced = SGKaraokeUnspacedScript(trimmed);
+ if (trimmed.length && open && !unspaced) {
+ open.text = [open.text stringByAppendingString:trimmed];
+ open.end = wordEnd;
+ } else if (trimmed.length) {
+ SGKaraokeWord *word = [SGKaraokeWord new];
+ word.text = trimmed;
+ word.start = wordStart;
+ word.end = wordEnd;
+ word.joined = !spaced;
+ [words addObject:word];
+ open = unspaced ? nil : word;
+ spaced = NO;
+ }
+ if (!trimmed.length || [NSCharacterSet.whitespaceAndNewlineCharacterSet characterIsMember:[said characterAtIndex:said.length - 1]]) {
+ open = nil;
+ spaced = YES;
+ }
+ }
+ if (!words.count) return nil;
+ SGKaraokeLine *line = [SGKaraokeLine new];
+ line.words = words;
+ line.start = start;
+ line.end = MAX(lineEnd, words.lastObject.end);
+ return line;
+}
+
+NSArray<SGKaraokeLine *> *SGLyricsfileLines(NSString *yaml) {
+ if (![yaml isKindOfClass:NSString.class]) return nil;
+ NSDictionary *root = parseYAML(yaml);
+ if (![root isKindOfClass:NSDictionary.class] || ![root[@"version"] isEqual:@"1.0"]) return nil;
+ NSArray *rawLines = root[@"lines"];
+ if (![rawLines isKindOfClass:NSArray.class]) return nil;
+ NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];
+ BOOL anyWords = NO;
+ for (NSDictionary *raw in rawLines) {
+ if (![raw isKindOfClass:NSDictionary.class]) return nil;
+ NSString *text = raw[@"text"];
+ NSArray *words = raw[@"words"];
+ NSInteger start = msOf(raw[@"start_ms"]), end = msOf(raw[@"end_ms"]);
+ if (![text isKindOfClass:NSString.class] || start < 0 || (raw[@"end_ms"] && end < start)) return nil;
+ if (words && ![words isKindOfClass:NSArray.class]) {
+ if (words != (id)NSNull.null) return nil;
+ words = nil;
+ }
+ for (NSDictionary *word in words) if (![word isKindOfClass:NSDictionary.class]) return nil;
+ SGKaraokeLine *line;
+ if (words.count) {
+ line = wordLine(words, start, end);
+ if (!line) return nil;
+ anyWords = YES;
+ } else {
+ // A line the file times only as a whole, among ones it times by the word.
+ line = SGKaraokeEstimatedLines(end >= 0 ? @[@(start), @(end)] : @[@(start)], end >= 0 ? @[text, @""] : @[text]).firstObject;
+ }
+ if (line) [lines addObject:line];
+ }
+ // Lines timed only as whole lines are what the record's LRC has already.
+ return anyWords ? lines : nil;
+}