diff --git a/mac/Sources/steno/AudioTapCapturer.swift b/mac/Sources/steno/AudioTapCapturer.swift index 0f76c74..83f30a8 100644 --- a/mac/Sources/steno/AudioTapCapturer.swift +++ b/mac/Sources/steno/AudioTapCapturer.swift @@ -81,6 +81,16 @@ final class AudioTapCapturer: @unchecked Sendable { private let deviceRate = OSAllocatedUnfairLock(initialState: Double(0)) // tap を作った時の出力デバイスの nominal rate private let outDevice = OSAllocatedUnfairLock(initialState: AudioObjectID(kAudioObjectUnknown)) // tap が乗る出力デバイス + // silent-death 検知(恒久)。tap は buffer を出し続けるのに中身が digital zero で張り付く死に方を + // 拾う。生きた経路はノイズフロアで rms が必ず非ゼロなので、「ちょうど 0( 0 ? (sum / Float(n)).squareRoot() : 0 + lastRMS.withLock { $0 = rms } + if rms >= silenceEpsilon { lastSignalAt.withLock { $0 = Date() } } + } + lastBufferAt.withLock { $0 = Date() } onBuffer(pcm) } @@ -296,6 +322,7 @@ final class AudioTapCapturer: @unchecked Sendable { try? await Task.sleep(for: .seconds(5)) guard let self else { break } let now = Date() + let silentFor = now.timeIntervalSince(self.lastSignalAt.withLock { $0 }) // (1) frames-stall: バッファ自体が来ない = tap が完全に死んでる let idle = now.timeIntervalSince(self.lastBufferAt.withLock { $0 }) @@ -305,6 +332,22 @@ final class AudioTapCapturer: @unchecked Sendable { continue } + // (1b) silent-death: buffer は来てる(idle 小、frames-stall は上で処理済み)のに tap の + // 中身が digital zero で張り付く死に方(実測)。生きた経路はノイズフロアで rms が必ず + // 非ゼロなので、誰かが出力中なのに厳密ゼロが silentDeathThreshold 続いたら経路が死んでいる。 + // 前版は「rms < 閾値」で静かな生音まで殺して誤発火した(1日679回、発話取りこぼし)。ここは + // 「厳密ゼロ( self.silentDeathThreshold, sinceSilentRecreate > self.silentDeathBackoff, + !OutputProcesses.currentOutputAppNames().isEmpty + { + self.lastSilentRecreateAt.withLock { $0 = now } + ilog("watchdog: system tap silent (digital zero) \(Int(silentFor))s while output active, recreating tap") + self.requestRecreate(reason: "silent-death \(Int(silentFor))s") + continue + } + // (2) device-rate reconciliation: 出力デバイスの nominal rate が tap 作成時から変わった // = BT プロファイル切り替え等の実イベント。format listener が取りこぼしても 5 秒以内に拾う // backstop。比較は「デバイスの今 vs 作成時」(同レイヤー)なので、recreate 後に基準が更新