Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Chat/SpeechController.swift3// Description: Voice input (Speech framework, on-device recognition when the system supports it) and text-to-speech (AVSpeechSynthesizer).4import AVFoundation5import Foundation6import Observation7import Speech89@MainActor @Observable10final class SpeechController: NSObject, AVSpeechSynthesizerDelegate {11 private(set) var isListening = false12 private(set) var speakingID: UUID?13 private(set) var errorText: String?14 /// True when the last dictation ran entirely on device.15 private(set) var usedOnDeviceRecognition = false1617 @ObservationIgnored private let synthesizer = AVSpeechSynthesizer()18 @ObservationIgnored private var recognizer: SFSpeechRecognizer?19 @ObservationIgnored private var audioEngine: AVAudioEngine?20 @ObservationIgnored private var request: SFSpeechAudioBufferRecognitionRequest?21 @ObservationIgnored private var task: SFSpeechRecognitionTask?2223 override init() { super.init(); synthesizer.delegate = self }2425 var isAvailable: Bool { SFSpeechRecognizer(locale: Locale.current)?.isAvailable ?? (SFSpeechRecognizer()?.isAvailable ?? false) }2627 // MARK: Dictation2829 func toggleDictation(onText: @escaping @MainActor (String) -> Void) {30 if isListening { stopDictation(); return }31 errorText = nil32 Task {33 guard await Self.requestSpeechAuthorization() else { errorText = "Speech recognition is not allowed. Enable it in Settings."; return }34 guard await Self.requestMicrophone() else { errorText = "Microphone access is not allowed. Enable it in Settings."; return }35 do { try start(onText: onText) } catch { errorText = "Dictation could not start: \(error.localizedDescription)"; stopDictation() }36 }37 }3839 private static func requestSpeechAuthorization() async -> Bool {40 await withCheckedContinuation { c in SFSpeechRecognizer.requestAuthorization { c.resume(returning: $0 == .authorized) } }41 }42 private static func requestMicrophone() async -> Bool {43 await withCheckedContinuation { c in AVCaptureDevice.requestAccess(for: .audio) { c.resume(returning: $0) } }44 }4546 private func start(onText: @escaping @MainActor (String) -> Void) throws {47 guard let recognizer = SFSpeechRecognizer(locale: Locale.current) ?? SFSpeechRecognizer(), recognizer.isAvailable else {48 errorText = "Speech recognition is unavailable for this language."; return49 }50 self.recognizer = recognizer51 #if os(iOS)52 let session = AVAudioSession.sharedInstance()53 try session.setCategory(.record, mode: .measurement, options: .duckOthers)54 try session.setActive(true, options: .notifyOthersOnDeactivation)55 #endif56 let request = SFSpeechAudioBufferRecognitionRequest()57 request.shouldReportPartialResults = true58 // keep audio on the device whenever the system has a local model for this language59 request.requiresOnDeviceRecognition = recognizer.supportsOnDeviceRecognition60 usedOnDeviceRecognition = recognizer.supportsOnDeviceRecognition61 if !recognizer.supportsOnDeviceRecognition { errorText = "On-device recognition is unavailable for this language: audio is sent to Apple for transcription." }62 let engine = AVAudioEngine()63 let input = engine.inputNode64 let format = input.outputFormat(forBus: 0)65 guard format.sampleRate > 0 else { errorText = "No microphone is available."; return }66 input.installTap(onBus: 0, bufferSize: 1024, format: format) { buffer, _ in request.append(buffer) }67 engine.prepare(); try engine.start()68 self.audioEngine = engine; self.request = request69 isListening = true70 task = recognizer.recognitionTask(with: request) { [weak self] result, error in71 let text = result?.bestTranscription.formattedString72 let finished = result?.isFinal ?? false73 Task { @MainActor in74 if let text { onText(text) }75 if finished || error != nil { self?.stopDictation() }76 }77 }78 }7980 func stopDictation() {81 audioEngine?.stop(); audioEngine?.inputNode.removeTap(onBus: 0)82 request?.endAudio(); task?.cancel()83 audioEngine = nil; request = nil; task = nil; isListening = false84 #if os(iOS)85 try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)86 #endif87 }8889 // MARK: Text to speech9091 func toggleSpeaking(_ text: String, id: UUID) {92 if speakingID == id { synthesizer.stopSpeaking(at: .immediate); speakingID = nil; return }93 synthesizer.stopSpeaking(at: .immediate)94 let utterance = AVSpeechUtterance(string: Self.plain(text))95 if let language = NSLinguisticTagger.dominantLanguage(for: text), let voice = AVSpeechSynthesisVoice(language: language) { utterance.voice = voice }96 speakingID = id97 synthesizer.speak(utterance)98 }99100 /// Markdown markers read badly aloud.101 static func plain(_ markdown: String) -> String {102 var out = markdown103 for marker in ["```", "**", "__", "`", "#"] { out = out.replacingOccurrences(of: marker, with: "") }104 return out105 }106107 nonisolated func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { Task { @MainActor in self.speakingID = nil } }108 nonisolated func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) { Task { @MainActor in self.speakingID = nil } }109}110