hex/crates/capture/helper/LiveTranscribe.swift
Kit Langton fb617e5082 Import scrawl-capture into crates/capture
Copy Scrawl's `crates/capture` from kitlangton/scrawl, branch
`engine/split`, commit e351ddb28be47c77481cb6821cc72d7f8fe7c769 (e351ddb),
without history, keeping the package name `scrawl-capture`, and its
`docs/app-handover-spec.md` as `docs/plans/capture-handover-spec.md`. Hex
depends on it by path instead of the Git pin at 86d7d6c; the Nix
`outputHashes."scrawl-capture-0.3.13"` entry goes.

Workspace: `crates/capture` is in `[workspace] exclude`, so `cargo build`,
`cargo test`, and `--workspace` never build it (Linux, Windows, and Nix
included). It compiles only as the `capture` feature's macOS dependency,
sharing the root Cargo.lock and target/. The root package stays a member
without being listed, since listing "." defeats `exclude`. On macOS
`scripts/check` and the macOS workflow name it (`-p voice-control
-p scrawl-capture --features capture`) to lint and test its 257 tests.

API adaptations in capture_host:
- `Finished` gains `gaps` and `missing_transcript`; the host records them
  and `hex capture status` (human and JSON `last`) shows them.
- `Engine::new` bridges the crate's log to `Host::log` when no sink is
  set, so Hex's own `TracingLog` sink goes and each line is logged once.
- Scrawl now writes its bundle files 0600; Hex still tightens
  recording.mp4, the window log, and folders.
- Nothing on Hex's path reads ~/.config/scrawl or writes under
  Application Support/scrawl: the crate does that only after
  `enable_scrawl_host_defaults`/`enable_file_log`, which Hex never calls.

Scrubbed (synthetic values): a file:///Users/<name> path in ax.rs, the
com.kitlangton.UpDown / dev.kitlangton.UpDown test bundle IDs in
handoff.rs and phone.rs (now com.example.UpDown), and a personal device
name in a phone.rs test. Kept: Scrawl's own product IDs
(dev.kitlangton.scrawl, .helper) and its public GitHub releases URL.

fmt and clippy -D warnings, no behaviour change: `cargo fmt` on five
files; `chunks_exact(N)` to `as_chunks::<N>()` (19 sites); collapsed
`if let` chains in capture/macos.rs and pipeline.rs and a `match` in
page.rs; `type` aliases for a drag start and two ingest hooks; explicit
lower bounds on two test `match` range arms in keyframes.rs;
`slice::from_ref` for two one-element slices; `and_then` for
`map().flatten()`; a dropped `to_owned`; `#[allow]` for one
`too_many_arguments` and one `single_range_in_vec_init`.
2026-10-25 19:14:23 -07:00

150 lines
6.3 KiB
Swift

