Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: an Apple Intelligence translation keeps its finished batches

My-Name-Is-Jeff59fea3d1c504
diff --git a/docs/tweaks.md b/docs/tweaks.md
index e5e59bdf..885032e3 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -134,7 +134,7 @@ Shared:
Intelligence's model (iOS 26 where it is on and speaks the language; permissive guardrails for changing
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 Gemini on the user's own key (Keychain),
- whose prompt and menu item say the lyrics go to Google. Apple Intelligence's lines show a batch at a time as they come. Every finished translation is kept by SavedTranslations.m in Caches/Vitrine/Translations, one JSON file a song and language mapping each line's text to its translation, the newest 200, applied when the song's lines are next shown; the Lyrics page's Saved translations row counts them and deletes them. Gemini's reply read and checked on the Mac
+ whose prompt and menu item say the lyrics go to Google. 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 mapping each line's text to its translation, the newest 200, applied when the song's lines are next shown; the Lyrics page's Saved translations row counts them and deletes them. 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
full-screen artwork (LyricsArtwork.x): a new artwork ID per line, its 3:4 H.264 clip (the line and the
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 26e94712..0db2e098 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1707,13 +1707,19 @@ typedef void (^SGRTranslated)(NSArray<NSString *> *translations, NSString *error
// `progress` may be called with the lines so far before `done`.
typedef void (^SGRTranslator)(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *language, SGRTranslated progress, SGRTranslated done);
-// The lines' text for a translator, "" for a line with no words.
+// The lines' text for a translator, "" for a line with no words or a translation already.
static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
NSMutableArray<NSString *> *texts = [NSMutableArray arrayWithCapacity:lines.count];
- for (SGKaraokeLine *line in lines) [texts addObject:hasWords(line) ? SGKaraokeLineText(line) ?: @"" : @""];
+ for (SGKaraokeLine *line in lines) [texts addObject:hasWords(line) && !line.translation.length ? SGKaraokeLineText(line) ?: @"" : @""];
return texts;
}
+// Whether a line with words is still without a translation.
+static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {
+ for (SGKaraokeLine *line in lines) if (!line.translation.length && hasWords(line)) return YES;
+ return NO;
+}
+
// When each line is sung and where the breaks are, worked out once per song for the frames to read.
- (void)timeLines {
free(_spans);
@@ -1905,8 +1911,10 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
_translating = lines;
[self offerExtras];
NSString *language = SGLyricsGeminiLanguage();
+ // Kept a batch at a time too, so a translation cut off by Spotify closing keeps what was done.
translator(lines, track, language, ^(NSArray<NSString *> *soFar, NSString *error) {
- if (lines == self->_lines) [self takeTranslations:soFar into:lines];
+ [self takeTranslations:soFar into:lines];
+ SGLyricsSaveTranslation(track, language, lines);
}, ^(NSArray<NSString *> *translations, NSString *error) {
if (self->_translating == lines) self->_translating = nil;
if (!translations) {
@@ -1921,7 +1929,8 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
}
[self takeTranslations:translations into:lines];
SGLyricsSaveTranslation(track, language, lines);
- if (lines == self->_lines) self->_untranslated = NO;
+ // A batch Apple Intelligence turned down stays untranslated, and asking again sends only its lines.
+ if (lines == self->_lines) self->_untranslated = anyUntranslated(lines);
[self offerExtras];
});
}
diff --git a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
index e9ba5c1e..70c642fd 100644
--- a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
+++ b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
@@ -101,24 +101,31 @@ public final class SGOnDeviceTranslation: NSObject {
done: @escaping ([String]?, String?) -> Void) {
guard #available(iOS 26.0, *) else { return finish(done, nil, "Apple Intelligence needs iOS 26.") }
let language = name(Locale.Language(identifier: languageTag))
+ // Only the lines given text ("" for those translated already or with no words), 12 to a batch. A batch
+ // the model refuses or fails is left out and the rest go on, so an explicit verse costs its own lines only.
+ let wanted = lines.indices.filter { !lines[$0].isEmpty }
Task {
- var out: [String] = []
+ var out = [String](repeating: "", count: lines.count)
+ var failure: Error?
+ var translated = 0
let started = Date()
- do {
- for start in stride(from: 0, to: lines.count, by: chunkLines) {
- let chunk = Array(lines[start..<min(start + chunkLines, lines.count)])
- let chunkStarted = Date()
- out += try await translateChunk(chunk, into: language)
- log.notice("intelligence: lines \(start + 1)-\(start + chunk.count) of \(lines.count) in \(Date().timeIntervalSince(chunkStarted), format: .fixed(precision: 1)) s")
- let soFar = out + [String](repeating: "", count: lines.count - out.count)
- if out.count < lines.count { DispatchQueue.main.async { progress(soFar) } }
+ for first in stride(from: 0, to: wanted.count, by: chunkLines) {
+ let indices = Array(wanted[first..<min(first + chunkLines, wanted.count)])
+ let chunkStarted = Date()
+ do {
+ let answer = try await translateChunk(indices.map { lines[$0] }, into: language)
+ for (index, translation) in zip(indices, answer) { out[index] = translation }
+ translated += indices.count
+ log.notice("intelligence: \(first + indices.count) of \(wanted.count) lines in \(Date().timeIntervalSince(chunkStarted), format: .fixed(precision: 1)) s")
+ } catch {
+ failure = error
+ log.error("intelligence: lines \(first + 1)-\(first + indices.count) of \(wanted.count) failed: \(String(describing: error), privacy: .public)")
}
- log.notice("intelligence: \(lines.count) lines into \(languageTag, privacy: .public) in \(Date().timeIntervalSince(started), format: .fixed(precision: 1)) s")
- finish(done, out, nil)
- } catch {
- log.error("intelligence: failed after \(out.count) of \(lines.count) lines, \(Date().timeIntervalSince(started), format: .fixed(precision: 1)) s: \(String(describing: error), privacy: .public)")
- finish(done, nil, problem(error))
+ if first + chunkLines < wanted.count { let soFar = out; DispatchQueue.main.async { progress(soFar) } }
}
+ log.notice("intelligence: \(translated) of \(wanted.count) lines into \(languageTag, privacy: .public) in \(Date().timeIntervalSince(started), format: .fixed(precision: 1)) s")
+ if translated == 0, let failure { return finish(done, nil, problem(failure)) }
+ finish(done, out, nil)
}
}
diff --git a/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m b/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
index 7af2487e..aea72660 100644
--- a/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
+++ b/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
@@ -51,7 +51,11 @@ void SGLyricsSaveTranslation(NSString *track, NSString *language, NSArray<SGKara
}
if (!saved.count) return;
NSData *data = [NSJSONSerialization dataWithJSONObject:saved options:0 error:nil];
- dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
+ // One at a time, in the order asked: a song is saved after every batch, and an older write must not land last.
+ static dispatch_queue_t writes;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ writes = dispatch_queue_create("vitrine.translations", DISPATCH_QUEUE_SERIAL); });
+ dispatch_async(writes, ^{
[NSFileManager.defaultManager createDirectoryAtURL:folder() withIntermediateDirectories:YES attributes:nil error:nil];
[data writeToURL:fileFor(track, language) atomically:YES];
NSArray<NSURL *> *all = files();