diff --git a/.gitignore b/.gitignore index f81ce98..7140312 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Swift / Xcode build output(mac / Core / mobile どこでも) .build/ +.swiftpm/ *.app/ DerivedData/ *.xcuserstate diff --git a/mobile/Sources/AppModel.swift b/mobile/Sources/AppModel.swift index cc92d26..a2a6711 100644 --- a/mobile/Sources/AppModel.swift +++ b/mobile/Sources/AppModel.swift @@ -53,7 +53,7 @@ final class AppModel { t?.stream(buffer) segmenter?.feed(buffer) } - try mic.start() + try mic.start(voiceProcessing: Config.voiceProcessingEnabled) self.mic = mic self.segmenter = segmenter self.micStatus = "録音中" diff --git a/mobile/Sources/Config.swift b/mobile/Sources/Config.swift index b3f7f4a..4657d30 100644 --- a/mobile/Sources/Config.swift +++ b/mobile/Sources/Config.swift @@ -7,6 +7,7 @@ enum Config { static let tokenKey = "token" // Bearer トークン static let deviceIdKey = "deviceId" // 送信元の識別子(サーバ側 dedup キーの一部) static let diarEnabledKey = "diarEnabled" // 話者ターンで発話を区切るか(既定 on) + static let voiceProcessingKey = "voiceProcessing" // Apple のノイズ抑制+AGC(既定 off、実験) static let defaultDeviceId = "iphone" @@ -21,4 +22,9 @@ enum Config { static var diarEnabled: Bool { UserDefaults.standard.object(forKey: diarEnabledKey) as? Bool ?? true } + + /// Apple の voice processing(ノイズ抑制 + AGC)。未設定なら off(実験用トグル)。 + static var voiceProcessingEnabled: Bool { + UserDefaults.standard.bool(forKey: voiceProcessingKey) + } } diff --git a/mobile/Sources/MicCapturer.swift b/mobile/Sources/MicCapturer.swift index cab91b9..001b5b7 100644 --- a/mobile/Sources/MicCapturer.swift +++ b/mobile/Sources/MicCapturer.swift @@ -18,6 +18,10 @@ final class MicCapturer: @unchecked Sendable { private let onBuffer: @Sendable (AVAudioPCMBuffer) -> Void private var engine = AVAudioEngine() + // Apple の voice processing(ノイズ抑制 + AGC)。ノイズ床を下げる実験用。restart 後も維持する + // よう保持する。近接 1 話者前提のチューニングなので遠い話者を抑える副作用がありうる(トグルで A/B)。 + private var voiceProcessing = false + // engine の生成/差し替えは全てこのキュー上でだけ行う(watchdog と通知ハンドラの競合を直列化)。 private let engineQueue = DispatchQueue(label: "com.m5d215.steno-mobile.mic.engine") @@ -30,14 +34,17 @@ final class MicCapturer: @unchecked Sendable { self.onBuffer = onBuffer } - func start() throws { + func start(voiceProcessing: Bool) throws { + self.voiceProcessing = voiceProcessing let session = AVAudioSession.sharedInstance() - try session.setCategory(.record, mode: .default, options: []) + // voice processing(AEC/NS/AGC の IO unit)は playAndRecord を要する。素の録音は record。 + let category: AVAudioSession.Category = voiceProcessing ? .playAndRecord : .record + try session.setCategory(category, mode: .default, options: []) try session.setActive(true) try startEngine() registerObservers() startWatchdog() - ilog("mic started") + ilog("mic started (voiceProcessing=\(voiceProcessing))") } func stop() { @@ -54,7 +61,15 @@ final class MicCapturer: @unchecked Sendable { private func startEngine() throws { let input = engine.inputNode - let format = input.outputFormat(forBus: 0) // 実 hardware format。固定しない。 + // voice processing は engine 起動前に有効化する。format はこの後に読む(VP 後の実 format)。 + if voiceProcessing { + do { + try input.setVoiceProcessingEnabled(true) + } catch { + ilog("mic: voice processing unavailable: \(error.localizedDescription)") + } + } + let format = input.outputFormat(forBus: 0) // 実 format。固定しない(VP で変わる)。 input.removeTap(onBus: 0) let onBuffer = self.onBuffer let lastBufferAt = self.lastBufferAt diff --git a/mobile/Sources/SettingsView.swift b/mobile/Sources/SettingsView.swift index c269de2..66a1fef 100644 --- a/mobile/Sources/SettingsView.swift +++ b/mobile/Sources/SettingsView.swift @@ -7,6 +7,7 @@ struct SettingsView: View { @AppStorage(Config.tokenKey) private var token = "" @AppStorage(Config.deviceIdKey) private var deviceId = "" @AppStorage(Config.diarEnabledKey) private var diarEnabled = true + @AppStorage(Config.voiceProcessingKey) private var voiceProcessing = false var body: some View { NavigationStack { @@ -46,6 +47,16 @@ struct SettingsView: View { Text("複数人の会話で、話者が変わった点で発話を分ける(オンデバイス)。" + "off だと間(無音)でしか切れず、掛け合いは 1 行に merge される。変更は次回の開始から反映。") } + + Section { + Toggle("ノイズ抑制(実験)", isOn: $voiceProcessing) + } header: { + Text("収音") + } footer: { + Text("Apple の voice processing(ノイズ抑制 + AGC)。屋外・風・騒音下のノイズ床を" + + "下げる狙い。近接 1 話者前提のチューニングなので、遠い相手の声を抑え込むことがある。" + + "屋外の散歩など騒がしい場面で on/off を A/B するとよい。変更は次回の開始から反映。") + } } .navigationTitle("設定") .navigationBarTitleDisplayMode(.inline)