知了 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
fix: keep sentence starts after a language switch, size the bar to its text, never speak untranslated words
wquguru0411dfc36f54
diff --git a/Sources/LiveSubs/ListenEngine.swift b/Sources/LiveSubs/ListenEngine.swiftindex 31ef701..282f393 100644--- a/Sources/LiveSubs/ListenEngine.swift+++ b/Sources/LiveSubs/ListenEngine.swift@@ -254,7 +254,7 @@ final class ListenEngine {await analyzer.cancelAndFinishNow()return}- let (stream, continuation) = AsyncStream.makeStream(of: AnalyzerInput.self, bufferingPolicy: .bufferingNewest(400))+ let (stream, continuation) = AsyncStream.makeStream(of: AnalyzerInput.self, bufferingPolicy: .unbounded)router.attach(format: format, continuation: continuation, replaySeconds: replaySeconds)self.analyzer = analyzerresultsTask = Task { [weak self] in@@ -395,7 +395,7 @@ final class ListenEngine {return}let currentScore = ranked.first(where: { $0.lang == activeLang })?.score ?? 0- let overwhelming = best.score - currentScore >= 0.35+ let overwhelming = best.score - currentScore >= 0.30if !languageConfirmed || overwhelming || pendingSwitch == best.lang {debugLog("detect: switching \(activeLang?.locale ?? "-") → \(best.lang.locale)")languageConfirmed = truediff --git a/Sources/LiveSubs/Overlay.swift b/Sources/LiveSubs/Overlay.swiftindex f9dac93..a27aa76 100644--- a/Sources/LiveSubs/Overlay.swift+++ b/Sources/LiveSubs/Overlay.swift@@ -67,6 +67,11 @@ final class OverlayController {let changed = source != view.source || translation != view.translationview.source = sourceview.translation = translation+ let lines = view.neededLines(barWidth: width)+ if lines != view.transLines, view.dragMode == nil {+ view.transLines = lines+ layout()+ }view.needsDisplay = trueif changed && !(source.isEmpty && translation.isEmpty) {lastContentAt = Date()@@ -429,6 +434,8 @@ final class OverlayView: NSView {var hover = falsevar snapped = falsevar collapsed = false+ /// Translation lines the bar is sized for (1 or 2), so a short line leaves no gap.+ var transLines = 1var dragMode: Bool?private let orange = NSColor(srgbRed: 1.0, green: 0.71, blue: 0.62, alpha: 1)@@ -447,7 +454,7 @@ final class OverlayView: NSView {func barHeight(fontSize fs: CGFloat) -> CGFloat {let pad = 14 * k(fs)- var h = pad * 2 + line(srcFont(fs)) + 6 * k(fs) + line(transFont(fs)) * 2+ var h = pad * 2 + line(srcFont(fs)) + 6 * k(fs) + line(transFont(fs)) * CGFloat(transLines)if speaking != nil {h += 12 * k(fs) + line(srcFont(fs)) + line(speakFont(fs)) * 2}@@ -511,7 +518,7 @@ final class OverlayView: NSView {draw(srcText, font: srcFont(fs), color: NSColor.white.withAlphaComponent(0.62), maxLines: 1)y -= 6 * k(fs)draw(translation, font: transFont(fs),- color: isStatus ? NSColor.white.withAlphaComponent(0.7) : .white, maxLines: 2)+ color: isStatus ? NSColor.white.withAlphaComponent(0.7) : .white, maxLines: transLines)if let s = speaking {y -= 12 * k(fs)@@ -539,6 +546,14 @@ final class OverlayView: NSView {}}+ /// How many lines the translation needs at the current width (1 or 2).+ func neededLines(barWidth: CGFloat) -> Int {+ let fs = fontSize+ let attrs: [NSAttributedString.Key: Any] = [.font: transFont(fs)]+ let w = barWidth - 2 * 26 * k(fs)+ return height(of: translation, attrs: attrs, width: w) > line(transFont(fs)) * 1.3 ? 2 : 1+ }+private func height(of s: String, attrs: [NSAttributedString.Key: Any], width: CGFloat) -> CGFloat {ceil((s as NSString).boundingRect(with: NSSize(width: width, height: .greatestFiniteMagnitude),options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attrs).height)diff --git a/Sources/LiveSubs/SpeakEngine.swift b/Sources/LiveSubs/SpeakEngine.swiftindex 8fc7cd0..90783d4 100644--- a/Sources/LiveSubs/SpeakEngine.swift+++ b/Sources/LiveSubs/SpeakEngine.swift@@ -342,7 +342,14 @@ final class SpeakEngine {let from = settings.speakFromLang.codelet to = targetCodeonLive?(.translating, text, "")- let translated = await translate?(text, from, to) ?? text+ // Never speak the untranslated original into the room: if translation+ // fails, say so on the bar and send nothing.+ guard let translated = await translate?(text, from, to) else {+ debugLog("speak: translation failed, nothing sent")+ phase = .idle+ onLive?(.idle, text, "翻译失败,这句没有发出")+ return+ }contentLog("SPEAK [\(from)→\(to)] \(text) => \(translated)")onUtterance?(text, translated, from, to, Date().timeIntervalSince(pressStarted))phase = .speaking