// StereoRoutingTests.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("Stereo routing") struct StereoRoutingTests { // MARK: - Helpers /// Audio bus pool size for test engines. private let numAudioBuses: Int = 128 /// Render duration in seconds. private let renderDuration: Double = 0.25 /// Splits interleaved stereo samples `[L0, R0, L1, R1, …]` into separate channel arrays. private func deinterleave(_ samples: [Float]) -> (left: [Float], right: [Float]) { var left: [Float] = [] var right: [Float] = [] left.reserveCapacity(samples.count / 2) right.reserveCapacity(samples.count / 2) var index: Int = 0 while index + 1 < samples.count { left.append(samples[index]) right.append(samples[index + 1]) index += 2 } return (left, right) } /// `true` if the two channels are sample-for-sample equal within `tol`. private func channelsEqual(_ a: [Float], _ b: [Float], tol: Float = 1e-5) -> Bool { guard a.count == b.count else { return false } for index: Int in 0.. tol { return false } return true } /// `true` if the two channels differ by more than `tol` somewhere. private func channelsDiffer(_ a: [Float], _ b: [Float], tol: Float = 1e-4) -> Bool { guard a.count == b.count else { return true } return zip(a, b).contains { abs($0 - $1) > tol } } // MARK: - Widener @Test func wideningPedalMakesBoardStereo() throws { 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() #expect(board.width == 1, "Board should start mono") try board.add(OverdrivePedal()) try board.add(OctaveUpSplitterPedal()) #expect(board.width == 2, "Widener should make the board stereo") #expect(board.outputBusRight != nil, "Stereo board should have a right tail bus") try shoeGaze.finalizeBoard(board) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(hasNonSilentOutput(samples), "Widener chain produced silence") let (left, right): ([Float], [Float]) = deinterleave(samples) #expect(channelsDiffer(left, right), "Clean and octave channels should differ") } // MARK: - Auto-stereoize @Test func monoPedalAutoStereoizesAfterWidener() throws { 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(OctaveUpSplitterPedal()) // A plain mono pedal added in a stereo region must run on both channels. try board.add(DelayPedal()) #expect(board.width == 2, "Width should stay stereo after a mono pedal") // The delay pedal runs two independent graphs (one per channel). guard let delay: DelayPedal = board.pedals.compactMap({ $0 as? DelayPedal }).first else { Issue.record("DelayPedal not found") return } #expect(delay.isStereo, "Auto-stereoized pedal should report isStereo") #expect(delay.outputBusRight != nil, "Auto-stereoized pedal should have a right output bus") try shoeGaze.finalizeBoard(board) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(hasNonSilentOutput(samples), "Auto-stereoized chain produced silence") } // MARK: - Stereo-native @Test func stereoNativePedalFromMonoWidensAndDiffersAcrossChannels() throws { 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() // Short tap so several echoes land inside the render window (the L→R bounce that // makes the channels differ only appears once the first echo arrives). let pingPong: PingPongDelayPedal = PingPongDelayPedal() pingPong.time = 0.04 try board.add(pingPong) #expect(board.width == 2, "Stereo-native pedal should widen a mono board") try shoeGaze.finalizeBoard(board) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(hasNonSilentOutput(samples), "Ping-pong produced silence") let (left, right): ([Float], [Float]) = deinterleave(samples) // Right is the left echo train delayed one tap → channels must differ. #expect(channelsDiffer(left, right), "Ping-pong channels should differ (bounce)") } // MARK: - mergeToMono @Test func mergeToMonoNarrowsBackToMono() throws { 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(OctaveUpSplitterPedal()) #expect(board.width == 2) try board.mergeToMono() #expect(board.width == 1, "mergeToMono should narrow the chain") #expect(board.outputBusRight == nil, "Mono tail should have no right bus") try board.add(ReverbPedal()) // mono pedal after merge, width-1 try shoeGaze.finalizeBoard(board) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(hasNonSilentOutput(samples), "Merged-to-mono chain produced silence") // Mono board → Pan2 centre → both hardware channels identical. let (left, right): ([Float], [Float]) = deinterleave(samples) #expect(channelsEqual(left, right), "Mono board should produce identical L/R") } // MARK: - Width cap @Test func wideningTwiceThrows() throws { 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() try board.add(OctaveUpSplitterPedal()) #expect(throws: ShoeGazeError.stereoWidthExceeded) { try board.add(OctaveUpSplitterPedal()) } } // MARK: - splitStereo route modes /// Builds a stereo split board (left = Fuzz, right = clean Overdrive) and renders it /// at the given route, returning the deinterleaved output channels. private func renderSplit(route: StereoRoute) throws -> (left: [Float], right: [Float]) { 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: StereoSplitSection = try board.splitStereo() try section.left.add(FuzzPedal()) try section.right.add(OverdrivePedal()) try board.collectStereo(section) section.route = route #expect(board.width == 2, "collectStereo should make the board stereo") try shoeGaze.finalizeBoard(board) try engine.start() return deinterleave(readStereoCAF(url: url)) } @Test func splitStereoRouteStereoDiffersAcrossChannels() throws { let (left, right): ([Float], [Float]) = try renderSplit(route: .stereo) #expect(hasNonSilentOutput(left + right), "stereo route produced silence") #expect(channelsDiffer(left, right), "stereo route: left and right chains should differ") } @Test func splitStereoLeftOnlyIsCenteredSolo() throws { let (left, right): ([Float], [Float]) = try renderSplit(route: .leftOnly) #expect(hasNonSilentOutput(left + right), "leftOnly produced silence") // Centered solo: the left chain feeds both output channels identically. #expect(channelsEqual(left, right), "leftOnly should be centered (L == R)") } @Test func splitStereoRightOnlyIsCenteredSolo() throws { let (left, right): ([Float], [Float]) = try renderSplit(route: .rightOnly) #expect(hasNonSilentOutput(left + right), "rightOnly produced silence") #expect(channelsEqual(left, right), "rightOnly should be centered (L == R)") } @Test func splitStereoBypassPassesCleanThroughBothChannels() throws { let (left, right): ([Float], [Float]) = try renderSplit(route: .bypass) #expect(hasNonSilentOutput(left + right), "bypass produced silence") // Clean pre-split signal to both channels → identical L/R. #expect(channelsEqual(left, right), "bypass should pass clean signal to both channels") } @Test func stereoRouteGetterReflectsSetMode() throws { 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: StereoSplitSection = try board.splitStereo() try section.left.add(FuzzPedal()) try section.right.add(OverdrivePedal()) try board.collectStereo(section) for route: StereoRoute in StereoRoute.allCases { section.route = route #expect(section.route == route, "route getter should reflect \(route)") } } // MARK: - Teardown @Test func teardownRemovesStereoNodes() throws { 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: StereoSplitSection = try board.splitStereo() try section.left.add(DelayPedal()) try section.right.add(PingPongDelayPedal()) try board.collectStereo(section) try board.add(ReverbPedal()) // auto-stereoized post-collect try shoeGaze.finalizeBoard(board) shoeGaze.teardown() #expect(shoeGaze.boards.isEmpty, "boards should be empty after teardown") } // MARK: - Mono regression @Test func plainMonoBoardStaysMono() throws { 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()) try board.add(DelayPedal()) try board.add(ReverbPedal()) #expect(board.width == 1, "Plain pedal chain should stay mono") #expect(board.outputBusRight == nil) try shoeGaze.finalizeBoard(board) try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(hasNonSilentOutput(samples), "Mono board produced silence") } }