From 4a0699286cb18784fa565a2ef517dd00495df93d Mon Sep 17 00:00:00 2001 From: "prompt.ac/@jeffrey" Date: Sat, 17 Oct 2026 10:43:07 -0700 Subject: [PATCH] Menu Band: fade Air out when the player leaves the app Air is a listening sound for whoever is at Menu Band. It now renders only while the app is in front or holds keyboard performance focus, ramping to silence over the existing 250 ms when the player switches away and back in at the remembered slider level on return. The engine's idle guards key off this effective activity instead of the raw slider, and the mic monitor's focus policy is unchanged. Co-Authored-By: Claude Fable 5.1 --- slab/menuband/SCORE.md | 9 +++- .../Sources/MenuBand/AppDelegate.swift | 24 +++++++++-- .../Sources/MenuBand/MenuBandAmbientAir.swift | 27 ++++++++++-- .../Sources/MenuBand/MenuBandSynth.swift | 4 +- .../Tests/MenuBandTests/AmbientAirTests.swift | 41 +++++++++++++++++++ 5 files changed, 95 insertions(+), 10 deletions(-) diff --git a/slab/menuband/SCORE.md b/slab/menuband/SCORE.md index 082b5e623c..8237531448 100644 --- a/slab/menuband/SCORE.md +++ b/slab/menuband/SCORE.md @@ -78,13 +78,18 @@ restart events into a local report directory. An optional argument selects The direct-download popover's **Air** slider adds White, Brown, or Cabin background noise: zero is silent, raising it fades the noise in. It shares -the instrument's output engine and keeps playing when the popover closes. +the instrument's output engine. Air plays while Menu Band is in front (the +popover open, or keyboard performance focus held) and fades out over 250 ms +when the player leaves for another app; the slider keeps its level, and +returning fades Air back in. The microphone monitor is not affected by this +app-switch gate. It does not capture or change other apps' audio, open another output engine, or require system-audio permission. Air bypasses gesture FX and rewind capture; its level is independent of the instrument mix. It is included in the full tape mix, but not the dry instrument or microphone stems. Dragging an active Air slider only updates render parameters. Crossing zero -wakes an idle engine or permits idle suspension; adding Air to a running mix +or leaving/returning to Menu Band wakes an idle engine or permits idle +suspension; adding Air to a running mix does not invoke recovery. Piano hover only redraws the keyboard and never queries CoreAudio. The independent timers own diagnostics and fault checks. diff --git a/slab/menuband/Sources/MenuBand/AppDelegate.swift b/slab/menuband/Sources/MenuBand/AppDelegate.swift index 8efea9d13c..65e280bb61 100644 --- a/slab/menuband/Sources/MenuBand/AppDelegate.swift +++ b/slab/menuband/Sources/MenuBand/AppDelegate.swift @@ -136,6 +136,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate { didSet { guard oldValue != keyboardPerformanceFocusActive else { return } refreshEngagement(reason: keyboardPerformanceFocusActive ? "focus gained" : "focus lost") + refreshAirFocus() // Leaving focus (Esc, the Command double-tap) ends a rolling take // and drops it on the Desktop — focus IS the recording session. if !keyboardPerformanceFocusActive, menuBand.tape.state == .recording { @@ -164,6 +165,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate { menuBand.logAudioBindings(context: "engagement \(now ? "on" : "off")") } + /// Air plays while the player is at Menu Band — the app in front (popover + /// open) or holding keyboard performance focus — and fades out when they + /// leave. The slider keeps its level for their return. Separate from the + /// mic's engagement, which an app switch must never mute. + private func refreshAirFocus() { + menuBand.ambientAir.setFocused(NSApp.isActive || keyboardPerformanceFocusActive) + } + /// Periodic check that the status item is actually visible in the /// menu bar. macOS silently hides items when there's no room (notch + /// many menubar apps). When that happens we shrink the layout — @@ -1429,9 +1438,18 @@ final class AppDelegate: NSObject, NSApplicationDelegate { registerInterfaceResetHotkey() NotificationCenter.default.addObserver( forName: NSApplication.didBecomeActiveNotification, object: nil, queue: .main - ) { [weak self] _ in self?.refreshEngagement(reason: "app active") } - // (No resign observer on purpose: clicking into Terminal or a DAW must - // not mute the monitor — only Esc / losing performance focus does.) + ) { [weak self] _ in + self?.refreshEngagement(reason: "app active") + self?.refreshAirFocus() + } + // (No resign observer for the monitor on purpose: clicking into + // Terminal or a DAW must not mute the mic — only Esc / losing + // performance focus does.) Air is the exception: it is a sound for + // the player at Menu Band, so leaving the app fades it out. + NotificationCenter.default.addObserver( + forName: NSApplication.didResignActiveNotification, object: nil, queue: .main + ) { [weak self] _ in self?.refreshAirFocus() } + refreshAirFocus() // Start the Stickies bridge — watches the focused sticky's text // and plays a note for each character typed after an `mbN` token, diff --git a/slab/menuband/Sources/MenuBand/MenuBandAmbientAir.swift b/slab/menuband/Sources/MenuBand/MenuBandAmbientAir.swift index fa078ff535..29174aee02 100644 --- a/slab/menuband/Sources/MenuBand/MenuBandAmbientAir.swift +++ b/slab/menuband/Sources/MenuBand/MenuBandAmbientAir.swift @@ -8,6 +8,9 @@ final class MenuBandAmbientAir { private var requestedLevel: Float = 0 private var requestedColor: MenuBandAirColor private var requestedCutoff: Double + /// Air is a listening sound for the player at Menu Band. Leaving the app + /// fades it out; coming back fades it in at the slider's level. + private var focused = true private let defaults: UserDefaults private var source: AVAudioSourceNode? private var noise = MenuBandAirNoise() @@ -29,6 +32,9 @@ final class MenuBandAmbientAir { } var level: Float { lock.lock(); defer { lock.unlock() }; return requestedLevel } + /// Whether Air is audible (or fading): slider above zero and Menu Band in + /// front. The engine's idle logic keys off this, not the raw slider. + var isActive: Bool { lock.lock(); defer { lock.unlock() }; return requestedLevel > 0 && focused } var color: MenuBandAirColor { lock.lock(); defer { lock.unlock() }; return requestedColor } var cutoff: Double { lock.lock(); defer { lock.unlock() }; return requestedCutoff } @@ -36,13 +42,27 @@ final class MenuBandAmbientAir { guard value.isFinite else { return } let value = max(0, min(1, value)) lock.lock() - let wasActive = requestedLevel > 0 + let wasActive = requestedLevel > 0 && focused requestedLevel = value + let nowActive = requestedLevel > 0 && focused lock.unlock() - if wasActive != (value > 0) { onActiveChange?(value > 0) } + if wasActive != nowActive { onActiveChange?(nowActive) } onChange?() } + /// Menu Band in front (popover open, performance focus, or app active): + /// Air renders at the slider level. Away: Air ramps to silence over the + /// same 250 ms as a slider move, keeping the slider where it was. + func setFocused(_ value: Bool) { + lock.lock() + guard focused != value else { lock.unlock(); return } + let wasActive = requestedLevel > 0 && focused + focused = value + let nowActive = requestedLevel > 0 && focused + lock.unlock() + if wasActive != nowActive { onActiveChange?(nowActive) } + } + func setColor(_ color: MenuBandAirColor) { lock.lock(); requestedColor = color; lock.unlock() defaults.set(color.rawValue, forKey: "systemAirColor") @@ -81,7 +101,8 @@ final class MenuBandAmbientAir { // Never wait for the UI on the audio thread; a concurrent slider move // can be picked up on the next buffer. if lock.try() { - targetLevel = requestedLevel; renderColor = requestedColor; renderCutoff = requestedCutoff + targetLevel = focused ? requestedLevel : 0 + renderColor = requestedColor; renderCutoff = requestedCutoff lock.unlock() } let buffers = UnsafeMutableAudioBufferListPointer(output) diff --git a/slab/menuband/Sources/MenuBand/MenuBandSynth.swift b/slab/menuband/Sources/MenuBand/MenuBandSynth.swift index b849416849..3f3b8b6705 100644 --- a/slab/menuband/Sources/MenuBand/MenuBandSynth.swift +++ b/slab/menuband/Sources/MenuBand/MenuBandSynth.swift @@ -1670,7 +1670,7 @@ final class MenuBandSynth { private func scheduleIdleSuspendIfNeeded() { guard started, !waveformCaptureEnabled, waveformTapPinReasons.isEmpty, !keepEngineWarm, activeNotes.isEmpty, - !sampleRecordingActive, !inputMonitor.isAttached, ambientAir.level == 0 else { return } + !sampleRecordingActive, !inputMonitor.isAttached, !ambientAir.isActive else { return } idleSuspendWorkItem?.cancel() let workItem = DispatchWorkItem { [weak self] in self?.suspendAudioEngineForHiddenIdleIfNeeded() @@ -1688,7 +1688,7 @@ final class MenuBandSynth { guard started, engine.isRunning, !waveformCaptureEnabled, waveformTapPinReasons.isEmpty, activeNotes.isEmpty, !sampleRecordingActive, !keepEngineWarm, !inputMonitor.isAttached, - ambientAir.level == 0 else { return } + !ambientAir.isActive else { return } interfaceFaultRecovery.suspend(at: ProcessInfo.processInfo.systemUptime) engine.pause() idleSuspended = true diff --git a/slab/menuband/Tests/MenuBandTests/AmbientAirTests.swift b/slab/menuband/Tests/MenuBandTests/AmbientAirTests.swift index 92ac863a3c..f4b3c6e1b2 100644 --- a/slab/menuband/Tests/MenuBandTests/AmbientAirTests.swift +++ b/slab/menuband/Tests/MenuBandTests/AmbientAirTests.swift @@ -95,6 +95,47 @@ final class AmbientAirTests: XCTestCase { } } + func testLeavingMenuBandFadesAirAndReturningRestoresTheSliderLevel() throws { + try withEngine(tone: 0.1) { air, render in + air.setLevel(0.8) + let playing = try render(48_000) + XCTAssertGreaterThan(playing.suffix(4800).map { abs($0 - 0.1) }.max()!, 0.001) + air.setFocused(false) + let faded = try render(48_000) + // Air is gone; the instrument is untouched; the slider still reads 80%. + XCTAssertTrue(faded.suffix(512).allSatisfy { abs($0 - 0.1) < 0.000001 }) + XCTAssertEqual(air.level, 0.8) + XCTAssertFalse(air.isActive) + // Fade, not a cut: the first buffers after leaving still carry Air. + XCTAssertGreaterThan(faded.prefix(512).map { abs($0 - 0.1) }.max()!, 0.001) + air.setFocused(true) + let back = try render(48_000) + XCTAssertGreaterThan(back.suffix(4800).map { abs($0 - 0.1) }.max()!, 0.001) + XCTAssertTrue(air.isActive) + } + } + + func testFocusEdgesDriveEngineActivityOnlyWhenAirIsAudible() { + let suite = "MenuBandAmbientAirTests.\(UUID().uuidString)" + let defaults = UserDefaults(suiteName: suite)! + defer { defaults.removePersistentDomain(forName: suite) } + let air = MenuBandAmbientAir(defaults: defaults) + var activeChanges: [Bool] = [] + air.onActiveChange = { activeChanges.append($0) } + // Slider at zero: focus changes are silent to the engine. + air.setFocused(false); air.setFocused(true) + XCTAssertEqual(activeChanges, []) + air.setLevel(0.5) + air.setFocused(false) + air.setFocused(false) + air.setLevel(0.9) // slider moves while away stay quiet + air.setFocused(true) + air.setLevel(0) // ...and zero while in front releases + air.setFocused(false) // nothing left to release + XCTAssertEqual(activeChanges, [true, false, true, false]) + XCTAssertEqual(air.level, 0) + } + func testRelaunchRemembersColorAndFilterButStartsSilent() { let suite = "MenuBandAmbientAirTests.\(UUID().uuidString)" let defaults = UserDefaults(suiteName: suite)! -- knotmirror 0.0.1