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: lock screen lyrics can show the line's translation (#130)

My-Name-Is-Jeffd5d29a8edfd9
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 4d4575b0..1225cb50 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -165,7 +165,9 @@ Shared:
the user's own text, 12 lines a session, greedy and capped at 60 tokens a line, exactly one string a line by a generation schema; Translate retried once, as its first ask can fail while it loads), both in
OnDeviceTranslation.swift, the frameworks weak linked; and by an AI translator on the user's own key, picked on the Lyrics page (SGKeyAITranslator): Gemini, an OpenAI-compatible server (chat completions; OpenAI's unless a server URL is given, such as OpenRouter's or an Ollama on the network, which Spotify's ATS allows for local addresses) or Anthropic (the Messages API). Each provider is one entry of LyricsTranslation.m's table: its Keychain slot (one key each, never in the settings, so an export never has one), its model presets (the cheapest first; OpenAI-compatible has none and needs a model), its request and its reply's reader. The answer's shape is forced where the API can (Gemini a bare array, the others {"lines": [...]} by a strict schema), the reader takes either, and a 400 that names an optional field (Gemini's thinking level, OpenAI's schema or reasoning effort, Anthropic's output_config) is asked once more without them. The prompt is shared (Vitrine's with {language} and {count}, editable in a sheet, SGKeyAIPrompt) and the provider adds its sentence on the answer's shape; the model (spotifyglass.lyrics.<id>.model) and the server are read at each ask and checked as they are read, and the log names the provider, the model, the host and the prompt's SHA-256 prefix, never the key. Temperature is not offered: Anthropic's API turns it down and OpenAI's reasoning models take only the default. The menu item says where the lyrics go. Translate on iPhone and Apple Intelligence each have a switch that leaves them out of the menu (SGKeyLyricsTranslateOnDevice, SGKeyLyricsAppleIntelligence, both on). A translation that reads the same as its line (SGLyricsBareText) is not drawn under it, and the line still counts as translated. The translate items show for every song with Translate any song on (SGKeyLyricsTranslateEverySong, off), else only for a song NaturalLanguage finds in another language than the Lyrics page's (or the iPhone's): one distinct line in five it is at least 80% sure of, or two in another script (a verse in Hangul among English), not the whole song's guess (short lines sway that: Havana reads as Dutch), so songs half in English count and a repeated chorus weighs as one line; Translate on iPhone translates from the language most of those lines are in and leaves lines already in the target as they are. Apple Intelligence's lines show and are saved a batch at a time, only lines without a translation are sent, and a batch it turns down is skipped (asking again sends only those lines). Every finished translation is kept by SavedTranslations.m in Caches/Vitrine/Translations, one JSON file a song and language with each line's text to its translation under "lines" and who made it and when ("by", "model", "made"; a file from before has the lines alone and reads as Unknown), the newest 200, synced when the song's lines are shown: each line knows whether its translation was made here (SGKaraokeLine.translationMade), and a made one with nothing saved is taken off, so the Lyrics page's Saved translations row deletes them at once, posting SGLyricsTranslationsDidChangeNotification as Translate any song does for the open lyrics to sync and redraw. The translate menu names who made a saved translation and when, and for one an AI made (or no one known) offers Delete This Translation, which removes the song's file and the launch's answer for it, so asking again sends the song. Gemini's reply read and checked on the Mac
against harness/lyrics-translation/
- LockScreenLyrics/ the line being sung in the system's now playing, and on iOS 26 the lyrics as the lock screen's
+ LockScreenLyrics/ the line being sung in the system's now playing (Lock screen translation, SGKeyLockScreenTranslation,
+ off: a translation the line has in its place, its words timed over the line's and cut in pieces
+ by its own length), and on iOS 26 the lyrics as the lock screen's
full-screen artwork (LyricsArtwork.x): a new artwork ID per line, its 3:4 H.264 clip (the line and the
next one dimmed over the blurred cover, SGLyricsClip.m) written only when the lock screen asks for it,
Still (one frame) or Animated (15 fps). The backdrop is blurred small and drawn large, on the CPU as the
@@ -822,6 +824,7 @@ model with the full one, is on the iPhone), Toggle from lyrics (the redesign's m
Ignore heat warnings, and under Advanced Runs on (Automatic: the Neural Engine
beside the CPU; CPU only) and Prepare after updates, all applying straight away. Lock screen, on the main
page under either look, opens the lock screen widget's page, titled Lock screen: Widget (the line in place of the artist,
+its translation in the line's place,
and Spotify's like and dislike buttons' flag, locked off while the star is on) then Artwork (Off, Moving artwork, Lyrics,
Every song or Moving artwork with lyrics, the lyrics' style, Still or Animated, Switch with the star, the sources and Low Data Mode; below iOS 26
one row saying why not). Spotify's podcast, audiobook and artwork flags stay in All flags. Player: Gestures and Blocked artists (with the count on the row), which work with either look;
diff --git a/harness/now-playing/main.m b/harness/now-playing/main.m
index 662f7675..0a1a803d 100644
--- a/harness/now-playing/main.m
+++ b/harness/now-playing/main.m
@@ -33,6 +33,7 @@ __attribute__((constructor)) static void underneath(void) {
#pragma mark - stubs
BOOL SGFlag(NSString *key, BOOL fallback) { return getenv("LSL") != NULL; }
+void SGPrefsChanged(void) {}
@implementation SPTPlayerTrack {
@public
@@ -122,6 +123,25 @@ static void progressBar(void) {
SGNowPlayingSetExtras(@"haptics", nil, nil);
}
+// Lock screen translation (on with LSL, as every flag is): a translation longer than the row is cut by its own
+// length, not the line's, and its pieces follow the line's clock.
+static void translation(void) {
+ MPNowPlayingInfoCenter *center = MPNowPlayingInfoCenter.defaultCenter;
+ NSArray<SGKaraokeLine *> *was = sg_lines;
+ SGKaraokeLine *line = lineOf(@"Hola", 0, 4000);
+ line.translation = @"This translation runs well past what the lock screen's artist row holds";
+ sg_lines = @[line];
+ center.nowPlayingInfo = spotifyInfo(@"Song", 0, 0);
+ NSString *first = sg_system[MPMediaItemPropertyArtist];
+ check(first.length > 0 && first.length <= 30 && [line.translation hasPrefix:first],
+ [NSString stringWithFormat:@"the translation's first piece shows: \"%@\"", first]);
+ center.nowPlayingInfo = spotifyInfo(@"Song", 3.9, 0);
+ NSString *last = sg_system[MPMediaItemPropertyArtist];
+ check(last.length > 0 && last.length <= 30 && [line.translation hasSuffix:last],
+ [NSString stringWithFormat:@"its last piece shows by the line's end: \"%@\"", last]);
+ sg_lines = was;
+}
+
// Two tracks with one title: what the first had must not stay on the second's info once it is cleared.
static void sameTitle(void) {
MPNowPlayingInfoCenter *center = MPNowPlayingInfoCenter.defaultCenter;
@@ -171,6 +191,7 @@ int main(void) {
BOOL lyrics = getenv("LSL") != NULL;
printf("lock screen lyrics %s\n", lyrics ? "on" : "off");
if (lyrics) progressBar();
+ if (lyrics) translation();
sameTitle();
printf(failures ? "%d failed\n" : "all passed\n", failures);
return failures ? 1 : 0;
diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.m
index 176c749a..72d80544 100644
--- a/tweak/Sources/App/Pages.m
+++ b/tweak/Sources/App/Pages.m
@@ -106,7 +106,7 @@ UIViewController *SGNavbarPage(void) {
// Pronunciation, romanized lines, translation, word sweeping and line meanings exist only in the redesign's lyrics view.
static UIViewController *lyricsPage(void) {
BOOL redesigned = SGRedesignedUIStored();
- NSMutableArray<SGModRow *> *more = [NSMutableArray arrayWithObject:SGLockScreenLyricsRow()];
+ NSMutableArray<SGModRow *> *more = [NSMutableArray arrayWithObjects:SGLockScreenLyricsRow(), SGLockScreenTranslationRow(), nil];
if (!redesigned) [more insertObject:SGGlassLyricsRow() atIndex:0];
SGModRow *sing = SGPageRow(@"Karaoke", ^UIViewController *{ return SGSingSettingsPage(); });
sing.value = ^NSString *{ return SGSingSummary(); };
diff --git a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.h b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.h
index fa1e17e4..010de77c 100644
--- a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.h
+++ b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.h
@@ -4,6 +4,8 @@
#import <Foundation/Foundation.h>
#define SGKeyLockScreenLyrics @"spotifyglass.lockScreenLyrics"
+// The line's translation in its place, for lines that already have one (as the Live Activity's Translations).
+#define SGKeyLockScreenTranslation @"spotifyglass.lockScreenLyrics.translation"
// Moving artwork with lyrics: the moving artwork clip found for the track `uri`, a local file, which
// LyricsArtwork.x draws the lines over while that track plays. Main queue.
diff --git a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
index c327d8ed..60248327 100644
--- a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
+++ b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
@@ -6,6 +6,7 @@
// are not in yet it looks four times a second. The lock screen keeps the line it was left on until the
// sound starts again.
#import <MediaPlayer/MediaPlayer.h>
+#import <objc/runtime.h>
#import "Core/SGCore.h"
#import "LockScreenLyrics.h"
#import "Shared/Lyrics/Lyrics.h"
@@ -24,6 +25,8 @@ static NSString *sg_shownLine;
static BOOL sg_resending;
static dispatch_source_t sg_timer;
static CFAbsoluteTime sg_due;
+// SGKeyLockScreenTranslation, read at launch.
+static BOOL sg_translation;
static NSString *textOf(NSArray<SGKaraokeWord *> *words) {
SGKaraokeLine *line = [SGKaraokeLine new];
@@ -55,6 +58,22 @@ static NSArray<NSArray<SGKaraokeWord *> *> *piecesOf(SGKaraokeLine *line) {
return pieces;
}
+static char kTranslatedKey;
+
+// A line's translation as a line of its own, its words' times estimated over the line's, so piecesOf splits it
+// by its own length and its pieces turn over at the line's wakes. Made once for each translation the line has.
+static SGKaraokeLine *translatedLine(SGKaraokeLine *line) {
+ NSString *translation = line.translation;
+ if (!translation.length) return nil;
+ NSArray *made = objc_getAssociatedObject(line, &kTranslatedKey);
+ if (made.firstObject != translation) {
+ SGKaraokeLine *translated = SGKaraokeEstimatedLines(@[@(line.start), @(MAX(line.end, line.start))], @[translation, @""]).firstObject;
+ made = @[translation, translated ?: NSNull.null];
+ objc_setAssociatedObject(line, &kTranslatedKey, made, OBJC_ASSOCIATION_RETAIN);
+ }
+ return made.lastObject == NSNull.null ? nil : made.lastObject;
+}
+
// Seconds into the track at `now`, run on from what Spotify last reported. A rate it left out runs at 1, as
// playingBy counts it: read as 0, every line written pinned the lock screen's time to Spotify's last report.
static double elapsedAt(NSDictionary *info, CFAbsoluteTime reportedAt, CFAbsoluteTime now) {
@@ -64,8 +83,9 @@ static double elapsedAt(NSDictionary *info, CFAbsoluteTime reportedAt, CFAbsolut
}
// nil between lines and for a track without synced lyrics, plain text included. Past a line's end by
-// SGKaraokeBreakMs, with the next line at least that far off, the artist comes back. `wait` is the playback
-// time in seconds until it can change: kTick while it cannot be worked out yet, negative for not again.
+// SGKaraokeBreakMs, with the next line at least that far off, the artist comes back. With translations on, a
+// line that has one shows it instead. `wait` is the playback time in seconds until it can change: kTick while it
+// cannot be worked out yet, negative for not again.
static NSString *lineFor(NSDictionary *info, double elapsed, NSTimeInterval *wait) {
*wait = kTick;
SPTPlayerState *state = [(id<SPTPlayer>)SGKaraokePlayer() state];
@@ -89,8 +109,9 @@ static NSString *lineFor(NSDictionary *info, double elapsed, NSTimeInterval *wai
if (index < 0) return nil;
BOOL nextFarOff = index + 1 == (NSInteger)lines.count || lines[index + 1].start - position > SGKaraokeBreakMs;
if (position > lines[index].end + SGKaraokeBreakMs && nextFarOff) return nil;
+ SGKaraokeLine *line = (sg_translation ? translatedLine(lines[index]) : nil) ?: lines[index];
NSString *shown = nil;
- for (NSArray<SGKaraokeWord *> *piece in piecesOf(lines[index])) {
+ for (NSArray<SGKaraokeWord *> *piece in piecesOf(line)) {
if (!shown || piece.firstObject.start <= position) shown = textOf(piece);
}
return shown;
@@ -216,6 +237,7 @@ static void lookNow(void) {
%ctor {
if (!SGFlag(SGKeyLockScreenLyrics, NO)) return;
sg_lock = [NSObject new];
+ sg_translation = SGFlag(SGKeyLockScreenTranslation, NO);
%init;
// Lines kept for a track, or replaced by better timed ones, can change the line at once.
[NSNotificationCenter.defaultCenter addObserverForName:SGKaraokeLinesKeptNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
@@ -234,5 +256,5 @@ static void lookNow(void) {
dispatch_async(dispatch_get_main_queue(), ^{ lookNow(); });
}];
// The timer waits for Spotify to report a playing track; nothing before that has a line to show.
- SGLog(@"lock screen lyrics: on");
+ SGLog(@"lock screen lyrics: on%@", sg_translation ? @", translations" : @"");
}
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index 1f001aa0..59615d0b 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -14,6 +14,7 @@
// community translations are asked in (the redesign's lyrics being where translations show), whether lines timed only by the line are swept word by word, whether lines in other alphabets show again in Latin letters, and how much later than the song every line shows.
SGModSection *SGLyricsSourcesSection(BOOL namingSource);
SGModRow *SGLockScreenLyricsRow(void);
+SGModRow *SGLockScreenTranslationRow(void);
SGModRow *SGLyricsTranslationLanguageRow(void);
SGModRow *SGLyricsWordTimingRow(void);
SGModRow *SGLyricsRomanisedRow(void);
diff --git a/tweak/Sources/Shared/Lyrics/LyricsSettings.m b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
index 26952eec..9b1ae92d 100644
--- a/tweak/Sources/Shared/Lyrics/LyricsSettings.m
+++ b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
@@ -39,6 +39,12 @@ SGModRow *SGLockScreenLyricsRow(void) {
return SGOptionRow(@"Lock screen lyrics", @"Current line in place of the artist", SGKeyLockScreenLyrics);
}
+SGModRow *SGLockScreenTranslationRow(void) {
+ SGModRow *row = SGOptionRow(@"Lock screen translation", @"The line's translation in its place, when the lyrics have one", SGKeyLockScreenTranslation);
+ row.waitsOn = SGKeyLockScreenLyrics;
+ return row;
+}
+
SGModRow *SGLyricsTranslationLanguageRow(void) {
SGModRow *row = SGChoiceRow(@"Translation language", nil, SGKeyLyricsTranslationLanguage, SGLyricsTranslationLanguageNames(), 0);
row.choiceFooter = @"A language brings Musixmatch's community translations to lyrics that have none, "
diff --git a/tweak/Sources/Shared/Player/PlayerSettings.m b/tweak/Sources/Shared/Player/PlayerSettings.m
index 45f23233..b7b35416 100644
--- a/tweak/Sources/Shared/Player/PlayerSettings.m
+++ b/tweak/Sources/Shared/Player/PlayerSettings.m
@@ -13,7 +13,7 @@ UIViewController *SGLockScreenWidgetPage(void) {
SGModRow *likes = SGFlagRow(@"Like and dislike buttons", @"ios-feature-lockscreen.like_dislike_enabled");
likes.subtitle = @"Off and locked while Switch with the star is on: the star takes their place";
return [[SGModPage alloc] initWithTitle:@"Lock screen" intro:SGRestartNote sections:@[
- SGSection(@"Widget", @[SGLockScreenLyricsRow(), likes]),
+ SGSection(@"Widget", @[SGLockScreenLyricsRow(), SGLockScreenTranslationRow(), likes]),
SGSection(@"Artwork", SGLockScreenMotionRows()),
] footer:nil];
}