// ParallelSectionTests.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("ParallelSection") struct ParallelSectionTests { // MARK: - Helpers /// Audio bus pool size for test engines. private let numAudioBuses: Int = 128 /// Render duration in seconds. private let renderDuration: Double = 0.2 // MARK: - Full parallel chain render @Test func parallelChainRendersNonSilent() throws { // Full board: OverdrivePedal → split → branchA(DelayPedal), branchB(FuzzPedal) // → merge → ReverbPedal → output. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() try board.add(OverdrivePedal()) let section: ParallelSection = try board.split() try section.branchA.add(DelayPedal()) try section.branchB.add(FuzzPedal()) try board.merge(section) try board.add(ReverbPedal()) let master: TestRouteMaster = TestRouteMaster(inputBusIndex: board.outputBus.index) guard let tailNode: any Synthesizer = board.tailNode else { Issue.record("Board tail node was nil after adding pedals") return } try engine.add(master, position: NodePosition.after(tailNode)) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "Parallel chain produced no samples") #expect(hasNonSilentOutput(samples), "Parallel chain produced silence") } @Test func parallelChainWithoutPreOrPostPedals() throws { // Board with only a parallel section — no pre-split or post-merge pedals. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() try section.branchA.add(OverdrivePedal()) try section.branchB.add(ReverbPedal()) try board.merge(section) let master: TestRouteMaster = TestRouteMaster(inputBusIndex: board.outputBus.index) guard let tailNode: any Synthesizer = board.tailNode else { Issue.record("Board tail node was nil") return } try engine.add(master, position: NodePosition.after(tailNode)) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "Board with only a parallel section produced no samples") #expect(hasNonSilentOutput(samples), "Board with only a parallel section produced silence") } // MARK: - Empty branches @Test func parallelSectionWithEmptyBranches() throws { // split → merge with no pedals in either branch — signal passes through unchanged. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() // No pedals added to either branch — both branch input buses are identical to their // tail buses. The merger sums two copies of the splitter outputs at unity gain. try board.merge(section) let master: TestRouteMaster = TestRouteMaster(inputBusIndex: board.outputBus.index) guard let tailNode: any Synthesizer = board.tailNode else { Issue.record("Board tail node was nil") return } try engine.add(master, position: NodePosition.after(tailNode)) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "Empty-branch parallel section produced no samples") #expect(hasNonSilentOutput(samples), "Empty-branch parallel section produced silence") } // MARK: - Mode: branch A only @Test func splitterModeAMutesB() throws { // Set splitter.mode = .a — branch A passes, branch B is muted. // The merger still produces non-silent output from branch A alone. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() try section.branchA.add(OverdrivePedal()) try section.branchB.add(FuzzPedal()) try board.merge(section) // Set mode .a (mute B) before starting. section.splitter.mode = .a let master: TestRouteMaster = TestRouteMaster(inputBusIndex: board.outputBus.index) guard let tailNode: any Synthesizer = board.tailNode else { Issue.record("Board tail node was nil") return } try engine.add(master, position: NodePosition.after(tailNode)) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "mode .a produced no samples") #expect(hasNonSilentOutput(samples), "mode .a: branch A should produce non-silent output") } // MARK: - Mode: branch B only @Test func splitterModeBMutesA() throws { // Set splitter.mode = .b — branch B passes, branch A is muted. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() try section.branchA.add(OverdrivePedal()) try section.branchB.add(FuzzPedal()) try board.merge(section) // Set mode .b (mute A) before starting. section.splitter.mode = .b let master: TestRouteMaster = TestRouteMaster(inputBusIndex: board.outputBus.index) guard let tailNode: any Synthesizer = board.tailNode else { Issue.record("Board tail node was nil") return } try engine.add(master, position: NodePosition.after(tailNode)) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "mode .b produced no samples") #expect(hasNonSilentOutput(samples), "mode .b: branch B should produce non-silent output") } // MARK: - ShoeGaze finalizeBoard with parallel section @Test func shoegazeFinalizeBoardAfterParallelSection() throws { // Confirm ShoeGaze.finalizeBoard positions the master after the merger correctly. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() try section.branchA.add(DelayPedal()) try section.branchB.add(FuzzPedal()) try board.merge(section) // finalizeBoard uses board.tailNode — should chain after the merger. try shoeGaze.finalizeBoard(board) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "Finalized board with parallel section produced no samples") #expect(hasNonSilentOutput(samples), "Finalized board with parallel section produced silence") } // MARK: - Teardown @Test func boardTeardownRemovesAllParallelNodes() throws { // Confirm teardown succeeds and does not throw when routing nodes are present. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: 0.05, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() try section.branchA.add(OverdrivePedal()) try section.branchB.add(ReverbPedal()) try board.merge(section) try shoeGaze.finalizeBoard(board) // Teardown should complete without error (remove-before-bus-release ordering). shoeGaze.teardown() #expect(shoeGaze.boards.isEmpty, "boards should be empty after teardown") } // MARK: - post-merge add chains correctly @Test func addAfterMergeChainsFurtherPedals() throws { // After merge, board.add(_:) should chain after the merger and still produce sound. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let board: PedalBoard = try shoeGaze.addBoard() let section: ParallelSection = try board.split() try section.branchA.add(DelayPedal()) try section.branchB.add(FuzzPedal()) try board.merge(section) // Add a post-merge pedal that chains after the merger. try board.add(ReverbPedal()) let master: TestRouteMaster = TestRouteMaster(inputBusIndex: board.outputBus.index) guard let tailNode: any Synthesizer = board.tailNode else { Issue.record("Board tail node was nil after post-merge add") return } try engine.add(master, position: NodePosition.after(tailNode)) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "Post-merge chain produced no samples") #expect(hasNonSilentOutput(samples), "Post-merge chain produced silence") } }