Files
ShoeGaze/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSAppModel.swift
T
josh 4627b3b800 Boards sheet, ⌘1–9 switching, unified player bar, input persistence
Boards sheet (BoardsSheetView): rename, drag-reorder, swipe-delete, plus
per-board enable/solo/level and a Solo/Blend + crossfade header. Opened from a
"Boards…" button in the builder; presented with medium/large detents on iOS.
Makes ShoeGaze.moveBoard public to back the reorder.

Keyboard switching: hidden ⌘1…⌘9 buttons in the builder solo (live-switch to)
the matching board and select it as the edit target.

Unified player: ShoeGazeView drops its bespoke Active picker + crossfade field
and hosts the shared BoardBarView, matching the builder.

Input persistence: the builder saves the input choice (test loop vs live
channel) to UserDefaults and restores it on appear. With no saved choice it
defaults to live input when the mic is already authorised and an input channel
exists, else stays on the test loop. The iOS builder now opens one input
channel so live input is actually available there.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 20:38:43 -07:00

91 lines
3.4 KiB
Swift

// ShoeGazeBuilderiOSAppModel.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import AVFoundation
import Observation
import LyrebirdSynth
import LyrebirdShoeGaze
/// Observable model owning the `SynthesisEngine` and `ShoeGaze` session for the iOS builder.
///
/// `AVAudioSession` is activated at init so the hardware sample rate is known before the engine
/// is constructed (iOS devices typically lock to 48 000 Hz). The session is seeded with one board
/// holding a single pedal so the builder opens with an audible starting point; the user adds /
/// removes / reorders pedals from the palette. Input defaults to `.testLoop`, so no microphone
/// permission is required to start.
@Observable
@MainActor
final class ShoeGazeBuilderiOSAppModel {
// MARK: - Engine configuration constants
private static let blockSize: Int = 512
private static let outputChannels: Int = 2
// Kept at 0: with input channels the iOS driver switches the session to `.playAndRecord`
// and enables the RemoteIO input element, which makes iOS prompt for microphone access at
// engine start even in test-loop mode. iOS live input needs a permission-gated engine
// rebuild (request mic → reconstruct the engine with an input channel) before it can be
// enabled here without that surprise prompt.
private static let numAudioBuses: Int = 256
private static let seedPedalType: String = "Overdrive"
// MARK: - Owned objects
/// The active ShoeGaze builder session.
private(set) var shoeGaze: ShoeGaze
private let engine: SynthesisEngine
private let iosDriver: iOSAudioDriver
/// The sample rate the audio session actually committed to (for the config mismatch warning).
var hardwareSampleRate: Double? { iosDriver.sessionSampleRate }
// MARK: - Init
init() {
let session: AVAudioSession = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playAndRecord,
mode: .default,
options: [.defaultToSpeaker, .allowBluetoothHFP])
try session.setActive(true)
} catch {
// Surface the failure: the engine is built from `session.sampleRate` below, which may
// report a stale/default rate if activation failed.
print("[ShoeGazeBuilderiOS] AVAudioSession setup failed: \(error)")
}
let sampleRate: Double = session.sampleRate
let driver: iOSAudioDriver = iOSAudioDriver()
let eng: SynthesisEngine = SynthesisEngine(
blockSize: ShoeGazeBuilderiOSAppModel.blockSize,
sampleRate: sampleRate,
numOutputChannels: ShoeGazeBuilderiOSAppModel.outputChannels,
numInputChannels: 0,
numAudioBuses: ShoeGazeBuilderiOSAppModel.numAudioBuses,
driver: driver
)
self.iosDriver = driver
self.engine = eng
self.shoeGaze = ShoeGaze(engine: eng)
do {
let board: PedalBoard = try shoeGaze.addBoard()
board.label = "Board 1"
try shoeGaze.appendPedal(type: ShoeGazeBuilderiOSAppModel.seedPedalType, to: board)
} catch {
print("[ShoeGazeBuilderiOS] seed board failed: \(error)")
}
}
// MARK: - Transport
/// Stops the audio engine. Call from `.onDisappear`.
func stop() {
engine.stop()
}
}