Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: Apple Intelligence translations finish, and Translate on iPhone tries twice
My-Name-Is-Jeff2685a35c062b
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 253c8431..2c96990b 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -132,7 +132,7 @@ Shared:ways, each its own item: on the iPhone by Apple's Translate (iOS 26, the song's language told byNaturalLanguage, both languages downloaded in the Translate app or the item says where to) and by AppleIntelligence's model (iOS 26 where it is on and speaks the language; permissive guardrails for changing- the user's own text, 40 lines a session, exactly one string a line by a generation schema), both in+ 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 inOnDeviceTranslation.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. Gemini's reply read and checked on the Macagainst harness/lyrics-translation/diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.mindex dce0ccf8..7840599f 100644--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m@@ -38,8 +38,9 @@ static const CGFloat kUnderlineDrop = 1, kUnderlineWidth = 2, kUnderlineAlpha =// opens the pages it links to.static const CGFloat kCreditSize = 12, kCreditBottom = 10, kCreditSlop = 8;// The button for the pronunciation and the translation, in the bottom leading corner as Apple Music-// has it, and the gap between it and the credit beside it.-static const CGFloat kExtrasSide = 44, kExtrasBottom = 12, kExtrasGlyph = 17, kExtrasCreditGap = 12;+// has it, and the gap between it and the credit beside it. The translate glyph is half again as wide as the+// mic's across from it, so it is drawn smaller to weigh the same: 14 pt is about the mic's 17 pt in area.+static const CGFloat kExtrasSide = 44, kExtrasBottom = 12, kExtrasGlyph = 14, kExtrasCreditGap = 12;static char kExtrasGlassKey;static const NSTimeInterval kRestyleFade = 0.3; // the lines crossfading to a new stylestatic const NSTimeInterval kBrowseHold = 3; // after scrolling by hand, how long until it follows the song againdiff --git a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swiftindex 853e0400..b5b7e5f3 100644--- a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift+++ b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift@@ -5,11 +5,15 @@ import Foundationimport FoundationModelsimport NaturalLanguageimport Translation+import os@objc(SGOnDeviceTranslation)public final class SGOnDeviceTranslation: NSObject {- // The model asked fresh for each run of this many lines, so a long song stays inside its context.- private static let chunkLines = 40+ private static let log = Logger(subsystem: "spotifyglass", category: "translation")+ // The model asked fresh for each run of this many lines, so a long song stays inside its context, with room+ // for this many tokens of answer a line: a model that runs on stops there instead of filling the context.+ private static let chunkLines = 12+ private static let tokensPerLine = 60private static func finish(_ done: @escaping ([String]?, String?) -> Void, _ lines: [String]?, _ error: String?) {DispatchQueue.main.async { done(lines, error) }@@ -59,15 +63,22 @@ public final class SGOnDeviceTranslation: NSObject {line.trimmingCharacters(in: .whitespaces).isEmpty || line == "♪" ? nil: TranslationSession.Request(sourceText: line, clientIdentifier: String(index))}- do {- let session = TranslationSession(installedSource: source, target: target)- var out = [String](repeating: "", count: lines.count)- for response in try await session.translations(from: requests) {- if let id = response.clientIdentifier, let index = Int(id) { out[index] = response.targetText }+ let started = Date()+ // A first ask can fail while Translate loads its languages; the second, a moment later, finds them in.+ for attempt in 1...2 {+ do {+ let session = TranslationSession(installedSource: source, target: target)+ var out = [String](repeating: "", count: lines.count)+ for response in try await session.translations(from: requests) {+ if let id = response.clientIdentifier, let index = Int(id) { out[index] = response.targetText }+ }+ log.notice("translate: \(source.minimalIdentifier, privacy: .public) to \(target.minimalIdentifier, privacy: .public), \(requests.count) lines in \(Date().timeIntervalSince(started), format: .fixed(precision: 1)) s")+ return finish(done, out, nil)+ } catch {+ log.error("translate: \(source.minimalIdentifier, privacy: .public) to \(target.minimalIdentifier, privacy: .public), try \(attempt) failed: \(String(describing: error), privacy: .public)")+ if attempt == 2 { return finish(done, nil, "Apple's Translate could not translate the song (\(error.localizedDescription)).") }+ try? await Task.sleep(for: .seconds(1))}- finish(done, out, nil)- } catch {- finish(done, nil, "Apple's Translate could not translate the song (\(error.localizedDescription)).")}}}@@ -91,13 +102,18 @@ public final class SGOnDeviceTranslation: NSObject {let language = name(Locale.Language(identifier: languageTag))Task {var out: [String] = []+ 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")}+ 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))}}@@ -139,7 +155,8 @@ public final class SGOnDeviceTranslation: NSObject {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 answer = try await session.respond(to: prompt, schema: schema).content.value([String].self)+ 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] : "" }}}