hex/ios/HEXKeyboard/KeyboardLiveDictation.swift
Ryan Vogel d7b690661e
iOS Settings: Home refreshes again while it shows, Into field falls back to the words over the keys, the speech model card lands on its row, docs match (#2471)
Four regressions from the Settings rework (#2450), nothing renamed or reordered:

1. Home and its setup sheet read the setup state once a second again while
   they show and Hex is active (.task(id:) on the navigation stack, stopping
   on any Settings page or in the background), as well as on foreground and
   the keyboard's Full Access report. #2450 dropped the 1 Hz loop, so a
   change made with Hex still in front never showed: the keyboard's Allow
   Paste flag (Hex's own field) and a keyboard added from Settings beside Hex
   in Split View. Timed on an iPhone 17 Pro simulator (iOS 27.2), Hex in
   front: at 0304eb1c neither showed in 8 s; now 1.2 s and 1.4 s (147: 1.1 s
   and 1.0 s); the setup sheet 1.8 s; the Keyboard page, by design, not.

2. Live words, Into field: where Type as I speak can't type this dictation
   (Translate what I say on, or a field LiveFieldTyping never types in:
   secure, password, one-time code or card number fields, a selection, a
   two-thumb Dictate's range, an app that reports no text), the words now
   show over the keys instead of nowhere, as build 147's two switches did.
   LiveDictationSettings.liveWords maps the two stored keys to what one
   dictation does; nothing stored changes. LiveFieldTyping.types(in:) is the
   field check its init already made; KeyboardLiveTyping.takesTyping adds
   the two-thumb case, read through LiveDictationText.fieldTakesTyping.

3. Home's "New: Hex's own speech model" card opens Settings › Dictation at
   Speech model in keyboard (SettingsPageScaffold.startsAt, a
   ScrollViewReader): the row's title lands at y 571 instead of 812 of
   874 pt. Opening Dictation from Home's list still starts at the top.

4. ios/docs/settings-and-home.md, dictation.md, verification.md and
   ios/README.md describe Settings as it is: Live words and its three
   choices, the three Keyboard cards, the four Experiments groups (21
   switches, 20 off a foldable), iOS Settings ↗, HEXMobile/Settings/.

Verified: testIntoFieldShowsTheWordsOverTheKeysWhereItCantType in
LiveDictationTests (DictationLearningTests.swift); the timed Home runs and
landing positions above on the simulator; full suite and code-shape --check
in the PR.
2026-11-22 23:00:12 -05:00

263 lines
11 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
import os
import Speech
/// One in-keyboard dictation's live words (Settings › Dictation › Show what I say and Type as
/// I speak):
/// started with the capture's first audio when a switch wants them, fed every chunk the page
/// sends, and stopped when the capture stops, keeping its words on screen until the
/// dictation goes in. It never changes the dictation itself: whenever it can't run (a switch
/// off, short of memory, the language's model not on the iPhone, an error, or no first words
/// within `firstWordsPatience`) the keyboard records and inserts exactly as without it.
@MainActor
final class KeyboardLiveDictation {
private let text: LiveDictationText
private let sampleRate: Double
private var source: LiveTranscriptSource?
/// Audio from before the source exists (the language's lookup), replayed into it.
private var held: [Data] = []
private var heldBytes = 0
private var stopped = false
private var resolving: Task<Void, Never>?
private var latest: LiveTranscript?
private var publishScheduled = false
private let startedAt = Date()
private var firstAudioAt: Date?
private var firstWordsAt: Date?
private var updates = 0
private var rewrites = 0
private var shownWords: [Substring] = []
private let footprintAtStart = KeyboardParakeet.memory().footprint
private var peakFootprint = 0
/// Memory the keyboard must still have once its capture runs: the live recogniser's own
/// share in the keyboard (about 2 MB measured, its model runs in Apple's speech service)
/// with margin, and the speech model's transcription after it
/// (`KeyboardParakeetBudget.transcribeHeadroom`, 24 MB).
static let headroomNeeded = 32 * 1_048_576
/// Below this while recording, the recogniser goes and the dictation goes on without it.
static let floor = 24 * 1_048_576
/// Audio held while the language is looked up; past it the live words give up.
static let maximumHeldSeconds: Double = 4
private let settings: LiveDictationSettings
private init(text: LiveDictationText, sampleRate: Double, settings: LiveDictationSettings) {
self.text = text
self.sampleRate = sampleRate
self.settings = settings
}
/// Starts live words for a capture at `sampleRate`, or returns nil (today's behaviour)
/// when neither switch wants them or memory is short.
static func start(sampleRate: Double, text: LiveDictationText?) -> KeyboardLiveDictation? {
guard let text else { return nil }
let settings = LiveDictationSettings()
// Words an earlier keyboard left are another dictation's: never replaced by this one's.
if settings.leftover != nil { settings.leftover = nil }
guard settings.wantsLiveWords else { return nil }
let available = os_proc_available_memory()
guard available == 0 || available >= headroomNeeded else {
KeyboardProbeLog.shared.write("live: skipped, short of memory \(KeyboardProbeLog.memory())")
return nil
}
let live = KeyboardLiveDictation(text: text, sampleRate: sampleRate, settings: settings)
live.resolve()
return live
}
func append(_ pcm: Data) {
guard !stopped else { return }
if firstAudioAt == nil { firstAudioAt = Date() }
if let source {
source.append(pcm)
return
}
held.append(pcm)
heldBytes += pcm.count
if Double(heldBytes / 2) / sampleRate > Self.maximumHeldSeconds {
giveUp("no recogniser after \(Int(Self.maximumHeldSeconds)) s of audio")
}
}
/// The capture stopped: the recogniser goes now, its words stay until the dictation is in.
func stop() {
guard !stopped else { return }
stopped = true
resolving?.cancel()
source?.stop()
source = nil
held = []
publish()
let first = firstWordsAt.map { "\(Int($0.timeIntervalSince(firstAudioAt ?? startedAt) * 1000)) ms after first audio" } ?? "none"
KeyboardProbeLog.shared.write(
"live: stopped after \(String(format: "%.1f", Date().timeIntervalSince(startedAt))) s, first words \(first), \(updates) updates, \(rewrites) words rewritten, footprint +\(String(format: "%.1f", Double(max(0, peakFootprint - footprintAtStart)) / 1_048_576)) MB at most \(KeyboardProbeLog.memory())"
)
}
/// iOS warned the keyboard about memory: the recogniser goes, the dictation goes on.
func memoryWarning() {
guard !stopped else { return }
KeyboardProbeLog.shared.write("live: stopped for a memory warning")
stop()
}
/// Finds the language the dictation will be transcribed in and starts Apple's recogniser
/// for it, only if its model is already on the iPhone.
private func resolve() {
guard #available(iOS 26.0, *) else {
giveUp("needs iOS 26")
return
}
let rate = sampleRate
resolving = Task { [weak self] in
let settings = LanguageSettings()
let picked = await AppleSpeechLanguages.resolve(settings: settings)
var installed = false
if let picked {
installed = await AssetInventory.status(forModules: [AppleSpeechModule(picked).module]) == .installed
}
guard let self, !self.stopped else { return }
guard let picked, installed else {
if let replay = LiveTranscriptReplay.load(log: { KeyboardProbeLog.shared.write($0) }) {
self.attach(replay)
return
}
self.giveUp(picked.map { "\($0.identifier) model not on this iPhone" } ?? "no language")
return
}
guard let source = AppleLiveTranscriber(
locale: picked.locale,
module: picked.module == .dictation ? .dictation : .speech,
sampleRate: rate,
log: { KeyboardProbeLog.shared.write($0) }
) else {
self.giveUp("no audio format")
return
}
self.attach(source)
}
}
/// The preview shows from here (empty until the first words), never while the language is
/// still looked up, so a dictation without live words never flashes it. Type as I speak
/// never types a dictation Translate what I say turns into another language, nor in a field
/// it never types in; the words then show over the keys instead (`liveWords`).
private func attach(_ source: LiveTranscriptSource) {
let words = settings.liveWords(
translating: DictationTranslationSettings().isEnabled,
fieldTakesTyping: text.fieldTakesTyping()
)
text.begin(showsPreview: words.showsPreview, typesIntoField: words.typesIntoField)
source.onUpdate = { [weak self] transcript in self?.received(transcript) }
source.onFailure = { [weak self] reason in self?.giveUp(reason) }
self.source = source
let audio = held
held = []
heldBytes = 0
audio.forEach { source.append($0) }
}
private func received(_ transcript: LiveTranscript) {
guard !stopped else { return }
let available = os_proc_available_memory()
if available > 0, available < Self.floor {
KeyboardProbeLog.shared.write("live: stopped, short of memory \(KeyboardProbeLog.memory())")
stop()
return
}
if firstWordsAt == nil, !transcript.isEmpty { firstWordsAt = Date() }
latest = transcript
peakFootprint = max(peakFootprint, KeyboardParakeet.memory().footprint)
// A recogniser's results come in bursts, a word at a time: the screen takes the last.
guard !publishScheduled else { return }
publishScheduled = true
DispatchQueue.main.async { [weak self] in
MainActor.assumeIsolated { self?.publish() }
}
}
private func publish() {
publishScheduled = false
guard let latest else { return }
self.latest = nil
updates += 1
let words = latest.text.split(whereSeparator: \.isWhitespace)
rewrites += Self.changedWords(from: shownWords, to: words)
shownWords = words
text.update(latest)
}
/// How many words already on screen `next` changes or takes away.
nonisolated static func changedWords(from shown: [Substring], to next: [Substring]) -> Int {
var same = 0
while same < min(shown.count, next.count), shown[same] == next[same] { same += 1 }
return shown.count - same
}
private func giveUp(_ reason: String) {
guard !stopped else { return }
KeyboardProbeLog.shared.write("live: none for this dictation (\(reason))")
stop()
// Without words, the keys come back as they are without the preview.
if text.transcript?.isEmpty ?? true { text.end() }
}
}
/// The simulator has no speech models: a recorded run of Apple's recogniser (`live-replay.json`
/// in the App Group, from `AppleLiveTranscriber` on a Mac) replayed at its own pace from the
/// capture's first audio, so the preview can be seen in the real keyboard there. Never on an
/// iPhone.
@MainActor
final class LiveTranscriptReplay: LiveTranscriptSource {
private struct Step: Decodable {
let t: Double
let settled: String
let tentative: String
}
var onUpdate: ((LiveTranscript) -> Void)?
var onFailure: ((String) -> Void)?
private let steps: [Step]
private var task: Task<Void, Never>?
private init(steps: [Step]) {
self.steps = steps
}
static func load(log: (String) -> Void) -> LiveTranscriptReplay? {
#if targetEnvironment(simulator)
// The App Group, or the keyboard's own Library where an unsigned build has no group.
let folders = [
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: KeyboardBridge.appGroup),
FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first,
].compactMap { $0 }
guard let data = folders.lazy.compactMap({ try? Data(contentsOf: $0.appendingPathComponent("live-replay.json")) }).first,
let steps = try? JSONDecoder().decode([Step].self, from: data), !steps.isEmpty else { return nil }
log("live: replaying \(steps.count) recorded results (simulator)")
return LiveTranscriptReplay(steps: steps)
#else
return nil
#endif
}
func append(_ pcm: Data) {
guard task == nil else { return }
let steps = steps
let start = Date()
task = Task { [weak self] in
for step in steps {
let wait = step.t - Date().timeIntervalSince(start)
if wait > 0 { try? await Task.sleep(for: .seconds(wait)) }
guard !Task.isCancelled else { return }
self?.onUpdate?(LiveTranscript(settled: step.settled, tentative: step.tentative))
}
}
}
func stop() {
task?.cancel()
task = nil
onUpdate = nil
}
}