Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

feat: speech recognition can go to your own server instead of the Mac, and falls back to the Mac when that server stops answering

wquguru0eea4471f2cb
diff --git a/Sources/Cicada/CloudHearing.swift b/Sources/Cicada/CloudHearing.swift
index 77de2c5..26bec1f 100644
--- a/Sources/Cicada/CloudHearing.swift
+++ b/Sources/Cicada/CloudHearing.swift
@@ -69,6 +69,30 @@ enum CloudHearing {
return CloudHeard(lang: code, original: o, translation: t)
}
+ /// What a speech model (`/audio/transcriptions`) heard: the text, in the language the server
+ /// reports ("zh", "chinese"), else the one its script shows, else the meeting's. No translation:
+ /// the text translator does that, as for the Mac's own recognition. nil for nothing or noise.
+ static func transcript(_ text: String, reported: String?, hint: String?, target: String, seconds: Double) -> CloudHeard? {
+ let o = text.trimmingCharacters(in: .whitespacesAndNewlines)
+ guard !o.isEmpty, o.contains(where: { $0.isLetter || $0.isNumber }), !isNoise(o, seconds: seconds) else { return nil }
+ let names = ["chinese": "zh-CN", "mandarin": "zh-CN", "cantonese": "zh-CN", "english": "en", "japanese": "ja",
+ "korean": "ko", "french": "fr", "german": "de", "spanish": "es", "portuguese": "pt", "russian": "ru",
+ "italian": "it"]
+ var code: String
+ if let r = reported?.trimmingCharacters(in: .whitespaces).lowercased(), let named = names[r] {
+ code = named
+ } else if let reported, !reported.isEmpty, normalize(reported, fallback: "") != "" {
+ code = normalize(reported, fallback: "")
+ } else {
+ let scalars = o.unicodeScalars
+ if scalars.contains(where: { (0x3040...0x30FF).contains($0.value) }) { code = "ja" }
+ else if scalars.contains(where: { (0xAC00...0xD7AF).contains($0.value) }) { code = "ko" }
+ else if scalars.contains(where: { (0x4E00...0x9FFF).contains($0.value) }) { code = hint?.hasPrefix("zh") == true ? hint! : "zh-CN" }
+ else { code = hint.map { $0.hasPrefix("zh") || $0 == "ja" || $0 == "ko" ? "en" : $0 } ?? "en" }
+ }
+ return CloudHeard(lang: code, original: o, translation: code == target ? o : nil)
+ }
+
/// What Ming says the language is ("zh", "zh-Hans", "cmn") as a source code.
static func normalize(_ reported: String?, fallback: String) -> String {
guard let reported, !reported.trimmingCharacters(in: .whitespaces).isEmpty else { return fallback }
diff --git a/Sources/Cicada/ListenEngine.swift b/Sources/Cicada/ListenEngine.swift
index 57b6bf3..5e4f6c9 100644
--- a/Sources/Cicada/ListenEngine.swift
+++ b/Sources/Cicada/ListenEngine.swift
@@ -82,11 +82,18 @@ final class ListenEngine {
/// The language the service says is spoken.
private var cloudLang: Lang?
private var cloudFailures = 0
+ /// The user's own service stopped answering in this meeting: listening went back to this Mac.
+ private var serverFellBack = false
+ /// The meeting is recognized by the user's own service (`Recognition.serverPreferred`).
+ var serverHears: Bool {
+ Recognition.serverPreferred && !serverFellBack && translator?.ling.transcribes == true
+ }
/// Back on this Mac after the service listened (its name), shown for a few seconds.
private(set) var cloudBack: (name: String, at: Date)?
- /// The service that can hear audio (only Ling's Ming today); nil when there is none.
+ /// The service that can hear audio (Ling's Ming, or a custom service's speech model); nil when
+ /// there is none.
var cloudService: String? {
- guard let ling = translator?.ling, ling.hearsAudio else { return nil }
+ guard let ling = translator?.ling, ling.hearsAudio || ling.transcribes else { return nil }
return ling.config().provider.name
}
/// 先用○○听 is offered: a model listening needs has been downloading a while, or couldn't be had.
@@ -334,6 +341,7 @@ final class ListenEngine {
mismatch = false
resetCloud()
cloudBack = nil
+ serverFellBack = false
startedAt = Date()
macSilent = false
updateHold()
@@ -604,7 +612,7 @@ final class ListenEngine {
private func switchTranscriber(to lang: Lang, replaySeconds: Double = 0) async {
// No models to switch between: the service hears every language, and a language picked in
// settings only becomes its hint (`cloudHint`).
- if Recognition.cloudOnly {
+ if Recognition.cloudOnly || serverHears {
guard isRunning else { return }
if case .fixed = settings.sourceMode { activeLang = lang } else if activeLang == nil { activeLang = lang }
if !cloudListening { startCloud() }
@@ -1129,7 +1137,14 @@ final class ListenEngine {
self.cloudFailures += 1
// The service is down or the key stopped working: listening shouldn't go quiet with it.
if self.cloudFailures >= 3 {
- if !Recognition.cloudOnly { self.stopCloud() }
+ if self.serverHears, !Recognition.cloudOnly {
+ // The user's own service is down: this Mac recognizes the rest of the meeting.
+ debugLog("cloud: own service failed 3 times, back to this Mac for this meeting")
+ self.serverFellBack = true
+ if self.cloudReplaced?.lang == nil { self.cloudReplaced = (self.activeLang, false) }
+ self.stopCloud()
+ self.setStatus(L("\(self.cloudService ?? "AI") 暂时连不上,先用本机识别"))
+ } else if !Recognition.cloudOnly { self.stopCloud() }
// Nothing to go back to on an Intel Mac: say why it's quiet, and keep trying.
else if case LingClient.Failure.invalidKey = error { self.setStatus(L("key 已失效,到设置 › AI 服务里重填")) }
else if case LingClient.Failure.noKey = error { self.setStatus(L("请先在设置 › AI 服务里填入 Ling key")) }
@@ -1193,6 +1208,8 @@ final class ListenEngine {
/// A model came in while the service listened: the awaited one goes back to listening on the Mac.
private func cloudModelArrived() {
+ // The user chose their service: a model arriving is only the fallback getting ready.
+ guard !serverHears else { return }
guard let awaiting = cloudAwaiting, installedLocales.contains(awaiting.locale) else { return }
let want: Lang
if case .fixed(let lang) = settings.sourceMode {
diff --git a/Sources/Cicada/Recognition.swift b/Sources/Cicada/Recognition.swift
index 0556d38..7c63c76 100644
--- a/Sources/Cicada/Recognition.swift
+++ b/Sources/Cicada/Recognition.swift
@@ -26,6 +26,11 @@ enum Recognition {
#endif
}
+ /// The user chose to have the meeting recognized by their own service (Settings › AI 服务 ›
+ /// 语音识别 › 自建服务), set by the app. Unlike `cloudOnly` this Mac still has its engines: they
+ /// stay ready, and listening falls back to them when the service stops answering.
+ nonisolated(unsafe) static var serverPreferred = false
+
/// `cloudOnly`: what the service heard in one stretch of `locale` (nil: any language), set by the
/// app. The live meeting goes through `ListenEngine.startCloud`; this serves imports and clips.
nonisolated(unsafe) static var cloudHear: (@MainActor @Sendable (_ wav: Data, _ seconds: Double, _ locale: String?) async -> CloudHeard?)?
diff --git a/Tests/CicadaTests/CloudHearingTests.swift b/Tests/CicadaTests/CloudHearingTests.swift
index bc2c34c..07a8f1b 100644
--- a/Tests/CicadaTests/CloudHearingTests.swift
+++ b/Tests/CicadaTests/CloudHearingTests.swift
@@ -82,4 +82,20 @@ struct CloudHearingTests {
#expect(cut(tone(3.0, 0.02, syllables: false), silence(1.0)).isEmpty)
#expect(cut(tone(3.0), silence(1.0)).count == 1)
}
+
+ @Test func aTranscriptKeepsItsLanguageAndLeavesTranslationToText() {
+ let zh = CloudHearing.transcript("这个 feature 我们下个 sprint 再做。", reported: nil, hint: "en", target: "en", seconds: 3)
+ #expect(zh?.lang == "zh-CN" && zh?.translation == nil)
+ let en = CloudHearing.transcript("We ship on Friday.", reported: nil, hint: "zh-CN", target: "zh-CN", seconds: 2)
+ #expect(en?.lang == "en" && en?.translation == nil)
+ #expect(CloudHearing.transcript("会議は金曜日です。", reported: nil, hint: nil, target: "zh-CN", seconds: 2)?.lang == "ja")
+ #expect(CloudHearing.transcript("회의는 금요일입니다.", reported: nil, hint: nil, target: "zh-CN", seconds: 2)?.lang == "ko")
+ #expect(CloudHearing.transcript("Hola a todos.", reported: "spanish", hint: nil, target: "zh-CN", seconds: 2)?.lang == "es")
+ #expect(CloudHearing.transcript("Bonjour.", reported: "fr", hint: nil, target: "zh-CN", seconds: 2)?.lang == "fr")
+ // Already in the language it's translated into: shown as it is.
+ let same = CloudHearing.transcript("周五发布。", reported: "zh", hint: nil, target: "zh-CN", seconds: 2)
+ #expect(same?.translation == "周五发布。")
+ #expect(CloudHearing.transcript(" ", reported: nil, hint: nil, target: "en", seconds: 1) == nil)
+ #expect(CloudHearing.transcript("嗯。", reported: nil, hint: nil, target: "en", seconds: 1) == nil)
+ }
}