Repository Public License
1//2// ORIX Audio3// File: ORIXAudio/Features/Detail/TranscriptPanel.swift4// Description: Transcript: on-device transcription with progress and cancel, synced highlighting, search, editing, manual speaker labels, copy.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9import ORIXAudioCore10import SwiftUI11import UIKit1213struct TranscriptPanel: View {14 @Environment(AppModel.self) private var model15 @Environment(AppSettings.self) private var settings16 let store: DetailStore17 let playhead: Double18 let onSeek: (Double) -> Void19 @State private var localeIdentifier = ""20 @State private var locales: [Locale] = []21 @State private var path: TranscriptionPath?22 @State private var allowDownload = false23 @State private var search = ""24 @State private var editing: TranscriptSegment?25 @State private var editText = ""26 @State private var copied = false2728 private var recording: Recording { store.recording }2930 var body: some View {31 VStack(spacing: ORIX.Space.m) {32 if let job = model.processing.job(recording.id, .transcription) {33 ORIXCard {34 VStack(alignment: .leading, spacing: 8) {35 ProgressRow(title: "Transcribing on this device…", progress: job.progress) { model.processing.cancel(recording.id, .transcription) }36 Text("You can leave this screen; transcription continues while the app is open.").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)37 }38 }39 }40 if let transcript = store.transcript {41 transcriptView(transcript)42 } else if !model.processing.isRunning(recording.id, .transcription) {43 startCard(title: "No transcript yet")44 }45 }46 .task {47 localeIdentifier = recording.localeIdentifier ?? settings.transcriptionLocaleIdentifier48 locales = await TranscriptionEngine.supportedLocales()49 await refreshPath()50 }51 .onChange(of: localeIdentifier) { _, _ in Task { await refreshPath() } }52 .alert("Edit text", isPresented: Binding(get: { editing != nil }, set: { if !$0 { editing = nil } })) {53 TextField("Text", text: $editText, axis: .vertical)54 Button("Save") { saveEdit() }55 Button("Cancel", role: .cancel) { editing = nil }56 }57 }5859 private func refreshPath() async { if !localeIdentifier.isEmpty { path = await TranscriptionEngine.path(for: Locale(identifier: localeIdentifier)) } }6061 private func startCard(title: LocalizedStringKey) -> some View {62 ORIXCard {63 VStack(alignment: .leading, spacing: 12) {64 SectionTitle(title, symbol: "text.quote")65 Picker("Language", selection: $localeIdentifier) {66 if !locales.contains(where: { $0.identifier(.bcp47) == localeIdentifier }) { Text(localeIdentifier).tag(localeIdentifier) }67 ForEach(locales, id: \.identifier) { Text(Locale.current.localizedString(forIdentifier: $0.identifier) ?? $0.identifier).tag($0.identifier(.bcp47)) }68 }69 switch path {70 case .unavailable:71 Label("This language can't be transcribed on this device. ORIX Audio never sends audio to a server, so no transcript can be made.", systemImage: "exclamationmark.triangle.fill")72 .font(.footnote).foregroundStyle(ORIX.Palette.warning)73 case .speechTranscriberNeedsDownload:74 Toggle("Download the speech model first (needs a connection once; audio is not uploaded)", isOn: $allowDownload).font(.footnote)75 default:76 Label("Runs entirely on this device (\(path?.engineName ?? "…")).", systemImage: "lock.shield").font(.footnote).foregroundStyle(ORIX.Palette.textSecondary)77 }78 if let error = model.processing.errors[recording.id] { Text(error).font(.footnote).foregroundStyle(ORIX.Palette.record) }79 Button("Transcribe", systemImage: "waveform.badge.mic") {80 model.processing.transcribeInBackground(recording, locale: Locale(identifier: localeIdentifier), allowModelDownload: allowDownload)81 }82 .buttonStyle(.borderedProminent)83 .disabled(path == .unavailable || path == nil || (path == .speechTranscriberNeedsDownload && !allowDownload))84 .accessibilityIdentifier("transcribeButton")85 }86 }87 }8889 private func transcriptView(_ transcript: Transcript) -> some View {90 let matches = Set(transcript.search(search))91 let current = transcript.segments.lastIndex { $0.start <= playhead + 0.05 }92 return VStack(spacing: ORIX.Space.m) {93 ORIXCard(padding: ORIX.Space.m) {94 VStack(alignment: .leading, spacing: 8) {95 HStack {96 Image(systemName: "magnifyingglass").foregroundStyle(ORIX.Palette.textSecondary)97 TextField("Search the transcript", text: $search).textInputAutocapitalization(.never).autocorrectionDisabled()98 if !search.isEmpty { Text("\(matches.count)").font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary) }99 }100 HStack {101 Chip(text: transcript.engine, symbol: "lock.shield", tint: ORIX.Palette.good)102 Chip(text: Locale.current.localizedString(forIdentifier: transcript.localeIdentifier) ?? transcript.localeIdentifier)103 Spacer()104 Button(copied ? "Copied" : "Copy all", systemImage: copied ? "checkmark" : "doc.on.doc") {105 UIPasteboard.general.string = TranscriptExport.plainText(transcript, timestamps: false)106 copied = true107 Task { try? await Task.sleep(for: .seconds(2)); copied = false }108 }.font(.caption).buttonStyle(.bordered).controlSize(.small)109 }110 Text("Automatic transcripts contain errors. \(transcript.wordCount) words · export from the share menu.").font(.caption2).foregroundStyle(ORIX.Palette.textSecondary)111 }112 }113 LazyVStack(alignment: .leading, spacing: 4) {114 ForEach(Array(transcript.segments.enumerated()), id: \.element.id) { index, segment in115 if search.isEmpty || matches.contains(index) {116 SegmentRow(segment: segment, isCurrent: index == current, highlight: search)117 .contentShape(Rectangle())118 .onTapGesture { onSeek(segment.start) }119 .contextMenu {120 Button("Edit text", systemImage: "pencil") { editText = segment.text; editing = segment }121 Menu("Speaker", systemImage: "person.crop.circle") {122 Button("None") { setSpeaker(nil, for: segment) }123 ForEach(1...6, id: \.self) { n in Button(String(localized: "Speaker \(n)")) { setSpeaker(String(localized: "Speaker \(n)"), for: segment) } }124 }125 Button("Copy", systemImage: "doc.on.doc") { UIPasteboard.general.string = segment.text }126 }127 .accessibilityAction(named: Text("Play from here")) { onSeek(segment.start) }128 }129 }130 }131 startCard(title: "Transcribe again")132 Text("Speakers are not detected automatically: Apple provides no on-device speaker separation API, so labels are assigned by you (long-press a line).")133 .font(.caption2).foregroundStyle(ORIX.Palette.textSecondary)134 }135 }136137 private func saveEdit() {138 guard var transcript = store.transcript, let editing, let i = transcript.segments.firstIndex(where: { $0.id == editing.id }) else { return }139 let text = editText.trimmingCharacters(in: .whitespacesAndNewlines)140 if !text.isEmpty, text != transcript.segments[i].text {141 transcript.segments[i].text = text142 transcript.segments[i].isEdited = true143 transcript.segments[i].words = [] // word timings no longer match edited text144 store.transcript = transcript145 model.processing.saveTranscript(transcript, for: recording)146 }147 self.editing = nil148 }149150 private func setSpeaker(_ name: String?, for segment: TranscriptSegment) {151 guard var transcript = store.transcript, let i = transcript.segments.firstIndex(where: { $0.id == segment.id }) else { return }152 transcript.segments[i].speaker = name153 store.transcript = transcript154 model.processing.saveTranscript(transcript, for: recording)155 }156}157158struct SegmentRow: View {159 let segment: TranscriptSegment160 let isCurrent: Bool161 var highlight = ""162163 private var attributed: AttributedString {164 var s = AttributedString(segment.text)165 let q = highlight.trimmingCharacters(in: .whitespaces)166 if !q.isEmpty, let range = s.range(of: q, options: [.caseInsensitive, .diacriticInsensitive]) {167 s[range].backgroundColor = ORIX.Palette.warning.opacity(0.35)168 }169 return s170 }171172 var body: some View {173 HStack(alignment: .top, spacing: 10) {174 Text(Timecode.clock(segment.start)).font(.orixReadout).foregroundStyle(isCurrent ? ORIX.Palette.cyan : ORIX.Palette.textSecondary).frame(width: 54, alignment: .leading)175 VStack(alignment: .leading, spacing: 3) {176 if let speaker = segment.speaker { Text(speaker).font(.caption.weight(.semibold)).foregroundStyle(ORIX.Palette.violet) }177 Text(attributed).font(.body).fixedSize(horizontal: false, vertical: true)178 if segment.isEdited { Text("Edited").font(.caption2).foregroundStyle(ORIX.Palette.textSecondary) }179 }180 Spacer(minLength: 0)181 }182 .padding(10)183 .background(isCurrent ? ORIX.Palette.cyan.opacity(0.12) : ORIX.Palette.surface, in: RoundedRectangle(cornerRadius: 12, style: .continuous))184 .overlay(alignment: .leading) { if isCurrent { Capsule().fill(ORIX.Palette.cyan).frame(width: 3).padding(.vertical, 8) } }185 .accessibilityElement(children: .combine)186 .accessibilityAddTraits(isCurrent ? .isSelected : [])187 }188}189