mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS: dictation follows the keyboard's language on Automatic
TestFlight build 56 (iPhone 17, iOS 27.3), English and Spanish chosen in
Settings › Keyboard › Languages: "dictation for Spanish is incomprehensible".
Reproduced in the iOS 27.2 simulator: with the keyboard switched to Español
and Dictation language on Automatic, the Hex key menu still said English, so
Spanish speech went to an English model. With Switch dictation too on, each
switch wrote the keyboard's language as a hand-picked choice, which stayed
after Spanish was unticked (English keys, es-ES dictation: stuck in Spanish).
- LanguageSettings.keyboardDictationLanguage: with two or more keyboard
languages and Automatic, dictation is in the language the space bar shows
(KeyboardLanguage.dictationLanguage, the iPhone's region where Apple Speech
has it). Not where the iPhone's own language has no layout (Hebrew), unless
Switch dictation too is on.
- AppleSpeechLanguages.resolve uses it for in-keyboard dictation and Hex; the
keyboard's Hex key menu names it; DictationController prepares the new
language between dictations when the keyboard switches.
- Replaced: Switch dictation too no longer writes the keyboard's language as
an explicit dictation language. It puts a hand-picked language back on
Automatic at each switch, which then follows live, so nothing is left
behind when the list or the switch changes.
Verified: KeyboardLayoutTests.testAutomaticDictationFollowsTheKeyboardsLanguage,
DictationControllerTests.testAutomaticDictationFollowsTheKeyboardsLanguage, the
updated Polish/Ukrainian/French switching tests; live on an iPhone 17 iOS 27.2
simulator (Release, Full Access): Español on the space bar now shows Language
Español in the Hex key menu, English shows English, and Switch dictation too
cleared a picked es-MX on the next switch.
* iOS: English and Spanish together type on the first one's keys
TestFlight build 56: with English and Spanish chosen, "the keyboard is always
stuck in Spanish, with the extra ñ key stuck on the right of the L key".
Black box on Apple's keyboard (iOS 27.2 simulator): adding Spanish offers "Add
to English (US) Keyboard", checked by default, which makes one bilingual
"English & Spanish" keyboard. With English first it is QWERTY with no ñ key, ñ
on a held n (N Ñ Ń Ņ Ň) and English's long presses; English added to the
Spanish keyboard keeps the ñ key and Spanish's long presses (ñ, ª, º, l·l).
Hex instead showed Spanish's own layout whenever the space bar was on
Español, so a bilingual English typist saw the ñ key and Spanish holds.
- KeyboardLanguageList.layout: with English and Spanish both chosen, both type
on the letters and long presses of whichever comes first; any other
language (and Spanish alone, or beside French) keeps its own.
- FullKeyboardEngine.layoutLanguage carries it to the keys and the accent
menu; language (autocorrect, the Spatial and swipe models, the space-bar
label, dictation) still switches on the space bar. The typing models already
read ñ typed from n where a layout has no ñ key.
Unchanged: the language last switched to is global, as Apple's is (a switch in
Messages held in Safari and back), and comes back after the keyboard process
restarts. A field gives Hex nothing to pick from: documentInputMode is nil in
Messages and Hex's own en-US in a test field that asks for English.
Verified: KeyboardLayoutTests.testEnglishAndSpanishTypeOnTheFirstOnesKeys,
FullKeyboardEngineTests.testEnglishAndSpanishShareTheFirstLanguagesKeys; live
on an iPhone 17 iOS 27.2 simulator (Release, Full Access): English + Español
shows QWERTY in both with ñ on a held n, Español + English the ñ key in both,
in Messages, Safari and an English-only field, before and after a keyboard
restart, with the iPhone's languages English or English + Spanish.
* iOS: Spanish dictation never goes to English-only Parakeet
Part of the TestFlight build 56 report ("dictation for Spanish is
incomprehensible"). Checked every path a Spanish dictation can take:
in-keyboard dictation is Apple Speech only, and Hex picks Parakeet V2 only for
English that Apple Speech would run on DictationTranscriber. One gap: when
Apple Speech gives no language at all (Speech not answering), Hex fell back to
Parakeet whatever the language, so Spanish would come out as English words.
- DictationController.appleSpeechLanguage decides Apple Speech or Parakeet for
both the launch check and defaultTranscriber: Apple Speech's pick unless it
is English Parakeet takes (AppleSpeechTranscriber.shouldUse, unchanged);
with no pick, English still goes to Parakeet and any other language (the one
set, else the keyboard's Automatic follows, else the iPhone's) stays on
Apple Speech, whose install then says it can't dictate it.
Verified: DictationControllerTests.testSpanishNeverGoesToEnglishOnlyParakeet
(es-ES and es-MX on either Apple module, es-ES and es-US with no pick go to
Apple Speech; en-US on DictationTranscriber and English with no pick still go
to Parakeet).
224 lines
9.6 KiB
Swift
224 lines
9.6 KiB
Swift
import AVFAudio
|
||
import Foundation
|
||
import Speech
|
||
|
||
/// The languages Apple Speech transcribes on this iPhone, shared by the app and in-keyboard
|
||
/// dictation so both pick the same language and the same downloaded model.
|
||
@available(iOS 26.0, *)
|
||
nonisolated enum AppleSpeechLanguages {
|
||
/// The language dictation uses: Settings › Dictation language, else the keyboard's
|
||
/// language Automatic follows (`LanguageSettings.keyboardDictationLanguage`), else
|
||
/// Automatic. Records the result in the App Group for the keyboard layout and translation.
|
||
static func resolve(settings: LanguageSettings = LanguageSettings()) async -> SpeechLanguage? {
|
||
let explicit = settings.dictationLanguage ?? settings.keyboardDictationLanguage()
|
||
let picked = await pick(explicit: explicit, settings: settings)
|
||
if let picked, settings.resolvedDictationLanguage != picked.identifier {
|
||
settings.resolvedDictationLanguage = picked.identifier
|
||
}
|
||
if explicit == nil, let picked, settings.automaticDictationLanguage != picked.identifier {
|
||
settings.automaticDictationLanguage = picked.identifier
|
||
}
|
||
return picked
|
||
}
|
||
|
||
/// What Automatic picks on this iPhone, whatever the setting says. Records it for the
|
||
/// keyboard's language switch.
|
||
static func automatic(settings: LanguageSettings = LanguageSettings()) async -> SpeechLanguage? {
|
||
let picked = await pick(explicit: nil, settings: settings)
|
||
if let picked, settings.automaticDictationLanguage != picked.identifier {
|
||
settings.automaticDictationLanguage = picked.identifier
|
||
}
|
||
return picked
|
||
}
|
||
|
||
private static func pick(explicit: String?, settings: LanguageSettings) async -> SpeechLanguage? {
|
||
await SpeechLanguagePicker.pick(
|
||
explicit: explicit,
|
||
candidates: SpeechLanguagePicker.automaticCandidates(
|
||
preferred: Locale.preferredLanguages,
|
||
current: Locale.current.identifier
|
||
),
|
||
speech: { locale in
|
||
guard SpeechTranscriber.isAvailable,
|
||
let found = await SpeechTranscriber.supportedLocale(equivalentTo: locale) else { return nil }
|
||
// Its model wouldn't install lately: `DictationTranscriber`'s, if it has one.
|
||
guard !settings.speechModuleFailed(found.identifier(.bcp47)) else { return nil }
|
||
return found
|
||
},
|
||
dictation: { locale in
|
||
await DictationTranscriber.supportedLocale(equivalentTo: locale)
|
||
},
|
||
skip: { settings.isUnavailable($0.identifier(.bcp47)) }
|
||
)
|
||
}
|
||
|
||
/// Whether neither module lists a single language here: Speech not answering (in a
|
||
/// keyboard, say), which isn't Speech lacking the iPhone's language.
|
||
static func listsNone() async -> Bool {
|
||
if SpeechTranscriber.isAvailable, !(await SpeechTranscriber.supportedLocales).isEmpty {
|
||
return false
|
||
}
|
||
return (await DictationTranscriber.supportedLocales).isEmpty
|
||
}
|
||
|
||
/// Every language either module transcribes, sorted by name (Hindi as Hinglish while that
|
||
/// experiment is on). Records the list for the keyboard's language switch.
|
||
static func options(
|
||
display: Locale = .current,
|
||
settings: LanguageSettings = LanguageSettings(),
|
||
hinglish: HinglishSettings = HinglishSettings()
|
||
) async -> [SpeechLanguageOption] {
|
||
var identifiers: [String] = []
|
||
var seen = Set<String>()
|
||
let speech = SpeechTranscriber.isAvailable ? await SpeechTranscriber.supportedLocales : []
|
||
for locale in speech + (await DictationTranscriber.supportedLocales) {
|
||
let identifier = locale.identifier(.bcp47)
|
||
if seen.insert(identifier).inserted {
|
||
identifiers.append(identifier)
|
||
}
|
||
}
|
||
if !identifiers.isEmpty, settings.supportedDictationLanguages != identifiers {
|
||
settings.supportedDictationLanguages = identifiers
|
||
}
|
||
return identifiers
|
||
.map {
|
||
SpeechLanguageOption(
|
||
id: $0,
|
||
name: hinglish.name(for: $0) ?? SpeechLanguagePicker.displayName(for: $0, in: display)
|
||
)
|
||
}
|
||
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
|
||
}
|
||
}
|
||
|
||
/// Apple Speech's model downloads, for the app and in-keyboard dictation alike.
|
||
@available(iOS 26.0, *)
|
||
nonisolated enum AppleSpeechAssets {
|
||
/// Installs `language`'s model (`SpeechModelInstall`: `SpeechTranscriber`, then
|
||
/// `DictationTranscriber`) and returns the language it installed. When neither installs,
|
||
/// the error names what Automatic picks instead, to offer or to say it switched.
|
||
static func install(
|
||
_ language: SpeechLanguage,
|
||
settings: LanguageSettings = LanguageSettings(),
|
||
log: @escaping @Sendable (String) -> Void = { _ in },
|
||
progress: @escaping @Sendable (Double) -> Void = { _ in }
|
||
) async throws -> SpeechLanguage {
|
||
do {
|
||
let installed = try await SpeechModelInstall.run(
|
||
language,
|
||
steps: steps,
|
||
settings: settings,
|
||
stallLimit: .seconds(60),
|
||
log: log,
|
||
progress: progress
|
||
)
|
||
settings.clearUnavailable(language.identifier)
|
||
return installed
|
||
} catch var unavailable as SpeechModelUnavailable {
|
||
unavailable.automatic = settings.dictationLanguage == nil && settings.keyboardDictationLanguage() == nil
|
||
if unavailable.reason == .notOnThisIPhone,
|
||
let next = await AppleSpeechLanguages.automatic(settings: settings),
|
||
next.languageCode != language.languageCode {
|
||
unavailable.fallback = next.identifier
|
||
}
|
||
throw unavailable
|
||
}
|
||
}
|
||
|
||
static let steps = SpeechModelInstall.Steps(
|
||
dictationEquivalent: { locale in
|
||
await DictationTranscriber.supportedLocale(equivalentTo: locale)
|
||
},
|
||
status: { language in
|
||
switch await AssetInventory.status(forModules: [AppleSpeechModule(language).module]) {
|
||
case .installed: .installed
|
||
case .unsupported: .unsupported
|
||
default: .downloadable
|
||
}
|
||
},
|
||
download: { language, progress in
|
||
let module = AppleSpeechModule(language)
|
||
await reserve(language.locale)
|
||
guard let request = try await AssetInventory.assetInstallationRequest(supporting: [module.module]) else {
|
||
return
|
||
}
|
||
let watched = request.progress
|
||
let monitor = Task {
|
||
while !Task.isCancelled {
|
||
progress(watched.fractionCompleted)
|
||
try? await Task.sleep(for: .milliseconds(200))
|
||
}
|
||
}
|
||
defer { monitor.cancel() }
|
||
try await withTaskCancellationHandler {
|
||
try await request.downloadAndInstall()
|
||
} onCancel: {
|
||
watched.cancel()
|
||
}
|
||
},
|
||
isOnline: { await SpeechModelInstall.isOnline() }
|
||
)
|
||
|
||
/// Reserves `locale`'s assets, first releasing others when Hex already holds
|
||
/// `AssetInventory.maximumReservedLocales` (each language picked in the keyboard reserves
|
||
/// one, and the limit shrinks with free storage). Best effort: without a reservation the
|
||
/// download still runs, or fails with its own reason.
|
||
static func reserve(_ locale: Locale) async {
|
||
let key = locale.identifier(.bcp47)
|
||
var reserved = await AssetInventory.reservedLocales
|
||
guard !reserved.contains(where: { $0.identifier(.bcp47) == key }) else { return }
|
||
let limit = AssetInventory.maximumReservedLocales
|
||
while reserved.count >= max(1, limit), let oldest = reserved.first {
|
||
reserved.removeFirst()
|
||
_ = await AssetInventory.release(reservedLocale: oldest)
|
||
}
|
||
_ = try? await AssetInventory.reserve(locale: locale)
|
||
}
|
||
}
|
||
|
||
/// One dictation's Apple Speech module: `SpeechTranscriber` or `DictationTranscriber`.
|
||
@available(iOS 26.0, *)
|
||
nonisolated enum AppleSpeechModule {
|
||
case speech(SpeechTranscriber)
|
||
case dictation(DictationTranscriber)
|
||
|
||
init(_ language: SpeechLanguage) {
|
||
switch language.module {
|
||
case .speech:
|
||
self = .speech(SpeechTranscriber(locale: language.locale, preset: .transcription))
|
||
case .dictation:
|
||
// Punctuated, without the short-form hint: dictations run up to a minute.
|
||
self = .dictation(DictationTranscriber(locale: language.locale, preset: .longDictation))
|
||
}
|
||
}
|
||
|
||
var module: any SpeechModule {
|
||
switch self {
|
||
case .speech(let transcriber): transcriber
|
||
case .dictation(let transcriber): transcriber
|
||
}
|
||
}
|
||
|
||
var name: String {
|
||
switch self {
|
||
case .speech: "SpeechTranscriber"
|
||
case .dictation: "DictationTranscriber"
|
||
}
|
||
}
|
||
|
||
/// The final results joined, once the analyzer has finished the file.
|
||
func finalText() async throws -> String {
|
||
var transcript = AttributedString()
|
||
switch self {
|
||
case .speech(let transcriber):
|
||
for try await result in transcriber.results where result.isFinal {
|
||
transcript += result.text
|
||
}
|
||
case .dictation(let transcriber):
|
||
for try await result in transcriber.results where result.isFinal {
|
||
transcript += result.text
|
||
}
|
||
}
|
||
return String(transcript.characters).trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
}
|