知了 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 system audio permission is one three-state value instead of two flags
wqugurucc31cec70369
diff --git a/Sources/Cicada/AppModel.swift b/Sources/Cicada/AppModel.swiftindex 0becd4b..9d86a93 100644--- a/Sources/Cicada/AppModel.swift+++ b/Sources/Cicada/AppModel.swift@@ -112,10 +112,9 @@ final class AppModel: ObservableObject {if settingsSection != oldValue { voicePickerOpen = false }}}- @Published var systemAudioOK = false- /// 系统音频录制 was refused. Turned on later in System Settings, it only takes once Cicada is- /// reopened (macOS says so too), so the guide offers that.- @Published var systemAudioRefused = false+ @Published private(set) var systemAudio = SystemAudioAccess.unknown+ var systemAudioOK: Bool { systemAudio == .granted }+ var systemAudioRefused: Bool { systemAudio == .refused }/// Quitting to reopen (`relaunch`): closing the guide now isn't finishing it.var relaunching = false/// Speech recognition can run: always on macOS 26; below it, Dictation on and 语音识别 allowed.@@ -436,7 +435,7 @@ final class AppModel: ObservableObject {func refresh() {running = listen.isRunning// Once the tap has run, the permission is there: pausing doesn't take it away.- systemAudioOK = systemAudioOK || (listen.isRunning && !settings.listensToRoom)+ if listen.isRunning && !settings.listensToRoom && systemAudio != .granted { systemAudio = .granted }summary = settings.listensToRoom ? L("麦克风 · \(listen.languageSummary())"): settings.hearsBoth ? L("电脑和麦克风 · \(listen.languageSummary())") : listen.languageSummary()candidates = listen.candidates@@ -475,13 +474,10 @@ final class AppModel: ObservableObject {checkProblem()let devices = AudioDevices.outputs()if devices.map(\.uid) != outputs.map(\.uid) { outputs = devices }- if listen.isRunning && !settings.listensToRoom && !systemAudioOK { systemAudioOK = true }// Refused (in the prompt, or turned off in System Settings later): the tap would only hear silence.- if let p = SystemAudioPermission.status() {- let ok = p == .granted || (p == .unknown && systemAudioOK)- if ok != systemAudioOK { systemAudioOK = ok }- if (p == .denied) != systemAudioRefused { systemAudioRefused = p == .denied }- }+ let access = systemAudio.next(status: SystemAudioPermission.status(),+ tapRan: listen.isRunning && !settings.listensToRoom)+ if access != systemAudio { systemAudio = access }let speech = Recognition.readyif speech != speechOK { speechOK = speech }refreshMicUsers()diff --git a/Sources/Cicada/SystemAudioTap.swift b/Sources/Cicada/SystemAudioTap.swiftindex f268446..e56225e 100644--- a/Sources/Cicada/SystemAudioTap.swift+++ b/Sources/Cicada/SystemAudioTap.swift@@ -28,6 +28,25 @@ enum SystemAudioPermission {}}+/// What the guide knows of 系统音频录制. Refused, turning it on later in System Settings only takes+/// once Cicada is reopened (macOS says so too), so the guide offers that.+enum SystemAudioAccess: Equatable {+ case unknown, granted, refused++ /// `tapRan`: listening to the system audio (not the room), which only runs once allowed; pausing+ /// doesn't take that away. `status`: what TCC says, nil when it can't be read.+ func next(status: SystemAudioPermission.Status?, tapRan: Bool) -> Self {+ let now: Self = tapRan ? .granted : self+ switch status {+ case nil: return now+ case .granted: return .granted+ case .denied: return .refused+ // Not yet asked as far as TCC can tell: a tap that ran says otherwise.+ case .unknown: return now == .granted ? .granted : .unknown+ }+ }+}+final class SystemAudioTap {/// Called on the real-time audio thread. Must not block.var onAudio: ((AVAudioPCMBuffer) -> Void)?diff --git a/Tests/CicadaTests/StateMachineTests.swift b/Tests/CicadaTests/StateMachineTests.swiftnew file mode 100644index 0000000..eb9aa79--- /dev/null+++ b/Tests/CicadaTests/StateMachineTests.swift@@ -0,0 +1,23 @@+import Foundation+import Testing+@testable import Cicada++/// The small state machines pulled out of the engines and views: each transition as the code relied on it.+@Suite struct SystemAudioAccessTests {+ @Test func followsWhatTCCSays() {+ #expect(SystemAudioAccess.unknown.next(status: .granted, tapRan: false) == .granted)+ #expect(SystemAudioAccess.granted.next(status: .denied, tapRan: false) == .refused)+ #expect(SystemAudioAccess.refused.next(status: .granted, tapRan: false) == .granted)+ #expect(SystemAudioAccess.refused.next(status: .unknown, tapRan: false) == .unknown)+ }++ @Test func aTapThatRanMeansAllowed() {+ #expect(SystemAudioAccess.unknown.next(status: nil, tapRan: true) == .granted)+ #expect(SystemAudioAccess.unknown.next(status: .unknown, tapRan: true) == .granted)+ // TCC unreadable or undecided: what was known stays, pausing doesn't take it away.+ #expect(SystemAudioAccess.granted.next(status: .unknown, tapRan: false) == .granted)+ #expect(SystemAudioAccess.refused.next(status: nil, tapRan: false) == .refused)+ // TCC's no wins over a tap that ran before it was turned off.+ #expect(SystemAudioAccess.granted.next(status: .denied, tapRan: true) == .refused)+ }+}