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.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
review/voice-phase1 round 4: VAD fix, Mochi listening, logs
Louis Raille84e8ddc6a8ae
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 349d4ad6..ed759bd9 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -39,6 +39,9 @@ jobs:- name: Wake phrase testsrun: bash scripts/test-voice-wake.sh+ - name: Energy VAD tests+ run: bash scripts/test-voice-vad.sh+- name: Voice island FSM testsrun: bash scripts/test-voice-island.shdiff --git a/NotchBuddy/Sources/App/BotCanvasView.swift b/NotchBuddy/Sources/App/BotCanvasView.swiftindex d73ac7fb..7f7a4e86 100644--- a/NotchBuddy/Sources/App/BotCanvasView.swift+++ b/NotchBuddy/Sources/App/BotCanvasView.swift@@ -72,6 +72,13 @@ struct BotCanvasView: View {engine.setOutfit(showOutfit ? state.resolvedOutfit : .none,animated: state.view != .wardrobe)+ #if !APPSTORE+ if state.view == .listening {+ engine.listeningLevel = CGFloat(VoiceEngine.shared.micLevel)+ engine.listeningHasWords = !VoiceEngine.shared.commandTranscript.isEmpty+ }+ #endif+engine.update(dt: dt)var ctx = contextengine.applyDance(&ctx, size: size)@@ -101,7 +108,7 @@ struct BotCanvasView: View {.onChange(of: state.effectiveState) { _, newState inengine.setState(newState)}- .onChange(of: state.view) { _, newView in+ .onChange(of: state.view) { oldView, newView in// Morph up when upload view is activeif state.mode == .expanded && newView == .upload {engine.anim("morph", keys: [TweenKey(target: 1, duration: 550, ease: Ease.inOut)])@@ -109,6 +116,13 @@ struct BotCanvasView: View {// Any other view (not mid-gulp): morph backengine.anim("morph", keys: [TweenKey(target: 0, duration: 550, ease: Ease.inOut)])}+ #if !APPSTORE+ if newView == .listening {+ engine.enterListening()+ } else if oldView == .listening {+ engine.exitListening(hadCommand: !VoiceEngine.shared.commandTranscript.isEmpty)+ }+ #endif}.onChange(of: state.mode) { _, newMode in// Hard-reset morph when island collapsesdiff --git a/NotchBuddy/Sources/App/IslandWindowController.swift b/NotchBuddy/Sources/App/IslandWindowController.swiftindex 6bff86d7..4038ccc7 100644--- a/NotchBuddy/Sources/App/IslandWindowController.swift+++ b/NotchBuddy/Sources/App/IslandWindowController.swift@@ -273,8 +273,6 @@ final class IslandWindowController: NSWindowController {case .listening:self.expand(to: .listening)- NotificationCenter.default.post(name: .triggerEmote,- object: BotEmote.listening)}}diff --git a/NotchBuddy/Sources/App/Voice/EnergyVAD.swift b/NotchBuddy/Sources/App/Voice/EnergyVAD.swiftnew file mode 100644index 00000000..3d34ccc8--- /dev/null+++ b/NotchBuddy/Sources/App/Voice/EnergyVAD.swift@@ -0,0 +1,87 @@+import Foundation++// MARK: - EnergyVAD+//+// Pure energy-based voice-activity detector.+// No AVFoundation dependency; usable in standalone test scripts.+//+// Algorithm:+// - Calibration phase (~0.5 s): fast noise-floor learning, no triggers.+// - After calibration: noise floor drifts slowly; rise when power > noise×8.+// - Max segment (~30 s): forces an end so the VAD never stays permanently active.+// - reset() after a command ends: clears activity counters, preserves the learned+// noise floor so the next speech cycle starts from a valid baseline.++struct EnergyVAD {++ enum Event { case none, start, end }++ // MARK: - Tunables+ static let calibFrames = 22 // ≈ 0.5 s at 43 Hz before triggering+ static let riseRatio = 8.0 // power/noise to start a segment+ static let fallRatio = 2.0 // power/noise to count silence+ static let silenceFrames = 35 // ≈ 800 ms at 43 Hz+ static let maxActiveFrames = 1300 // ≈ 30 s safety limit++ // MARK: - Readable state+ private(set) var isActive: Bool = false+ private(set) var noisePower: Double = 1e-7++ // MARK: - Internal counters+ private var calibCount = 0+ private var silentCount = 0+ private var activeCount = 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+ // noisePower/calibCount: keep — calibration is done, ambient is known.+ }++ /// Feed one frame's mean-square power. Returns whether a segment started/ended.+ mutating func feed(_ power: Double) -> Event {+ // Phase 1 — fast calibration: converge noisePower to ambient in ~0.5 s.+ if calibCount < Self.calibFrames {+ calibCount += 1+ noisePower = noisePower * 0.85 + power * 0.15+ return .none+ }++ if !isActive {+ noisePower = noisePower * 0.995 + power * 0.005+ if power > noisePower * Self.riseRatio {+ isActive = true+ silentCount = 0+ activeCount = 0+ return .start+ }+ return .none+ } else {+ activeCount += 1+ if activeCount >= Self.maxActiveFrames {+ isActive = false+ silentCount = 0+ activeCount = 0+ return .end+ }+ if power < noisePower * Self.fallRatio {+ silentCount += 1+ noisePower = noisePower * 0.999 + power * 0.001+ if silentCount >= Self.silenceFrames {+ isActive = false+ silentCount = 0+ activeCount = 0+ return .end+ }+ } else {+ silentCount = 0+ }+ return .none+ }+ }+}diff --git a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift b/NotchBuddy/Sources/App/Voice/VoiceAudio.swiftindex bccd6e16..0098bff7 100644--- a/NotchBuddy/Sources/App/Voice/VoiceAudio.swift+++ b/NotchBuddy/Sources/App/Voice/VoiceAudio.swift@@ -6,29 +6,25 @@ import AVFoundation/// Wraps AVAudioEngine to deliver a low-level audio tap for voice detection.////// Thread model:-/// - `start()` / `stop()` called on the main thread.-/// - `onVoiceStart`, `onVoiceEnd` dispatched to the main thread.+/// - `start()` / `stop()` / `resetVAD()` called on the main thread.+/// - `onVoiceStart`, `onVoiceEnd`, `onMicLevel` dispatched to the main thread./// - `onBuffer` called **on the audio tap thread** — its closure must be thread-safe.////// VAD modes:-/// - Normal (wake): energy VAD drives `onVoiceStart`/`onVoiceEnd`; ~800 ms silence to end.+/// - Normal (wake): EnergyVAD drives `onVoiceStart`/`onVoiceEnd`./// - Bypass (`bypassVAD = true`): every buffer is delivered via `onBuffer` regardless of-/// silence, so the command recognizer receives continuous audio. Set by VoiceEngine after-/// a wake event, cleared after command ends.+/// silence; `onMicLevel` fires at ~20 Hz with a smoothed level.final class VoiceAudio: @unchecked Sendable {private let engine = AVAudioEngine()private var tapInstalled = false- // Adaptive VAD — read/written only on the audio tap thread.- private var noisePower: Double = 1e-7- private var vadActive = false- private var silentBufs = 0+ // VAD — read/written only on the audio tap thread.+ private var vad = EnergyVAD()- private static let riseRatio: Double = 8.0- private static let fallRatio: Double = 2.0- /// ~800 ms at 44 100 Hz / 1 024 frames ≈ 34.9 → 35 frames.- private static let silenceFrames = 35+ // Smoothed mic level during bypass mode — audio tap thread.+ private var smoothedLevel: Double = 0+ private var levelFrameCount = 0// Pre-roll circular buffer (~500 ms).// Read on main (via `drainPreroll()`), written on audio tap thread.@@ -37,9 +33,7 @@ final class VoiceAudio: @unchecked Sendable {private static let prerollCapacity = 22 // ~500 ms at 43 Hz/// When true, every audio buffer is delivered via `onBuffer` regardless of VAD.- /// Written on the main thread before the next tap fires; read on the audio tap thread.- /// Safe to use `nonisolated(unsafe)` because the write is always ordered before the read- /// (main sets it before the next tap period, audio tap reads it on the next cycle).+ /// Written on the main thread; read on the audio tap thread.nonisolated(unsafe) var bypassVAD: Bool = false// MARK: Callbacks@@ -49,9 +43,11 @@ final class VoiceAudio: @unchecked Sendable {/// Fired on the main thread when voice activity ends (VAD fall, only when bypassVAD is false).var onVoiceEnd: (() -> Void)?/// Fired on the **audio tap thread** with each PCM buffer.- /// In normal mode: only during voice activity.- /// In bypass mode: every buffer.+ /// Normal mode: only during voice activity. Bypass mode: every buffer.var onBuffer: ((AVAudioPCMBuffer, AVAudioTime) -> Void)?+ /// Fired on the main thread at ~20 Hz during bypass mode with smoothed mic level (0…1 approx).+ /// Nil or 0 when not in bypass mode.+ var onMicLevel: ((Double) -> Void)?// MARK: - Control@@ -73,12 +69,23 @@ final class VoiceAudio: @unchecked Sendable {engine.inputNode.removeTap(onBus: 0)tapInstalled = falseprerollLock.withLock { prerollBuffers.removeAll() }- if vadActive {- vadActive = false+ if vad.isActive {+ vad.reset()DispatchQueue.main.async { [weak self] in self?.onVoiceEnd?() }}}+ /// 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.+ 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+ }+/// Return a snapshot of the pre-roll ring buffer and clear it./// Called on the main thread just before `WakeSpotter.beginWindow`.func drainPreroll() -> [AVAudioPCMBuffer] {@@ -99,32 +106,32 @@ final class VoiceAudio: @unchecked Sendable {}if bypassVAD {- // Command mode: deliver every buffer, skip VAD logic.onBuffer?(buf, time)+ // Smoothed mic level for Mochi animation (~20 Hz).+ let power = buf.meanSquarePower+ smoothedLevel = smoothedLevel * 0.6 + power * 0.4+ levelFrameCount += 1+ if levelFrameCount % 2 == 0 {+ let level = min(1.0, smoothedLevel * 2000) // rough normalisation to 0…1+ DispatchQueue.main.async { [weak self] in self?.onMicLevel?(level) }+ }return}let power = buf.meanSquarePower+ let event = vad.feed(power)- if !vadActive {- noisePower = noisePower * 0.995 + power * 0.005- if power > noisePower * Self.riseRatio {- vadActive = true- silentBufs = 0- DispatchQueue.main.async { [weak self] in self?.onVoiceStart?() }- }- } else {+ switch event {+ case .start:+ DispatchQueue.main.async { [weak self] in self?.onVoiceStart?() }+ case .end:+ DispatchQueue.main.async { [weak self] in self?.onVoiceEnd?() }+ case .none:+ break+ }++ if vad.isActive {onBuffer?(buf, time)- if power < noisePower * Self.fallRatio {- silentBufs += 1- if silentBufs >= Self.silenceFrames {- vadActive = false- silentBufs = 0- DispatchQueue.main.async { [weak self] in self?.onVoiceEnd?() }- }- } else {- silentBufs = 0- }}}}diff --git a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift b/NotchBuddy/Sources/App/Voice/VoiceEngine.swiftindex 231cf02f..39993aea 100644--- a/NotchBuddy/Sources/App/Voice/VoiceEngine.swift+++ b/NotchBuddy/Sources/App/Voice/VoiceEngine.swift@@ -27,6 +27,7 @@ final class VoiceEngine: ObservableObject {@Published private(set) var commandTranscript: String = ""@Published private(set) var recognizerUnavailable: Bool = false@Published private(set) var audioError: String? = nil+ @Published private(set) var micLevel: Double = 0// MARK: - Pause reasons@@ -114,24 +115,32 @@ final class VoiceEngine: ObservableObject {self?.endCommand(postFinished: true)}+ a.onMicLevel = { [weak self] level in+ self?.micLevel = level+ }+// Wake window ended naturally (final result in wake phase) — reopen if voice is still active.s.onWakeWindowEnded = { [weak self] inguard let self, !isListeningForCommand, voiceSegmentActive else { return }let preroll = a.drainPreroll()s.beginWindow(locale: loc, preroll: preroll)+ print("[Voice] window open")scheduleWakeWindowRestart(audio: a, spotter: s, locale: loc)}a.onVoiceStart = { [weak self] inguard let self else { return }+ print("[Voice] vad start")voiceSegmentActive = truelet preroll = a.drainPreroll()s.beginWindow(locale: loc, preroll: preroll)+ print("[Voice] window open")scheduleWakeWindowRestart(audio: a, spotter: s, locale: loc)}a.onVoiceEnd = { [weak self] inguard let self else { return }+ print("[Voice] vad end")voiceSegmentActive = falsewakeWindowWork?.cancel(); wakeWindowWork = nil// Only close the wake window if we're not already in command mode.@@ -139,6 +148,7 @@ final class VoiceEngine: ObservableObject {// isListeningForCommand is set (race with onWake dispatch).if !isListeningForCommand && !s.isInCommandPhase {s.endWindow()+ print("[Voice] window close")}}@@ -173,7 +183,9 @@ final class VoiceEngine: ObservableObject {guard let self, !isListeningForCommand else { return }let preroll = audio.drainPreroll()spotter.endWindow()+ print("[Voice] window close")spotter.beginWindow(locale: locale, preroll: preroll)+ print("[Voice] window open")scheduleWakeWindowRestart(audio: audio, spotter: spotter, locale: locale)}wakeWindowWork = item@@ -202,6 +214,7 @@ final class VoiceEngine: ObservableObject {commandTranscript = ""lastWordCount = 0audio?.bypassVAD = true+ print("[Voice] wake")NotificationCenter.default.post(name: .voiceWoke, object: nil)// Initial timeout: 3 s to start speaking, then 1.2 s silence to finish.resetSilenceTimer()@@ -243,8 +256,13 @@ final class VoiceEngine: ObservableObject {private func endCommand(postFinished: Bool) {silenceWork?.cancel(); silenceWork = nilcommandMaxWork?.cancel(); commandMaxWork = nil- audio?.bypassVAD = false+ audio?.bypassVAD = false+ audio?.resetVAD() // reset VAD so next speech triggers a fresh start+ voiceSegmentActive = false // treat the voice segment as ended after command+ micLevel = 0spotter?.endWindow()+ print("[Voice] command end (had transcript: \(!commandTranscript.isEmpty))")+ if !postFinished { commandTranscript = "" }isListeningForCommand = falseif postFinished {let cmd = commandTranscriptdiff --git a/NotchBuddy/Sources/CoucouKit/BotEngine.swift b/NotchBuddy/Sources/CoucouKit/BotEngine.swiftindex 6dc8b389..038c14fc 100644--- a/NotchBuddy/Sources/CoucouKit/BotEngine.swift+++ b/NotchBuddy/Sources/CoucouKit/BotEngine.swift@@ -263,6 +263,12 @@ final class BotEngine: ObservableObject {// Slap tracking (for dizzy on 3 slaps)var slapTimes: [Double] = []+ // Listening mode+ var listenHandRaise: CGFloat = 0 // 0=rest, 1=raised to listening position (right hand)+ var listeningLevel: CGFloat = 0 // smoothed mic level 0…1, set each frame by BotCanvasView+ var isInListeningMode = false+ var listeningHasWords = false+// Dancing (Apple Music)var isDancing: Bool = falsevar dancingLevel: CGFloat = 0 // 0→1 over 0.3s, 1→0 over 0.5s@@ -588,6 +594,74 @@ final class BotEngine: ObservableObject {anim("hands", keys: [TweenKey(target: 0, duration: 150, ease: Ease.inOut)])}+ // MARK: - Listening mode++ /// Call when the island enters listening state (.voiceWoke).+ func enterListening() {+ isInListeningMode = true+ listeningHasWords = false+ interruptGreet()++ // Small jump+ anim("oy", keys: [+ TweenKey(target: -0.20, duration: 130, ease: Ease.out),+ TweenKey(target: 0, duration: 280, ease: Ease.back),+ ])+ squash()++ // Wide eyes — permanent (survive blinks)+ setPermanentEmote(.listening)++ // Head tilt held (no return-to-zero)+ anim("tilt", keys: [TweenKey(target: -0.10, duration: 200, ease: Ease.out)])++ // Show and raise right hand+ anim("hands", keys: [TweenKey(target: 1, duration: 150, ease: Ease.out)])+ anim("listenHandRaise",keys: [TweenKey(target: 1, duration: 280, ease: Ease.out)])++ // Attentive sound+ SoundEngine.shared.play("question")++ // First slow blink sooner than normal+ nextBlink = CACurrentMediaTime() + 2.5 + Double.random(in: 0...1.0)+ }++ /// Call when the island leaves listening state.+ /// `hadCommand` = commandTranscript was non-empty when the session ended.+ func exitListening(hadCommand: Bool) {+ isInListeningMode = false+ listeningLevel = 0+ listeningHasWords = false++ // Release permanent wide eyes+ setPermanentEmote(nil)++ // Return head tilt to rest+ anim("tilt", keys: [TweenKey(target: 0, duration: 250, ease: Ease.inOut)])++ // Lower hand+ anim("listenHandRaise", keys: [TweenKey(target: 0, duration: 200, ease: Ease.inOut)])+ anim("hands", keys: [TweenKey(target: 0, duration: 180, ease: Ease.inOut)])++ if hadCommand {+ // Nod+ anim("oy", keys: [+ TweenKey(target: -0.08, duration: 110, ease: Ease.out),+ TweenKey(target: 0, duration: 200, ease: Ease.back),+ ])+ triggerEmote(.happy, duration: 1.0, silent: true)+ } else {+ // Shrug+ anim("sy", keys: [+ TweenKey(target: 0.93, duration: 180, ease: Ease.out),+ TweenKey(target: 1, duration: 350, ease: Ease.back),+ ])+ }++ // Resume normal blink interval+ nextBlink = CACurrentMediaTime() + 1.5 + Double.random(in: 0...2)+ }+/// Sets a permanent eye expression that survives blinks and transient emotes.func setPermanentEmote(_ emote: BotEmote?) {permanentEmote = emote@@ -775,6 +849,22 @@ final class BotEngine: ObservableObject {tgPitch = tptgTilt = cfg.tilt+ // Listening mode: waiting sway + voice reactive+ if isInListeningMode {+ if !listeningHasWords {+ ty += sin(CGFloat(now - t0) * 1.1) * 0.06 // gentle waiting sway+ tp += -0.12 // look slightly upward+ }+ tgYaw = ty+ tgPitch = tp+ if listeningLevel > 0.01 {+ let lvl = listeningLevel+ tgEs = max(tgEs, 1.0 + lvl * 0.15)+ if !locks.contains("sy") { tgSy = max(tgSy, 1.0 + lvl * 0.04) }+ if !locks.contains("sx") { tgSx = min(tgSx, 1.0 - lvl * 0.02) }+ }+ }+// Body sway during greeting waveif now > waveStart && now < waveUntil {let wt = CGFloat(now - waveStart)@@ -824,7 +914,8 @@ final class BotEngine: ObservableObject {DispatchQueue.main.asyncAfter(deadline: .now() + 0.23) { [weak self] in self?.blink() }}}- nextBlink = now + 2.2 + Double.random(in: 0...3.2)+ nextBlink = now + (isInListeningMode ? 3.0 + Double.random(in: 0...1.5)+ : 2.2 + Double.random(in: 0...3.2))}// Clear expired eye override (restore permanent if set)@@ -1032,6 +1123,13 @@ final class BotEngine: ObservableObject {localX = -hwB * 1.08localY = hhB * 0.70 + sin(6 * wt) * 0.04 * bodyH+ } else if sd > 0 && listenHandRaise > 0.01 {+ // Right hand: raised to attentive listening position (not waving)+ let h = listenHandRaise+ localX = lerp(hwB * 1.08, hwB * 1.05, h)+ localY = lerp(hhB * 0.70, -hhB * 0.28, h)+ handRot = -0.45 * h // tilts slightly toward head+} else {// Rest: lower-side, clearly peeking behind body bottomlocalX = CGFloat(sd) * hwB * 1.08@@ -1554,6 +1652,7 @@ final class BotEngine: ObservableObject {case "es": es = valuecase "badgeS": badgeS = valuecase "outfitPresence": outfitPresence = value+ case "listenHandRaise": listenHandRaise = valuedefault: break}}@@ -1576,6 +1675,7 @@ final class BotEngine: ObservableObject {case "es": return escase "badgeS": return badgeScase "outfitPresence": return outfitPresence+ case "listenHandRaise": return listenHandRaisedefault: return 0}}diff --git a/scripts/test-voice-vad.sh b/scripts/test-voice-vad.shnew file mode 100755index 00000000..ec3e2ca3--- /dev/null+++ b/scripts/test-voice-vad.sh@@ -0,0 +1,11 @@+#!/usr/bin/env bash+# Energy VAD tests — pure struct, no AVFoundation dependency.+set -euo pipefail+cd "$(dirname "$0")/.."+TEST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/coucou-voice-vad.XXXXXX")"+trap 'rm -rf "$TEST_DIR"' EXIT+swiftc \+ NotchBuddy/Sources/App/Voice/EnergyVAD.swift \+ tests/EnergyVADTests.swift \+ -o "$TEST_DIR/energy-vad-tests"+"$TEST_DIR/energy-vad-tests"diff --git a/tests/EnergyVADTests.swift b/tests/EnergyVADTests.swiftnew file mode 100644index 00000000..b80a50b9--- /dev/null+++ b/tests/EnergyVADTests.swift@@ -0,0 +1,114 @@+import Foundation++@main+enum EnergyVADTests {++ static var failures = 0+ static var total = 0++ static func main() {++ // MARK: 1. Calibration: no trigger during first 22 frames+ do {+ var vad = EnergyVAD()+ var triggered = false+ for _ in 0..<EnergyVAD.calibFrames {+ if vad.feed(1e-4) == .start { triggered = true } // high power during calib+ }+ check("calibration: no trigger during first \(EnergyVAD.calibFrames) frames", !triggered)+ }++ // MARK: 2. After calibration, ambient never triggers+ do {+ var vad = EnergyVAD()+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(1e-5) } // calibrate at ambient+ var triggered = false+ for _ in 0..<100 {+ if vad.feed(1e-5) == .start { triggered = true } // ambient: should NOT trigger+ }+ check("ambient does not trigger after calibration", !triggered)+ }++ // MARK: 3. Speech above threshold → start+ do {+ var vad = EnergyVAD()+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(1e-5) }+ var startSeen = false+ for _ in 0..<10 {+ if vad.feed(1e-4) == .start { startSeen = true; break }+ }+ check("speech above threshold → start", startSeen)+ }++ // MARK: 4. Start then silence → end+ do {+ var vad = EnergyVAD()+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(1e-5) }+ // Trigger start+ while vad.feed(1e-4) != .start {}+ // Silence (below fallRatio * noisePower ≈ 2 * 1e-5 = 2e-5)+ var endSeen = false+ for _ in 0..<(EnergyVAD.silenceFrames + 5) {+ if vad.feed(1e-7) == .end { endSeen = true }+ }+ check("silence after speech → end", endSeen)+ }++ // MARK: 5. 10 cycles: speech→start, reset, speech→start again+ // Use 5e-4 (well above 8× ambient of 1e-5) so noise-floor drift doesn't prevent+ // the trigger even after the noisePower has crept up slightly from previous cycles.+ do {+ var vad = EnergyVAD()+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(1e-5) }+ var allOk = true+ for i in 0..<10 {+ // Trigger start+ var startSeen = false+ for _ in 0..<20 {+ if vad.feed(5e-4) == .start { startSeen = true; break }+ }+ if !startSeen { allOk = false; print(" cycle \(i): no start"); break }+ vad.reset()+ if vad.isActive { allOk = false; print(" cycle \(i): still active after reset"); break }+ }+ check("10 cycles: speech→start, reset, repeat", allOk)+ }++ // MARK: 6. Max segment: 1300+ active frames → end+ do {+ var vad = EnergyVAD()+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(1e-5) }+ while vad.feed(1e-4) != .start {}+ var endSeen = false+ for _ in 0..<(EnergyVAD.maxActiveFrames + 5) {+ if vad.feed(1e-4) == .end { endSeen = true }+ }+ check("max segment (\(EnergyVAD.maxActiveFrames) frames) → forced end", endSeen)+ }++ // MARK: 7. Ambient that rises mid-session eventually falls (via max segment)+ do {+ var vad = EnergyVAD()+ for _ in 0..<EnergyVAD.calibFrames { _ = vad.feed(1e-5) }+ // Start with normal speech+ while vad.feed(1e-4) != .start {}+ // Now feed high ambient (simulates room noise rising): never reach silence threshold+ var endSeen = false+ for _ in 0..<(EnergyVAD.maxActiveFrames + 10) {+ if vad.feed(1e-3) == .end { endSeen = true; break } // high ambient, no silence+ }+ check("ambient rise forces end via max segment", endSeen)+ check("inactive after forced end", !vad.isActive)+ }++ // Summary+ if failures == 0 { print("\n\(total)/\(total) tests passed.") }+ else { print("\n\(failures) test(s) FAILED."); exit(1) }+ }++ static func check(_ label: String, _ condition: Bool) {+ total += 1+ if condition { print("✓ \(label)") }+ else { print("✗ \(label)"); failures += 1 }+ }+}