知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
feat(ios): remember people by their voice, so later meetings name them, with a People list to manage it
wquguru70fe04a0add8
diff --git a/ios/Sources/Cicada/PeoplePage.swift b/ios/Sources/Cicada/PeoplePage.swiftnew file mode 100644index 0000000..2a51aaa--- /dev/null+++ b/ios/Sources/Cicada/PeoplePage.swift@@ -0,0 +1,154 @@+import SwiftUI++/// 记住的人, as the Mac's 人员库 (SpeakerLibraryView, AppModel.rememberSpeaker): a voice in a+/// meeting is remembered only when the user asks and confirms (voiceprints are biometric data),+/// from the meeting's own recording; later meetings name it as soon as it is heard. Everything stays+/// on this phone and every row can be deleted.+extension AppModel {+ /// The confirmation's question, and then the voiceprint built from the meeting's recording.+ func rememberSpeaker(session id: String, voice: Int, name: String) {+ guard let s = store.session(id) else { return }+ let chosen = name.trimmingCharacters(in: .whitespacesAndNewlines)+ guard !chosen.isEmpty else { speakerProblem = (L("先给这个人起个名字"), L("在“是谁”里输入名字后,再记住这个声音。")); return }+ // An imported file's own audio, else the meeting's recording (still partial while it goes on).+ let fm = FileManager.default+ let audio = store.systemAudioURL(s.id), partial = AudioFileWriter.partial(audio)+ let source = s.sourceFile != nil ? s.sourcePath.map(Self.inThisContainer) : nil+ let url = [source, audio, partial].compactMap { $0 }.first { fm.fileExists(atPath: $0.path) }+ guard let url else {+ speakerProblem = (L("没有可用的录音"), L("打开“保存会议录音”,或用导入的录音文件建立声纹。"))+ return+ }+ speakerMemoryBusy = true+ Task {+ defer { speakerMemoryBusy = false }+ do {+ let result = try await SpeakerEnrollment.build(session: s, voice: voice, audioURL: url)+ let existing = s.voiceProfiles?[String(voice)]?.id+ var profile: SpeakerProfile?+ if let existing, profiles.updateEmbedding(id: existing, embedding: result.embedding) {+ _ = profiles.rename(id: existing, to: chosen)+ profile = profiles.snapshot().first { $0.id == existing }+ } else {+ profile = profiles.remember(name: chosen, embedding: result.embedding)+ }+ guard let profile else {+ speakerProblem = (L("这个声音还不够清楚"), L("换一段更长、更安静的发言再试。"))+ return+ }+ store.setVoiceProfile(id, voice: voice, ref: VoiceProfileRef(id: profile.id, name: profile.name, similarity: 1))+ profiles.markSeen(id: profile.id)+ if id == store.liveID { room.bind(voice: voice, profile: profile) }+ speakerProfiles = profiles.all()+ debugLog("voice profiles: remembered \(chosen) from \(result.clips) clips / \(Int(result.seconds)) s")+ } catch {+ speakerProblem = (L("没记住这个声音"), error.localizedDescription)+ }+ }+ }++ /// An imported file's stored path under the app's container as it is now: the container's folder+ /// changes when the app is reinstalled (and can on an update), the part inside it doesn't.+ static func inThisContainer(_ path: String) -> URL {+ let marker = "/Library/Application Support/"+ guard !FileManager.default.fileExists(atPath: path), let r = path.range(of: marker) else { return URL(fileURLWithPath: path) }+ return URL(fileURLWithPath: NSHomeDirectory() + marker + path[r.upperBound...])+ }++ func forgetProfile(_ id: UUID) {+ profiles.delete(id: id)+ speakerProfiles = profiles.all()+ debugLog("voice profiles: deleted one, \(speakerProfiles.count) left")+ }++ func forgetAllProfiles() {+ profiles.deleteAll()+ speakerProfiles = []+ debugLog("voice profiles: deleted all")+ }+}++/// 设置 › 人员库: who is remembered, a note for each (公司、职位、下次要记住的事), delete one or all.+struct PeoplePage: View {+ @EnvironmentObject var model: AppModel+ @State private var confirmAll = false+ @State private var editing: SpeakerProfile?+ @State private var noteDraft = ""++ var body: some View {+ ScrollView {+ VStack(alignment: .leading, spacing: 0) {+ if model.speakerProfiles.isEmpty {+ Card {+ VStack(alignment: .leading, spacing: 6) {+ Text("还没有记住的人").font(.headline).foregroundStyle(Ink.ink)+ Text("在会议里点一个说话人,选“记住这个声音”,下次就会自动认出。")+ .font(.footnote).foregroundStyle(Ink.secondary).fixedSize(horizontal: false, vertical: true)+ }+ .padding(16).frame(maxWidth: .infinity, alignment: .leading)+ }+ .padding(.top, 12)+ } else {+ Card {+ ForEach(Array(model.speakerProfiles.enumerated()), id: \.element.id) { i, p in+ if i > 0 { Hairline() }+ row(p)+ }+ }+ .padding(.top, 12)+ }+ Text("声纹只保存在这台手机;可以写备注,也可以删除。")+ .font(.footnote).foregroundStyle(Ink.secondary).padding(.horizontal, 4).padding(.top, 10)+ if !model.speakerProfiles.isEmpty {+ Button("全部删除", role: .destructive) { confirmAll = true }+ .font(.subheadline).padding(.horizontal, 4).padding(.top, 14)+ }+ }+ .padding(.horizontal, 16).padding(.bottom, 120)+ .frame(maxWidth: 640).frame(maxWidth: .infinity)+ }+ .background(Ink.ground)+ .navigationTitle(Text("人员库"))+ .navigationBarTitleDisplayMode(.inline)+ .onAppear { model.speakerProfiles = model.profiles.all() }+ .confirmationDialog(Text("全部删除"), isPresented: $confirmAll, titleVisibility: .visible) {+ Button("全部删除", role: .destructive) { model.forgetAllProfiles() }+ } message: {+ Text("之后所有会议都不会再自动认出人;已有记录保持原样。")+ }+ .alert(Text(verbatim: editing?.name ?? ""), isPresented: Binding(get: { editing != nil }, set: { if !$0 { editing = nil } })) {+ TextField(L("备注(公司、职位、下次要记住的事)"), text: $noteDraft)+ Button("保存") {+ if let p = editing { _ = model.profiles.setNotes(id: p.id, noteDraft); model.speakerProfiles = model.profiles.all() }+ editing = nil+ }+ Button("取消", role: .cancel) { editing = nil }+ }+ }++ private func row(_ p: SpeakerProfile) -> some View {+ HStack(spacing: 12) {+ Text(verbatim: String(p.name.prefix(1)).uppercased()).font(.subheadline.weight(.semibold)).foregroundStyle(.white)+ .frame(width: 36, height: 36).background(Ink.ink, in: Circle())+ VStack(alignment: .leading, spacing: 2) {+ Text(verbatim: p.name).font(.body.weight(.semibold)).foregroundStyle(Ink.ink)+ Text(verbatim: p.notes?.isEmpty == false ? p.notes! : seen(p)).font(.caption).foregroundStyle(Ink.secondary).lineLimit(1)+ }+ Spacer(minLength: 8)+ Menu {+ Button { noteDraft = p.notes ?? ""; editing = p } label: { Label(L("备注(公司、职位、下次要记住的事)"), systemImage: "note.text") }+ Button(role: .destructive) { model.forgetProfile(p.id) } label: { Label(L("删除"), systemImage: "trash") }+ } label: {+ Image(systemName: "ellipsis").font(.body.weight(.semibold)).foregroundStyle(Ink.secondary)+ .frame(width: 44, height: 44).contentShape(Rectangle())+ }+ .accessibilityLabel(Text(verbatim: p.name))+ }+ .padding(.leading, 16).padding(.trailing, 6).frame(minHeight: 60)+ }++ private func seen(_ p: SpeakerProfile) -> String {+ guard let at = p.lastSeenAt, at > p.createdAt.addingTimeInterval(60) else { return L("还没在新会议里出现") }+ return dateText(at, "MMMd")+ }+}diff --git a/ios/Sources/Cicada/RecordView.swift b/ios/Sources/Cicada/RecordView.swiftindex c8928e6..749f120 100644--- a/ios/Sources/Cicada/RecordView.swift+++ b/ios/Sources/Cicada/RecordView.swift@@ -207,6 +207,8 @@ struct SpeakerChips: View {@Binding var picked: Int?@AppStorage(Pref.voices) private var voicesOn = true@State private var draft = ""+ /// 记住这个声音: the voice and the name, waiting for the user to confirm.+ @State private var remembering: (voice: Int, name: String)?/// The speaker number being named; on the model so a debug command can open it too.private var naming: Int? { model.namingSpeaker }@@ -222,6 +224,9 @@ struct SpeakerChips: View {// Tap picks the speaker (only they stay in ink); a long press renames or merges.Menu {Button { draft = names[n] ?? ""; model.namingSpeaker = n } label: { Label("改名字", systemImage: "pencil") }+ if let v = byNumber[n] {+ Button { remembering = (v, names[n] ?? "") } label: { Label("记住这个声音", systemImage: "person.wave.2") }+ }Section("其实是同一个人") {ForEach(numbers.filter { $0 != n }, id: \.self) { other inButton(L("并入 \(SpeakerLabels.label(other, names))")) {@@ -248,6 +253,17 @@ struct SpeakerChips: View {}}.onChange(of: model.namingSpeaker) { if let n = model.namingSpeaker, draft.isEmpty { draft = names[n] ?? "" } }+ .alert(Text(L("记住“\(remembering?.name ?? "")”的声音?")), isPresented: Binding(get: { remembering != nil }, set: { if !$0 { remembering = nil } })) {+ Button("记住") { if let r = remembering { model.rememberSpeaker(session: session.id, voice: r.voice, name: r.name) }; remembering = nil }+ Button("不用", role: .cancel) { remembering = nil }+ } message: {+ Text("请先确认对方知道。声纹只保存在这台手机,用于以后会议认出这个人,可随时在人员库里删除。")+ }+ .alert(Text(verbatim: model.speakerProblem?.0 ?? ""), isPresented: Binding(get: { model.speakerProblem != nil }, set: { if !$0 { model.speakerProblem = nil } })) {+ Button("好") { model.speakerProblem = nil }+ } message: {+ Text(verbatim: model.speakerProblem?.1 ?? "")+ }.alert(Text("是谁"), isPresented: Binding(get: { naming != nil }, set: { if !$0 { model.namingSpeaker = nil } })) {TextField(L("输入名字"), text: $draft)Button("保存") { save(byNumber) }diff --git a/ios/Sources/Cicada/RoomListen.swift b/ios/Sources/Cicada/RoomListen.swiftindex 96515b0..996407b 100644--- a/ios/Sources/Cicada/RoomListen.swift+++ b/ios/Sources/Cicada/RoomListen.swift@@ -40,6 +40,8 @@ final class RoomListen: ObservableObject {/// A line was written or its words changed (a joined sentence): the word list's known/// misheard forms are fixed at once (`LiveFixes.applyKnown`).var onHeard: ((_ session: String, _ lineID: String) -> Void)?+ /// A meeting was stored as ended (存到文件夹 writes it once its notes are done).+ var onEnded: ((_ session: String) -> Void)?/// Debug (`silentListen`): a meeting with no microphone and no recognizer, fed by `heard:`.static var silent = falseprivate var silentClock = 0.0@@ -73,8 +75,23 @@ final class RoomListen: ObservableObject {self.store.setVoice(lineID: lineID, voice)if let i = self.lines.firstIndex(where: { $0.id == lineID }) { self.lines[i].voice = voice }}+ // 记住的人: a remembered voice is named as soon as it is heard, as on the Mac.+ speakers.onProfile = { [weak self] lineID, voice, match in+ guard let self, let id = self.store.liveID else { return }+ self.store.setVoice(lineID: lineID, voice)+ self.store.setVoiceProfile(id, voice: voice, ref: match.ref)+ self.profiles?.markSeen(id: match.profile.id)+ if let i = self.lines.firstIndex(where: { $0.id == lineID }) { self.lines[i].voice = voice }+ debugLog("voice profiles: heard \(match.profile.name) (\(String(format: "%.2f", match.similarity)))")+ }}+ /// 记住的人 (PeoplePage.swift): voices remembered by voiceprint, matched while listening.+ var profiles: SpeakerProfileStore? { didSet { speakers.profiles = profiles } }++ /// A voice just remembered in the meeting still going: it keeps that name from now on.+ func bind(voice: Int, profile: SpeakerProfile) { speakers.bind(voice: voice, profile: profile) }+/// Distinct voices heard so far.var speakerCount: Int { Set(lines.compactMap(\.voice)).count }@@ -268,7 +285,9 @@ final class RoomListen: ObservableObject {reader?.cancel()notes.finish()// A meeting with no line is kept only when the microphone heard voices (its recording holds them).+ let ended = sessionIDstore.end(at: endedAt, heardSpeech: heardVoice)+ if let ended, store.session(ended) != nil { onEnded?(ended) }debugLog("room: stopped lines=\(lines.count)")sessionID = nilstartedAt = nil