// 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 testLoopBoardsShareGlobalSourceViaTaps() throws { // In .testLoop mode every board reads one shared global source through a TestLoopTap, // rather than owning an independently-phased TestLoopSource — this is what keeps the // boards phase-locked. Verify the head input node of each board is a tap, and that a // board removal leaves the others (and their signal) intact. 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) // Both boards feed from a shared tap, not their own source. #expect(board1.headInputNode != nil) #expect(board1.headInputNode is TestLoopTap) #expect(board2.headInputNode is TestLoopTap) #expect(!(board1.headInputNode is TestLoopSource)) // Removing one board does not disturb the other's input node. shoeGaze.removeBoard(board1) #expect(shoeGaze.boards.count == 1) #expect(board2.headInputNode is TestLoopTap) } @Test func teardownReleasesGlobalTestBusForReuse() throws { // teardown() must remove the global source AND release its bus, so a later addBoard() // can re-allocate cleanly (guards against the global test bus being leaked). let renderDuration: Double = 0.05 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: 16 ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) // Repeatedly build a board then tear the whole rig down. With a small bus pool, a leaked // global test bus on each cycle would eventually exhaust the allocator and throw. for _ in 0 ..< 8 { let board: PedalBoard = try shoeGaze.addBoard() try board.add(OverdrivePedal()) try shoeGaze.finalizeBoard(board) #expect(board.headInputNode is TestLoopTap) shoeGaze.teardown() #expect(shoeGaze.boards.isEmpty) } // Still able to build after all the teardown cycles. let finalBoard: PedalBoard = try shoeGaze.addBoard() #expect(finalBoard.headInputNode is TestLoopTap) } @Test func activeBoardIndexTracksReorderAndRemoval() 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) // Solo board3 (index 2), then move it to the front — index must follow the board. shoeGaze.soloBoard(board3, crossfadeSeconds: 0.0) #expect(shoeGaze.activeBoardIndex == 2) shoeGaze.moveBoard(board3, to: 0) #expect(shoeGaze.boards.first === board3) #expect(shoeGaze.activeBoardIndex == 0) // Now order is [board3, board1, board2], soloed index 0. Removing a later board // leaves the soloed index unchanged. shoeGaze.removeBoard(board2) #expect(shoeGaze.activeBoardIndex == 0) // Re-solo board1 (now index 1), then remove the earlier board3 (index 0): // the soloed index shifts down to 0. shoeGaze.soloBoard(board1, crossfadeSeconds: 0.0) #expect(shoeGaze.activeBoardIndex == 1) shoeGaze.removeBoard(board3) #expect(shoeGaze.activeBoardIndex == 0) // Removing the soloed board clears the index. shoeGaze.removeBoard(board1) #expect(shoeGaze.activeBoardIndex == nil) } @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") } }