@preconcurrency import AVFoundation
import Speech
/// `transcribe-stream`: transcribes a recording while it's made. Reads 16 kHz mono 16-bit
/// little-endian PCM on stdin from the recording's first sample (the samples `audio.wav` will
/// hold), and writes JSON lines: `{"ready":true,"locale"}` once the model is loaded, one
/// `{"segment":{t0,t1,text},"words":[…]}` per finalized result, and after stdin closes and the
/// tail is finalized `{"done":true,"samples":N}`. Times are ms from the first sample, as
/// `transcribe` gives them for the WAV. It never downloads a model: one that isn't installed
/// fails at once, and Scrawl transcribes the finished recording instead.
/// Before macOS 26 there is no SpeechAnalyzer to stream to, so it fails at once the same way.
enum LiveTranscriber {
static func run(preferred: String? = nil) async throws {
#if compiler(>=6.2)
if #available(macOS 26, *) { return try await stream(preferred: preferred) }
#endif
throw HelperError("live transcription needs macOS 26's SpeechAnalyzer")
}
#if compiler(>=6.2)
@available(macOS 26, *)
private static func stream(preferred: String?) async throws {
guard let choice = await SpeechModel.choose(preferred: preferred), choice.engine == .speech else {
throw HelperError("SpeechTranscriber isn't available on this Mac")
}
let locale = choice.locale
let transcriber = SpeechTranscriber(locale: locale, transcriptionOptions: [], reportingOptions: [], attributeOptions: [.audioTimeRange])
guard await AssetInventory.status(forModules: [transcriber]) == .installed else {
throw HelperError("the speech model for \(locale.identifier) isn't installed")
}
let pcm = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16_000, channels: 1, interleaved: false)!
let format = await SpeechAnalyzer.bestAvailableAudioFormat(compatibleWith: [transcriber]) ?? pcm
let converter = format == pcm ? nil : AVAudioConverter(from: pcm, to: format)
if format != pcm && converter == nil { throw HelperError("can't convert audio to \(format)") }
let analyzer = SpeechAnalyzer(modules: [transcriber], options: SpeechAnalyzer.Options(priority: .userInitiated, modelRetention: .whileInUse))
let (inputs, continuation) = AsyncStream<AnalyzerInput>.makeStream()
let results = Task {
for try await result in transcriber.results {
let text = String(result.text.characters).trimmingCharacters(in: .whitespacesAndNewlines)
guard !text.isEmpty else { continue }
var words: [[String: Any]] = []
for run in result.text.runs {
guard let range = run.audioTimeRange else { continue }
let word = String(result.text[run.range].characters).trimmingCharacters(in: .whitespacesAndNewlines)
guard !word.isEmpty else { continue }
words.append(["t0": ms(range.start), "t1": ms(range.end), "text": word])
}
emit(["segment": ["t0": ms(result.range.start), "t1": ms(result.range.end), "text": text], "words": words])
}
}
// Audio arrives before the model is ready; the stream holds it until the analyzer starts.
let reader = StdinPCM(format: pcm, converter: converter, output: format, continuation: continuation)
let samples = Task.detached { try reader.pump() }
try await analyzer.prepareToAnalyze(in: format)
emit(["ready": true, "locale": locale.identifier])
try await analyzer.start(inputSequence: inputs)
let total = try await samples.value
try await analyzer.finalizeAndFinishThroughEndOfInput()
try await results.value
emit(["done": true, "samples": total])
}
/// As `Transcriber` rounds them, so both paths give the same times.
private static func ms(_ time: CMTime) -> Int {
time.seconds.isFinite ? max(0, Int((time.seconds * 1000).rounded())) : 0
}
#endif
}
#if compiler(>=6.2)
/// Turns stdin into analyzer buffers, about 100 ms at a time.
@available(macOS 26, *)
private final class StdinPCM: @unchecked Sendable {
let format: AVAudioFormat
let converter: AVAudioConverter?
let output: AVAudioFormat
let continuation: AsyncStream<AnalyzerInput>.Continuation
init(format: AVAudioFormat, converter: AVAudioConverter?, output: AVAudioFormat, continuation: AsyncStream<AnalyzerInput>.Continuation) {
self.format = format
self.converter = converter
self.output = output
self.continuation = continuation
}
/// Reads to the end of stdin and returns how many samples it passed on.
func pump() throws -> Int {
defer { continuation.finish() }
let stdin = FileHandle.standardInput
var pending = Data()
var total = 0
while true {
let chunk = stdin.availableData
if chunk.isEmpty { break }
pending.append(chunk)
let usable = pending.count & ~1
guard usable >= 3_200 else { continue }
try send(pending.prefix(usable))
total += usable / 2
pending.removeFirst(usable)
}
let usable = pending.count & ~1
if usable > 0 {
try send(pending.prefix(usable))
total += usable / 2
}
return total
}
private func send(_ bytes: Data) throws {
let frames = AVAudioFrameCount(bytes.count / 2)
guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frames) else {
throw HelperError("couldn't allocate an audio buffer")
}
buffer.frameLength = frames
bytes.withUnsafeBytes { raw in
let source = raw.bindMemory(to: Int16.self)
let channel = buffer.int16ChannelData![0]
for i in 0..<Int(frames) { channel[i] = Int16(littleEndian: source[i]) }
}
continuation.yield(AnalyzerInput(buffer: try convert(buffer)))
}
private func convert(_ buffer: AVAudioPCMBuffer) throws -> AVAudioPCMBuffer {
guard let converter else { return buffer }
let ratio = output.sampleRate / format.sampleRate
let capacity = AVAudioFrameCount((Double(buffer.frameLength) * ratio).rounded(.up)) + 32
guard let out = AVAudioPCMBuffer(pcmFormat: output, frameCapacity: capacity) else {
throw HelperError("couldn't allocate an audio buffer")
}
var given = false
var error: NSError?
converter.convert(to: out, error: &error) { _, status in
if given {
status.pointee = .noDataNow
return nil
}
given = true
status.pointee = .haveData
return buffer
}
if let error { throw error }
return out
}
}
#endif