Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: add LRCLIB as the floor under the other lyrics sources
Vojtěch Škopekab78945ecc11
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex cb1926ae..fb72c1c9 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -52,8 +52,9 @@ app:best answer (LyricsSources.m, the list to drag in LyricsSourcesPage.m): Apple Music's TTML fromBiniLyrics.m and Unison.m, read by SGTTML.m, which is the only shape carrying a second voice and thebacking vocals; 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. color-lyrics is- answered with whichever won and has_lyrics forced for every track (LyricsHook.x)+ 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. color-lyrics is answered with whichever won and+ has_lyrics forced for every track (LyricsHook.x)Onboarding/ the welcome tour over Home on the first launch (Onboarding.x, the pages in Tour.m), offered again from the Mod pageAbout/ the update check and the Mod page: the build, its updates, the links and the resetdiff --git a/tweak/Sources/Features/LyricsSources/BiniLyrics.m b/tweak/Sources/Features/LyricsSources/BiniLyrics.mindex 2e03f7a4..9af6987f 100644--- a/tweak/Sources/Features/LyricsSources/BiniLyrics.m+++ b/tweak/Sources/Features/LyricsSources/BiniLyrics.m@@ -61,7 +61,7 @@ SGLyricsAsk SGBiniLyricsAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResulresult.karaokeLines = lines;NSArray<NSNumber *> *starts;NSArray<NSString *> *texts;- SGTTMLPageLines(lines, &starts, &texts);+ SGLyricsPageLines(lines, &starts, &texts);result.starts = starts;result.texts = texts;SGLog(@"binilyrics: %@ by %@ has %lu %@ lines", query.title, query.artist,diff --git a/tweak/Sources/Features/LyricsSources/LrcLib.m b/tweak/Sources/Features/LyricsSources/LrcLib.mnew file mode 100644index 00000000..6d6fa492--- /dev/null+++ b/tweak/Sources/Features/LyricsSources/LrcLib.m@@ -0,0 +1,167 @@+// LRCLIB, the floor of the order. It is an open library with no key and no catalogue to license, so+// it holds what the rest cannot: the tracks Apple's word by word programme 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.+//+// 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+// clients say who they are, and the mod does.+#import "Core/SGCore.h"+#import "LyricsSources.h"++static NSString *const kGet = @"https://lrclib.net/api/get";+static NSString *const kSearch = @"https://lrclib.net/api/search";+// Line timing is only worth taking from a recording of about the same length as the one playing.+static const NSInteger kLengthSlack = 4;++static NSDictionary<NSString *, NSString *> *headers(void) {+ return @{@"User-Agent": @"spoti.pw " @SG_VERSION @" (https://github.com/skopevoj/spoti.pw)"};+}++// [00:34.30] Look — the timestamp in minutes, seconds and hundredths, or thousandths where a line+// carries three digits. A line may be stamped more than once when it is sung more than once, and+// the tags LRC opens with ([ar:…], [length:…]) are not timestamps, so they fall out on their own.+static NSArray<SGKaraokeLine *> *linesFromLRC(NSString *lrc) {+ static NSRegularExpression *stamp;+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ stamp = [NSRegularExpression regularExpressionWithPattern:@"\\[(\\d{1,3}):(\\d{1,2})(?:[.:](\\d{1,3}))?\\]" options:0 error:nil];+ });+ NSMutableArray<NSDictionary *> *stamped = [NSMutableArray array];+ for (NSString *row in [lrc componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) {+ NSArray<NSTextCheckingResult *> *found = [stamp matchesInString:row options:0 range:NSMakeRange(0, row.length)];+ // Only the stamps a line opens with are its own; one further in is part of the words.+ NSUInteger end = 0;+ NSMutableArray<NSNumber *> *at = [NSMutableArray array];+ for (NSTextCheckingResult *match in found) {+ if (match.range.location != end) break;+ end = NSMaxRange(match.range);+ NSInteger minutes = [row substringWithRange:[match rangeAtIndex:1]].integerValue;+ NSInteger seconds = [row substringWithRange:[match rangeAtIndex:2]].integerValue;+ NSInteger fraction = 0;+ NSRange part = [match rangeAtIndex:3];+ if (part.location != NSNotFound) {+ NSString *digits = [row substringWithRange:part];+ fraction = digits.integerValue * (digits.length == 1 ? 100 : digits.length == 2 ? 10 : 1);+ }+ [at addObject:@((minutes * 60 + seconds) * 1000 + fraction)];+ }+ if (!at.count) continue;+ NSString *text = [[row substringFromIndex:end] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];+ for (NSNumber *ms in at) [stamped addObject:@{@"ms": ms, @"text": text}];+ }+ if (!stamped.count) return nil;+ [stamped sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {+ return [a[@"ms"] compare:b[@"ms"]];+ }];+ // The empty rows stay in: they are the breaks, and each one is the end of the line before it.+ NSMutableArray<NSNumber *> *starts = [NSMutableArray array];+ NSMutableArray<NSString *> *texts = [NSMutableArray array];+ for (NSDictionary *row in stamped) {+ [starts addObject:row[@"ms"]];+ [texts addObject:row[@"text"]];+ }+ return SGKaraokeEstimatedLines(starts, texts);+}++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"];+ NSArray<SGKaraokeLine *> *lines = [synced isKindOfClass:NSString.class] ? linesFromLRC(synced) : nil;+ if (lines) {+ result.synced = YES;+ result.karaokeLines = lines;+ NSArray<NSNumber *> *starts;+ NSArray<NSString *> *texts;+ SGLyricsPageLines(lines, &starts, &texts);+ result.starts = starts;+ result.texts = texts;+ return result;+ }+ // Nothing timed, but the words still beat an empty page when no other source has any.+ if (![plain isKindOfClass:NSString.class] || ![plain length]) return nil;+ NSMutableArray<NSNumber *> *starts = [NSMutableArray array];+ NSMutableArray<NSString *> *texts = [NSMutableArray array];+ for (NSString *row in [plain componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) {+ [starts addObject:@0];+ [texts addObject:row.length ? row : @"♪"];+ }+ result.starts = starts;+ result.texts = texts;+ 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.+static BOOL betterRecord(NSDictionary *record, NSDictionary *best, NSInteger seconds) {+ if (!best) return YES;+ 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;+ if (seconds > 0) {+ NSInteger off = labs((NSInteger)[record[@"duration"] doubleValue] - seconds);+ NSInteger wasOff = labs((NSInteger)[best[@"duration"] doubleValue] - seconds);+ return off < wasOff;+ }+ NSString *text = record[@"syncedLyrics"] ?: record[@"plainLyrics"] ?: @"";+ NSString *wasText = best[@"syncedLyrics"] ?: best[@"plainLyrics"] ?: @"";+ return [text isKindOfClass:NSString.class] && [text length] > [wasText length];+}++static NSDictionary *bestOf(id found, NSInteger seconds) {+ NSDictionary *best = nil;+ for (NSDictionary *record in [found isKindOfClass:NSArray.class] ? found : @[]) {+ if (![record isKindOfClass:NSDictionary.class]) continue;+ NSInteger length = (NSInteger)[record[@"duration"] doubleValue];+ if (seconds > 0 && length > 0 && labs(length - seconds) > kLengthSlack) continue;+ if (betterRecord(record, best, seconds)) best = record;+ }+ return best;+}++SGLyricsAsk SGLrcLibAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) {+ if (!query.title.length || !query.artist.length) {+ SGLog(@"lrclib: nothing to search with for %@", query.trackID);+ done(nil);+ return;+ }+ NSMutableDictionary<NSString *, NSString *> *search = [NSMutableDictionary dictionaryWithDictionary:@{+ @"track_name": query.title,+ @"artist_name": query.artist,+ }];+ if (query.album.length) search[@"album_name"] = query.album;++ 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 timed lines", (unsigned long)result.karaokeLines.count]+ : [NSString stringWithFormat:@"%lu untimed lines", (unsigned long)result.texts.count]);+ done(result);+ });+ };++ // With no length to match on, the exact lookup would be a guess; the search ranks by itself.+ if (query.seconds <= 0) {+ bySearch();+ return;+ }+ 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.synced ? @"timed" : @"untimed");+ done(result);+ });+};diff --git a/tweak/Sources/Features/LyricsSources/LyricsSources.h b/tweak/Sources/Features/LyricsSources/LyricsSources.hindex 9caccc38..7c8b6c3a 100644--- a/tweak/Sources/Features/LyricsSources/LyricsSources.h+++ b/tweak/Sources/Features/LyricsSources/LyricsSources.h@@ -42,6 +42,9 @@@property (nonatomic) NSInteger seconds;@end+// The lines as Spotify's own page takes them, ♪ over a break and an empty line at the end.+void SGLyricsPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **starts, NSArray<NSString *> **texts);+// Calls back on the main queue, nil when the source has nothing for the track.typedef void (^SGLyricsAsk)(SGLyricsQuery *query, void (^done)(SGLyricsResult *result));@@ -79,13 +82,13 @@ void SGLyricsGetText(NSURL *url, void (^done)(NSString *text));// SGTTML.m. Apple Music's TTML as timed lines, the voices already turned into alignments; nil when// the document holds no line the page could show.NSArray<SGKaraokeLine *> *SGTTMLLines(NSString *xml);-// The lines as Spotify's own page takes them, ♪ over a break and an empty line at the end.-void SGTTMLPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **starts, NSArray<NSString *> **texts);+// The sources themselves, each in its own file.extern SGLyricsAsk SGBiniLyricsAsk;extern SGLyricsAsk SGMusixmatchAsk;extern SGLyricsAsk SGUnisonAsk;extern SGLyricsAsk SGNetEaseAsk;+extern SGLyricsAsk SGLrcLibAsk;UIViewController *SGLyricsSourcesPage(void); // the ordered list on the Lyrics pagediff --git a/tweak/Sources/Features/LyricsSources/LyricsSources.m b/tweak/Sources/Features/LyricsSources/LyricsSources.mindex d5173d62..73a1ca97 100644--- a/tweak/Sources/Features/LyricsSources/LyricsSources.m+++ b/tweak/Sources/Features/LyricsSources/LyricsSources.m@@ -70,6 +70,33 @@ void SGLyricsGetText(NSURL *url, void (^done)(NSString *text)) {});}+// A pause this long between two lines gets a ♪, so Spotify's page does not hold the last one.+static const NSInteger kBreakMs = 3000;++void SGLyricsPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **starts, NSArray<NSString *> **texts) {+ NSMutableArray<NSNumber *> *at = [NSMutableArray array];+ NSMutableArray<NSString *> *said = [NSMutableArray array];+ SGKaraokeLine *last = nil;+ for (SGKaraokeLine *line in lines) {+ if (last && line.start - last.end >= kBreakMs) {+ [at addObject:@(last.end)];+ [said addObject:@"♪"];+ }+ NSString *text = SGKaraokeLineText(line);+ // The backing vocals read on the same line on Spotify's own page, which has one row a line.+ if (line.backing) text = [text stringByAppendingFormat:@" %@", SGKaraokeLineText(line.backing)];+ [at addObject:@(line.start)];+ [said addObject:text];+ last = line;+ }+ if (last) {+ [at addObject:@(last.end)];+ [said addObject:@""];+ }+ *starts = at;+ *texts = said;+}+#pragma mark - which sources there are, and in what orderNSArray<SGLyricsProvider *> *SGLyricsAllProviders(void) {@@ -90,6 +117,7 @@ NSArray<SGLyricsProvider *> *SGLyricsAllProviders(void) {make(@"musixmatch", @"Musixmatch", @"The catalogue Spotify licenses; matched by track, never by name", SGMusixmatchAsk),make(@"unison", @"Unison", @"Written by hand for Better Lyrics: few tracks, the best of them", SGUnisonAsk),make(@"netease", @"NetEase", @"Word timing only, for what the others line time; swearing is starred out", SGNetEaseAsk),+ make(@"lrclib", @"LRCLIB", @"Open and keyless, timed by the line: the floor under the rest", SGLrcLibAsk),];});return all;diff --git a/tweak/Sources/Features/LyricsSources/SGTTML.m b/tweak/Sources/Features/LyricsSources/SGTTML.mindex 5fa79e96..5256182c 100644--- a/tweak/Sources/Features/LyricsSources/SGTTML.m+++ b/tweak/Sources/Features/LyricsSources/SGTTML.m@@ -195,30 +195,3 @@ NSArray<SGKaraokeLine *> *SGTTMLLines(NSString *xml) {SGKaraokeAlignVoices(reader.lines);return reader.lines;}--// A pause this long between two lines gets a ♪, so Spotify's page does not hold the last one.-static const NSInteger kBreakMs = 3000;--void SGTTMLPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **starts, NSArray<NSString *> **texts) {- NSMutableArray<NSNumber *> *at = [NSMutableArray array];- NSMutableArray<NSString *> *said = [NSMutableArray array];- SGKaraokeLine *last = nil;- for (SGKaraokeLine *line in lines) {- if (last && line.start - last.end >= kBreakMs) {- [at addObject:@(last.end)];- [said addObject:@"♪"];- }- NSString *text = SGKaraokeLineText(line);- // The backing vocals read on the same line on Spotify's own page, which has one row a line.- if (line.backing) text = [text stringByAppendingFormat:@" %@", SGKaraokeLineText(line.backing)];- [at addObject:@(line.start)];- [said addObject:text];- last = line;- }- if (last) {- [at addObject:@(last.end)];- [said addObject:@""];- }- *starts = at;- *texts = said;-}diff --git a/tweak/Sources/Features/LyricsSources/Unison.m b/tweak/Sources/Features/LyricsSources/Unison.mindex 2770af51..ac6ebb1d 100644--- a/tweak/Sources/Features/LyricsSources/Unison.m+++ b/tweak/Sources/Features/LyricsSources/Unison.m@@ -53,7 +53,7 @@ SGLyricsAsk SGUnisonAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *rresult.karaokeLines = lines;NSArray<NSNumber *> *starts;NSArray<NSString *> *texts;- SGTTMLPageLines(lines, &starts, &texts);+ SGLyricsPageLines(lines, &starts, &texts);result.starts = starts;result.texts = texts;SGLog(@"unison: %@ by %@ has %lu %@ lines (%@ confidence)", query.title, query.artist,diff --git a/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m b/tweak/Sources/Features/NowPlaying/NowPlayingSettings.mindex d56edffb..272ee29c 100644--- a/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m+++ b/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m@@ -29,7 +29,7 @@ static UIViewController *nowPlayingBarPage(void) {static UIViewController *lyricsPage(void) {// The row reads the order out, so which sources are on is visible without opening it.SGModRow *sources = SGPageRow(@"Lyrics sources", ^UIViewController *{ return SGLyricsSourcesPage(); });- sources.subtitle = @"BiniLyrics, Musixmatch, Unison and NetEase, in the order you put them";+ sources.subtitle = @"BiniLyrics, Musixmatch, Unison, NetEase and LRCLIB, in the order you put them";sources.value = ^NSString *{NSMutableArray<NSString *> *names = [NSMutableArray array];for (NSString *key in SGLyricsOrder()) [names addObject:SGLyricsProviderFor(key).name];