641 lines
25 KiB
Swift
641 lines
25 KiB
Swift
// 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..<tableSize { peak = max(peak, abs(buffer[i])) }
|
||
#expect(abs(peak - 1.0) < 1e-10)
|
||
|
||
// Quarter-cycle should be near +1
|
||
#expect(abs(buffer[tableSize / 4] - 1.0) < 1e-3)
|
||
// Half-cycle should be near 0
|
||
#expect(abs(buffer[tableSize / 2]) < 1e-3)
|
||
}
|
||
|
||
@Test("fill(partials:) many partials stays within [-1, 1] after normalisation")
|
||
func manyPartialsNormalised() {
|
||
let tableSize: Int = 2048
|
||
let partialCount: Int = 32
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fill(partials: partialCount)
|
||
|
||
for i: Int in 0..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(abs(bufA[i] - bufB[i]) < 1e-10,
|
||
"Mismatch at index \(i): \(bufA[i]) vs \(bufB[i])")
|
||
}
|
||
}
|
||
|
||
@Test("fill(harmonics:amplitudes:) square wave has only odd harmonics")
|
||
func squareWaveOddHarmonics() {
|
||
let tableSize: Int = 2048
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
// Square wave: odd harmonics with 1/k amplitudes
|
||
buffer.fill(harmonics: [1, 3, 5, 7, 9], amplitudes: [1.0, 1.0/3, 1.0/5, 1.0/7, 1.0/9])
|
||
// Result must be in [-1, 1]
|
||
for i: Int in 0..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(abs(bufA[i] - bufB[i]) < 1e-10)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 2b.2 Chebyshev polynomial tables
|
||
|
||
@Suite("LyrebirdBuffer fillChebyshev(coefficients:)")
|
||
struct BufferFillChebyshevTests {
|
||
|
||
@Test("T1 only (fundamental) produces a line x in [-1, 1]")
|
||
func t1OnlyIsLinear() {
|
||
let tableSize: Int = 256
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillChebyshev(coefficients: [1.0]) // T1(x) = x
|
||
|
||
// After normalisation the result should be linear from -1 to +1
|
||
// At i=0 x = -1, at i=N-1 x = +1
|
||
#expect(abs(buffer[0] - (-1.0)) < 1e-10)
|
||
#expect(abs(buffer[tableSize - 1] - 1.0) < 1e-10)
|
||
// Mid-point should be near 0
|
||
let mid: Int = tableSize / 2
|
||
#expect(abs(buffer[mid]) < 0.01)
|
||
}
|
||
|
||
@Test("T2 only produces a parabola — minimum near centre")
|
||
func t2OnlyIsParabola() {
|
||
let tableSize: Int = 256
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillChebyshev(coefficients: [0.0, 1.0]) // T2(x) = 2x²−1
|
||
|
||
// T2(-1) = 1, T2(0) = -1, T2(1) = 1 (then normalised)
|
||
// After peak-normalisation: endpoints ≈ +1, midpoint ≈ -1
|
||
#expect(abs(buffer[0] - 1.0) < 1e-6)
|
||
#expect(abs(buffer[tableSize - 1] - 1.0) < 1e-6)
|
||
let mid: Int = tableSize / 2
|
||
#expect(buffer[mid] < -0.5) // should be near -1 at centre
|
||
}
|
||
|
||
@Test("fillChebyshev output is in [-1, 1] after normalisation")
|
||
func chebyshevNormalised() {
|
||
let tableSize: Int = 1024
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillChebyshev(coefficients: [0.5, 0.3, 0.2, 0.1, 0.05])
|
||
for i: Int in 0..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(buffer[i] >= 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..<tableSize {
|
||
#expect(buffer[i] >= 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..<tableSize {
|
||
#expect(buffer[i] >= 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..<tableSize {
|
||
#expect(abs(buffer[i] - 1.0) < 1e-6, "Kaiser beta=0 should be rectangular at index \(i)")
|
||
}
|
||
}
|
||
|
||
@Test("kaiser(beta:5) endpoints near 0, peaks near 1")
|
||
func kaiserBeta5Shape() {
|
||
let tableSize: Int = 512
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillWindow(.kaiser(beta: 5.0))
|
||
// Kaiser windows have near-zero endpoints and a peak near the centre
|
||
#expect(buffer[0] < 0.1)
|
||
#expect(buffer[tableSize / 2] > 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..<tableSize {
|
||
#expect(buffer[i] >= 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..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(buffer[i] >= -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<Double> = Set<Double>()
|
||
for i: Int in 0..<tableSize {
|
||
let sample: Double = buffer[i]
|
||
let rounded: Double = (sample * 1000.0).rounded() / 1000.0 // round to 3dp
|
||
distinctValues.insert(rounded)
|
||
}
|
||
// Should have exactly stepCount distinct output levels
|
||
#expect(distinctValues.count == stepCount)
|
||
}
|
||
|
||
@Test("fill(body:) cubic transfer: centre near 0, endpoints near ±1")
|
||
func closureBasedFillCubic() {
|
||
let tableSize: Int = 512
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fill { x in x * x * x } // cubic: f(-1)=-1, f(0)=0, f(1)=1
|
||
#expect(abs(buffer[0] - (-1.0)) < 1e-6)
|
||
#expect(abs(buffer[tableSize - 1] - 1.0) < 1e-6)
|
||
#expect(abs(buffer[tableSize / 2]) < 0.02)
|
||
}
|
||
}
|
||
|
||
// MARK: - 2b.5 Band-limited waveform tables
|
||
|
||
@Suite("LyrebirdBuffer fillBandLimited(_:fundamental:sampleRate:)")
|
||
struct BufferFillBandLimitedTests {
|
||
|
||
@Test("sawtooth: stays within [-1, 1]")
|
||
func sawtoothRange() {
|
||
let tableSize: Int = 2048
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillBandLimited(.sawtooth, fundamental: 100.0, sampleRate: 44100.0)
|
||
for i: Int in 0..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize { peak = max(peak, abs(buffer[i])) }
|
||
#expect(abs(peak - 1.0) < 1e-10)
|
||
}
|
||
|
||
@Test("suppressGibbs: output stays in [-1, 1]")
|
||
func suppressGibbsRange() {
|
||
let tableSize: Int = 2048
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillBandLimited(.sawtooth, fundamental: 100.0, sampleRate: 44100.0, suppressGibbs: true)
|
||
for i: Int in 0..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(abs(bufA[i] - bufB[i]) < 1e-10,
|
||
"Mismatch at index \(i)")
|
||
}
|
||
}
|
||
|
||
@Test("fillPartials: result is normalised to peak 1.0")
|
||
func fillPartialsNormalised() {
|
||
let tableSize: Int = 1024
|
||
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
buffer.fillPartials([
|
||
(ratio: 1.0, amp: 0.5, phase: 0.0),
|
||
(ratio: 2.76, amp: 0.3, phase: 0.0),
|
||
])
|
||
var peak: Double = 0.0
|
||
for i: Int in 0..<tableSize { peak = max(peak, abs(buffer[i])) }
|
||
#expect(abs(peak - 1.0) < 1e-10)
|
||
}
|
||
|
||
@Test("fillPartials: phase offset shifts the waveform")
|
||
func phaseOffsetShiftsWave() {
|
||
let tableSize: Int = 1024
|
||
let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
bufA.fillPartials([(ratio: 1.0, amp: 1.0, phase: 0.0)])
|
||
bufB.fillPartials([(ratio: 1.0, amp: 1.0, phase: Double.pi / 2)]) // 90° shift
|
||
// A should peak at N/4; B should peak at 0
|
||
let quarterIdx: Int = tableSize / 4
|
||
#expect(abs(bufA[quarterIdx] - 1.0) < 1e-3)
|
||
#expect(abs(bufB[0] - 1.0) < 1e-3)
|
||
}
|
||
|
||
@Test("fillInharmonicPartials: beta=0 produces harmonic spectrum")
|
||
func betaZeroIsHarmonic() {
|
||
let tableSize: Int = 1024
|
||
let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
bufA.fill(harmonics: [1, 2, 3], amplitudes: [1.0, 0.5, 0.25])
|
||
bufB.fillInharmonicPartials(fundamental: 440.0, beta: 0.0, amplitudes: [1.0, 0.5, 0.25])
|
||
for i: Int in 0..<tableSize {
|
||
#expect(abs(bufA[i] - bufB[i]) < 1e-8,
|
||
"Mismatch at index \(i): \(bufA[i]) vs \(bufB[i])")
|
||
}
|
||
}
|
||
|
||
@Test("fillInharmonicPartials: positive beta stretches partials (result differs from harmonic)")
|
||
func positiveBetaChangesSpectrum() {
|
||
let tableSize: Int = 1024
|
||
let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
bufA.fillInharmonicPartials(fundamental: 440.0, beta: 0.0, amplitudes: [1.0, 0.5, 0.25])
|
||
bufB.fillInharmonicPartials(fundamental: 440.0, beta: 0.01, amplitudes: [1.0, 0.5, 0.25])
|
||
var maxDiff: Double = 0.0
|
||
for i: Int in 0..<tableSize { maxDiff = max(maxDiff, abs(bufA[i] - bufB[i])) }
|
||
#expect(maxDiff > 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..<tableSize {
|
||
#expect(buffer[i] >= -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..<tableSize {
|
||
#expect(abs(set.buffers[0][i] - from[i]) < 1e-10,
|
||
"Table[0] should match 'from' at index \(i)")
|
||
}
|
||
// Table[steps-1] should match 'to'
|
||
for i: Int in 0..<tableSize {
|
||
#expect(abs(set.buffers[steps - 1][i] - to[i]) < 1e-10,
|
||
"Table[last] should match 'to' at index \(i)")
|
||
}
|
||
}
|
||
|
||
@Test("interpolating: middle tables are blends of from and to")
|
||
func interpolatingMidBlend() {
|
||
let tableSize: Int = 64
|
||
let from: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
let to: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
from.fill(with: 0.0)
|
||
to.fill(with: 1.0)
|
||
|
||
let set: WavetableSet = WavetableSet.interpolating(from: from, to: to, steps: 5)
|
||
// Table[2] (middle, alpha = 0.5) should be all 0.5
|
||
for i: Int in 0..<tableSize {
|
||
#expect(abs(set.buffers[2][i] - 0.5) < 1e-10,
|
||
"Middle table should be 0.5 at index \(i)")
|
||
}
|
||
}
|
||
|
||
@Test("interpolating: all tables have correct size and are mono")
|
||
func interpolatingTableProperties() {
|
||
let tableSize: Int = 256
|
||
let from: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
let to: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
from.fillSine()
|
||
to.fill(partials: 8)
|
||
|
||
let set: WavetableSet = WavetableSet.interpolating(from: from, to: to, steps: 4)
|
||
for (i, buf) in set.buffers.enumerated() {
|
||
#expect(buf.count == tableSize, "Table[\(i)] count should be \(tableSize)")
|
||
#expect(buf.channelCount == 1, "Table[\(i)] should be mono")
|
||
}
|
||
}
|
||
|
||
@Test("rotatingPhases: produces correct number of tables")
|
||
func rotatingPhasesCount() {
|
||
let tableSize: Int = 512
|
||
let source: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
source.fillSine()
|
||
|
||
let steps: Int = 6
|
||
let set: WavetableSet = WavetableSet.rotatingPhases(buffer: source, steps: steps)
|
||
#expect(set.count == steps)
|
||
#expect(set.tableSize == tableSize)
|
||
}
|
||
|
||
@Test("rotatingPhases: step 0 matches the source buffer exactly")
|
||
func rotatingPhasesStep0MatchesSource() {
|
||
let tableSize: Int = 512
|
||
let source: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
source.fillSine()
|
||
|
||
let set: WavetableSet = WavetableSet.rotatingPhases(buffer: source, steps: 4)
|
||
for i: Int in 0..<tableSize {
|
||
#expect(abs(set.buffers[0][i] - source[i]) < 1e-10,
|
||
"Table[0] should match source at index \(i)")
|
||
}
|
||
}
|
||
|
||
@Test("rotatingPhases: all tables are mono and have correct size")
|
||
func rotatingPhasesTableProperties() {
|
||
let tableSize: Int = 256
|
||
let source: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
source.fill(partials: 3)
|
||
|
||
let set: WavetableSet = WavetableSet.rotatingPhases(buffer: source, steps: 8)
|
||
for (i, buf) in set.buffers.enumerated() {
|
||
#expect(buf.count == tableSize, "Table[\(i)] count mismatch")
|
||
#expect(buf.channelCount == 1, "Table[\(i)] should be mono")
|
||
}
|
||
}
|
||
|
||
@Test("fromPartialMatrix: produces correct number of tables")
|
||
func fromPartialMatrixCount() {
|
||
let partials: [[Double]] = [
|
||
[1.0, 0.5, 0.25], // table 0
|
||
[1.0, 0.0, 0.25], // table 1
|
||
[0.5, 0.5, 0.0], // table 2
|
||
]
|
||
let set: WavetableSet = WavetableSet.fromPartialMatrix(partials: partials, tableSize: 512)
|
||
#expect(set.count == 3)
|
||
}
|
||
|
||
@Test("fromPartialMatrix: all tables are valid and mono")
|
||
func fromPartialMatrixProperties() {
|
||
let partials: [[Double]] = [
|
||
[1.0, 0.5],
|
||
[0.0, 1.0],
|
||
]
|
||
let tableSize: Int = 256
|
||
let set: WavetableSet = WavetableSet.fromPartialMatrix(partials: partials, tableSize: tableSize)
|
||
for (i, buf) in set.buffers.enumerated() {
|
||
#expect(buf.isValid, "Table[\(i)] should be valid")
|
||
#expect(buf.count == tableSize, "Table[\(i)] count mismatch")
|
||
#expect(buf.channelCount == 1, "Table[\(i)] should be mono")
|
||
}
|
||
}
|
||
|
||
@Test("fromPartialMatrix: first table with single partial matches fill(partials:1)")
|
||
func fromPartialMatrixSinglePartialMatchesFill() {
|
||
let tableSize: Int = 1024
|
||
let ref: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
ref.fill(partials: 1)
|
||
|
||
// A 1×1 partial matrix with harmonic 1, amplitude 1.0
|
||
let set: WavetableSet = WavetableSet.fromPartialMatrix(partials: [[1.0]], tableSize: tableSize)
|
||
let table: LyrebirdBuffer = set.buffers[0]
|
||
|
||
for i: Int in 0..<tableSize {
|
||
#expect(abs(table[i] - ref[i]) < 1e-10,
|
||
"Mismatch at index \(i)")
|
||
}
|
||
}
|
||
|
||
@Test("WavetableSet init: count and tableSize reflect input")
|
||
func wavetableSetInit() {
|
||
let tableSize: Int = 128
|
||
let bufA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
let bufB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||
bufA.fillSine()
|
||
bufB.fill(partials: 4)
|
||
|
||
let set: WavetableSet = WavetableSet(buffers: [bufA, bufB])
|
||
#expect(set.count == 2)
|
||
#expect(set.tableSize == tableSize)
|
||
}
|
||
}
|