Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Swift / Xcode build output(mac / Core / mobile どこでも)
.build/
.swiftpm/
*.app/
DerivedData/
*.xcuserstate
Expand Down
2 changes: 1 addition & 1 deletion mobile/Sources/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "録音中"
Expand Down
6 changes: 6 additions & 0 deletions mobile/Sources/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}
}
23 changes: 19 additions & 4 deletions mobile/Sources/MicCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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() {
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions mobile/Sources/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Loading