iOS live input via permission-gated engine rebuild

Live input on iOS needs an input channel, but constructing the engine with one
up-front makes iOS prompt for the microphone at the first Start even in
test-loop mode. Instead the engine starts input-less (no prompt) and is rebuilt
on demand:

- ShoeGazeBuilderView gains an optional onEnableLiveInput hook. When the user
  picks live input but the engine has no input channel, the picker offers a
  "Live Input" option that calls the hook; the input binding routes there too.
  On macOS (engine already has an input channel) the hook is nil and live input
  is selected directly.
- The iOS app model implements the hook: request mic access, then on grant
  snapshot the rig (rigData), stop + teardown the engine, rebuild it with one
  input channel, restore the rig (loadRig), and select live input. The new
  ShoeGaze is published so the @Observable host view re-binds.
- Input persistence restores live on relaunch: a saved live choice with the mic
  already authorised triggers the same rebuild silently (no prompt).

Test: a rig captured on a 0-input engine restores onto a freshly built 1-input
engine and live input is then selectable.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 20:52:11 -07:00
parent 4627b3b800
commit 705b4ed534
4 changed files with 208 additions and 37 deletions
@@ -63,6 +63,12 @@ public struct ShoeGazeBuilderView: View {
private let shoeGaze: ShoeGaze
private let hardwareSampleRate: () -> Double?
/// Host hook to enable live input when the current engine has no input channel (iOS).
/// The host requests microphone access and rebuilds the engine with one input channel,
/// preserving the rig, then swaps in a new `ShoeGaze`. `nil` on platforms (macOS) whose
/// engine already exposes an input channel, where live input is selected directly.
private let onEnableLiveInput: (() -> Void)?
/// Index of the board currently being edited.
@State private var selectedBoardIndex: Int = 0
@@ -99,12 +105,19 @@ public struct ShoeGazeBuilderView: View {
/// create at least one board before presenting this view.
/// - hardwareSampleRate: Returns the actual hardware/session sample rate (or `nil`), used by
/// the engine-config Start to warn on a mismatch. Defaults to `nil`.
/// - onEnableLiveInput: Host hook invoked when the user requests live input but the engine
/// has no input channel (iOS). The host should request microphone access, rebuild the
/// engine with one input channel (preserving the rig), and select live input. Defaults to
/// `nil` — on macOS the engine already has an input channel, so live input is selected
/// directly without a rebuild.
public init(
shoeGaze: ShoeGaze,
hardwareSampleRate: @escaping () -> Double? = { nil }
hardwareSampleRate: @escaping () -> Double? = { nil },
onEnableLiveInput: (() -> Void)? = nil
) {
self.shoeGaze = shoeGaze
self.hardwareSampleRate = hardwareSampleRate
self.onEnableLiveInput = onEnableLiveInput
}
// MARK: - Body
@@ -220,8 +233,14 @@ public struct ShoeGazeBuilderView: View {
.foregroundStyle(Color.white.opacity(ShoeGazeBuilderView.labelOpacity))
Picker("Input", selection: inputBinding) {
Text("Test Loop").tag(ShoeGazeBuilderView.testLoopTag)
ForEach(Array(0..<max(0, shoeGaze.engine.numInputChannels)), id: \.self) { channel in
Text("In \(channel)").tag(channel)
if shoeGaze.engine.numInputChannels > 0 {
ForEach(Array(0..<shoeGaze.engine.numInputChannels), id: \.self) { channel in
Text("In \(channel)").tag(channel)
}
} else if onEnableLiveInput != nil {
// No input channel yet: offer live input; selecting it asks the host to
// request mic access and rebuild the engine with one (channel 0).
Text("Live Input").tag(0)
}
}
.pickerStyle(.menu)
@@ -303,10 +322,15 @@ public struct ShoeGazeBuilderView: View {
}
},
set: { newValue in
let source: InputSource = newValue == ShoeGazeBuilderView.testLoopTag
? .testLoop
: .liveInput(channel: newValue)
switchInput(to: source)
if newValue == ShoeGazeBuilderView.testLoopTag {
switchInput(to: .testLoop)
} else if newValue < shoeGaze.engine.numInputChannels {
switchInput(to: .liveInput(channel: newValue))
} else if let onEnableLiveInput {
// Requested a channel the current engine doesn't have (iOS, 0 channels):
// hand off to the host to grant mic + rebuild with an input channel.
onEnableLiveInput()
}
}
)
}
@@ -395,8 +419,13 @@ public struct ShoeGazeBuilderView: View {
let stored: Int = defaults.integer(forKey: ShoeGazeBuilderView.inputSelectionDefaultsKey)
if stored >= 0, stored < shoeGaze.engine.numInputChannels {
switchInput(to: .liveInput(channel: stored))
} else if stored >= 0, shoeGaze.engine.numInputChannels == 0,
MicrophonePermission.isAuthorized, let onEnableLiveInput {
// iOS: a live choice was saved and the mic is already authorised — rebuild the
// engine with an input channel silently (no prompt, since access is granted).
onEnableLiveInput()
}
// A test-loop choice (or an out-of-range channel) leaves the default test loop.
// A test-loop choice (or live with no host hook / no permission) leaves the test loop.
} else if MicrophonePermission.isAuthorized, shoeGaze.engine.numInputChannels > 0 {
// First launch with no saved choice but mic already granted → default to live input.
switchInput(to: .liveInput(channel: 0))