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