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: Apple Intelligence translates each batch knowing the lines before it

My-Name-Is-Jeffeef25912738f
diff --git a/harness/lyrics/stubs.m b/harness/lyrics/stubs.m
index 0c02f188..4c636804 100644
--- a/harness/lyrics/stubs.m
+++ b/harness/lyrics/stubs.m
@@ -70,7 +70,7 @@ void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *li
[self answer:lines as:@"iPhone: " done:done];
}
// A batch at a time, as the model works: the first half shows a second before the rest.
-+ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag progress:(void (^)(NSArray<NSString *> *))progress
++ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag song:(NSString *)song progress:(void (^)(NSArray<NSString *> *))progress
done:(void (^)(NSArray<NSString *> *, NSString *))done {
NSMutableArray<NSString *> *half = [NSMutableArray array];
[lines enumerateObjectsUsingBlock:^(NSString *line, NSUInteger i, BOOL *stop) {
@@ -152,3 +152,5 @@ void SGKaraokeSeek(NSInteger ms) {
SGHarnessStartClock(ms, sg_rate, -1, 0);
});
}
+// "“Title” by Artist" the translators are told.
+NSString *SGLyricsSongName(NSString *trackID) { return @"“Sample” by Harness"; }
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 0db2e098..eba89df7 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1890,7 +1890,7 @@ static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {
if (intelligence) {
offer(@"Translate with Apple Intelligence", @"apple.intelligence", @"On this iPhone",
^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated progress, SGRTranslated done) {
- [SGOnDeviceTranslation translateWithAppleIntelligence:textsOf(lines) to:to progress:^(NSArray<NSString *> *soFar) { progress(soFar, nil); } done:done];
+ [SGOnDeviceTranslation translateWithAppleIntelligence:textsOf(lines) to:to song:SGLyricsSongName(track) progress:^(NSArray<NSString *> *soFar) { progress(soFar, nil); } done:done];
});
}
if (gemini) offer(@"Translate with Gemini", @"sparkles", @"Sends the lyrics to Google", ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated progress, SGRTranslated done) {
diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
index 82430539..88f7893c 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
@@ -12,6 +12,9 @@ BOOL SGGeminiKeySet(void);
void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *lines, NSString *languageTag,
void (^done)(NSArray<NSString *> *translations, NSString *error));
+// "“Title” by Artist" for a track the player or the local files know, for a translator's instructions; else nil.
+NSString *SGLyricsSongName(NSString *trackID);
+
// The language a translation is asked in: the Lyrics page's, else the phone's own.
NSString *SGLyricsGeminiLanguage(void);
@@ -30,7 +33,7 @@ SGModRow *SGGeminiKeyRow(void);
+ (BOOL)appleIntelligenceAvailable:(NSString *)languageTag;
+ (void)translate:(NSArray<NSString *> *)lines to:(NSString *)languageTag done:(void (^)(NSArray<NSString *> *lines, NSString *error))done;
// Apple Intelligence works through the song a batch at a time: `progress` gets the lines so far, "" for the rest.
-+ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag
++ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag song:(NSString *)song
progress:(void (^)(NSArray<NSString *> *lines))progress
done:(void (^)(NSArray<NSString *> *lines, NSString *error))done;
@end
diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
index ce4acd4a..ae8bf35c 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
@@ -4,6 +4,8 @@
#import "Settings/SGPageStyle.h"
#import "Shared/Lyrics/Lyrics.h"
#import "Shared/LyricsSources/LyricsSources.h"
+#import "Shared/LocalFiles/LocalFiles.h"
+#import "Headers/SPTPlayer.h"
#import "LyricsTranslation.h"
// gemini-flash-latest follows each Flash release; Google gives two weeks' notice of a breaking change.
@@ -43,6 +45,15 @@ BOOL SGGeminiKeySet(void) {
#pragma mark - asking
+NSString *SGLyricsSongName(NSString *trackID) {
+ NSDictionary *local = trackID ? SGLocalFileInfo(trackID) : nil;
+ SPTPlayerTrack *track = trackID && !local ? SGKaraokeTrackFor(trackID) : nil;
+ NSString *title = local ? local[@"title"] : track.trackTitle, *artist = local ? local[@"artist"] : track.artistName;
+ if (![title isKindOfClass:NSString.class] || !title.length) return nil;
+ return [artist isKindOfClass:NSString.class] && artist.length ? [NSString stringWithFormat:@"\u201C%@\u201D by %@", title, artist]
+ : [NSString stringWithFormat:@"\u201C%@\u201D", title];
+}
+
NSString *SGLyricsGeminiLanguage(void) {
return SGLyricsTranslationLanguage() ?: NSLocale.preferredLanguages.firstObject ?: @"en";
}
diff --git a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
index 70c642fd..1efe5321 100644
--- a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
+++ b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
@@ -97,7 +97,7 @@ public final class SGOnDeviceTranslation: NSObject {
return model.availability == .available && model.supportedLanguages.contains { same($0, target) }
}
- @objc public static func translateWithAppleIntelligence(_ lines: [String], to languageTag: String, progress: @escaping ([String]) -> Void,
+ @objc public static func translateWithAppleIntelligence(_ lines: [String], to languageTag: String, song: String?, progress: @escaping ([String]) -> Void,
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))
@@ -106,6 +106,7 @@ public final class SGOnDeviceTranslation: NSObject {
let wanted = lines.indices.filter { !lines[$0].isEmpty }
Task {
var out = [String](repeating: "", count: lines.count)
+ var before: [(String, String)] = [] // the batch just done, lines and translations, for the next one's sense
var failure: Error?
var translated = 0
let started = Date()
@@ -113,12 +114,15 @@ public final class SGOnDeviceTranslation: NSObject {
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)
+ let chunk = indices.map { lines[$0] }
+ let answer = try await translateChunk(chunk, into: language, song: song, before: before)
for (index, translation) in zip(indices, answer) { out[index] = translation }
+ before = Array(zip(chunk, answer))
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
+ before = []
log.error("intelligence: lines \(first + 1)-\(first + indices.count) of \(wanted.count) failed: \(String(describing: error), privacy: .public)")
}
if first + chunkLines < wanted.count { let soFar = out; DispatchQueue.main.async { progress(soFar) } }
@@ -153,18 +157,24 @@ public final class SGOnDeviceTranslation: NSObject {
return "Apple Intelligence could not translate the song (\(error.localizedDescription))."
}
- // Exactly one string per line, held to the count by the schema rather than read out of free text.
+ // Exactly one string per line, held to the count by the schema rather than read out of free text. The batch
+ // before it comes along, already translated, so a sentence across the two reads as one and names, pronouns
+ // and tone stay as they were; it is context only and gets no answer.
@available(iOS 26.0, *)
- private static func translateChunk(_ lines: [String], into language: String) async throws -> [String] {
+ private static func translateChunk(_ lines: [String], into language: String, song: String?, before: [(String, String)]) async throws -> [String] {
let session = LanguageModelSession(model: model, instructions: """
- You translate song lyrics into \(language). You get a JSON array of lines and answer with an array of \
- the same length: each line's translation at the same place, natural as sung, keeping the meaning of \
- slang and idioms. A line that is empty, a sound or already in \(language) is given back as it is.
+ You translate the lyrics of \(song ?? "a song") into \(language). You get a JSON array of lines and answer \
+ with an array of the same length: each line's translation at the same place, natural as sung, keeping the \
+ meaning of slang and idioms. A line that is empty, a sound or already in \(language) is given back as it is. \
+ Lines from just before may be given with their translations: they are not to be translated again, only \
+ followed, so names, pronouns and tone carry on and a sentence that runs on reads as one.
""")
let line = DynamicGenerationSchema(type: String.self)
let schema = try GenerationSchema(root: DynamicGenerationSchema(arrayOf: line, minimumElements: lines.count, maximumElements: lines.count),
dependencies: [])
- let prompt = String(decoding: try JSONSerialization.data(withJSONObject: lines), as: UTF8.self)
+ let json = { (value: Any) in String(decoding: (try? JSONSerialization.data(withJSONObject: value)) ?? Data(), as: UTF8.self) }
+ let prompt = before.isEmpty ? json(lines)
+ : "Just before, already translated: \(json(before.map { [$0.0, $0.1] }))\n\nTranslate: \(json(lines))"
let options = GenerationOptions(samplingMode: .greedy, maximumResponseTokens: tokensPerLine * lines.count + 32)
let answer = try await session.respond(to: prompt, schema: schema, options: options).content.value([String].self)
return answer.count == lines.count ? answer : lines.indices.map { $0 < answer.count ? answer[$0] : "" }