// UnipolarRangeMapperTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Darwin import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("UnipolarRangeMapper — unipolar curve mapping") struct UnipolarRangeMapperTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } // MARK: - Endpoints @Test func linearAtZeroMapsToLow() { let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.0), low: 80.0, high: 8000.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 80.0) < 1e-9 }) } @Test func linearAtOneMapsToHigh() { let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(1.0), low: 80.0, high: 8000.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 8000.0) < 1e-9 }) } @Test func linearAtHalfMapsToMidpoint() { let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.5), low: 0.0, high: 100.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 50.0) < 1e-9 }) } // MARK: - Curves @Test func exponentialAtHalfIsGeometricMean() { let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.5), low: 200.0, high: 2000.0, curve: .exponential) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) let expected: Double = (200.0 * 2000.0).squareRoot() #expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-6 }) } @Test func powerTwoAtHalfIsQuarter() { let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.5), low: 0.0, high: 100.0, curve: .power(2.0)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 25.0) < 1e-9 }) } // MARK: - mul / add @Test func mulAddAppliesAfterCurve() { // input 0 → low = 100; * 2 + 1 = 201 let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.0), low: 100.0, high: 200.0, mul: 2.0, add: 1.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 201.0) < 1e-9 }) } // MARK: - Control @Test func controlReachesTarget() { let gen: UnipolarRangeMapper.Control = UnipolarRangeMapper.Control(ConstantAudioGen(1.0), low: 80.0, high: 8000.0, curve: .exponential) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(abs(samples[0][7] - 8000.0) < 1e-6) } // MARK: - Method sugar @Test func methodSugarMatchesExplicit() { let src1: ConstantAudioGen = ConstantAudioGen(0.5) let src2: ConstantAudioGen = ConstantAudioGen(0.5) let viaMethod: UnipolarRangeMapper.Audio = src1.unipolarRange(low: 80.0, high: 8000.0, curve: .exponential) let viaInit: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(src2, low: 80.0, high: 8000.0, curve: .exponential) let m: [[Double]] = viaMethod.calculatedSamples(graph: graph, count: 8) let i: [[Double]] = viaInit.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8) for k: Int in 0..<8 { #expect(abs(m[0][k] - i[0][k]) < 1e-12) } } @Test func methodSugarOnControlReturnsControl() { let src: ConstantControlGen = ConstantControlGen(1.0) let wrapped: UnipolarRangeMapper.Control = src.unipolarRange(low: 80.0, high: 8000.0) let samples: [[Double]] = wrapped.calculatedSamples(graph: graph, count: 8) #expect(abs(samples[0][7] - 8000.0) < 1e-9) } }