Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

build(ios): recognize the simulator's audio on the Mac it runs on

wqugurue6c7452572e3
diff --git a/ios/scripts/host-recognizer.sh b/ios/scripts/host-recognizer.sh
new file mode 100755
index 0000000..1232757
--- /dev/null
+++ b/ios/scripts/host-recognizer.sh
@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+# Runs the Mac-side recognizer for the iOS simulator's dev build (ios/tools/host-recognizer):
+# the simulator has no working speech models, so the app's audio is recognized here, on device.
+# ios/scripts/host-recognizer.sh ["<device>"...] (default: every booted simulator)
+set -euo pipefail
+cd "$(dirname "$0")/../.."
+OUT=ios/build/host-recognizer
+mkdir -p ios/build
+xcrun --sdk macosx swiftc -O -swift-version 5 -parse-as-library -target arm64-apple-macos26.0 \
+ Sources/LiveSubs/Recognition.swift Sources/LiveSubs/Languages.swift Sources/LiveSubs/AudioFiles.swift \
+ ios/tools/host-recognizer/main.swift -o "$OUT"
+# Simulator UDIDs: the booted ones, or the named devices.
+if [[ $# -eq 0 ]]; then
+ ids=($(xcrun simctl list devices booted | grep -oE '[0-9A-F-]{36}'))
+else
+ ids=(); for d in "$@"; do ids+=($(xcrun simctl list devices | grep -F "$d (" | grep -oE '[0-9A-F-]{36}' | head -1)); done
+fi
+exec "$OUT" "${ids[@]}"
diff --git a/ios/tools/host-recognizer/main.swift b/ios/tools/host-recognizer/main.swift
new file mode 100644
index 0000000..981c908
--- /dev/null
+++ b/ios/tools/host-recognizer/main.swift
@@ -0,0 +1,113 @@
+import AVFoundation
+import Foundation
+
+// Recognizes speech for the iOS simulator's dev build, whose speech models are empty placeholders
+// (ios/Sources/Cicada/HostRecognizer.swift). Watches each app container's Caches/host-recognizer:
+// a folder with request.json is one stream; its audio.f32 (16 kHz mono Float32, growing) goes
+// through the Mac's own Recognition.swift, and every piece is appended to results.jsonl.
+// ios/scripts/host-recognizer.sh (builds this and watches the booted simulators)
+
+enum DevBuild { static let isDev = true }
+func debugLog(_ message: String) { print("\(Date()) \(message)") }
+func contentLog(_ message: @autoclosure () -> String) {}
+func L(_ s: String.LocalizationValue) -> String { String(localized: s) }
+
+let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 16_000, channels: 1, interleaved: false)!
+
+func write(_ obj: [String: Any], to url: URL) {
+ guard var data = try? JSONSerialization.data(withJSONObject: obj) else { return }
+ data.append(0x0A)
+ if let h = try? FileHandle(forWritingTo: url) { h.seekToEndOfFile(); h.write(data); try? h.close() }
+}
+
+func serve(_ dir: URL) async {
+ let results = dir.appendingPathComponent("results.jsonl")
+ guard let req = try? JSONSerialization.jsonObject(with: Data(contentsOf: dir.appendingPathComponent("request.json"))) as? [String: Any],
+ let locale = req["locale"] as? String else { return }
+ let drafts = req["drafts"] as? Bool ?? true
+ debugLog("stream \(dir.lastPathComponent) locale=\(locale) drafts=\(drafts)")
+ do {
+ try await Recognition.install(locale)
+ let stream = try await Recognition.start(locale, drafts: drafts, confidence: true)
+ let reader = Task {
+ do {
+ for try await p in stream.results {
+ var o: [String: Any] = ["text": p.text, "isFinal": p.isFinal, "start": p.start, "end": p.end]
+ if let c = p.confidence { o["confidence"] = c }
+ write(o, to: results)
+ if p.isFinal { debugLog("final: \(p.text)") }
+ }
+ } catch { write(["error": "\(error)"], to: results) }
+ }
+ let conv = PCMConverter()
+ var offset: UInt64 = 0
+ let audioURL = dir.appendingPathComponent("audio.f32")
+ while true {
+ let ended = FileManager.default.fileExists(atPath: dir.appendingPathComponent("end").path)
+ if let h = try? FileHandle(forReadingFrom: audioURL) {
+ try? h.seek(toOffset: offset)
+ var data = h.readDataToEndOfFile()
+ try? h.close()
+ data = data.prefix(data.count - data.count % 4)
+ offset += UInt64(data.count)
+ let frames = data.count / 4
+ if frames > 0, let b = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(frames)) {
+ b.frameLength = AVAudioFrameCount(frames)
+ data.withUnsafeBytes { raw in
+ b.floatChannelData![0].update(from: raw.bindMemory(to: Float.self).baseAddress!, count: frames)
+ }
+ if let c = conv.convert(b, to: stream.format) { stream.append(c) }
+ }
+ } else if !FileManager.default.fileExists(atPath: dir.path) {
+ await stream.cancel(); break
+ }
+ if ended {
+ await stream.finish()
+ break
+ }
+ try? await Task.sleep(nanoseconds: 100_000_000)
+ }
+ _ = await reader.value
+ write(["done": true], to: results)
+ debugLog("stream \(dir.lastPathComponent) done")
+ } catch {
+ debugLog("stream failed: \(error)")
+ write(["error": "\(error)"], to: results)
+ }
+}
+
+/// Every simulator app container's Caches/host-recognizer, found again on each pass: reinstalling
+/// the app moves its container.
+func roots(_ devices: [String]) -> [URL] {
+ let fm = FileManager.default
+ let base = fm.homeDirectoryForCurrentUser.appendingPathComponent("Library/Developer/CoreSimulator/Devices")
+ var out: [URL] = []
+ for d in devices {
+ let apps = base.appendingPathComponent("\(d)/data/Containers/Data/Application")
+ for a in (try? fm.contentsOfDirectory(at: apps, includingPropertiesForKeys: nil)) ?? [] {
+ let r = a.appendingPathComponent("Library/Caches/host-recognizer")
+ if fm.fileExists(atPath: r.path) { out.append(r) }
+ }
+ }
+ return out
+}
+
+@main
+struct HostRecognizerMain {
+ static func main() async {
+ setvbuf(stdout, nil, _IOLBF, 0)
+ let devices = Array(CommandLine.arguments.dropFirst())
+ debugLog("watching simulators \(devices) recognizer=\(Recognition.name)")
+ while true {
+ for root in roots(devices) {
+ let dirs = (try? FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil)) ?? []
+ for d in dirs where FileManager.default.fileExists(atPath: d.appendingPathComponent("request.json").path)
+ && !FileManager.default.fileExists(atPath: d.appendingPathComponent("claimed").path) {
+ FileManager.default.createFile(atPath: d.appendingPathComponent("claimed").path, contents: nil)
+ Task { await serve(d) }
+ }
+ }
+ try? await Task.sleep(nanoseconds: 300_000_000)
+ }
+ }
+}