// RangeMapperTests.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("RangeMapper — bipolar curve mapping") struct RangeMapperTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } // MARK: - Endpoints — linear @Test func linearAtMinusOneMapsToLow() { let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 220.0, high: 440.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 220.0) < 1e-9 }) } @Test func linearAtPlusOneMapsToHigh() { let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 220.0, high: 440.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 440.0) < 1e-9 }) } @Test func linearAtZeroMapsToMidpoint() { let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 220.0, high: 440.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 330.0) < 1e-9 }) } // MARK: - Endpoints — exponential @Test func exponentialAtMinusOneMapsToLow() { let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 200.0, high: 2000.0, curve: .exponential) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 200.0) < 1e-9 }) } @Test func exponentialAtPlusOneMapsToHigh() { let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 200.0, high: 2000.0, curve: .exponential) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 2000.0) < 1e-6 }) } @Test func exponentialAtZeroIsGeometricMean() { // t = 0.5 → low * exp(0.5 * log(high/low)) = sqrt(low * high) let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), 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 }) } // MARK: - Endpoints — power @Test func powerTwoAtZeroIsQuarter() { // t = 0.5, t^2 = 0.25 → low + 0.25 * (high - low) = 0 + 25 = 25 let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), 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 }) } @Test func powerHalfAtZeroIsSqrtHalf() { // t = 0.5, t^0.5 = sqrt(0.5) → low + sqrt(0.5)*(high-low) let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .power(0.5)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) let expected: Double = 100.0 * 0.5.squareRoot() #expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-9 }) } @Test func powerAtEndpointsIsExact() { let lo: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 0.0, high: 100.0, curve: .power(2.0)) let hi: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 0.0, high: 100.0, curve: .power(2.0)) let loS: [[Double]] = lo.calculatedSamples(graph: graph, count: 8) let hiS: [[Double]] = hi.calculatedSamples(graph: graph, count: 8) #expect(loS[0].prefix(8).allSatisfy { abs($0 - 0.0) < 1e-9 }) #expect(hiS[0].prefix(8).allSatisfy { abs($0 - 100.0) < 1e-9 }) } // MARK: - Endpoints — curve(k) @Test func curveAtEndpointsIsExact() { let lo: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 0.0, high: 100.0, curve: .curve(4.0)) let hi: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 0.0, high: 100.0, curve: .curve(4.0)) let loS: [[Double]] = lo.calculatedSamples(graph: graph, count: 8) let hiS: [[Double]] = hi.calculatedSamples(graph: graph, count: 8) #expect(loS[0].prefix(8).allSatisfy { abs($0 - 0.0) < 1e-9 }) #expect(hiS[0].prefix(8).allSatisfy { abs($0 - 100.0) < 1e-9 }) } @Test func curveAtMidpointMatchesFormula() { // t = 0.5, k = 4: low + (high-low) * (1 - exp(2)) / (1 - exp(4)) let k: Double = 4.0 let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .curve(k)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) let expected: Double = 100.0 * (1.0 - Foundation.exp(0.5 * k)) / (1.0 - Foundation.exp(k)) #expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-9 }) } @Test func curveNearZeroFallsThroughToLinear() { // |k| < rangeCurveLinearEpsilon (1e-4) → linear behaviour. let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .curve(1.0e-5)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) // t = 0.5 → midpoint = 50 #expect(samples[0].prefix(8).allSatisfy { abs($0 - 50.0) < 1e-9 }) } // MARK: - Modulated low / high @Test func modulatedLowRamps() { // Line ramps low from 100 to 200 over 1 block (8 samples @ 44100 Hz is fine — Line is control-rate, target is reached at end-of-block). let line: Line.Control = Line.Control(start: 100.0, end: 200.0, dur: 8.0 / 44100.0) let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: line, high: 1000.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) // At input = -1 the output equals low exactly. Low is ramping 100 → 200 across the block. // Because RangeMapper reads `low` once per block via floatValue (the block target), the output // is constant within the block at the new target = 200. #expect(abs(samples[0][7] - 200.0) < 1e-6) } @Test func modulatedHighRamps() { let line: Line.Control = Line.Control(start: 500.0, end: 900.0, dur: 8.0 / 44100.0) let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 100.0, high: line) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(abs(samples[0][7] - 900.0) < 1e-6) } // MARK: - mul / add @Test func mulAddAppliesAfterCurve() { // input -1 → low = 100; * 2 + 1 = 201 let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.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 variant @Test func controlLinearReachesTarget() { // -1 → 220; last sample of ramp = 220. let gen: RangeMapper.Control = RangeMapper.Control(ConstantAudioGen(-1.0), low: 220.0, high: 440.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(abs(samples[0][7] - 220.0) < 1e-9) } @Test func controlExponentialReachesTarget() { let gen: RangeMapper.Control = RangeMapper.Control(ConstantAudioGen(0.0), 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(abs(samples[0][7] - expected) < 1e-6) } @Test func controlRampsBetweenBlockTargets() { let src: MutableControlGen = MutableControlGen(-1.0) let gen: RangeMapper.Control = RangeMapper.Control(src, low: 0.0, high: 100.0) // First block: target = 0.0 (from -1), ramp from 0.0 (initial) → 0.0. _ = gen.calculatedSamples(graph: graph, count: 8) advanceBlock(engine) // Second block: change source to +1 → target = 100.0. src.target = 1.0 let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) // Linear ramp from 0 to 100 across 8 samples: last sample == 100. #expect(abs(samples[0][7] - 100.0) < 1e-9) // Earlier samples are strictly below 100. #expect(samples[0][0] < 100.0) // Step size = (100 - 0) / 8 = 12.5; sample[0] = 12.5. #expect(abs(samples[0][0] - 12.5) < 1e-9) } // MARK: - Method sugar @Test func methodSugarMatchesExplicitConstruction() { // osc.range(low: 220, high: 440) ≡ RangeMapper.Audio(osc, low: 220, high: 440) let osc1: ConstantAudioGen = ConstantAudioGen(0.5) let osc2: ConstantAudioGen = ConstantAudioGen(0.5) let viaMethod: RangeMapper.Audio = osc1.range(low: 220.0, high: 440.0) let viaInit: RangeMapper.Audio = RangeMapper.Audio(osc2, low: 220.0, high: 440.0) 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 methodSugarPropagatesCurve() { let osc1: ConstantAudioGen = ConstantAudioGen(0.0) let osc2: ConstantAudioGen = ConstantAudioGen(0.0) let viaMethod: RangeMapper.Audio = osc1.range(low: 200.0, high: 2000.0, curve: .exponential) let viaInit: RangeMapper.Audio = RangeMapper.Audio(osc2, low: 200.0, high: 2000.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(0.0) let wrapped: RangeMapper.Control = src.range(low: 220.0, high: 440.0) let samples: [[Double]] = wrapped.calculatedSamples(graph: graph, count: 8) // 0 → midpoint = 330; ramp ends at 330. #expect(abs(samples[0][7] - 330.0) < 1e-9) } // MARK: - .power(n) shaping @Test func powerWithPositiveN() { let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .power(3.0)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) // t = 0.5, t^3 = 0.125 → 12.5 #expect(samples[0].prefix(8).allSatisfy { abs($0 - 12.5) < 1e-9 }) } @Test func powerWithNegativeNMidpoint() { // n = -1, t = 0.5 → pow(0.5, -1) = 2 → low + 2*(high-low) = 100 + 200 = 300 (overshoots). let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 100.0, high: 200.0, curve: .power(-1.0)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 300.0) < 1e-9 }) } @Test func powerWithNegativeNDivergesAtMinusOne() { // n = -1, t = 0 (input == -1 bipolar) → pow(0, -1) = +inf per IEEE 754. let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 100.0, high: 200.0, curve: .power(-1.0)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { $0.isInfinite && $0 > 0 }) } @Test func powerWithZeroNIsConstantHigh() { // pow(t, 0) = 1 for all t (including t == 0 by IEEE 754) → low + 1*(high-low) = high. let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 100.0, high: 200.0, curve: .power(0.0)) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8) #expect(samples[0].prefix(8).allSatisfy { abs($0 - 200.0) < 1e-9 }) } }