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 phase 1: data race, music re-trigger, logs, emote restore

Louis Raille8a164530f09a
diff --git a/NotchBuddy/Sources/App/Voice/EnergyVAD.swift b/NotchBuddy/Sources/App/Voice/EnergyVAD.swift
index 3d34ccc8..83acc1b1 100644
--- a/NotchBuddy/Sources/App/Voice/EnergyVAD.swift
+++ b/NotchBuddy/Sources/App/Voice/EnergyVAD.swift
@@ -32,14 +32,20 @@ struct EnergyVAD {
private var silentCount = 0
private var activeCount = 0
+ // Power accumulator for the current active segment (used to update noise on forced end).
+ private var segmentPowerSum: Double = 0
+ private var segmentPowerCount: Int = 0
+
// MARK: - API
/// Hard-reset to inactive. Preserves the calibrated noise floor.
/// Call after a command ends so the next speech triggers a fresh VAD start.
mutating func reset() {
- isActive = false
- silentCount = 0
- activeCount = 0
+ isActive = false
+ silentCount = 0
+ activeCount = 0
+ segmentPowerSum = 0
+ segmentPowerCount = 0
// noisePower/calibCount: keep — calibration is done, ambient is known.
}
@@ -55,18 +61,29 @@ struct EnergyVAD {
if !isActive {
noisePower = noisePower * 0.995 + power * 0.005
if power > noisePower * Self.riseRatio {
- isActive = true
- silentCount = 0
- activeCount = 0
+ isActive = true
+ silentCount = 0
+ activeCount = 0
+ segmentPowerSum = power
+ segmentPowerCount = 1
return .start
}
return .none
} else {
- activeCount += 1
+ activeCount += 1
+ segmentPowerSum += power
+ segmentPowerCount += 1
if activeCount >= Self.maxActiveFrames {
- isActive = false
- silentCount = 0
- activeCount = 0
+ // Update noise floor to the average power of this forced-end segment
+ // (continuous music/noise → treat it as new ambient level).
+ if segmentPowerCount > 0 {
+ noisePower = segmentPowerSum / Double(segmentPowerCount)
+ }
+ isActive = false
+ silentCount = 0
+ activeCount = 0
+ segmentPowerSum = 0
+ segmentPowerCount = 0
return .end
}
if power < noisePower * Self.fallRatio {
diff --git a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
index 0098bff7..850a637b 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift
@@ -36,6 +36,10 @@ final class VoiceAudio: @unchecked Sendable {
/// Written on the main thread; read on the audio tap thread.
nonisolated(unsafe) var bypassVAD: Bool = false
+ /// Set on the main thread after a command ends; consumed (and acted on) by the audio
+ /// tap thread on its next buffer — avoids a data race on the `vad` struct.
+ nonisolated(unsafe) private var pendingVADReset: Bool = false
+
// MARK: Callbacks
/// Fired on the main thread when voice activity starts (VAD rise).
@@ -77,13 +81,9 @@ final class VoiceAudio: @unchecked Sendable {
/// Hard-reset the VAD to inactive state; preserves the learned noise floor.
/// Call from the main thread after a command ends so the next speech restarts cleanly.
+ /// Uses a flag consumed by the audio tap thread to avoid a data race on `vad`.
func resetVAD() {
- // vad is audio-tap-thread state, but we write it under no lock since resetVAD
- // is called synchronously on main thread after bypassVAD has been cleared — the
- // tap will not read vad until the next buffer, which is always after this returns.
- vad.reset()
- smoothedLevel = 0
- levelFrameCount = 0
+ pendingVADReset = true
}
/// Return a snapshot of the pre-roll ring buffer and clear it.
@@ -99,6 +99,14 @@ final class VoiceAudio: @unchecked Sendable {
// MARK: - Tap processing (audio thread)
private func processTap(_ buf: AVAudioPCMBuffer, time: AVAudioTime) {
+ // Consume a pending VAD reset requested by the main thread.
+ if pendingVADReset {
+ pendingVADReset = false
+ vad.reset()
+ smoothedLevel = 0
+ levelFrameCount = 0
+ }
+
// Always maintain the pre-roll ring buffer.
prerollLock.withLock {
if prerollBuffers.count >= Self.prerollCapacity { prerollBuffers.removeFirst() }
diff --git a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
index 39993aea..2263b4cd 100644
--- a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
+++ b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift
@@ -124,23 +124,23 @@ final class VoiceEngine: ObservableObject {
guard let self, !isListeningForCommand, voiceSegmentActive else { return }
let preroll = a.drainPreroll()
s.beginWindow(locale: loc, preroll: preroll)
- print("[Voice] window open")
+ appendAppLog("nb.log", "[Voice] window open")
scheduleWakeWindowRestart(audio: a, spotter: s, locale: loc)
}
a.onVoiceStart = { [weak self] in
guard let self else { return }
- print("[Voice] vad start")
+ appendAppLog("nb.log", "[Voice] vad start")
voiceSegmentActive = true
let preroll = a.drainPreroll()
s.beginWindow(locale: loc, preroll: preroll)
- print("[Voice] window open")
+ appendAppLog("nb.log", "[Voice] window open")
scheduleWakeWindowRestart(audio: a, spotter: s, locale: loc)
}
a.onVoiceEnd = { [weak self] in
guard let self else { return }
- print("[Voice] vad end")
+ appendAppLog("nb.log", "[Voice] vad end")
voiceSegmentActive = false
wakeWindowWork?.cancel(); wakeWindowWork = nil
// Only close the wake window if we're not already in command mode.
@@ -148,7 +148,7 @@ final class VoiceEngine: ObservableObject {
// isListeningForCommand is set (race with onWake dispatch).
if !isListeningForCommand && !s.isInCommandPhase {
s.endWindow()
- print("[Voice] window close")
+ appendAppLog("nb.log", "[Voice] window close")
}
}
@@ -183,9 +183,9 @@ final class VoiceEngine: ObservableObject {
guard let self, !isListeningForCommand else { return }
let preroll = audio.drainPreroll()
spotter.endWindow()
- print("[Voice] window close")
+ appendAppLog("nb.log", "[Voice] window close")
spotter.beginWindow(locale: locale, preroll: preroll)
- print("[Voice] window open")
+ appendAppLog("nb.log", "[Voice] window open")
scheduleWakeWindowRestart(audio: audio, spotter: spotter, locale: locale)
}
wakeWindowWork = item
@@ -214,7 +214,7 @@ final class VoiceEngine: ObservableObject {
commandTranscript = ""
lastWordCount = 0
audio?.bypassVAD = true
- print("[Voice] wake")
+ appendAppLog("nb.log", "[Voice] wake")
NotificationCenter.default.post(name: .voiceWoke, object: nil)
// Initial timeout: 3 s to start speaking, then 1.2 s silence to finish.
resetSilenceTimer()
@@ -261,7 +261,7 @@ final class VoiceEngine: ObservableObject {
voiceSegmentActive = false // treat the voice segment as ended after command
micLevel = 0
spotter?.endWindow()
- print("[Voice] command end (had transcript: \(!commandTranscript.isEmpty))")
+ appendAppLog("nb.log", "[Voice] command end (had transcript: \(!commandTranscript.isEmpty))")
if !postFinished { commandTranscript = "" }
isListeningForCommand = false
if postFinished {
diff --git a/NotchBuddy/Sources/CoucouKit/BotEngine.swift b/NotchBuddy/Sources/CoucouKit/BotEngine.swift
index 038c14fc..715d1c08 100644
--- a/NotchBuddy/Sources/CoucouKit/BotEngine.swift
+++ b/NotchBuddy/Sources/CoucouKit/BotEngine.swift
@@ -268,6 +268,7 @@ final class BotEngine: ObservableObject {
var listeningLevel: CGFloat = 0 // smoothed mic level 0…1, set each frame by BotCanvasView
var isInListeningMode = false
var listeningHasWords = false
+ private var listeningPrevPermanentEmote: BotEmote? = nil
// Dancing (Apple Music)
var isDancing: Bool = false
@@ -600,6 +601,7 @@ final class BotEngine: ObservableObject {
func enterListening() {
isInListeningMode = true
listeningHasWords = false
+ listeningPrevPermanentEmote = permanentEmote
interruptGreet()
// Small jump
@@ -633,8 +635,9 @@ final class BotEngine: ObservableObject {
listeningLevel = 0
listeningHasWords = false
- // Release permanent wide eyes
- setPermanentEmote(nil)
+ // Restore the permanent emote that was active before listening started
+ setPermanentEmote(listeningPrevPermanentEmote)
+ listeningPrevPermanentEmote = nil
// Return head tilt to rest
anim("tilt", keys: [TweenKey(target: 0, duration: 250, ease: Ease.inOut)])
diff --git a/tests/EnergyVADTests.swift b/tests/EnergyVADTests.swift
index b80a50b9..6d89a417 100644
--- a/tests/EnergyVADTests.swift
+++ b/tests/EnergyVADTests.swift
@@ -101,6 +101,34 @@ enum EnergyVADTests {
check("inactive after forced end", !vad.isActive)
}
+ // MARK: 8. Continuous music (~60 s): at most 1 segment, no re-trigger after forced end
+ // After the 30 s forced end the noise floor is updated to the music level.
+ // A second forced-end segment may occur if music continues, but the VAD must NOT
+ // immediately re-trigger on the very next frame after the forced end.
+ do {
+ var vad = EnergyVAD()
+ let musicPower = 5e-4 // simulated music level
+ // Calibrate at music level (as if music was already playing before app launch).
+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(musicPower) }
+
+ var segments = 0
+ let totalFrames = EnergyVAD.maxActiveFrames * 2 + 100 // ~60 s at 43 Hz
+ var prevEvent: EnergyVAD.Event = .none
+ for _ in 0..<totalFrames {
+ let event = vad.feed(musicPower)
+ if event == .start { segments += 1 }
+ // Immediately after a forced end, must NOT re-trigger on the next frame.
+ if prevEvent == .end && event == .start {
+ check("music: no immediate re-trigger after forced end", false)
+ break
+ }
+ prevEvent = event
+ }
+ // ≤ 2 segments over 60 s (one per 30 s window) is acceptable;
+ // what matters is that each forced end is followed by a clean gap.
+ check("music (~60 s): no immediate re-trigger after forced end", prevEvent != .start || segments <= 2)
+ }
+
// Summary
if failures == 0 { print("\n\(total)/\(total) tests passed.") }
else { print("\n\(failures) test(s) FAILED."); exit(1) }