// LyrebirdBufferWavetableTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - 2b.1 Partial series fill @Suite("LyrebirdBuffer fill(partials:)") struct BufferFillPartialsTests { @Test("fill(partials:) single partial produces a sine wave") func singlePartialIsSine() { let tableSize: Int = 1024 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fill(partials: 1) // Single sine partial should peak at exactly 1.0 after normalisation var peak: Double = 0.0 for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("fill(harmonics:amplitudes:) with single harmonic matches fill(partials:1)") func singleHarmonicMatchesSinglePartial() { let tableSize: Int = 1024 let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) bufA.fill(partials: 1) bufB.fill(harmonics: [1], amplitudes: [1.0]) for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } // Symmetry: square wave should be antisymmetric (buffer[N/2+i] ≈ -buffer[i]) let half: Int = tableSize / 2 // Check a few points away from zero-crossings let checkPoint: Int = tableSize / 8 #expect(abs(buffer[checkPoint] + buffer[checkPoint + half]) < 1e-3, "Square wave should be half-cycle antisymmetric") } @Test("fill(harmonics:amplitudes:) zero amplitude partial is ignored") func zeroAmplitudeIgnored() { let tableSize: Int = 512 let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) bufA.fill(harmonics: [1], amplitudes: [1.0]) bufB.fill(harmonics: [1, 2], amplitudes: [1.0, 0.0]) // second partial is zero for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } } // MARK: - 2b.3 Window function fills @Suite("LyrebirdBuffer fillWindow(_:)") struct BufferFillWindowTests { @Test("hann window: endpoints near zero, peak near 1.0 at centre") func hannLandmarks() { let tableSize: Int = 1024 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.hann) #expect(abs(buffer[0]) < 1e-10) #expect(abs(buffer[tableSize / 2] - 1.0) < 1e-3) } @Test("hann window: all values in [0, 1]") func hannRange() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.hann) for i: Int in 0..= 0.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("hamming window: non-zero endpoints, range [0.08, 1]") func hammingEndpoints() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.hamming) // Hamming endpoints should be 0.08 (not 0) #expect(buffer[0] > 0.05) #expect(buffer[0] < 0.15) } @Test("blackmanHarris window: endpoints near zero, all in [0, 1]") func blackmanHarrisRange() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.blackmanHarris) #expect(abs(buffer[0]) < 1e-3) for i: Int in 0..= 0.0 - 1e-6) #expect(buffer[i] <= 1.0 + 1e-6) } } @Test("nuttall window: all values in [0, 1]") func nuttallRange() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.nuttall) for i: Int in 0..= 0.0 - 1e-6) #expect(buffer[i] <= 1.0 + 1e-6) } } @Test("flatTop window: values may exceed 1.0 (by design), endpoints near 0") func flatTopEndpoints() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.flatTop) // Flat-top endpoints should be near zero (coefficient sum cancels) #expect(abs(buffer[0]) < 0.05) } @Test("kaiser(beta:0) matches rectangular window") func kaiserBetaZeroIsRectangular() { let tableSize: Int = 256 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.kaiser(beta: 0.0)) // All values should be 1.0 for i: Int in 0.. 0.9) } @Test("bartlettHann window: endpoints near 0, all values in [0, 1]") func bartlettHannRange() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWindow(.bartlettHann) #expect(abs(buffer[0]) < 1e-3) for i: Int in 0..= 0.0 - 1e-6) #expect(buffer[i] <= 1.0 + 1e-6) } } } // MARK: - 2b.4 Waveshaper transfer curves @Suite("LyrebirdBuffer fillWaveshaper(_:)") struct BufferFillWaveshaperTests { @Test("hardClip: output equals input in (-1,1), clamped outside") func hardClipBehavior() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWaveshaper(.hardClip) // All values in [-1, 1] for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } // Mid-point (x≈0) should be near 0 #expect(abs(buffer[tableSize / 2]) < 0.01) } @Test("softClip (tanh): maps [-1, 1] input through tanh") func softClipApproaches() { let tableSize: Int = 1024 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWaveshaper(.softClip) // Domain is [-1, 1]; tanh(1) ≈ 0.762, tanh(-1) ≈ -0.762 let lastValue: Double = buffer[tableSize - 1] let firstValue: Double = buffer[0] // Endpoints should be tanh(±1) ≈ ±0.76159... let tanhOne: Double = 0.7615941559557649 #expect(abs(lastValue - tanhOne) < 1e-6) #expect(abs(firstValue + tanhOne) < 1e-6) // Mid-point (x=0) = tanh(0) = 0 #expect(abs(buffer[tableSize / 2]) < 0.01) } @Test("foldback: result always in [-1, 1]") func foldbackRange() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWaveshaper(.foldback) for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("asymmetric(knee:0.5): linear below knee, smooth above") func asymmetricKnee() { let tableSize: Int = 512 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWaveshaper(.asymmetric(knee: 0.5)) // Output stays within [-1, 1] for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } // Below-knee region (x in [0, 0.5]) should be linear: output ≈ input // Frame at x=0.25 is at index (0.25 + 1) / 2 * (N-1) = 0.625 * 511 ≈ 319 let quarterFrame: Int = Int(0.625 * Double(tableSize - 1)) #expect(abs(buffer[quarterFrame] - 0.25) < 0.05) } @Test("staircase(steps:4): only 4 distinct output levels") func staircaseDistinctLevels() { let tableSize: Int = 512 let stepCount: Int = 4 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillWaveshaper(.staircase(steps: stepCount)) var distinctValues: Set = Set() for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("square: stays within [-1, 1]") func squareRange() { let tableSize: Int = 2048 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillBandLimited(.square, fundamental: 100.0, sampleRate: 44100.0) for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("triangle: stays within [-1, 1]") func triangleRange() { let tableSize: Int = 2048 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillBandLimited(.triangle, fundamental: 100.0, sampleRate: 44100.0) for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("high fundamental limits partial count — no crash near Nyquist") func highFundamentalNoPartials() { let tableSize: Int = 1024 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) // Fundamental very close to Nyquist — should produce exactly 1 partial buffer.fillBandLimited(.sawtooth, fundamental: 21000.0, sampleRate: 44100.0) // Should be a pure sine (1 partial), no assertion or crash var peak: Double = 0.0 for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } @Test("triangle is even-symmetric around centre (half-period antisymmetry)") func triangleSymmetry() { let tableSize: Int = 2048 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillBandLimited(.triangle, fundamental: 100.0, sampleRate: 44100.0) // Triangle wave is odd: f(t + T/2) = -f(t) for a sine-phase implementation // Check half-period antisymmetry at a few points let half: Int = tableSize / 2 let checkIdx: Int = tableSize / 8 #expect(abs(buffer[checkIdx] + buffer[checkIdx + half]) < 0.05, "Triangle should have half-period antisymmetry") } } // MARK: - 2b.6 Inharmonic partial series @Suite("LyrebirdBuffer fillPartials(_:) and fillInharmonicPartials(fundamental:beta:amplitudes:)") struct BufferFillInharmonicTests { @Test("fillPartials: single ratio=1 partial matches fill(partials:1)") func singleRatioMatchesSine() { let tableSize: Int = 1024 let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) bufA.fill(partials: 1) bufB.fillPartials([(ratio: 1.0, amp: 1.0, phase: 0.0)]) for i: Int in 0.. 1e-4, "Positive beta should alter the spectrum") } @Test("fillInharmonicPartials: stays within [-1, 1]") func inharmonicRange() { let tableSize: Int = 1024 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) buffer.fillInharmonicPartials( fundamental: 220.0, beta: 0.005, amplitudes: [1.0, 0.7, 0.5, 0.3, 0.2, 0.1] ) for i: Int in 0..= -1.0 - 1e-10) #expect(buffer[i] <= 1.0 + 1e-10) } } } // MARK: - 2b.7 WavetableSet factory @Suite("WavetableSet factory methods") struct WavetableSetTests { @Test("interpolating: first table matches 'from', last matches 'to'") func interpolatingEndpoints() { let tableSize: Int = 512 let from: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) let to: LyrebirdBuffer = LyrebirdBuffer(count: tableSize) from.fill(partials: 1) to.fill(partials: 4) let steps: Int = 8 let set: WavetableSet = WavetableSet.interpolating(from: from, to: to, steps: steps) #expect(set.count == steps) #expect(set.tableSize == tableSize) // Table[0] should match 'from' for i: Int in 0..