Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

Voice: robust AEC fallback, logging, gitignore hash, SWIFT_EMIT_LOC_STRINGS

Louis Raille649cbd85cbe6
diff --git a/.gitignore b/.gitignore
index 52c3a580..b753fba6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,3 +19,6 @@ DerivedData/
# Local only (never published)
.claude/
_prive/
+
+# Auto-generated by the Write Git Hash build phase
+NotchBuddy/Sources/App/Voice/GitHash.generated.swift
diff --git a/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj b/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj
index c6f08e5d..9ac9aa23 100644
--- a/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj
+++ b/NotchBuddy/NotchBuddy.xcodeproj/project.pbxproj
@@ -1364,6 +1364,7 @@
OTHER_SWIFT_FLAGS = "-strict-concurrency=complete";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_COMPILATION_MODE = wholemodule;
+ SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 6.0;
};
@@ -1845,6 +1846,7 @@
OTHER_SWIFT_FLAGS = "-strict-concurrency=complete";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
+ SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 6.0;
};
@@ -1960,6 +1962,7 @@
OTHER_SWIFT_FLAGS = "-strict-concurrency=complete";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_COMPILATION_MODE = wholemodule;
+ SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 6.0;
};
@@ -2029,6 +2032,7 @@
OTHER_SWIFT_FLAGS = "-strict-concurrency=complete";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
+ SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 6.0;
};
diff --git a/NotchBuddy/Sources/App/Voice/GitHash.generated.swift b/NotchBuddy/Sources/App/Voice/GitHash.generated.swift
deleted file mode 100644
index 001bf66e..00000000
--- a/NotchBuddy/Sources/App/Voice/GitHash.generated.swift
+++ /dev/null
@@ -1,3 +0,0 @@
-// AUTO-GENERATED by the "Write Git Hash" build phase — do not edit by hand.
-// Regenerated on every build with the short SHA of the current HEAD commit.
-let voiceBuildHash = "8ef1006"
diff --git a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
index 0c0956cd..2a399e87 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
@@ -15,14 +15,16 @@ import AVFoundation
// - Bypass (`bypassVAD = true`): every buffer is delivered via `onBuffer` regardless of
// silence; `onMicLevel` fires at ~20 Hz with a smoothed level.
//
-// AEC: `setVoiceProcessingEnabled(true)` is applied to inputNode before reading its
-// format. VP requires the output I/O unit to supply a valid downlink clock; the default
-// mainMixerNode → outputNode path provides this — we just silence it (outputVolume = 0).
-// `voiceProcessingOtherAudioDuckingConfiguration` prevents VP from ducking other apps.
-// If VP activation fails, the engine continues without echo cancellation.
+// AEC strategy (robust two-attempt start):
+// 1. Enable VP on inputNode BEFORE reading format (VP may renegotiate format/rate).
+// The default mainMixerNode → outputNode path provides the downlink clock VP needs;
+// outputVolume = 0 so Coucou never makes sound.
+// 2. If engine.start() fails with VP active (e.g. kAudioUnitErr_FailedInitialization
+// −10875 on some Macs), log the full error, tear down, recreate AVAudioEngine,
+// and start again WITHOUT VP. The engine must never fail silently.
final class VoiceAudio: @unchecked Sendable {
- private let engine = AVAudioEngine()
+ private var engine = AVAudioEngine()
private var tapInstalled = false
private var configObserver: NSObjectProtocol? = nil
@@ -62,42 +64,32 @@ final class VoiceAudio: @unchecked Sendable {
// MARK: - Control
+ /// Start the audio pipeline. Tries AEC first; falls back to no-AEC on failure.
+ /// Throws only when both attempts fail.
func start() throws {
guard !tapInstalled else { return }
- let input = engine.inputNode
- // AEC: enable voice processing BEFORE reading the input format and installing
- // the tap — VP may renegotiate the format after activation. VP's downlink DSP
- // needs the output I/O unit running; the default mainMixerNode → outputNode
- // connection handles this. Silence the output so Coucou makes no sound.
+ // Attempt 1 — with AEC.
do {
- try input.setVoiceProcessingEnabled(true)
- input.voiceProcessingOtherAudioDuckingConfiguration =
- AVAudioVoiceProcessingOtherAudioDuckingConfiguration(
- enableAdvancedDucking: false, duckingLevel: .min)
- engine.mainMixerNode.outputVolume = 0
+ let fmt = try setupEngine(aec: true)
+ let desc = "\(Int(fmt.sampleRate)) Hz \(fmt.channelCount)ch"
+ appendAppLog("nb.log", "[Voice] engine started (AEC on, \(desc))")
+ return
} catch {
+ let e = error as NSError
appendAppLog("nb.log",
- "[Voice] AEC unavailable: \(error.localizedDescription) — no echo cancellation")
+ "[Voice] engine start failed with AEC (\(e.domain)/\(e.code)): \(e.localizedDescription) — retrying without AEC")
}
- // Read format AFTER VP activation (VP may renegotiate sample rate / channel count).
- let fmt = input.outputFormat(forBus: 0)
- input.installTap(onBus: 0, bufferSize: 1024, format: fmt) { [weak self] buf, time in
- self?.processTap(buf, time: time)
- }
- tapInstalled = true
-
- // Rebuild on device change (headphones, sample-rate switch…).
- configObserver = NotificationCenter.default.addObserver(
- forName: .AVAudioEngineConfigurationChange,
- object: engine, queue: .main
- ) { [weak self] _ in
- self?.onConfigChange?()
- }
+ // Tear down any partially installed state (setupEngine's catch already undoes tap +
+ // observer, but the engine itself may be in a bad state — always recreate it).
+ engine.stop()
+ engine = AVAudioEngine()
- engine.prepare()
- try engine.start()
+ // Attempt 2 — without AEC. Let any error propagate to the caller.
+ let fmt = try setupEngine(aec: false)
+ let desc = "\(Int(fmt.sampleRate)) Hz \(fmt.channelCount)ch"
+ appendAppLog("nb.log", "[Voice] engine started (AEC off, \(desc))")
}
func stop() {
@@ -126,6 +118,51 @@ final class VoiceAudio: @unchecked Sendable {
}
}
+ // MARK: - Private setup
+
+ /// Configure `engine`, install tap, start. Returns the input format used.
+ /// On failure, undoes tap + observer and rethrows (engine.stop() left to caller).
+ private func setupEngine(aec: Bool) throws -> AVAudioFormat {
+ let input = engine.inputNode
+
+ if aec {
+ // VP must be enabled BEFORE reading the format — it may change after activation.
+ try input.setVoiceProcessingEnabled(true)
+ input.voiceProcessingOtherAudioDuckingConfiguration =
+ AVAudioVoiceProcessingOtherAudioDuckingConfiguration(
+ enableAdvancedDucking: false, duckingLevel: .min)
+ engine.mainMixerNode.outputVolume = 0
+ }
+
+ let fmt = input.outputFormat(forBus: 0)
+ input.installTap(onBus: 0, bufferSize: 1024, format: fmt) { [weak self] buf, time in
+ self?.processTap(buf, time: time)
+ }
+ tapInstalled = true
+
+ configObserver = NotificationCenter.default.addObserver(
+ forName: .AVAudioEngineConfigurationChange,
+ object: engine, queue: .main
+ ) { [weak self] _ in
+ self?.onConfigChange?()
+ }
+
+ engine.prepare()
+ do {
+ try engine.start()
+ } catch {
+ // Undo tap and observer so caller can safely recreate engine.
+ input.removeTap(onBus: 0)
+ tapInstalled = false
+ if let obs = configObserver {
+ NotificationCenter.default.removeObserver(obs)
+ configObserver = nil
+ }
+ throw error
+ }
+ return fmt
+ }
+
// MARK: - Tap processing (audio thread)
private func processTap(_ buf: AVAudioPCMBuffer, time: AVAudioTime) {
diff --git a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
index cfa5bd6d..ae8160ac 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
@@ -142,6 +142,7 @@ final class VoiceEngine: ObservableObject {
guard audio == nil else { return }
guard let loc = suitableLocale() else {
recognizerUnavailable = true
+ appendAppLog("nb.log", "[Voice] no suitable on-device recognizer — voice disabled")
return
}
recognizerUnavailable = false
@@ -241,7 +242,10 @@ final class VoiceEngine: ObservableObject {
pipelineStartTime = Date()
startAudioStallTask(audio: a)
} catch {
- audioError = error.localizedDescription
+ let e = error as NSError
+ let msg = "[Voice] pipeline start failed: \(e.domain)/\(e.code) \(e.localizedDescription)"
+ appendAppLog("nb.log", msg)
+ audioError = "\(e.domain)/\(e.code): \(e.localizedDescription)"
}
}
diff --git a/NotchBuddy/project.yml b/NotchBuddy/project.yml
index e9269340..4b2cec18 100644
--- a/NotchBuddy/project.yml
+++ b/NotchBuddy/project.yml
@@ -25,6 +25,8 @@ settings:
ENABLE_HARDENED_RUNTIME: YES
CODE_SIGN_STYLE: Manual
OTHER_SWIFT_FLAGS: "-strict-concurrency=complete"
+ # Prevent Xcode from scanning Swift source and rewriting Localizable.xcstrings.
+ SWIFT_EMIT_LOC_STRINGS: NO
configs:
Debug:
CODE_SIGNING_REQUIRED: NO