The best way to control Claude Code, Codex and more from any computer or phone

main
swift 98 lines 3.29 KB
Raw
1 // Adapted from AppleTranscriptionImpl.swift in @react-native-ai/apple (MIT, Callstack).
2 import AVFoundation
3 import Foundation
4 import Speech
5
6 @available(iOS 26, macOS 26, *)
7 enum AppleSpeechEngine {
8 enum Failure: LocalizedError {
9 case unavailable
10 case unsupportedLocale(String)
11 case unknownAssetStatus
12
13 var errorDescription: String? {
14 switch self {
15 case .unavailable: return "Speech transcription is not available."
16 case .unsupportedLocale(let locale): return "Locale not supported: \(locale)"
17 case .unknownAssetStatus: return "Unknown asset inventory status."
18 }
19 }
20 }
21
22 static var isAvailable: Bool { SpeechTranscriber.isAvailable }
23
24 static func supportedLocales() async throws -> [String] {
25 try requireAvailable()
26 return await SpeechTranscriber.supportedLocales.map { $0.identifier(.bcp47) }
27 }
28
29 private static func requireAvailable() throws {
30 guard isAvailable else {
31 throw Failure.unavailable
32 }
33 }
34
35 private static func makeTranscriber(for locale: Locale) -> SpeechTranscriber {
36 let preset = SpeechTranscriber.Preset.timeIndexedTranscriptionWithAlternatives
37 return SpeechTranscriber(
38 locale: locale,
39 transcriptionOptions: preset.transcriptionOptions,
40 reportingOptions: preset.reportingOptions.subtracting([.alternativeTranscriptions]),
41 attributeOptions: preset.attributeOptions
42 )
43 }
44
45 static func prepare(_ identifier: String) async throws -> String {
46 try requireAvailable()
47 guard
48 let locale = await SpeechTranscriber.supportedLocale(
49 equivalentTo: Locale(identifier: identifier))
50 else {
51 throw Failure.unsupportedLocale(identifier)
52 }
53 let transcriber = makeTranscriber(for: locale)
54 switch await AssetInventory.status(forModules: [transcriber]) {
55 case .installed:
56 break
57 case .supported, .downloading:
58 if let request = try await AssetInventory.assetInstallationRequest(supporting: [transcriber]) {
59 try await request.downloadAndInstall()
60 }
61 case .unsupported:
62 throw Failure.unsupportedLocale(identifier)
63 @unknown default:
64 throw Failure.unknownAssetStatus
65 }
66 return locale.identifier(.bcp47)
67 }
68
69 static func transcribe(_ identifier: String, file: URL) async throws -> String {
70 try requireAvailable()
71 guard file.isFileURL else { throw CocoaError(.fileReadUnsupportedScheme) }
72 let audioFile = try AVAudioFile(forReading: file)
73 let transcriber = makeTranscriber(for: Locale(identifier: identifier))
74 let analyzer = SpeechAnalyzer(modules: [transcriber])
75 let collector = Task { () throws -> [String] in
76 var texts: [String] = []
77 for try await result in transcriber.results where result.isFinal {
78 let text = String(result.text.characters).trimmingCharacters(in: .whitespacesAndNewlines)
79 if !text.isEmpty { texts.append(text) }
80 }
81 return texts
82 }
83 do {
84 if let lastSampleTime = try await analyzer.analyzeSequence(from: audioFile) {
85 try await analyzer.finalizeAndFinish(through: lastSampleTime)
86 } else {
87 await analyzer.cancelAndFinishNow()
88 }
89 return try await collector.value.joined(separator: " ")
90 } catch {
91 collector.cancel()
92 await analyzer.cancelAndFinishNow()
93 _ = try? await collector.value
94 throw error
95 }
96 }
97
98 }