Repository Public License
1//2// ORIX Audio3// File: ORIXAudio/App/DebugHooks.swift4// Description: DEBUG-only launch arguments used by UI tests, screenshots and the in-app transcription benchmark. Not compiled into release builds.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9#if DEBUG10import Foundation11import ORIXAudioCore1213/// Launch arguments (all optional):14/// -ORIXInput <file> feed the capture pipeline from an audio file instead of the microphone15/// -ORIXImport <file>[,<file>] import files at launch16/// -ORIXRecoverAll recover every interrupted capture found at launch17/// -ORIXSkipWelcome mark the welcome and consent screens as seen18/// -ORIXSection <name> record | library | actions | settings19/// -ORIXOpen <title-substring> open the first recording whose title contains the text20/// -ORIXTranscribe <locale> transcribe the recording selected by -ORIXOpen (add -ORIXSummarize for notes)21/// -ORIXLoadTranscript <json> attach a saved transcript to the recording selected by -ORIXOpen22/// -ORIXPanel <name> info | spectrum | spectrogram | analysis | transcript | notes23/// -ORIXAutoRecord <seconds> start recording at launch and stop after the given time (0 = keep recording)24/// -ORIXBench <audio> <locale> <reference.txt> <out.json> transcribe and write WER / CER25enum DebugHooks {26 static func value(_ flag: String) -> String? {27 let args = ProcessInfo.processInfo.arguments28 guard let i = args.firstIndex(of: flag), i + 1 < args.count else { return nil }29 return args[i + 1]30 }31 static func has(_ flag: String) -> Bool { ProcessInfo.processInfo.arguments.contains(flag) }3233 static var initialPanel: DetailPanel? {34 switch value("-ORIXPanel") {35 case "info": .overview; case "spectrum": .spectrum; case "spectrogram": .spectrogram36 case "analysis": .analysis; case "transcript": .transcript; case "notes": .notes37 default: nil38 }39 }40 static var initialSection: AppSection? { value("-ORIXSection").flatMap(AppSection.init(rawValue:)) }4142 @MainActor static func run(model: AppModel) async {43 if has("-ORIXSkipWelcome") { model.settings.hasSeenWelcome = true; model.settings.hasAcknowledgedConsent = true }44 if let input = value("-ORIXInput") { AudioCaptureEngine.debugInputFile = URL(fileURLWithPath: input) }45 if has("-ORIXRecoverAll") { for capture in model.recoverable { await model.recover(capture) }; model.pendingOpen = nil }46 if let list = value("-ORIXImport") {47 let existing = Set(((try? model.context.fetch(.init())) as [Recording]? ?? []).compactMap(\.originalFileName))48 let urls = list.split(separator: ",").map { URL(fileURLWithPath: String($0)) }.filter { !existing.contains($0.lastPathComponent) }49 if !urls.isEmpty { await model.importFiles(urls); model.pendingOpen = nil }50 }51 if let query = value("-ORIXOpen") {52 let all = ((try? model.context.fetch(.init())) as [Recording]? ?? []).filter { $0.deletedAt == nil }53 if let match = all.first(where: { $0.title.localizedCaseInsensitiveContains(query) }) { model.pendingOpen = match.id }54 }55 if let file = value("-ORIXLoadTranscript"), let id = model.pendingOpen, let recording = model.recording(id),56 let transcript = LibraryFiles.load(Transcript.self, from: URL(fileURLWithPath: file)) {57 // A transcript produced by the same engine on the Mac (Scripts/bench-transcription.sh) for the same audio.58 model.processing.saveTranscript(transcript, for: recording)59 recording.transcriptStatus = .done; recording.localeIdentifier = transcript.localeIdentifier60 if has("-ORIXSummarize") { try? await model.processing.summarize(recording) }61 // Notes produced by the same engine on the Mac: Foundation Models cannot generate inside the iOS 26.5 simulator.62 if let notesFile = value("-ORIXLoadNotes"), let notes = LibraryFiles.load(MeetingNotes.self, from: URL(fileURLWithPath: notesFile)) {63 try? LibraryFiles.save(notes, to: LibraryFiles.notesURL(recording.id))64 recording.hasNotes = true65 model.processing.saveTranscript(transcript, for: recording)66 }67 }68 if let locale = value("-ORIXTranscribe"), let id = model.pendingOpen, let recording = model.recording(id) {69 try? await model.processing.transcribe(recording, locale: Locale(identifier: locale), allowModelDownload: true)70 if has("-ORIXSummarize") { try? await model.processing.summarize(recording) }71 }72 if let seconds = value("-ORIXAutoRecord").flatMap(Double.init) {73 if let preset = value("-ORIXPreset").flatMap(RecordingPreset.init(rawValue:)) { model.recorder.preset = preset }74 await model.recorder.start()75 if seconds > 0 {76 try? await Task.sleep(for: .seconds(seconds))77 await model.recorder.stop()78 }79 }80 let args = ProcessInfo.processInfo.arguments81 if let i = args.firstIndex(of: "-ORIXBench"), i + 4 < args.count { await bench(audio: args[i + 1], locale: args[i + 2], reference: args[i + 3], out: args[i + 4]) }82 }8384 /// Runs the same on-device transcription path the app uses and records the measured error rates.85 static func bench(audio: String, locale: String, reference: String, out: String) async {86 var result: [String: Any] = ["file": (audio as NSString).lastPathComponent, "locale": locale]87 let loc = Locale(identifier: locale)88 result["path"] = "\(await TranscriptionEngine.path(for: loc))"89 do {90 let ref = try String(contentsOfFile: reference, encoding: .utf8)91 _ = await TranscriptionEngine.requestAuthorization()92 let start = Date()93 let transcript = try await TranscriptionEngine.transcribe(url: URL(fileURLWithPath: audio), locale: loc, allowModelDownload: true)94 let elapsed = Date().timeIntervalSince(start)95 let duration = (try? AudioFileReader(url: URL(fileURLWithPath: audio)).duration) ?? 096 let wer = TranscriptionScoring.wordErrorRate(reference: ref, hypothesis: transcript.plainText)97 let cer = TranscriptionScoring.characterErrorRate(reference: ref, hypothesis: transcript.plainText)98 result.merge(["engine": transcript.engine, "audioSeconds": duration, "elapsedSeconds": elapsed, "realTimeFactor": duration / max(elapsed, 1e-6),99 "segments": transcript.segments.count, "referenceWords": wer.referenceLength, "wer": wer.rate, "substitutions": wer.substitutions,100 "deletions": wer.deletions, "insertions": wer.insertions, "cer": cer.rate,101 "timedWords": transcript.segments.reduce(0) { $0 + $1.words.count }, "hypothesis": transcript.plainText]) { $1 }102 } catch { result["error"] = error.localizedDescription }103 if let data = try? JSONSerialization.data(withJSONObject: result, options: [.prettyPrinted, .sortedKeys]) { try? data.write(to: URL(fileURLWithPath: out)) }104 }105}106#endif107