// PanXYTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("PanXY") struct PanXYTests { func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine { let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) try engine.start() return engine } /// Last-sample gains for each channel with unit input. /// /// `x` / `y` are `@LiveParam` ParameterNodes — when set immediately before the /// first block they ramp from 0 → target. The last sample equals the target. func gains(x: Double, y: Double) throws -> [Double] { let engine: SynthesisEngine = try makeEngine() let graph: SynthGraph = makeSynthGraph(engine: engine) defer { engine.stop() } let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0)) pan.x = x pan.y = y let blockSize: Int = engine.blockSize let out: [[Double]] = pan.calculatedSamples(graph: graph, count: blockSize) let lastIdx: Int = blockSize - 1 return out.map { ch in lastIdx < ch.count ? ch[lastIdx] : 0.0 } } // MARK: - Channel count @Test func outputHasFourChannels() throws { let engine: SynthesisEngine = try makeEngine() let graph: SynthGraph = makeSynthGraph(engine: engine) defer { engine.stop() } let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0)) let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 64) #expect(out.count == 4, "PanXY must produce exactly 4 channels") } @Test func outputChannelCountIsFour() { let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0)) #expect(pan.outputChannelCount == 4) } // MARK: - Hard corners @Test func hardLFCorner() throws { let g: [Double] = try gains(x: -1.0, y: +1.0) #expect(abs(g[0] - 1.0) < 1e-6, "Hard LF: ch0 should be 1.0") #expect(abs(g[1]) < 1e-6, "Hard LF: ch1 should be 0") #expect(abs(g[2]) < 1e-6, "Hard LF: ch2 should be 0") #expect(abs(g[3]) < 1e-6, "Hard LF: ch3 should be 0") } @Test func hardRFCorner() throws { let g: [Double] = try gains(x: +1.0, y: +1.0) #expect(abs(g[0]) < 1e-6) #expect(abs(g[1] - 1.0) < 1e-6) #expect(abs(g[2]) < 1e-6) #expect(abs(g[3]) < 1e-6) } @Test func hardRRCorner() throws { let g: [Double] = try gains(x: +1.0, y: -1.0) #expect(abs(g[0]) < 1e-6) #expect(abs(g[1]) < 1e-6) #expect(abs(g[2] - 1.0) < 1e-6) #expect(abs(g[3]) < 1e-6) } @Test func hardLRCorner() throws { let g: [Double] = try gains(x: -1.0, y: -1.0) #expect(abs(g[0]) < 1e-6) #expect(abs(g[1]) < 1e-6) #expect(abs(g[2]) < 1e-6) #expect(abs(g[3] - 1.0) < 1e-6) } // MARK: - Center @Test func centerPositionHasEqualGainAllChannels() throws { let g: [Double] = try gains(x: 0.0, y: 0.0) let expected: Double = 0.5 // cos(π/4)*sin(π/4) = (√2/2)² = 0.5 for ch: Int in 0..<4 { #expect(abs(g[ch] - expected) < 1e-6, "Center: ch\(ch) should be \(expected), got \(g[ch])") } } // MARK: - Power invariant @Test func powerInvariantAtVariousPositions() throws { let positions: [(Double, Double)] = [ (0.0, 0.0), (-1.0, 1.0), (1.0, -1.0), (0.5, -0.3), (-0.7, 0.8), (-0.5, -0.5), (0.25, 0.75) ] for (xv, yv) in positions { let g: [Double] = try gains(x: xv, y: yv) let sumSq: Double = g.reduce(0.0) { $0 + $1 * $1 } #expect(abs(sumSq - 1.0) < 1e-5, "Power invariant failed at x=\(xv) y=\(yv): Σ ch² = \(sumSq)") } } // MARK: - Axis edges @Test func fullLeftGivesZeroRightChannels() throws { let g: [Double] = try gains(x: -1.0, y: 0.0) #expect(abs(g[1]) < 1e-6, "Full left: RF (ch1) should be 0") #expect(abs(g[2]) < 1e-6, "Full left: RR (ch2) should be 0") } @Test func fullRightGivesZeroLeftChannels() throws { let g: [Double] = try gains(x: +1.0, y: 0.0) #expect(abs(g[0]) < 1e-6, "Full right: LF (ch0) should be 0") #expect(abs(g[3]) < 1e-6, "Full right: LR (ch3) should be 0") } @Test func fullFrontGivesZeroRearChannels() throws { let g: [Double] = try gains(x: 0.0, y: +1.0) #expect(abs(g[2]) < 1e-6, "Full front: RR (ch2) should be 0") #expect(abs(g[3]) < 1e-6, "Full front: LR (ch3) should be 0") } @Test func fullRearGivesZeroFrontChannels() throws { let g: [Double] = try gains(x: 0.0, y: -1.0) #expect(abs(g[0]) < 1e-6, "Full rear: LF (ch0) should be 0") #expect(abs(g[1]) < 1e-6, "Full rear: RF (ch1) should be 0") } // MARK: - Clamping @Test func outOfRangeXIsClamped() throws { let gClamped: [Double] = try gains(x: 2.0, y: 0.0) // clamps to 1.0 let gRef: [Double] = try gains(x: 1.0, y: 0.0) for ch: Int in 0..<4 { #expect(abs(gClamped[ch] - gRef[ch]) < 1e-6, "x > 1 should clamp: ch\(ch) \(gClamped[ch]) vs \(gRef[ch])") } } @Test func outOfRangeYIsClamped() throws { let gClamped: [Double] = try gains(x: 0.0, y: -3.0) // clamps to -1.0 let gRef: [Double] = try gains(x: 0.0, y: -1.0) for ch: Int in 0..<4 { #expect(abs(gClamped[ch] - gRef[ch]) < 1e-6, "y < -1 should clamp: ch\(ch)") } } // MARK: - mul / add @Test func mulIsAppliedPerChannel() throws { let engine: SynthesisEngine = try makeEngine() let graph: SynthGraph = makeSynthGraph(engine: engine) defer { engine.stop() } let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0), mul: 2.0) pan.x = 0.0 pan.y = 0.0 let out: [[Double]] = pan.calculatedSamples(graph: graph, count: engine.blockSize) let last: Int = engine.blockSize - 1 // Center gains = 0.5, ×2 = 1.0 per channel at last sample (ramp settled). for ch: Int in 0..<4 { #expect(abs(out[ch][last] - 1.0) < 1e-6, "mul=2 at center: ch\(ch) should equal 1.0, got \(out[ch][last])") } } // MARK: - Channel aliases @Test func channelAliasesRouteToCorrectRows() throws { let engine: SynthesisEngine = try makeEngine() let graph: SynthGraph = makeSynthGraph(engine: engine) defer { engine.stop() } let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0)) pan.x = -1.0 pan.y = +1.0 // ramp toward hard LF; last sample = corner let direct: [[Double]] = pan.calculatedSamples(graph: graph, count: engine.blockSize) let last: Int = engine.blockSize - 1 let fl: [[Double]] = pan.frontLeft.calculatedSamples(graph: graph, count: engine.blockSize) let fr: [[Double]] = pan.frontRight.calculatedSamples(graph: graph, count: engine.blockSize) let rr: [[Double]] = pan.rearRight.calculatedSamples(graph: graph, count: engine.blockSize) let rl: [[Double]] = pan.rearLeft.calculatedSamples(graph: graph, count: engine.blockSize) #expect(abs(fl[0][last] - direct[0][last]) < 1e-9, "frontLeft must equal ch0") #expect(abs(fr[0][last] - direct[1][last]) < 1e-9, "frontRight must equal ch1") #expect(abs(rr[0][last] - direct[2][last]) < 1e-9, "rearRight must equal ch2") #expect(abs(rl[0][last] - direct[3][last]) < 1e-9, "rearLeft must equal ch3") } // MARK: - Fan-out cache @Test func fanOutCacheProducesIdenticalOutput() throws { let engine: SynthesisEngine = try makeEngine(blockSize: 128) let graph: SynthGraph = makeSynthGraph(engine: engine) defer { engine.stop() } let pan: PanXY = PanXY(input: ConstantGeneratorInput(0.7)) pan.x = 0.3 pan.y = -0.4 let out1: [[Double]] = pan.calculatedSamples(graph: graph, count: 128) let out2: [[Double]] = pan.calculatedSamples(graph: graph, count: 128) for ch: Int in 0..<4 { #expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)") } } } // Parameter ramping suite lives in `PanXYParameterRampingTests.swift` // to keep one `@Suite` per file.