Files
Lyrebird/Tests/LyrebirdTests/WavetableMorphTests.swift
T
2026-06-01 18:27:40 -07:00

648 lines
28 KiB
Swift

// WavetableMorphTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - WavetableMorphTests
@Suite("WavetableMorph")
struct WavetableMorphTests {
// MARK: - Rate
@Test("WavetableMorph.Audio rate is .audio")
func testRate() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fillSine()
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0)
#expect(morph.rate == .audio)
}
// MARK: - Output count
@Test("WavetableMorph.Audio produces 64 samples")
func testOutputCount() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fillSine()
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0)
let out: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
#expect(Array(out[0].prefix(64)).count == 64)
}
// MARK: - Fan-out cache
@Test("WavetableMorph.Audio fan-out returns identical first channel")
func testFanOut() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fill(partials: 4)
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0, position: 0.5)
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
let out2: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
for i: Int in 0..<64 {
#expect(out1[0][i] == out2[0][i])
}
}
// MARK: - State advances once per block (cache hit on second call)
@Test("Second calculatedSamples in same segment is a cache hit, does not double-advance phase")
func testCacheHitDoesNotDoubleAdvance() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fillSine()
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0)
// First call — computes and caches.
let block1: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
// Second call in same segment — must be a cache hit (identical output).
let block1b: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
for i: Int in 0..<512 {
#expect(block1[0][i] == block1b[0][i])
}
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
// Third call (new block) — phase has advanced, output must differ.
let block2: [[Double]] = morph.calculatedSamples(graph: graph2, count: 512)
let identical: Bool = zip(Array(block1[0].prefix(512)), Array(block2[0].prefix(512)))
.allSatisfy { abs($0 - $1) < 1e-12 }
#expect(!identical)
}
// MARK: - position == 0 matches OscTable on tables[0]
@Test("position == 0 matches OscTable reading tables[0] sample-for-sample")
func testPositionZeroMatchesOscTable() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let testFreq: Double = 440.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 0.0)
let osc: OscTable.Audio = OscTable.Audio(table: tA, freq: testFreq)
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
let tolerance: Double = 1e-9
for i: Int in 0..<512 {
#expect(
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
)
}
}
// MARK: - position == N-1 matches OscTable on tables[N-1]
@Test("position == N-1 matches OscTable reading tables[N-1] sample-for-sample")
func testPositionLastMatchesOscTable() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let tC: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tC.fill(partials: 16)
let testFreq: Double = 330.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// N-1 == 2.0 for three tables
let lastPosition: Double = 2.0
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB, tC], freq: testFreq, position: lastPosition)
let osc: OscTable.Audio = OscTable.Audio(table: tC, freq: testFreq)
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
let tolerance: Double = 1e-9
for i: Int in 0..<512 {
#expect(
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
)
}
}
// MARK: - position == 0.5 produces average of two single-table renders
@Test("position == 0.5 between two tables produces average of two OscTable renders")
func testMidpointIsAverage() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let testFreq: Double = 220.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 0.5)
let oscA: OscTable.Audio = OscTable.Audio(table: tA, freq: testFreq)
let oscB: OscTable.Audio = OscTable.Audio(table: tB, freq: testFreq)
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
let outA: [[Double]] = oscA.calculatedSamples(graph: graph, count: 512)
let outB: [[Double]] = oscB.calculatedSamples(graph: graph, count: 512)
let tolerance: Double = 1e-9
for i: Int in 0..<512 {
let expected: Double = (outA[0][i] + outB[0][i]) * 0.5
#expect(
abs(morphOut[0][i] - expected) < tolerance,
"sample \(i): morph=\(morphOut[0][i]) expected=\(expected)"
)
}
}
// MARK: - Position clamped above N-1
@Test("position clamped above N-1 returns last table only")
func testPositionClampAbove() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let testFreq: Double = 110.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// position == 99 far exceeds N-1 == 1; must clamp to tables[1].
let morphClamped: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 99.0)
let oscLast: OscTable.Audio = OscTable.Audio(table: tB, freq: testFreq)
let morphOut: [[Double]] = morphClamped.calculatedSamples(graph: graph, count: 512)
let oscOut: [[Double]] = oscLast.calculatedSamples(graph: graph, count: 512)
let tolerance: Double = 1e-9
for i: Int in 0..<512 {
#expect(
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
)
}
}
// MARK: - Position clamped below 0
@Test("position clamped below 0 returns tables[0] only")
func testPositionClampBelow() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let testFreq: Double = 110.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// position == -5.0 is far below 0; must clamp to tables[0].
let morphClamped: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: -5.0)
let oscFirst: OscTable.Audio = OscTable.Audio(table: tA, freq: testFreq)
let morphOut: [[Double]] = morphClamped.calculatedSamples(graph: graph, count: 512)
let oscOut: [[Double]] = oscFirst.calculatedSamples(graph: graph, count: 512)
let tolerance: Double = 1e-9
for i: Int in 0..<512 {
#expect(
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
)
}
}
// MARK: - Phase modulation: 0.5-cycle offset inverts a sine
@Test("phase = 0.5 cycles inverts a sine table relative to phase = 0.0")
func testPhaseModulationHalfCycleInversion() {
let tableSize: Int = lyrebirdDefaultTableSize
let tSine: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tSine.fillSine()
let tSine2: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tSine2.fillSine()
let testFreq: Double = 440.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// Both morphers use position == 0 so they read only tables[0].
// morphNoPM uses phase == 0.0; morphWithPM uses phase == 0.5 (half-cycle).
let morphNoPM: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tSine, tSine2], freq: testFreq, position: 0.0, phase: 0.0)
let morphWithPM: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tSine, tSine2], freq: testFreq, position: 0.0, phase: 0.5)
let outNoPM: [[Double]] = morphNoPM.calculatedSamples(graph: graph, count: 512)
let outWithPM: [[Double]] = morphWithPM.calculatedSamples(graph: graph, count: 512)
// Compare against independent OscTable references to confirm sign inversion.
// A 0.5-cycle phase shift on a sine table: sin(x + π) = -sin(x).
let tolerance: Double = 1e-6
for i: Int in 0..<512 {
#expect(
abs(outNoPM[0][i] + outWithPM[0][i]) < tolerance,
"sample \(i): expected sign inversion but noPM=\(outNoPM[0][i]) withPM=\(outWithPM[0][i])"
)
}
}
// MARK: - mulAdd
@Test("WavetableMorph.Audio with mul:2.0, add:1.0 doubles and offsets output")
func testMulAdd() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fillSine()
let testFreq: Double = 440.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morphBase: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 0.0)
let morphMulAdd: WavetableMorph.Audio = WavetableMorph.Audio(
tables: [tA, tB],
freq: testFreq,
position: 0.0,
mul: 2.0,
add: 1.0
)
let baseOut: [[Double]] = morphBase.calculatedSamples(graph: graph, count: 512)
let mulAddOut: [[Double]] = morphMulAdd.calculatedSamples(graph: graph, count: 512)
let tolerance: Double = 1e-9
for i: Int in 0..<512 {
let expected: Double = baseOut[0][i] * 2.0 + 1.0
#expect(
abs(mulAddOut[0][i] - expected) < tolerance,
"sample \(i): expected \(expected), got \(mulAddOut[0][i])"
)
}
}
// MARK: - Position as a generator (no crash, output in range for sine tables)
@Test("Position driven by a generator does not crash and keeps sine output in [-1, 1]")
func testGeneratorPositionNoCrash() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fillSine()
// LFSawtooth.Control output is in [-1, 1]; after scaling by 0.5 + 0.5 it maps to [0, 1],
// which is a valid position range for two tables (N-1 == 1).
let posGen: BinaryOpGenerator = LFSawtooth.Control(freq: 2.0) * 0.5 + 0.5
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0, position: posGen)
// Two blocks — test block-boundary instance reuse per CLAUDE.md feedback memory.
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
let out2: [[Double]] = morph.calculatedSamples(graph: graph2, count: 512)
let outRange: Double = 1.1 // slight headroom above [-1, 1] for linear interpolation rounding
for i: Int in 0..<512 {
#expect(out1[0][i] >= -outRange, "block1 sample \(i) below range: \(out1[0][i])")
#expect(out1[0][i] <= outRange, "block1 sample \(i) above range: \(out1[0][i])")
#expect(out2[0][i] >= -outRange, "block2 sample \(i) below range: \(out2[0][i])")
#expect(out2[0][i] <= outRange, "block2 sample \(i) above range: \(out2[0][i])")
}
}
// MARK: - Silence after invalidation
@Test("WavetableMorph.Audio outputs silence when any table is invalidated")
func testInvalidatedTableOutputsSilence() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0, position: 0.5)
// First block — should be non-silent.
let before: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
let allZeroBefore: Bool = Array(before[0].prefix(512)).allSatisfy { $0 == 0.0 }
#expect(!allZeroBefore)
tB.invalidate()
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
// Next block — must be all zeros.
let after: [[Double]] = morph.calculatedSamples(graph: graph2, count: 512)
for i: Int in 0..<512 {
#expect(after[0][i] == 0.0, "sample \(i) expected 0.0 after invalidation, got \(after[0][i])")
}
}
// MARK: - Control: silence after any table is invalidated (including unused one)
@Test("WavetableMorph.Control outputs silence when any table is invalidated, including an unused one")
func testControlInvalidatedTableOutputsSilence() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let tC: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tC.fill(partials: 16)
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// position: 0.5 → reads between tables[0] and tables[1]; tables[2] is never touched.
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB, tC], freq: 1.0, position: 0.5)
// First block — should be non-zero (sine table midpoint at phase 0 may start at 0,
// so check previousOutputValue after a second block instead).
_ = morph.calculatedSamples(graph: graph, count: 64)
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
let before: [[Double]] = morph.calculatedSamples(graph: graph2, count: 64)
let nonZeroBefore: Bool = (0..<64).contains { before[0][$0] != 0.0 }
#expect(nonZeroBefore, "expected non-zero output before invalidation")
// Invalidate the unused table (tables[2]). The full-scan must detect it.
tC.invalidate()
advanceBlock(engine)
let graph3: SynthGraph = makeSynthGraph(engine: engine)
_ = morph.calculatedSamples(graph: graph3, count: 64)
// After one ramp-to-zero block, previousOutputValue must be 0.
#expect(morph.previousOutputValue == 0.0,
"expected previousOutputValue == 0.0 after invalidation, got \(morph.previousOutputValue)")
}
// MARK: - Integration: runs inside Synthesizer wired to engine without crash
@Test("WavetableMorph.Audio runs inside a Synthesizer without crash")
func testIntegration() throws {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
try engine.start()
final class MorphSynth: SynthesizerBase {
let tables: [LyrebirdBuffer]
init(tables: [LyrebirdBuffer]) {
self.tables = tables
super.init()
}
override func build() {
let morph: WavetableMorph.Audio = WavetableMorph.Audio(
tables: tables,
freq: 440.0,
position: 0.5
)
addOutputGenerator(Out.Audio(bus: 0, source: morph))
}
}
let synth: MorphSynth = MorphSynth(tables: [tA, tB])
try engine.add(synth)
// Run two blocks via processBlock — the proper engine path that initialises buses.
engine.processBlock(inputSamples: [])
engine.processBlock(inputSamples: [])
// Verify the engine wrote non-zero samples to bus 0.
let anyNonZero: Bool = engine.audioBuses[0].prefix(512).contains { $0 != 0.0 }
#expect(anyNonZero)
}
// MARK: - Control rate
@Test("WavetableMorph.Control rate is .control")
func testControlRate() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fillSine()
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0)
#expect(morph.rate == .control)
}
// MARK: - Control output count
@Test("WavetableMorph.Control produces 64 samples")
func testControlOutputCount() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fillSine()
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0)
let out: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
#expect(Array(out[0].prefix(64)).count == 64)
}
// MARK: - Control fan-out cache
@Test("WavetableMorph.Control fan-out returns identical first channel in same segment")
func testControlFanOut() {
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
tB.fill(partials: 4)
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0, position: 0.5)
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
let out2: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
for i: Int in 0..<64 {
#expect(out1[0][i] == out2[0][i])
}
}
// MARK: - Control position == 0 matches OscTable.Control on tables[0]
@Test("WavetableMorph.Control position==0 matches OscTable.Control reading tables[0]")
func testControlPositionZeroMatchesOscTable() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let testFreq: Double = 1.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: testFreq, position: 0.0)
let osc: OscTable.Control = OscTable.Control(table: tA, freq: testFreq)
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
// Both advance phase by the same amount; last sample of the ramp should match.
let tolerance: Double = 1e-9
#expect(
abs(morphOut[0][63] - oscOut[0][63]) < tolerance,
"last sample: morph=\(morphOut[0][63]) osc=\(oscOut[0][63])"
)
}
// MARK: - Control midpoint averages two OscTable.Control renders
@Test("WavetableMorph.Control position==0.5 target is average of two OscTable.Control targets")
func testControlMidpointIsAverage() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 8)
let testFreq: Double = 2.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: testFreq, position: 0.5)
let oscA: OscTable.Control = OscTable.Control(table: tA, freq: testFreq)
let oscB: OscTable.Control = OscTable.Control(table: tB, freq: testFreq)
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
let outA: [[Double]] = oscA.calculatedSamples(graph: graph, count: 64)
let outB: [[Double]] = oscB.calculatedSamples(graph: graph, count: 64)
// The ramp target is the average; last sample reflects that.
let expectedTarget: Double = (outA[0][63] + outB[0][63]) * 0.5
let tolerance: Double = 1e-9
#expect(
abs(morphOut[0][63] - expectedTarget) < tolerance,
"last sample: morph=\(morphOut[0][63]) expected=\(expectedTarget)"
)
}
// MARK: - Control ramp: last sample equals target
@Test("WavetableMorph.Control ramp last sample equals the new table-read target")
func testControlRamp() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fill(partials: 4)
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// Use position: 1.0 so the generator reads tables[1] exclusively.
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 0.5, position: 1.0)
let out: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
// The last sample of the ramp is previousOutputValue after the block.
#expect(out[0][63] == morph.previousOutputValue)
}
// MARK: - Control mulAdd
@Test("WavetableMorph.Control with mul:2.0, add:1.0 doubles and offsets output")
func testControlMulAdd() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fillSine()
let testFreq: Double = 1.0
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morphBase: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: testFreq, position: 0.0)
let morphMulAdd: WavetableMorph.Control = WavetableMorph.Control(
tables: [tA, tB],
freq: testFreq,
position: 0.0,
mul: 2.0,
add: 1.0
)
let baseOut: [[Double]] = morphBase.calculatedSamples(graph: graph, count: 64)
let mulAddOut: [[Double]] = morphMulAdd.calculatedSamples(graph: graph, count: 64)
let tolerance: Double = 1e-9
for i: Int in 0..<64 {
let expected: Double = baseOut[0][i] * 2.0 + 1.0
#expect(
abs(mulAddOut[0][i] - expected) < tolerance,
"sample \(i): expected \(expected), got \(mulAddOut[0][i])"
)
}
}
// MARK: - Control: generator-driven position does not crash
@Test("WavetableMorph.Control position driven by a generator does not crash")
func testControlGeneratorPositionNoCrash() {
let tableSize: Int = lyrebirdDefaultTableSize
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tA.fillSine()
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
tB.fillSine()
// OscSin.Control output mapped to [0, 1] → valid position range for 2 tables.
let posGen: BinaryOpGenerator = OscSin.Control(freq: 0.5) * 0.5 + 0.5
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0, position: posGen)
// Two blocks to test cross-block instance reuse.
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
let out2: [[Double]] = morph.calculatedSamples(graph: graph2, count: 64)
// Output must stay in [-1, 1] range (sine tables).
let outRange: Double = 1.1
for i: Int in 0..<64 {
#expect(out1[0][i] >= -outRange && out1[0][i] <= outRange,
"block1 sample \(i) out of range: \(out1[0][i])")
#expect(out2[0][i] >= -outRange && out2[0][i] <= outRange,
"block2 sample \(i) out of range: \(out2[0][i])")
}
}
}