Files
Lyrebird/Tests/LyrebirdPortableTests/UIParamTests.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

159 lines
4.7 KiB
Swift

// UIParamTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - @UIParam property wrapper
@MainActor
@Suite("@UIParam property wrapper") struct UIParamTests {
@Test func wrappedValueReturnsInitialValue() {
class S {
@UIParam var freq: Double = 440.0
}
let s: S = S()
#expect(s.freq == 440.0)
}
@Test func wrappedValueSetterUpdatesValue() {
class S {
@UIParam var freq: Double = 440.0
}
let s: S = S()
s.freq = 880.0
#expect(s.freq == 880.0)
}
@Test func projectedValueIsUIParamNode() {
class S {
@UIParam var freq: Double = 440.0
}
let s: S = S()
let projected: Any = s.$freq
#expect(projected is UIParamNode)
}
@Test func projectedValueNodeValueMatchesWrappedValue() {
class S {
@UIParam var amp: Double = 0.5
}
let s: S = S()
#expect(s.$amp.value == s.amp)
s.amp = 0.8
#expect(s.$amp.value == 0.8)
}
@Test func projectedValueIsStableIdentity() {
class S {
@UIParam var freq: Double = 440.0
}
let s: S = S()
let ref1: UIParamNode = s.$freq
let ref2: UIParamNode = s.$freq
#expect(ref1 === ref2)
}
@Test func projectedValueConformsToValidGeneratorInput() {
class S {
@UIParam var freq: Double = 440.0
}
let s: S = S()
let input: any ValidGeneratorInput = s.$freq
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 8, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
numAudioBuses: 8, numControlBuses: 8,
driver: OfflineDriver()
)
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
#expect(input.floatValue(graph: graph) == 440.0)
}
@Test func multipleParamsAreIndependent() {
class S {
@UIParam var freq: Double = 440.0
@UIParam var amp: Double = 0.5
}
let s: S = S()
s.freq = 880.0
#expect(s.amp == 0.5) // amp unchanged
#expect(s.freq == 880.0)
#expect(s.$freq !== s.$amp)
}
@Test func wrappedValueSetterAndNodeUpdateAreEquivalent() {
class S {
@UIParam var freq: Double = 440.0
}
let s1: S = S()
let s2: S = S()
s1.freq = 660.0 // via wrappedValue setter
s2.$freq.update(660.0) // via node directly
#expect(s1.freq == s2.freq)
#expect(s1.$freq.value == s2.$freq.value)
}
}
// MARK: - @UIParam in a SynthesizerBase subclass
@MainActor
@Suite("@UIParam in synthesizer") struct UIParamInSynthTests {
final class ToneWithParam: SynthesizerBase {
@UIParam var frequency: Double = 440.0
@UIParam var amplitude: Double = 0.0
override func build() {
// Direct typed wiring — `$amplitude` is a UIParamNode (ControlGenerator)
// delivering a per-(sub-)block ramp.
addOutputGenerator(Out.Audio(bus: 0, source: $amplitude))
}
}
@Test func buildWiresParamToOutput() throws {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 8, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
numAudioBuses: 8, numControlBuses: 8,
driver: OfflineDriver()
)
try engine.start()
let synth: UIParamInSynthTests.ToneWithParam = ToneWithParam()
synth.amplitude = 1.0
try engine.add(synth)
engine.processBlock(inputSamples: [])
let anyNonZero: Bool = engine.audioBuses[0].contains { $0 != 0.0 }
#expect(anyNonZero)
}
@Test func updatingParamAffectsNextBlock() throws {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 8, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
numAudioBuses: 8, numControlBuses: 8,
driver: OfflineDriver()
)
try engine.start()
let synth: UIParamInSynthTests.ToneWithParam = ToneWithParam()
synth.amplitude = 0.0 // silent at first
try engine.add(synth)
engine.processBlock(inputSamples: [])
let allZero: Bool = engine.audioBuses[0].allSatisfy { $0 == 0.0 }
#expect(allZero)
synth.amplitude = 1.0
engine.processBlock(inputSamples: [])
let anyNonZero: Bool = engine.audioBuses[0].contains { $0 != 0.0 }
#expect(anyNonZero)
}
}