// 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 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() } }