Files
ShoeGaze/Sources/LyrebirdShoeGaze/Input/MicrophonePermission.swift
T
josh 46e075fb8c Add live-input mic permission, audio-input entitlement, and engine-config review fixes
Live input was silent on macOS because the apps ran under hardened runtime
without the microphone entitlement, so AUHAL input received silent zeros and
the OS never prompted. Fixes:

- Add com.apple.security.device.audio-input entitlement to the macOS Builder
  and Demo apps (CODE_SIGN_ENTITLEMENTS wired in both targets).
- Add MicrophonePermission helper (AVCaptureDevice.requestAccess) and gate
  live-input selection through it in the Builder view and Demo controller;
  deny falls back to the test loop with an explanatory alert.
- Fix ShoeGazeDemo inputChannels 6 -> 1 (built-in mics are mono; 6 made
  CoreAudio deliver silence).

Engine-config review fixes:
- installScopeTap now requires the engine stopped (precondition) and
  reconfigureEngine reinstalls the tap on every exit path via defer, so
  engine.scopeTap never points at a stale channel layout.
- EngineConfigView stops the engine on a post-start (iOS) sample-rate
  mismatch instead of running pitch-shifted, and surfaces non-mismatch
  start errors through the alert.
- Remove redundant installScopeTap call in the demo controller (ShoeGaze.init
  already installs it).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 09:36:35 -07:00

53 lines
2.1 KiB
Swift

// MicrophonePermission.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import AVFoundation
/// Microphone (audio-input) authorisation helper for live-input mode.
///
/// On macOS, an AUHAL input unit does **not** reliably trigger the system microphone prompt;
/// without an explicit `AVCaptureDevice.requestAccess(for: .audio)` the OS hands the app a stream
/// of silent zeros (no error). On iOS the record-permission prompt is likewise required before the
/// session delivers input. This helper centralises the cross-platform request so every app routes
/// live input through the same gate.
///
/// The Info.plist must contain `NSMicrophoneUsageDescription`, or the request crashes at runtime.
public enum MicrophonePermission {
/// Current authorisation state for audio capture.
public static var status: AVAuthorizationStatus {
AVCaptureDevice.authorizationStatus(for: .audio)
}
/// `true` only when audio capture is already authorised.
public static var isAuthorized: Bool {
status == .authorized
}
/// Ensures microphone access, prompting the user once if the state is undetermined.
///
/// - `authorized` → `completion(true)` immediately (next run loop, on the main actor).
/// - `notDetermined` → shows the system prompt; `completion` fires with the user's choice.
/// - `denied` / `restricted` → `completion(false)` (the app must direct the user to Settings).
///
/// `completion` is always delivered on the main actor so callers can drive UI / engine state
/// directly without further hops.
///
/// - Parameter completion: Receives the final grant state.
public static func request(_ completion: @escaping @MainActor (Bool) -> Void) {
switch status {
case .authorized:
Task { @MainActor in completion(true) }
case .notDetermined:
AVCaptureDevice.requestAccess(for: .audio) { granted in
Task { @MainActor in completion(granted) }
}
default:
Task { @MainActor in completion(false) }
}
}
}