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: Spicy Lyrics as a lyrics source, matched by Spotify's track id and timed to the syllable

Vojtěch Škopek1d28754aa192
diff --git a/docs/tweaks.md b/docs/tweaks.md
index a426897a..6665f160 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -85,9 +85,14 @@ Shared:
for the lock screen and the Live Activity), and the Lyrics page's parts
LyricsSources/ the sources lyrics come from, asked in the order the Lyrics page puts them in and merged into the
best answer (LyricsSources.m, the list to drag in LyricsSourcesPage.m): Apple Music's TTML from
- BiniLyrics.m and Unison.m, read by SGTTML.m, which is the only shape carrying a second voice and the
+ BiniLyrics.m and Unison.m, read by SGTTML.m, which carries a second voice and the
backing vocals, and in its head Apple's translation and its pronunciation of a line, 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
+ timed word by word (the translation taken in the Lyrics page's language); SpicyLyrics.m (spicylyrics.org),
+ matched by Spotify's track id and carrying Apple Music's syllables, the backing vocals and the two sides
+ of a duet as the TTML does, answered in a packed shape of its own (every distinct value once, then an
+ opcode stream rebuilding the document) and only to a caller that sends the app's own Spotify token, which
+ SGKaraokeSpotifyAuthorization() lends it — the one source told anything of the account, which is why
+ the page says so; 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. color-lyrics is answered with whichever won
(LyricsHook.x): Spotify's own 200 gets our lines swapped in; a track Spotify's metadata says has none has
diff --git a/harness/spicylyrics/README.md b/harness/spicylyrics/README.md
new file mode 100644
index 00000000..d7900680
--- /dev/null
+++ b/harness/spicylyrics/README.md
@@ -0,0 +1,22 @@
+# Spicy Lyrics harness
+
+`Shared/LyricsSources/SpicyLyrics.m` compiled and run on the Mac, with the two things it reaches
+out through — the Spotify token it borrows and the POST — replaced by ones the test drives
+(`stubs.m`). Everything else in it is the real file.
+
+ ./build.sh
+
+What it is for is the packed shape the API answers in: every distinct value in the document once,
+then a stream of opcodes rebuilding it. `pack.ts` packs the test documents with the extension's
+**own packer** (cloned into `build/`), so the decoder in `SpicyLyrics.m` is checked against the only
+other implementation of that shape there is, rather than against a reading of it. The documents
+cover all three shapes the API sends — `Syllable`, `Line` and `Static` — and the `-1` and `-3`
+opcodes between them; the words in them are placeholders, not a song.
+
+It also holds the source to what the chain needs of it: a track the server has queued (a 503 inside
+a 200 envelope) counts as a failure, so `LyricsSources.m` does not keep the track as having no
+lyrics, while a 404 is an answer and does not; a reply that does not hold together is refused
+whole; and with no Spotify request seen yet to borrow a token from, nothing is sent at all.
+
+What it cannot check is the live API: it is behind Cloudflare and answers only a signed-in Spotify
+client, so the request as a whole is only proven on the phone.
diff --git a/harness/spicylyrics/build.sh b/harness/spicylyrics/build.sh
new file mode 100755
index 00000000..4a281ef6
--- /dev/null
+++ b/harness/spicylyrics/build.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+# Runs Shared/LyricsSources/SpicyLyrics.m against replies packed by Spicy Lyrics' own packer.
+# Needs bun, and clones the extension the first time to borrow that packer.
+set -e
+HERE=$(cd "$(dirname "$0")" && pwd)
+SRC=$HERE/../../tweak/Sources
+OUT=$HERE/build
+mkdir -p "$OUT"
+
+[ -d "$OUT/spicy-lyrics" ] || git clone --depth 1 https://github.com/Spikerko/spicy-lyrics.git "$OUT/spicy-lyrics"
+(cd "$HERE" && bun run pack.ts > "$OUT/fixtures.json")
+
+clang -fobjc-arc -g -O0 -o "$OUT/spicytest" \
+ "$HERE/main.m" "$HERE/stubs.m" "$SRC/Shared/LyricsSources/SpicyLyrics.m" \
+ -I"$HERE/include" -I"$SRC" -I"$SRC/Shared/LyricsSources" \
+ -framework Foundation
+
+cd "$OUT" && ./spicytest
diff --git a/harness/spicylyrics/include/Core/SGCore.h b/harness/spicylyrics/include/Core/SGCore.h
new file mode 100644
index 00000000..6f8cb55a
--- /dev/null
+++ b/harness/spicylyrics/include/Core/SGCore.h
@@ -0,0 +1,2 @@
+#import <Foundation/Foundation.h>
+#define SGLog(fmt, ...) do { if (getenv("SGVERBOSE")) fprintf(stderr, " log: %s\n", [NSString stringWithFormat:(fmt), ##__VA_ARGS__].UTF8String); } while (0)
diff --git a/harness/spicylyrics/include/UIKit/UIKit.h b/harness/spicylyrics/include/UIKit/UIKit.h
new file mode 100644
index 00000000..5bef64e9
--- /dev/null
+++ b/harness/spicylyrics/include/UIKit/UIKit.h
@@ -0,0 +1,4 @@
+// Enough UIKit for the lyrics headers to parse on the Mac; the source under test draws nothing.
+#import <Foundation/Foundation.h>
+@interface UIView : NSObject @end
+@interface UIViewController : NSObject @end
diff --git a/harness/spicylyrics/main.m b/harness/spicylyrics/main.m
new file mode 100644
index 00000000..59174341
--- /dev/null
+++ b/harness/spicylyrics/main.m
@@ -0,0 +1,127 @@
+// Runs Shared/LyricsSources/SpicyLyrics.m for real against replies packed by Spicy Lyrics' own
+// packer (../pack.ts), so the decoder and the line building are checked against the shape the
+// server actually sends rather than against my reading of it.
+#import "LyricsSources.h"
+#import "Shared/Lyrics/Lyrics.h"
+
+extern NSString *sg_token;
+extern NSURL *sg_sentTo;
+extern NSDictionary *sg_sentHeaders, *sg_sentBody;
+extern NSUInteger sg_sentCount;
+extern id sg_reply;
+extern NSInteger sg_notedFailures;
+extern NSArray<NSNumber *> *sg_estimatedStarts;
+extern NSArray<NSString *> *sg_estimatedTexts;
+
+static int failures;
+
+static void check(BOOL ok, NSString *what) {
+ printf(" %s %s\n", ok ? "ok " : "FAIL", what.UTF8String);
+ if (!ok) failures++;
+}
+
+static void checkEqual(id got, id want, NSString *what) {
+ check([got isEqual:want], [NSString stringWithFormat:@"%@ (got %@, want %@)", what, got, want]);
+}
+
+// The one reply shape the API uses: a 200 envelope with the query's own status inside it.
+static id envelope(NSInteger status, id data) {
+ return @{@"queries": @[@{@"operation": @"lyrics", @"operationId": @"0",
+ @"result": @{@"httpStatus": @(status), @"format": @"json", @"data": data ?: NSNull.null}}]};
+}
+
+static SGLyricsResult *ask(NSString *trackID) {
+ SGLyricsQuery *query = [SGLyricsQuery new];
+ query.trackID = trackID;
+ __block SGLyricsResult *answer = nil;
+ __block BOOL called = NO;
+ SGSpicyLyricsAsk(query, ^(SGLyricsResult *result) {
+ answer = result;
+ called = YES;
+ });
+ check(called, @"the source answered");
+ return answer;
+}
+
+int main(void) {
+ @autoreleasepool {
+ NSData *raw = [NSData dataWithContentsOfFile:@"fixtures.json"];
+ NSDictionary *packed = [NSJSONSerialization JSONObjectWithData:raw options:0 error:nil];
+ NSCAssert(packed.count, @"no fixtures; run pack.ts first");
+
+ printf("syllable lyrics\n");
+ sg_reply = envelope(200, packed[@"syllable"]);
+ SGLyricsResult *result = ask(@"0VjIjW4GlUZAMYd2vXMi3b");
+ check(result != nil, @"there is a result");
+ check(result.synced && result.wordTimed, @"synced and word timed");
+ checkEqual(@(result.karaokeLines.count), @2, @"two lines");
+ SGKaraokeLine *first = result.karaokeLines.firstObject;
+ checkEqual(@(first.start), @1500, @"the line starts at its own StartTime, in ms");
+ checkEqual(@(first.end), @4250, @"the line ends at its own EndTime, in ms");
+ checkEqual(@(first.words.count), @3, @"three syllables are three words");
+ checkEqual(@(first.words[0].start), @1500, @"the first syllable's start");
+ checkEqual(@(first.words[1].end), @2400, @"a fractional second lands on the right ms");
+ check(!first.words[0].joined, @"the first word of a line is never joined");
+ check(first.words[1].joined, @"a syllable after IsPartOfWord is laid flush");
+ check(!first.words[2].joined, @"a syllable after a whole word is not");
+ checkEqual(@(first.align), @(SGKaraokeAlignLeading), @"the first voice leads");
+ check(first.backing != nil, @"the line has its backing vocals");
+ checkEqual(SGKaraokeLineText(first.backing), @"echo", @"the backing words");
+ checkEqual(@(first.backing.start), @3000, @"the backing starts with its own first word");
+ SGKaraokeLine *second = result.karaokeLines[1];
+ checkEqual(@(second.align), @(SGKaraokeAlignTrailing), @"OppositeAligned puts the line on the far side");
+ check(second.backing == nil, @"a line with no Background has no backing line");
+ checkEqual(@(result.texts.count), @2, @"the page gets a line for each");
+
+ printf("the request itself\n");
+ checkEqual(sg_sentTo.absoluteString, @"https://api.spicylyrics.org/query", @"the address");
+ checkEqual(sg_sentHeaders[@"SpicyLyrics-WebAuth"], sg_token, @"the token goes in the header it names");
+ checkEqual(sg_sentHeaders[@"X-mode"], @"2", @"the packed shape is asked for");
+ check([sg_sentHeaders[@"SpicyLyrics-Version"] length] > 0, @"a client version is sent");
+ NSDictionary *sent = [sg_sentBody[@"queries"] firstObject];
+ checkEqual(sent[@"operation"], @"lyrics", @"the operation");
+ checkEqual(sent[@"variables"][@"id"], @"0VjIjW4GlUZAMYd2vXMi3b", @"the track id, not a name");
+ checkEqual(sent[@"variables"][@"auth"], @"SpicyLyrics-WebAuth", @"the header the token is in is named");
+
+ printf("line timed lyrics\n");
+ sg_reply = envelope(200, packed[@"line"]);
+ result = ask(@"track2");
+ check(result.synced && !result.wordTimed, @"synced but not word timed");
+ checkEqual(sg_estimatedStarts, (@[@2000, @4500]), @"the estimator gets the line starts in ms");
+ checkEqual(sg_estimatedTexts, (@[@"delta foxtrot", @"golf hotel"]), @"and their text");
+
+ printf("static lyrics\n");
+ sg_reply = envelope(200, packed[@"static"]);
+ result = ask(@"track3");
+ check(!result.synced, @"not synced");
+ checkEqual(@(result.karaokeLines.count), @0, @"nothing to sweep");
+ checkEqual(result.texts, (@[@"india", @"♪", @"juliett"]), @"the words, a break for the empty line");
+ checkEqual(result.starts, (@[@0, @0, @0]), @"every start is zero");
+
+ printf("what goes wrong\n");
+ NSInteger before = sg_notedFailures;
+ sg_reply = envelope(503, nil);
+ check(ask(@"track4") == nil, @"a queued track has no lyrics yet");
+ checkEqual(@(sg_notedFailures), @(before + 1), @"and the walk is told, so it is not kept as having none");
+ before = sg_notedFailures;
+ sg_reply = envelope(404, nil);
+ check(ask(@"track5") == nil, @"a track it does not have");
+ checkEqual(@(sg_notedFailures), @(before), @"which is an answer, not a failure");
+ sg_reply = envelope(200, @[@[@"Type"], @[@0, @0, @0]]);
+ check(ask(@"track6") == nil, @"a stream that does not hold together");
+ sg_reply = envelope(200, @[packed[@"static"][0], [packed[@"static"][1] arrayByAddingObject:@0]]);
+ check(ask(@"track7") == nil, @"a document with something tacked on the end");
+ sg_reply = @{@"queries": @[]};
+ check(ask(@"track8") == nil, @"an envelope with no answer in it");
+
+ printf("with no token to borrow\n");
+ NSUInteger sentBefore = sg_sentCount;
+ sg_token = nil;
+ sg_reply = envelope(200, packed[@"syllable"]);
+ check(ask(@"track9") == nil, @"nothing is answered");
+ checkEqual(@(sg_sentCount), @(sentBefore), @"and nothing is sent");
+
+ printf("\n%s\n", failures ? [NSString stringWithFormat:@"%d failed", failures].UTF8String : "all passed");
+ return failures ? 1 : 0;
+ }
+}
diff --git a/harness/spicylyrics/pack.ts b/harness/spicylyrics/pack.ts
new file mode 100644
index 00000000..5999e87b
--- /dev/null
+++ b/harness/spicylyrics/pack.ts
@@ -0,0 +1,54 @@
+// Packs test documents with Spicy Lyrics' own packer, so the ObjC decoder is checked against the
+// only implementation of this shape there is. The words are placeholders, not anyone's lyrics.
+import { SLObjPack } from "./build/spicy-lyrics/src/utils/objpack.ts";
+const packer = new SLObjPack();
+
+const syllable = {
+ Type: "Syllable",
+ SongWriters: ["A Writer"],
+ Content: [
+ {
+ Type: "Vocal",
+ Lead: {
+ StartTime: 1.5, EndTime: 4.25,
+ Syllables: [
+ { Text: "al", StartTime: 1.5, EndTime: 1.9, IsPartOfWord: true },
+ { Text: "pha", StartTime: 1.9, EndTime: 2.4 },
+ { Text: "bravo", StartTime: 2.6, EndTime: 4.25 },
+ ],
+ },
+ Background: [
+ { StartTime: 3.0, EndTime: 4.0, Syllables: [{ Text: "echo", StartTime: 3.0, EndTime: 4.0 }] },
+ ],
+ },
+ {
+ Type: "Vocal",
+ OppositeAligned: true,
+ Lead: {
+ StartTime: 9.0, EndTime: 11.0,
+ Syllables: [{ Text: "charlie", StartTime: 9.0, EndTime: 11.0 }],
+ },
+ },
+ ],
+};
+
+const line = {
+ Type: "Line",
+ Content: [
+ { Type: "Vocal", Text: "delta foxtrot", StartTime: 2.0, EndTime: 4.0 },
+ { Type: "Vocal", Text: "golf hotel", StartTime: 4.5, EndTime: 6.0 },
+ ],
+};
+
+const staticDoc = { Type: "Static", Lines: [{ Text: "india" }, { Text: "" }, { Text: "juliett" }] };
+
+const out = {
+ syllable: packer.pack(syllable),
+ line: packer.pack(line),
+ static: packer.pack(staticDoc),
+ // Round-trip proof that the packer and its unpacker agree on these documents.
+ roundTrips: [syllable, line, staticDoc].every(
+ (d) => JSON.stringify(packer.unpack(packer.pack(d))) === JSON.stringify(d)
+ ),
+};
+console.log(JSON.stringify(out));
diff --git a/harness/spicylyrics/stubs.m b/harness/spicylyrics/stubs.m
new file mode 100644
index 00000000..016782ff
--- /dev/null
+++ b/harness/spicylyrics/stubs.m
@@ -0,0 +1,77 @@
+// Just enough of the mod around SpicyLyrics.m for it to run on the Mac: the lyrics value types as
+// they really are, and the two things the source reaches out through — the borrowed Spotify token
+// and the POST — replaced by ones the test drives.
+#import "LyricsSources.h"
+#import "Shared/Lyrics/Lyrics.h"
+
+@implementation SGKaraokeWord @end
+@implementation SGKaraokeLine @end
+@implementation SGLyricsResult @end
+@implementation SGLyricsQuery @end
+@implementation SGLyricsProvider @end
+
+NSString *sg_token = @"Bearer test-token";
+NSString *SGKaraokeSpotifyAuthorization(void) { return sg_token; }
+
+// What the last request carried, and what the next one is answered with.
+NSURL *sg_sentTo;
+NSDictionary *sg_sentHeaders, *sg_sentBody;
+NSUInteger sg_sentCount;
+id sg_reply;
+NSInteger sg_notedFailures;
+
+void SGLyricsNoteReply(NSURLResponse *response, NSError *error) {
+ NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;
+ if (error || status == 429 || status >= 500) sg_notedFailures++;
+}
+
+void SGLyricsPostJSON(NSURL *url, NSDictionary<NSString *, NSString *> *headers, id body, void (^done)(id root)) {
+ sg_sentTo = url;
+ sg_sentHeaders = headers;
+ sg_sentBody = body;
+ sg_sentCount++;
+ // The real one refuses a body it cannot write, so hold this one to the same bar.
+ NSCAssert([NSJSONSerialization isValidJSONObject:body], @"the body is not JSON");
+ done(sg_reply);
+}
+
+// The page lines are LyricsSources.m's own, proven work; here they only need to be countable.
+void SGLyricsPageLines(NSArray<SGKaraokeLine *> *lines, NSArray<NSNumber *> **starts, NSArray<NSString *> **texts) {
+ NSMutableArray<NSNumber *> *at = [NSMutableArray array];
+ NSMutableArray<NSString *> *said = [NSMutableArray array];
+ for (SGKaraokeLine *line in lines) {
+ [at addObject:@(line.start)];
+ [said addObject:SGKaraokeLineText(line)];
+ }
+ *starts = at;
+ *texts = said;
+}
+
+// The estimator is Shared/Lyrics' own; what matters here is what the source hands it.
+NSArray<NSNumber *> *sg_estimatedStarts;
+NSArray<NSString *> *sg_estimatedTexts;
+
+NSArray<SGKaraokeLine *> *SGKaraokeEstimatedLines(NSArray<NSNumber *> *starts, NSArray<NSString *> *texts) {
+ sg_estimatedStarts = starts;
+ sg_estimatedTexts = texts;
+ NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];
+ for (NSUInteger i = 0; i < texts.count; i++) {
+ SGKaraokeWord *word = [SGKaraokeWord new];
+ word.text = texts[i];
+ word.start = starts[i].integerValue;
+ SGKaraokeLine *line = [SGKaraokeLine new];
+ line.words = @[word];
+ line.start = word.start;
+ [lines addObject:line];
+ }
+ return lines.count ? lines : nil;
+}
+
+NSString *SGKaraokeLineText(SGKaraokeLine *line) {
+ NSMutableString *text = [NSMutableString string];
+ for (SGKaraokeWord *word in line.words) {
+ if (text.length && !word.joined) [text appendString:@" "];
+ [text appendString:word.text];
+ }
+ return text;
+}
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
index a7734f41..91617064 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x
+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
@@ -89,6 +89,10 @@ NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID) {
return trackID ? sg_lyrics[trackID] : nil;
}
+NSString *SGKaraokeSpotifyAuthorization(void) {
+ return sg_spclientHeaders[@"authorization"];
+}
+
static void requestFromSpotify(NSString *trackID) {
NSDictionary<NSString *, NSString *> *headers = sg_spclientHeaders;
if (!headers) return;
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index 47762d87..61097e40 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -74,6 +74,10 @@ void SGKaraokeKeepLines(NSString *trackID, NSArray<SGKaraokeLine *> *lines);
// Asks spclient for a track's lyrics once, with the headers of Spotify's own requests, for when no
// page of Spotify's has asked for them, e.g. with the app in the background.
void SGKaraokeRequestLyrics(NSString *trackID);
+// The Authorization header Spotify's own requests carry, "Bearer ..." and the account's own token;
+// nil until one has gone out. Only for a source that answers no one who cannot show they are a
+// signed-in Spotify client, and only with the mod's user having switched that source on.
+NSString *SGKaraokeSpotifyAuthorization(void);
NSString *SGKaraokePlayingTrack(void); // the base62 id, nil before the player reported
NSInteger SGKaraokePositionMs(void); // negative when unknown
void SGKaraokeSeek(NSInteger ms);
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
index 0d1f8a3b..186a7b44 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
@@ -4,7 +4,8 @@
// later one that times every word.
//
// SGTTML.m reads the TTML that Apple Music's own lyrics are written in, which is what BiniLyrics and
-// Unison serve; it is the only shape carrying a second voice and the (oh, aye) sung under a line.
+// Unison serve. It and Spicy Lyrics are the only shapes carrying a second voice and the (oh, aye)
+// sung under a line; every other source times lines, or the words inside them, and nothing more.
#import <UIKit/UIKit.h>
#import "Shared/Lyrics/Lyrics.h"
@@ -56,7 +57,7 @@ typedef void (^SGLyricsAsk)(SGLyricsQuery *query, void (^done)(SGLyricsResult *r
@property (nonatomic, copy) NSString *name; // "BiniLyrics", what the credit reads
@property (nonatomic, copy) NSString *detail; // one line under the name on the Lyrics page
// Searches by title and artist, so it has nothing to ask with until someone has named the track.
-// Only Musixmatch matches by Spotify's id and can go without.
+// Musixmatch and Spicy Lyrics match by Spotify's id and can go without.
@property (nonatomic) BOOL needsName;
@property (nonatomic, copy) SGLyricsAsk ask;
@end
@@ -98,6 +99,9 @@ void SGLyricsMigrateLegacyKeys(void);
NSURL *SGLyricsURL(NSString *base, NSDictionary<NSString *, NSString *> *query);
void SGLyricsGetJSON(NSURL *url, NSDictionary<NSString *, NSString *> *headers, void (^done)(id root));
void SGLyricsGetText(NSURL *url, void (^done)(NSString *text));
+// For the one source that is asked a question rather than sent to an address. body is anything
+// NSJSONSerialization writes; nothing is sent at all when it is not.
+void SGLyricsPostJSON(NSURL *url, NSDictionary<NSString *, NSString *> *headers, id body, void (^done)(id root));
// Every source's reply goes through this, so a walk that lost a request to the network or a busy
// server is not kept as "no lyrics". The two above call it themselves.
void SGLyricsNoteReply(NSURLResponse *response, NSError *error);
@@ -122,5 +126,6 @@ extern SGLyricsAsk SGMusixmatchAsk;
extern SGLyricsAsk SGUnisonAsk;
extern SGLyricsAsk SGNetEaseAsk;
extern SGLyricsAsk SGLrcLibAsk;
+extern SGLyricsAsk SGSpicyLyricsAsk;
UIViewController *SGLyricsSourcesPage(void); // the ordered list on the Lyrics page
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.m b/tweak/Sources/Shared/LyricsSources/LyricsSources.m
index a57b43e1..3145d26c 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.m
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.m
@@ -52,15 +52,21 @@ NSURL *SGLyricsURL(NSString *base, NSDictionary<NSString *, NSString *> *query)
return url.URL;
}
-static void get(NSURL *url, NSDictionary<NSString *, NSString *> *headers, void (^done)(NSData *body)) {
- if (!url) {
- dispatch_async(dispatch_get_main_queue(), ^{ done(nil); });
- return;
- }
+static NSMutableURLRequest *requestFor(NSURL *url, NSDictionary<NSString *, NSString *> *headers) {
+ if (!url) return nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:kTimeout];
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {
[request setValue:value forHTTPHeaderField:name];
}];
+ return request;
+}
+
+static void send(NSURLRequest *request, void (^done)(NSData *body)) {
+ if (!request) {
+ dispatch_async(dispatch_get_main_queue(), ^{ done(nil); });
+ return;
+ }
+ NSURL *url = request.URL;
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;
SGLyricsNoteReply(response, error);
@@ -69,18 +75,34 @@ static void get(NSURL *url, NSDictionary<NSString *, NSString *> *headers, void
}] resume];
}
+static id jsonIn(NSData *body) {
+ return body.length ? [NSJSONSerialization JSONObjectWithData:body options:0 error:nil] : nil;
+}
+
void SGLyricsGetJSON(NSURL *url, NSDictionary<NSString *, NSString *> *headers, void (^done)(id root)) {
- get(url, headers, ^(NSData *body) {
- done(body.length ? [NSJSONSerialization JSONObjectWithData:body options:0 error:nil] : nil);
+ send(requestFor(url, headers), ^(NSData *body) {
+ done(jsonIn(body));
});
}
void SGLyricsGetText(NSURL *url, void (^done)(NSString *text)) {
- get(url, nil, ^(NSData *body) {
+ send(requestFor(url, nil), ^(NSData *body) {
done(body.length ? [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] : nil);
});
}
+void SGLyricsPostJSON(NSURL *url, NSDictionary<NSString *, NSString *> *headers, id body, void (^done)(id root)) {
+ NSData *written = [NSJSONSerialization isValidJSONObject:body]
+ ? [NSJSONSerialization dataWithJSONObject:body options:0 error:nil] : nil;
+ NSMutableURLRequest *request = written ? requestFor(url, headers) : nil;
+ request.HTTPMethod = @"POST";
+ request.HTTPBody = written;
+ [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
+ send(request, ^(NSData *answer) {
+ done(jsonIn(answer));
+ });
+}
+
// A pause this long between two lines gets a ♪, so Spotify's page does not hold the last one.
static const NSInteger kBreakMs = 3000;
@@ -120,11 +142,14 @@ NSArray<SGLyricsProvider *> *SGLyricsAllProviders(void) {
provider.key = key;
provider.name = name;
provider.detail = detail;
- provider.needsName = ![key isEqualToString:@"musixmatch"];
+ // A source that matches by Spotify's own track id has everything it needs from the
+ // start; the rest wait for the player to name the track before they can search.
+ provider.needsName = ![@[@"musixmatch", @"spicylyrics"] containsObject:key];
provider.ask = ask;
return provider;
};
all = @[
+ make(@"spicylyrics", @"Spicy Lyrics", @"Syllable timing matched by track id; asks with your Spotify token", SGSpicyLyricsAsk),
make(@"binilyrics", @"BiniLyrics", @"Apple Music's own word timing, over a million tracks", SGBiniLyricsAsk),
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),
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSourcesPage.m b/tweak/Sources/Shared/LyricsSources/LyricsSourcesPage.m
index 6fcac87a..7c8773cb 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSourcesPage.m
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSourcesPage.m
@@ -47,9 +47,11 @@ typedef NS_ENUM(NSInteger, SGSourcesSection) {
self.tableView.allowsSelectionDuringEditing = YES;
_footer = SGNote(@"Each source is asked in turn until every word is timed. A source that only has "
"the plain words does not shut out a later one that times them.\n\n"
- "Musixmatch is sent the track's id with an anonymous token; the rest are sent the "
- "title, artist and length. None of them is told anything of your Spotify account. "
- "Changes apply after you restart Spotify.");
+ "Musixmatch is sent the track's id with an anonymous token; BiniLyrics, Unison, "
+ "NetEase and LRCLIB are sent the title, artist and length. None of those is told "
+ "anything of your Spotify account. Spicy Lyrics is the one exception: it answers "
+ "only a signed-in Spotify client, so it is sent this app's own access token along "
+ "with the track's id. Changes apply after you restart Spotify.");
self.tableView.tableFooterView = _footer;
}
diff --git a/tweak/Sources/Shared/LyricsSources/SpicyLyrics.m b/tweak/Sources/Shared/LyricsSources/SpicyLyrics.m
new file mode 100644
index 00000000..ec050680
--- /dev/null
+++ b/tweak/Sources/Shared/LyricsSources/SpicyLyrics.m
@@ -0,0 +1,359 @@
+// Spicy Lyrics, the source behind the Spicetify extension of the same name (spicylyrics.org). Like
+// Musixmatch it is asked by Spotify's own track id rather than by name, so it never answers with a
+// live cut of the song by mistake; unlike Musixmatch, what it has is Apple Music's syllable timing,
+// down to the backing vocals and the two sides of a duet.
+//
+// Two things about it are like nothing else here. It answers no one who cannot show they are a
+// signed-in Spotify client, so the request carries the same Authorization header Spotify's own
+// requests carry — the account's own access token. That is more than any other source is told, which
+// is why the Lyrics page says so plainly and why this source, like the rest, is off until you turn it
+// on. And it answers in a packed shape rather than plain JSON: every distinct value in the document
+// once, then a stream of opcodes rebuilding it. unpack() below puts it back together.
+#import "Core/SGCore.h"
+#import "LyricsSources.h"
+#import "Shared/Lyrics/Lyrics.h"
+
+static NSString *const kAPI = @"https://api.spicylyrics.org/query";
+// The header the token rides in. The request names it in the query's variables as well, which is how
+// the server is told which of the headers it accepts this caller is using.
+static NSString *const kAuthHeader = @"SpicyLyrics-WebAuth";
+// The extension release this file was written against. The server reads it to tell old clients from
+// new, so it moves when the shapes read below do.
+static NSString *const kClientVersion = @"6.3.20";
+// Only one query goes out per ask, and the reply names its answers by the index they came in at.
+static NSString *const kOperationID = @"0";
+
+#pragma mark - the packed shape
+
+// A reply is [values, stream]: every distinct string, number, boolean and null in the document, and
+// a stream of integers rebuilding it. A number at or above zero is an index into the values; below
+// zero it opens a structure. Straight from the extension's own packer, which is the only writer of
+// this shape there is.
+typedef NS_ENUM(NSInteger, SGSpicyOp) {
+ SGSpicyOpObject = -1, // key count, that many key indexes, then that many values
+ SGSpicyOpArray = -2, // item count, then that many values
+ SGSpicyOpRows = -3, // item count, key count, the keys once, then the rows' values
+ SGSpicyOpEmptyArray = -4,
+ SGSpicyOpOneItemArray = -5, // the one value
+ SGSpicyOpEmptyObject = -6,
+};
+
+// Deep enough for any lyrics document and shallow enough that a malformed reply cannot run the
+// stack out: the shape below nests six levels, not sixty.
+static const NSUInteger kMaxDepth = 64;
+
+@interface SGSpicyReader : NSObject {
+@public
+ NSArray *values, *stream;
+ NSUInteger cursor;
+ BOOL bad; // the reply did not hold together; whatever has been read of it is thrown away
+}
+@end
+
+@implementation SGSpicyReader
+@end
+
+static NSInteger nextNumber(SGSpicyReader *reader) {
+ if (reader->cursor >= reader->stream.count) {
+ reader->bad = YES;
+ return 0;
+ }
+ id number = reader->stream[reader->cursor++];
+ if (![number isKindOfClass:NSNumber.class]) {
+ reader->bad = YES;
+ return 0;
+ }
+ return [number integerValue];
+}
+
+static id valueAt(SGSpicyReader *reader, NSInteger index) {
+ if (index < 0 || (NSUInteger)index >= reader->values.count) {
+ reader->bad = YES;
+ return nil;
+ }
+ return reader->values[index];
+}
+
+static NSString *nextKey(SGSpicyReader *reader) {
+ id key = valueAt(reader, nextNumber(reader));
+ if (![key isKindOfClass:NSString.class]) {
+ reader->bad = YES;
+ return nil;
+ }
+ return key;
+}
+
+static id decode(SGSpicyReader *reader, NSUInteger depth) {
+ if (reader->bad || depth > kMaxDepth) {
+ reader->bad = YES;
+ return nil;
+ }
+ NSInteger op = nextNumber(reader);
+ if (reader->bad) return nil;
+ if (op >= 0) return valueAt(reader, op);
+ // A count is read before anything is built with it, so a reply claiming a million keys cannot
+ // have a million slots reserved for it before the stream is found to be shorter than that.
+ NSInteger count = op == SGSpicyOpObject || op == SGSpicyOpArray || op == SGSpicyOpRows ? nextNumber(reader) : 0;
+ if (reader->bad || count < 0 || (NSUInteger)count > reader->stream.count) {
+ reader->bad = YES;
+ return nil;
+ }
+ switch (op) {
+ case SGSpicyOpObject: {
+ NSMutableArray<NSString *> *keys = [NSMutableArray arrayWithCapacity:count];
+ for (NSInteger i = 0; i < count; i++) {
+ NSString *key = nextKey(reader);
+ if (reader->bad) return nil;
+ [keys addObject:key];
+ }
+ NSMutableDictionary *object = [NSMutableDictionary dictionaryWithCapacity:count];
+ for (NSInteger i = 0; i < count; i++) {
+ id value = decode(reader, depth + 1);
+ if (reader->bad) return nil;
+ object[keys[i]] = value;
+ }
+ return object;
+ }
+ case SGSpicyOpArray: {
+ NSMutableArray *items = [NSMutableArray arrayWithCapacity:count];
+ for (NSInteger i = 0; i < count; i++) {
+ id item = decode(reader, depth + 1);
+ if (reader->bad) return nil;
+ [items addObject:item];
+ }
+ return items;
+ }
+ // A run of objects sharing their keys — every line of a song — with the keys written once.
+ case SGSpicyOpRows: {
+ NSInteger keyCount = nextNumber(reader);
+ if (reader->bad || keyCount < 0 || (NSUInteger)keyCount > reader->stream.count) {
+ reader->bad = YES;
+ return nil;
+ }
+ NSMutableArray<NSString *> *keys = [NSMutableArray arrayWithCapacity:keyCount];
+ for (NSInteger i = 0; i < keyCount; i++) {
+ NSString *key = nextKey(reader);
+ if (reader->bad) return nil;
+ [keys addObject:key];
+ }
+ NSMutableArray *rows = [NSMutableArray arrayWithCapacity:count];
+ for (NSInteger i = 0; i < count; i++) {
+ NSMutableDictionary *row = [NSMutableDictionary dictionaryWithCapacity:keyCount];
+ for (NSInteger k = 0; k < keyCount; k++) {
+ id value = decode(reader, depth + 1);
+ if (reader->bad) return nil;
+ row[keys[k]] = value;
+ }
+ [rows addObject:row];
+ }
+ return rows;
+ }
+ case SGSpicyOpEmptyArray: return @[];
+ case SGSpicyOpOneItemArray: {
+ id only = decode(reader, depth + 1);
+ return reader->bad ? nil : @[only];
+ }
+ case SGSpicyOpEmptyObject: return @{};
+ }
+ reader->bad = YES;
+ return nil;
+}
+
+static id unpack(id packed) {
+ // Should the server ever answer a caller of ours with the document itself, it is already what
+ // the rest of this file wants.
+ if ([packed isKindOfClass:NSDictionary.class]) return packed;
+ if (![packed isKindOfClass:NSArray.class] || [packed count] != 2) return nil;
+ id values = packed[0], stream = packed[1];
+ if (![values isKindOfClass:NSArray.class] || ![stream isKindOfClass:NSArray.class]) return nil;
+ SGSpicyReader *reader = [SGSpicyReader new];
+ reader->values = values;
+ reader->stream = stream;
+ id document = decode(reader, 0);
+ // Anything left in the stream means it was not the document it claimed to be.
+ if (reader->bad || reader->cursor != reader->stream.count) {
+ SGLog(@"spicy: the packed reply did not hold together, %lu of %lu read",
+ (unsigned long)reader->cursor, (unsigned long)reader->stream.count);
+ return nil;
+ }
+ return document;
+}
+
+#pragma mark - the document as lines
+
+// The API times in seconds, the mod in milliseconds.
+static NSInteger msIn(id seconds) {
+ return [seconds isKindOfClass:NSNumber.class] ? (NSInteger)llround([seconds doubleValue] * 1000.0) : 0;
+}
+
+static NSDictionary *dictionaryIn(id value) {
+ return [value isKindOfClass:NSDictionary.class] ? value : nil;
+}
+
+static NSArray *arrayIn(id value) {
+ return [value isKindOfClass:NSArray.class] ? value : nil;
+}
+
+// One voice's syllables as words. A syllable is a piece of a word — the API marks the ones that run
+// into the next with IsPartOfWord, which is the same thing the mod calls a word being joined to the
+// one before it, read from the other end.
+static NSArray<SGKaraokeWord *> *wordsFrom(NSArray *syllables) {
+ NSMutableArray<SGKaraokeWord *> *words = [NSMutableArray array];
+ BOOL joinToPrevious = NO;
+ for (id raw in syllables ?: @[]) {
+ NSDictionary *syllable = dictionaryIn(raw);
+ if (!syllable) continue;
+ BOOL runsOn = [syllable[@"IsPartOfWord"] boolValue];
+ id text = syllable[@"Text"];
+ NSString *said = [text isKindOfClass:NSString.class]
+ ? [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet] : nil;
+ if (said.length) {
+ SGKaraokeWord *word = [SGKaraokeWord new];
+ word.text = said;
+ word.start = msIn(syllable[@"StartTime"]);
+ word.end = msIn(syllable[@"EndTime"]);
+ word.joined = joinToPrevious && words.count > 0;
+ [words addObject:word];
+ }
+ joinToPrevious = runsOn;
+ }
+ return words;
+}
+
+static SGKaraokeLine *lineFrom(NSArray<SGKaraokeWord *> *words, id start, id end) {
+ if (!words.count) return nil;
+ SGKaraokeLine *line = [SGKaraokeLine new];
+ line.words = words;
+ // The line's own times where it has them; the words it is made of where it does not.
+ line.start = start ? msIn(start) : words.firstObject.start;
+ line.end = end ? msIn(end) : words.lastObject.end;
+ return line;
+}
+
+// Content: [ { Type: "Vocal", OppositeAligned, Lead: { StartTime, EndTime, Syllables }, Background } ]
+static NSArray<SGKaraokeLine *> *linesFromSyllables(NSArray *content) {
+ NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];
+ for (id raw in content ?: @[]) {
+ NSDictionary *vocal = dictionaryIn(raw);
+ NSDictionary *lead = dictionaryIn(vocal[@"Lead"]);
+ if (!lead) continue;
+ SGKaraokeLine *line = lineFrom(wordsFrom(arrayIn(lead[@"Syllables"])), lead[@"StartTime"], lead[@"EndTime"]);
+ if (!line) continue;
+ // The (oh, aye) sung under the line. The API keeps it in groups, one per run of it; the page
+ // has one backing line under each line, so the runs read on as one.
+ NSMutableArray<SGKaraokeWord *> *backing = [NSMutableArray array];
+ for (id rawGroup in arrayIn(vocal[@"Background"]) ?: @[]) {
+ NSDictionary *group = dictionaryIn(rawGroup);
+ if (group) [backing addObjectsFromArray:wordsFrom(arrayIn(group[@"Syllables"]))];
+ }
+ line.backing = lineFrom(backing, nil, nil);
+ // The far side of a duet. The API says outright which lines belong to the second voice, so
+ // unlike TTML there are no voices here to work an alignment out from.
+ if ([vocal[@"OppositeAligned"] boolValue] || [lead[@"OppositeAligned"] boolValue]) {
+ line.align = SGKaraokeAlignTrailing;
+ }
+ [lines addObject:line];
+ }
+ return lines.count ? lines : nil;
+}
+
+// Content: [ { Type: "Vocal", Text, StartTime, EndTime } ] — timed by the line, the words inside it
+// left to the mod's own estimate, as Spotify's own lines and LRCLIB's are.
+static NSArray<SGKaraokeLine *> *linesFromLines(NSArray *content) {
+ NSMutableArray<NSNumber *> *starts = [NSMutableArray array];
+ NSMutableArray<NSString *> *texts = [NSMutableArray array];
+ for (id raw in content ?: @[]) {
+ NSDictionary *vocal = dictionaryIn(raw);
+ id text = vocal[@"Text"];
+ if (![text isKindOfClass:NSString.class]) continue;
+ [starts addObject:@(msIn(vocal[@"StartTime"]))];
+ [texts addObject:text];
+ }
+ return SGKaraokeEstimatedLines(starts, texts);
+}
+
+#pragma mark - the source
+
+SGLyricsAsk SGSpicyLyricsAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResult *result)) {
+ NSString *authorization = SGKaraokeSpotifyAuthorization();
+ if (!query.trackID.length || !authorization.length) {
+ SGLog(@"spicy: nothing to ask with for %@%@", query.trackID,
+ authorization.length ? @"" : @", no request of Spotify's own seen yet to borrow the token from");
+ done(nil);
+ return;
+ }
+ NSDictionary *body = @{
+ @"queries": @[@{@"operation": @"lyrics", @"variables": @{@"id": query.trackID, @"auth": kAuthHeader}}],
+ @"client": @{@"version": kClientVersion},
+ };
+ NSURL *url = [NSURL URLWithString:kAPI];
+ SGLyricsPostJSON(url, @{
+ @"X-mode": @"2", // answer in the packed shape unpack() reads
+ @"SpicyLyrics-Version": kClientVersion,
+ kAuthHeader: authorization, // already a "Bearer ..." of Spotify's own making
+ }, body, ^(id root) {
+ NSDictionary *job = nil;
+ for (id raw in arrayIn(dictionaryIn(root)[@"queries"]) ?: @[]) {
+ if ([dictionaryIn(raw)[@"operationId"] isEqual:kOperationID]) {
+ job = raw;
+ break;
+ }
+ }
+ NSDictionary *answer = dictionaryIn(job[@"result"]);
+ NSInteger status = [answer[@"httpStatus"] integerValue];
+ if (!answer) {
+ SGLog(@"spicy: no answer to the query for %@", query.trackID);
+ done(nil);
+ return;
+ }
+ // The query's own status rides inside an envelope that is a 200 either way, so the walk is
+ // told here about a server too busy to answer: a 503 means the lyrics are being prepared and
+ // a later ask will have them, which must not stick to the track as "this one has none".
+ if (status == 429 || status >= 500) {
+ SGLyricsNoteReply([[NSHTTPURLResponse alloc] initWithURL:url statusCode:status HTTPVersion:nil headerFields:nil], nil);
+ }
+ if (status != 200) {
+ SGLog(@"spicy: %@ answered %ld", query.trackID, (long)status);
+ done(nil);
+ return;
+ }
+ NSDictionary *document = dictionaryIn(unpack(answer[@"data"]));
+ NSString *type = [document[@"Type"] isKindOfClass:NSString.class] ? document[@"Type"] : nil;
+ NSArray *content = arrayIn(document[@"Content"]);
+ BOOL wordTimed = [type isEqualToString:@"Syllable"];
+ NSArray<SGKaraokeLine *> *lines = wordTimed ? linesFromSyllables(content)
+ : [type isEqualToString:@"Line"] ? linesFromLines(content) : nil;
+ SGLyricsResult *result = [SGLyricsResult new];
+ if (lines) {
+ result.synced = YES;
+ result.wordTimed = wordTimed;
+ result.karaokeLines = lines;
+ NSArray<NSNumber *> *starts;
+ NSArray<NSString *> *texts;
+ SGLyricsPageLines(lines, &starts, &texts);
+ result.starts = starts;
+ result.texts = texts;
+ } else if ([type isEqualToString:@"Static"]) {
+ // The words with nothing to time them by: the page shows them, and a source under this
+ // one in the order can still time them.
+ NSMutableArray<NSNumber *> *starts = [NSMutableArray array];
+ NSMutableArray<NSString *> *texts = [NSMutableArray array];
+ for (id raw in arrayIn(document[@"Lines"]) ?: @[]) {
+ id text = dictionaryIn(raw)[@"Text"];
+ if (![text isKindOfClass:NSString.class]) continue;
+ [starts addObject:@0];
+ [texts addObject:[text length] ? text : @"♪"];
+ }
+ result.starts = starts;
+ result.texts = texts;
+ }
+ if (!result.texts.count) {
+ SGLog(@"spicy: %@ came back as %@ with nothing the page could show", query.trackID, type ?: @"no shape at all");
+ done(nil);
+ return;
+ }
+ SGLog(@"spicy: %@ has %lu %@ lines", query.trackID, (unsigned long)result.texts.count,
+ result.wordTimed ? @"word timed" : result.synced ? @"line timed" : @"untimed");
+ done(result);
+ });
+};