知了 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
refactor: the room mic's silence watch is one state (waiting, silent since, settled)
wquguru2797142dd048
diff --git a/Sources/Cicada/RoomMic.swift b/Sources/Cicada/RoomMic.swiftindex 3a5df5d..69cbce0 100644--- a/Sources/Cicada/RoomMic.swift+++ b/Sources/Cicada/RoomMic.swift@@ -312,8 +312,7 @@ final class RoomMic: AudioSource {func start() throws {startedAt = Date()- silentSince = nil- silenceChecked = false+ silence = .waitingAudioDevices.pinInput(engine, to: device)let input = engine.inputNode// The hardware format: after pinning, the client format can still be the system mic's,@@ -381,23 +380,29 @@ final class RoomMic: AudioSource {nonisolated(unsafe) static var simulateSilence = false/// Called on the main queue when the mic stayed silent after being opened again.var onSilent: (() -> Void)?- private var silentSince: Date?- private var silentRestarts = 0- private var silenceChecked = false+ /// Per opening of the mic, on the audio thread: whether it has been silent since it started.+ private enum SilenceWatch { case waiting, silentSince(Date), settled }+ private var silence = SilenceWatch.waiting+ /// Opened again once already for silence (main queue): the next time it is reported instead.+ private var reopenedForSilence = false/// On the audio thread, per block: a mic silent since it started is opened again once, and/// reported (`onSilent`) when it is still silent after that.private func watchSilence(sound: Bool) {- guard !silenceChecked else { return }- if sound { silenceChecked = true; return }- let since = silentSince ?? Date()- silentSince = since+ let since: Date+ switch silence {+ case .settled: return+ case .waiting: since = Date()+ case .silentSince(let d): since = d+ }+ if sound { silence = .settled; return }+ silence = .silentSince(since)guard Date().timeIntervalSince(since) >= Self.silentAfter else { return }- silenceChecked = true+ silence = .settledDispatchQueue.main.async { [weak self] inguard let self, !self.stopped else { return }- if self.silentRestarts == 0 {- self.silentRestarts += 1+ if !self.reopenedForSilence {+ self.reopenedForSilence = trueself.startedAt = .distantPastself.restart("silent since it started")} else {