Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/ShoeGazeMultiBoardTests.swift
T
josh 705b4ed534 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>
2026-06-23 20:52:11 -07:00

219 lines
8.3 KiB
Swift

// ShoeGazeMultiBoardTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("ShoeGaze multi-board")
struct ShoeGazeMultiBoardTests {
@Test func addBoardAssignsLabelAndFinalizeWiresMasterNode() throws {
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
let board1: PedalBoard = try shoeGaze.addBoard()
#expect(board1.label == "Board 1")
#expect(board1.masterGainNode == nil, "Master node is nil until finalizeBoard")
try board1.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board1)
#expect(board1.masterGainNode != nil, "finalizeBoard must expose the master gain node")
let board2: PedalBoard = try shoeGaze.addBoard()
#expect(board2.label == "Board 2")
try board2.add(DelayPedal())
try shoeGaze.finalizeBoard(board2)
#expect(shoeGaze.boards.count == 2)
}
@Test func rigSnapshotRestoresAcrossEnginesWithInputChannel() throws {
// Backs the iOS "enable live input" rebuild: a rig captured on a 0-input engine must
// restore onto a freshly built 1-input engine, and live input must then be selectable.
let renderDuration: Double = 0.05
// Source engine: no input channels (the iOS default before live input is enabled).
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numInputChannels: 0,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: urlA) }
let shoeGazeA: ShoeGaze = ShoeGaze(engine: engineA)
let board1: PedalBoard = try shoeGazeA.addBoard()
board1.label = "Lead"
try shoeGazeA.appendPedal(type: "Overdrive", to: board1)
try shoeGazeA.finalizeBoard(board1)
let board2: PedalBoard = try shoeGazeA.addBoard()
board2.label = "Clean"
try shoeGazeA.appendPedal(type: "Graphic EQ", to: board2)
try shoeGazeA.finalizeBoard(board2)
let snapshot: Data = try shoeGazeA.rigData()
// Destination engine: one input channel (the rebuilt engine for live input).
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numInputChannels: 1,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: urlB) }
let shoeGazeB: ShoeGaze = ShoeGaze(engine: engineB)
try shoeGazeB.loadRig(from: snapshot)
// The rig round-trips: same board count and labels.
#expect(shoeGazeB.boards.count == 2)
#expect(shoeGazeB.boards.first?.label == "Lead")
#expect(shoeGazeB.boards.last?.label == "Clean")
// Live input is now selectable on the rebuilt engine.
#expect(throws: Never.self) {
try shoeGazeB.setInputSource(.liveInput(channel: 0))
}
}
@Test func soloBlendAndAllControlBoardAudibility() throws {
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
let board1: PedalBoard = try shoeGaze.addBoard()
try board1.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board1)
let board2: PedalBoard = try shoeGaze.addBoard()
try board2.add(DelayPedal())
try shoeGaze.finalizeBoard(board2)
let board3: PedalBoard = try shoeGaze.addBoard()
try board3.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board3)
let instantCrossfade: Double = 0.0
// Default: every board audible, no solo.
#expect(shoeGaze.isBoardAudible(board1))
#expect(shoeGaze.isBoardAudible(board2))
#expect(shoeGaze.isBoardAudible(board3))
#expect(shoeGaze.activeBoardIndex == nil)
// Solo board2 — exclusive.
shoeGaze.soloBoard(board2, crossfadeSeconds: instantCrossfade)
#expect(!shoeGaze.isBoardAudible(board1))
#expect(shoeGaze.isBoardAudible(board2))
#expect(!shoeGaze.isBoardAudible(board3))
#expect(shoeGaze.activeBoardIndex == 1)
// Blend: re-enable board1 without disturbing the others, and clear the solo index.
shoeGaze.setBoard(board1, audible: true, crossfadeSeconds: instantCrossfade)
#expect(shoeGaze.isBoardAudible(board1))
#expect(shoeGaze.isBoardAudible(board2))
#expect(!shoeGaze.isBoardAudible(board3))
#expect(shoeGaze.activeBoardIndex == nil)
// Silence a single board.
shoeGaze.setBoard(board1, audible: false, crossfadeSeconds: instantCrossfade)
#expect(!shoeGaze.isBoardAudible(board1))
#expect(shoeGaze.isBoardAudible(board2))
// All back to summed.
shoeGaze.selectAllBoards(crossfadeSeconds: instantCrossfade)
#expect(shoeGaze.isBoardAudible(board1))
#expect(shoeGaze.isBoardAudible(board2))
#expect(shoeGaze.isBoardAudible(board3))
#expect(shoeGaze.activeBoardIndex == nil)
}
@Test func setBoardIgnoresUnmanagedBoard() throws {
let renderDuration: Double = 0.05
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, numAudioBuses: 64)
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, numAudioBuses: 64)
defer {
try? FileManager.default.removeItem(at: urlA)
try? FileManager.default.removeItem(at: urlB)
}
let shoeGazeA: ShoeGaze = ShoeGaze(engine: engineA)
let shoeGazeB: ShoeGaze = ShoeGaze(engine: engineB)
let boardA: PedalBoard = try shoeGazeA.addBoard()
try boardA.add(OverdrivePedal())
try shoeGazeA.finalizeBoard(boardA)
let boardB: PedalBoard = try shoeGazeB.addBoard()
try boardB.add(DelayPedal())
try shoeGazeB.finalizeBoard(boardB)
// Toggling a board not owned by this manager is a no-op and must not change state.
shoeGazeA.setBoard(boardB, audible: false, crossfadeSeconds: 0.0)
#expect(shoeGazeA.isBoardAudible(boardA))
#expect(shoeGazeB.isBoardAudible(boardB))
}
@Test func removeBoardLeavesOtherBoards() throws {
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
let board1: PedalBoard = try shoeGaze.addBoard()
try board1.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board1)
let board2: PedalBoard = try shoeGaze.addBoard()
try board2.add(DelayPedal())
try shoeGaze.finalizeBoard(board2)
#expect(shoeGaze.boards.count == 2)
shoeGaze.removeBoard(board1)
#expect(shoeGaze.boards.count == 1)
#expect(shoeGaze.boards.first === board2, "Remaining board must be the one not removed")
}
@Test func twoBoardsRenderNonSilent() throws {
let renderDuration: Double = 0.2
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
let board1: PedalBoard = try shoeGaze.addBoard()
try board1.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board1)
let board2: PedalBoard = try shoeGaze.addBoard()
try board2.add(DelayPedal())
try shoeGaze.finalizeBoard(board2)
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
#expect(!samples.isEmpty, "Two-board ShoeGaze produced no samples")
#expect(hasNonSilentOutput(samples), "Two-board ShoeGaze produced silence")
}
}