// ParameterSystemTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // --------------------------------------------------------------------------- // Test fixtures // --------------------------------------------------------------------------- /// Single-`@LiveParam` synth that drives one audio bus from the parameter directly. /// Auto-registered key = the property name (`freq`). private final class FreqSynth: SynthesizerBase { @LiveParam var freq: Double = 0.0 let outputBus: Int init(defaultValue: Double, outputBus: Int = 0) { self.outputBus = outputBus super.init() // prime() resets both atomic and ramp origin so the first block is flat at defaultValue. // Plain `freq = defaultValue` would leave previousOutputValue at 0.0 → first block ramps. $freq.prime(defaultValue) } override func build() { addOutputGenerator(Out.Audio(bus: outputBus, source: $freq)) } } /// Two-`@LiveParam` synth — verifies independent registration and update routing. private final class FreqAmpSynth: SynthesizerBase { @LiveParam var freq: Double = 440.0 @LiveParam var amp: Double = 0.5 override func build() { addOutputGenerator(Out.Audio(bus: 0, source: $freq)) addOutputGenerator(Out.Audio(bus: 1, source: $amp)) } } /// Base + subclass for inheritance tests. Note: Swift forbids a subclass from re-declaring /// `@LiveParam var freq` if the parent already has one (would require `override` on a stored /// property wrapper, which is not supported). Subclasses must use distinct property names. private class BaseFreq: SynthesizerBase { @LiveParam var freq: Double = 100.0 override func build() { addOutputGenerator(Out.Audio(bus: 0, source: $freq)) } } private final class DerivedFreq: BaseFreq { @LiveParam var freqDerived: Double = 999.0 } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- private func lastSample(_ engine: SynthesisEngine, bus: Int) -> Double { Double(engine.audioBuses[bus][engine.blockSize - 1]) } private func firstSample(_ engine: SynthesisEngine, bus: Int) -> Double { Double(engine.audioBuses[bus][0]) } private func allSamples(_ engine: SynthesisEngine, bus: Int) -> [Double] { engine.audioBuses[bus].map { Double($0) } } // --------------------------------------------------------------------------- // Suite: Mirror auto-registration // --------------------------------------------------------------------------- @Suite("Mirror auto-registration") struct MirrorAutoRegistrationTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) init() throws { try engine.start() } @Test func liveParamRegistersUnderStrippedName() throws { let synth: FreqSynth = FreqSynth(defaultValue: 440.0) try engine.add(synth) // After engine.add → rebuildGraph → _autoRegisterLiveParams, the projected // ParameterNode must be reachable by the property name with leading `_` stripped. try synth.updateParameter("freq", value: 880.0) engine.processBlock(inputSamples: []) // Ramp 440 → 880; last sample reaches the new target. #expect(abs(lastSample(engine, bus: 0) - 880.0) < 1e-3) } @Test func multipleLiveParamsRegisterIndependently() throws { let synth: FreqAmpSynth = FreqAmpSynth() try engine.add(synth) engine.processBlock(inputSamples: []) // first block: flat at defaults try synth.updateParameter("freq", value: 1.0) try synth.updateParameter("amp", value: 0.0) engine.processBlock(inputSamples: []) // freq: ramp 440 → 1, last sample == 1; amp: ramp 0.5 → 0, last sample == 0 #expect(abs(lastSample(engine, bus: 0) - 1.0) < 1e-6) #expect(abs(lastSample(engine, bus: 1) - 0.0) < 1e-6) } @Test func subclassParamsRegisteredAlongsideSuperclass() throws { // DerivedFreq has its own @LiveParam var freqDerived plus the inherited freq. // Both should be reachable by name. let synth: DerivedFreq = DerivedFreq() try engine.add(synth) engine.processBlock(inputSamples: []) try synth.updateParameter("freq", value: 200.0) try synth.updateParameter("freqDerived", value: 0.5) engine.processBlock(inputSamples: []) // Only `freq` is wired to bus 0; verify the by-name update routed to it. #expect(abs(lastSample(engine, bus: 0) - 200.0) < 1e-3) // The subclass's freqDerived should also be writeable by name (even though // it isn't wired into the graph, the update must succeed without throwing). try synth.updateParameter("freqDerived", value: 1.5) // No assertion on bus output — just confirms the entry exists. } @Test func twoSynthInstancesHaveIndependentRegistries() throws { let synthA: FreqSynth = FreqSynth(defaultValue: 100.0, outputBus: 0) let synthB: FreqSynth = FreqSynth(defaultValue: 200.0, outputBus: 1) try engine.add(synthA) try engine.add(synthB) engine.processBlock(inputSamples: []) try synthA.updateParameter("freq", value: 500.0) engine.processBlock(inputSamples: []) // synthA ramps to 500; synthB unchanged at 200. #expect(abs(lastSample(engine, bus: 0) - 500.0) < 1e-3) #expect(abs(lastSample(engine, bus: 1) - 200.0) < 1e-6) } } // --------------------------------------------------------------------------- // Suite: updateParameter (Mirror-registered fast path) // --------------------------------------------------------------------------- @Suite("updateParameter on @LiveParam") struct UpdateLiveParamTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) init() throws { try engine.start() } @Test func throwsKeyNotFoundForUnknownKey() throws { let synth: FreqSynth = FreqSynth(defaultValue: 440.0) try engine.add(synth) #expect(throws: ParameterUpdateError.self) { try synth.updateParameter("unknown", value: 0.5) } } @Test func directTypedWriteEquivalentToByName() throws { let a: FreqSynth = FreqSynth(defaultValue: 0.0, outputBus: 0) let b: FreqSynth = FreqSynth(defaultValue: 0.0, outputBus: 1) try engine.add(a) try engine.add(b) engine.processBlock(inputSamples: []) a.freq = 4.0 // typed direct write try b.updateParameter("freq", value: 4.0) // by-name write engine.processBlock(inputSamples: []) #expect(allSamples(engine, bus: 0) == allSamples(engine, bus: 1)) } @Test func atomicLastWriteWins() throws { let synth: FreqSynth = FreqSynth(defaultValue: 0.0) try engine.add(synth) engine.processBlock(inputSamples: []) try synth.updateParameter("freq", value: 1.0) try synth.updateParameter("freq", value: 2.0) try synth.updateParameter("freq", value: 3.0) // wins engine.processBlock(inputSamples: []) #expect(abs(lastSample(engine, bus: 0) - 3.0) < 1e-6) } @Test func rampReachesTargetAtLastSample() throws { let synth: FreqSynth = FreqSynth(defaultValue: 0.0) try engine.add(synth) engine.processBlock(inputSamples: []) // flat at 0 try synth.updateParameter("freq", value: 8.0) engine.processBlock(inputSamples: []) // blockSize = 8, prev = 0, target = 8 → step = 1.0 → samples [1..8] let s: [Double] = allSamples(engine, bus: 0) for (i, v) in s.enumerated() { #expect(abs(v - Double(i + 1)) < 1e-6, "sample[\(i)] should be \(i+1)") } } @Test func subsequentBlockFlatAtTarget() throws { let synth: FreqSynth = FreqSynth(defaultValue: 0.0) try engine.add(synth) engine.processBlock(inputSamples: []) // block 1: flat at 0 try synth.updateParameter("freq", value: 1.0) engine.processBlock(inputSamples: []) // block 2: ramp 0 → 1 engine.processBlock(inputSamples: []) // block 3: flat at 1 let s: [Double] = allSamples(engine, bus: 0) #expect(s.allSatisfy { abs($0 - 1.0) < 1e-6 }) } @Test func updateAfterRemoveDoesNotCrash() throws { let synth: FreqSynth = FreqSynth(defaultValue: 1.0) try engine.add(synth) engine.processBlock(inputSamples: []) engine.remove(synth) engine.processBlock(inputSamples: []) // Atomic store still works on the detached synth — no graph effect, no crash. try synth.updateParameter("freq", value: 999.0) #expect(synth.freq == 999.0) } @Test func valuesSurviveRemoveAndReAdd() throws { // The @LiveParam wrapper's projectedValue is a `let` — the same ParameterNode // is re-registered on every rebuildGraph(), so user-set values must persist // across engine.remove() + try engine.add() cycles. let synth: FreqSynth = FreqSynth(defaultValue: 0.0) try engine.add(synth) engine.processBlock(inputSamples: []) try synth.updateParameter("freq", value: 880.0) engine.processBlock(inputSamples: []) // ramp 0 → 880 engine.processBlock(inputSamples: []) // flat at 880 engine.remove(synth) engine.processBlock(inputSamples: []) try engine.add(synth) // triggers rebuildGraph + re-registration engine.processBlock(inputSamples: []) // must remain flat at 880, not reset let s: [Double] = allSamples(engine, bus: 0) #expect(s.allSatisfy { abs($0 - 880.0) < 1e-3 }, "Parameter value must survive remove + re-add cycle") } } // --------------------------------------------------------------------------- // Suite: Sub-block ramping (controlBlockSize < blockSize) // --------------------------------------------------------------------------- @Suite("ParameterNode sub-block ramp") struct ParameterNodeSubBlockTests { @Test func rampSpansAllSubBlocks() throws { // controlBlockSize = 2, blockSize = 8 → K = 4 sub-blocks. // The framework calls _computeSamples 4 times with count=2, each re-loading the // atomic and ramping. With a single update before the block, all 4 sub-blocks see // the same target — the assembled buffer rises monotonically from 0 to target // (4 piecewise linear segments, last sample reaches target at end of block). let engine: SynthesisEngine = SynthesisEngine( blockSize: 8, controlBlockSize: 2, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 4, numControlBuses: 16, driver: OfflineDriver() ) try engine.start() let synth: FreqSynth = FreqSynth(defaultValue: 0.0) try engine.add(synth) engine.processBlock(inputSamples: []) // first block flat at 0 try synth.updateParameter("freq", value: 4.0) engine.processBlock(inputSamples: []) let last: Double = Double(engine.audioBuses[0][7]) #expect(abs(last - 4.0) < 1e-6, "Sub-block ramp must reach target at last sample of audio block") // Output must be monotonic non-decreasing across the audio block. let samples: [Double] = (0..<8).map { Double(engine.audioBuses[0][$0]) } for i: Int in 1..<8 { #expect(samples[i] >= samples[i - 1] - 1e-9, "sub-block ramp should not regress: i=\(i) prev=\(samples[i-1]) cur=\(samples[i])") } } }