1.0.0 release
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
// LyrebirdEnvelopeViewTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable import LyrebirdUI
|
||||
import LyrebirdSynth
|
||||
|
||||
// MARK: - _regionForSegment
|
||||
|
||||
@Suite("_regionForSegment") struct RegionForSegmentTests {
|
||||
|
||||
// No loop or release — everything is preLoop
|
||||
@Test func noLoopNoRelease_allPreLoop() {
|
||||
for i: Int in 0..<4 {
|
||||
#expect(_regionForSegment(index: i, loopNode: nil, releaseNode: nil, segmentCount: 4) == .preLoop)
|
||||
}
|
||||
}
|
||||
|
||||
// Loop only (no releaseNode)
|
||||
@Test func loopOnlyAtZero_loopSegmentIsLoop() {
|
||||
#expect(_regionForSegment(index: 0, loopNode: 0, releaseNode: nil, segmentCount: 3) == .loop)
|
||||
#expect(_regionForSegment(index: 1, loopNode: 0, releaseNode: nil, segmentCount: 3) == .preLoop)
|
||||
#expect(_regionForSegment(index: 2, loopNode: 0, releaseNode: nil, segmentCount: 3) == .preLoop)
|
||||
}
|
||||
|
||||
@Test func loopOnlyAtMid_beforeIsPreLoop_atIsLoop_afterIsPreLoop() {
|
||||
#expect(_regionForSegment(index: 0, loopNode: 1, releaseNode: nil, segmentCount: 3) == .preLoop)
|
||||
#expect(_regionForSegment(index: 1, loopNode: 1, releaseNode: nil, segmentCount: 3) == .loop)
|
||||
#expect(_regionForSegment(index: 2, loopNode: 1, releaseNode: nil, segmentCount: 3) == .preLoop)
|
||||
}
|
||||
|
||||
// Release only (no loopNode)
|
||||
@Test func releaseOnlyAtZero_allRelease() {
|
||||
#expect(_regionForSegment(index: 0, loopNode: nil, releaseNode: 0, segmentCount: 3) == .release)
|
||||
#expect(_regionForSegment(index: 1, loopNode: nil, releaseNode: 0, segmentCount: 3) == .release)
|
||||
}
|
||||
|
||||
@Test func releaseOnlyAtMid_beforeIsPreLoop_atAndAfterIsRelease() {
|
||||
#expect(_regionForSegment(index: 0, loopNode: nil, releaseNode: 2, segmentCount: 4) == .preLoop)
|
||||
#expect(_regionForSegment(index: 1, loopNode: nil, releaseNode: 2, segmentCount: 4) == .preLoop)
|
||||
#expect(_regionForSegment(index: 2, loopNode: nil, releaseNode: 2, segmentCount: 4) == .release)
|
||||
#expect(_regionForSegment(index: 3, loopNode: nil, releaseNode: 2, segmentCount: 4) == .release)
|
||||
}
|
||||
|
||||
// Both loop and release
|
||||
@Test func loopAtZeroReleaseAtTwo_correctRegions() {
|
||||
#expect(_regionForSegment(index: 0, loopNode: 0, releaseNode: 2, segmentCount: 4) == .loop)
|
||||
#expect(_regionForSegment(index: 1, loopNode: 0, releaseNode: 2, segmentCount: 4) == .loop)
|
||||
#expect(_regionForSegment(index: 2, loopNode: 0, releaseNode: 2, segmentCount: 4) == .release)
|
||||
#expect(_regionForSegment(index: 3, loopNode: 0, releaseNode: 2, segmentCount: 4) == .release)
|
||||
}
|
||||
|
||||
@Test func loopAtOneReleaseAtThree_preLoopLoopRelease() {
|
||||
#expect(_regionForSegment(index: 0, loopNode: 1, releaseNode: 3, segmentCount: 5) == .preLoop)
|
||||
#expect(_regionForSegment(index: 1, loopNode: 1, releaseNode: 3, segmentCount: 5) == .loop)
|
||||
#expect(_regionForSegment(index: 2, loopNode: 1, releaseNode: 3, segmentCount: 5) == .loop)
|
||||
#expect(_regionForSegment(index: 3, loopNode: 1, releaseNode: 3, segmentCount: 5) == .release)
|
||||
#expect(_regionForSegment(index: 4, loopNode: 1, releaseNode: 3, segmentCount: 5) == .release)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - _segmentBoundaryTimes
|
||||
|
||||
@Suite("_segmentBoundaryTimes") struct SegmentBoundaryTimesTests {
|
||||
|
||||
@Test func emptyDurations_returnsOnlyZero() {
|
||||
let times: [Double] = _segmentBoundaryTimes(durations: [])
|
||||
#expect(times == [0])
|
||||
}
|
||||
|
||||
@Test func singleSegment_returnsTwoTimes() {
|
||||
let times: [Double] = _segmentBoundaryTimes(durations: [0.5])
|
||||
#expect(times.count == 2)
|
||||
#expect(times[0] == 0)
|
||||
#expect(times[1] == 0.5)
|
||||
}
|
||||
|
||||
@Test func multipleSegments_cumulativeSum() {
|
||||
let times: [Double] = _segmentBoundaryTimes(durations: [0.01, 0.3, 0.2])
|
||||
#expect(times.count == 4)
|
||||
#expect(times[0] == 0)
|
||||
#expect(abs(times[1] - 0.01) < 1e-9)
|
||||
#expect(abs(times[2] - 0.31) < 1e-9)
|
||||
#expect(abs(times[3] - 0.51) < 1e-9)
|
||||
}
|
||||
|
||||
@Test func zeroSegment_preservedInBoundaries() {
|
||||
let times: [Double] = _segmentBoundaryTimes(durations: [0.1, 0.0, 0.2])
|
||||
#expect(abs(times[1] - 0.1) < 1e-9)
|
||||
#expect(abs(times[2] - 0.1) < 1e-9)
|
||||
#expect(abs(times[3] - 0.3) < 1e-9)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - _envelopeYBounds
|
||||
|
||||
@Suite("_envelopeYBounds") struct EnvelopeYBoundsTests {
|
||||
|
||||
@Test func emptyLevels_returnsDefaultRange() {
|
||||
let (yMin, yMax) = _envelopeYBounds(levels: [])
|
||||
#expect(yMin == 0)
|
||||
#expect(yMax == 1)
|
||||
}
|
||||
|
||||
@Test func singleLevel_rangeWithHeadroom() {
|
||||
let (yMin, yMax) = _envelopeYBounds(levels: [0.5])
|
||||
#expect(yMin < 0.5)
|
||||
#expect(yMax > 0.5)
|
||||
}
|
||||
|
||||
@Test func normalLevels_tenPercentHeadroom() {
|
||||
let (yMin, yMax) = _envelopeYBounds(levels: [0.0, 1.0])
|
||||
// range = 1.0, headroom = 0.1
|
||||
#expect(abs(yMin - (-0.1)) < 1e-9)
|
||||
#expect(abs(yMax - 1.1) < 1e-9)
|
||||
}
|
||||
|
||||
@Test func negativeLevels_boundsIncludeNegative() {
|
||||
let (yMin, yMax) = _envelopeYBounds(levels: [-1.0, 0.0, 1.0])
|
||||
#expect(yMin < -1.0)
|
||||
#expect(yMax > 1.0)
|
||||
}
|
||||
|
||||
@Test func flatLevels_usesMinimumRange() {
|
||||
let (yMin, yMax) = _envelopeYBounds(levels: [0.5, 0.5])
|
||||
#expect(yMin < 0.5)
|
||||
#expect(yMax > 0.5)
|
||||
#expect(yMax > yMin)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Envelope.valueAtTime boundary sampling
|
||||
|
||||
@Suite("Envelope valueAtTime edge cases for plotting") struct EnvelopeValueAtTimePlottingTests {
|
||||
|
||||
@Test func clampsBelowZero_returnsFirstLevel() {
|
||||
let env: Envelope = Envelope(levels: [0.2, 1.0, 0.0], durations: [0.1, 0.4], curves: [.lin, .lin])
|
||||
#expect(env.valueAtTime(-0.1) == 0.2)
|
||||
#expect(env.valueAtTime(0.0) == 0.2)
|
||||
}
|
||||
|
||||
@Test func clampsAboveDuration_returnsLastLevel() {
|
||||
let env: Envelope = Envelope(levels: [0.0, 1.0, 0.5], durations: [0.1, 0.4], curves: [.lin, .lin])
|
||||
#expect(env.valueAtTime(0.5) == 0.5)
|
||||
#expect(env.valueAtTime(1.0) == 0.5)
|
||||
}
|
||||
|
||||
@Test func singleSegmentLinear_midpointIsHalfway() {
|
||||
let env: Envelope = Envelope(levels: [0.0, 1.0], durations: [1.0], curves: [.lin])
|
||||
let mid: Double = env.valueAtTime(0.5)
|
||||
#expect(abs(mid - 0.5) < 1e-9)
|
||||
}
|
||||
|
||||
@Test func loopNodeAndReleaseNodePresent_valueAtTimeIgnoresThem() {
|
||||
// valueAtTime is stateless — loopNode/releaseNode don't affect the shape lookup
|
||||
let gated: Envelope = Envelope(levels: [0, 1, 0.6, 0], durations: [0.01, 0.3, 0.2],
|
||||
curves: [.sine, .lin, .lin], loopNode: 1, releaseNode: 2)
|
||||
let plain: Envelope = Envelope(levels: [0, 1, 0.6, 0], durations: [0.01, 0.3, 0.2],
|
||||
curves: [.sine, .lin, .lin])
|
||||
for t: Double in stride(from: 0.0, through: 0.51, by: 0.05) {
|
||||
#expect(abs(gated.valueAtTime(t) - plain.valueAtTime(t)) < 1e-12)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// LyrebirdSynthViewTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import LyrebirdSynth
|
||||
@testable import LyrebirdUI
|
||||
|
||||
private final class UIParamSynth: SynthesizerBase {
|
||||
@UIParam var cutoff: Double = 1000
|
||||
@UIParam var resonance: Double = 0.3
|
||||
@LiveParam var hiddenLP: Double = 0 // should not appear (LiveParam ≠ UIParam)
|
||||
@UIParam var amp: Double = 0.5
|
||||
override func build() { output(bus: 0, source: 0.0) }
|
||||
}
|
||||
|
||||
@Suite("LyrebirdSynthView") struct LyrebirdSynthViewTests {
|
||||
|
||||
@Test func discoverEnumeratesOnlyUIParamSlots() {
|
||||
let synth: UIParamSynth = UIParamSynth()
|
||||
let entries: [LyrebirdSynthView.Entry] = LyrebirdSynthView.discover(synth: synth)
|
||||
// hiddenLP (@LiveParam) must be skipped.
|
||||
let names: [String] = entries.map { $0.propertyName }
|
||||
#expect(names == ["cutoff", "resonance", "amp"])
|
||||
#expect(!names.contains("hiddenLP"))
|
||||
}
|
||||
|
||||
@Test func discoverUsesMetadataForRangeAndDisplayName() {
|
||||
let synth: UIParamSynth = UIParamSynth()
|
||||
let metadata: [String: LyrebirdAUParameterInfo] = [
|
||||
"cutoff": .init(min: 20, max: 20000, unit: .hertz, displayName: "Cutoff Frequency"),
|
||||
"resonance": .init(min: 0, max: 1),
|
||||
]
|
||||
let entries: [LyrebirdSynthView.Entry] = LyrebirdSynthView.discover(synth: synth, metadata: metadata)
|
||||
let cutoff: LyrebirdSynthView.Entry? = entries.first { $0.propertyName == "cutoff" }
|
||||
#expect(cutoff?.displayName == "Cutoff Frequency")
|
||||
#expect(cutoff?.range == 20.0...20000.0)
|
||||
let res: LyrebirdSynthView.Entry? = entries.first { $0.propertyName == "resonance" }
|
||||
#expect(res?.range == 0.0...1.0)
|
||||
// No metadata → defaults.
|
||||
let amp: LyrebirdSynthView.Entry? = entries.first { $0.propertyName == "amp" }
|
||||
#expect(amp?.range == lyrebirdSynthViewDefaultMin...lyrebirdSynthViewDefaultMax)
|
||||
#expect(amp?.displayName == "amp")
|
||||
}
|
||||
|
||||
@Test func entriesPointAtUnderlyingUIParamNodes() {
|
||||
let synth: UIParamSynth = UIParamSynth()
|
||||
let entries: [LyrebirdSynthView.Entry] = LyrebirdSynthView.discover(synth: synth)
|
||||
let cutoffEntry: LyrebirdSynthView.Entry? = entries.first { $0.propertyName == "cutoff" }
|
||||
#expect(cutoffEntry?.node === synth.$cutoff)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// MultiSliderTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable import LyrebirdUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@Suite("MultiSlider")
|
||||
struct MultiSliderTests {
|
||||
|
||||
/// `MultiSlider.init` reads initial bar values from the buffer's current
|
||||
/// contents — verify by constructing one against a buffer with known values.
|
||||
/// We can't exercise the SwiftUI body in a unit test, so this is a structural
|
||||
/// smoke covering the precondition path and the `@State` seed loop.
|
||||
@Test func initReadsValuesFromBuffer() {
|
||||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
buf.setValue(Double(i) / 8.0, at: i)
|
||||
}
|
||||
// Construct does not crash; preconditions all pass for valid args.
|
||||
_ = MultiSlider(buffer: buf, count: 4, startIndex: 2, range: 0.0...1.0, height: 80)
|
||||
}
|
||||
|
||||
@Test func initWithFullBufferRange() {
|
||||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16)
|
||||
buf.fill(with: 0.5)
|
||||
_ = MultiSlider(buffer: buf, count: 16, startIndex: 0, range: 0.0...1.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
// UIParamNodeTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable import LyrebirdUI
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
@Suite("UIParamNode init") struct UIParamNodeInitTests {
|
||||
|
||||
@Test func initialValueStoredAsObservable() {
|
||||
let node: UIParamNode = UIParamNode(440.0)
|
||||
#expect(node.value == 440.0)
|
||||
}
|
||||
|
||||
@Test func initialValueReadableViaFloatValue() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(0.5)
|
||||
#expect(node.floatValue(graph: graph) == 0.5)
|
||||
}
|
||||
|
||||
@Test func initialValueReadableViaIntValue() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(3.7)
|
||||
#expect(node.intValue(graph: graph) == 3)
|
||||
}
|
||||
|
||||
@Test func intValueAfterUpdate() {
|
||||
// intValue must reflect the updated value (direct atomic read, no ramp).
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(1.0)
|
||||
node.update(5.7)
|
||||
#expect(node.intValue(graph: graph) == 5)
|
||||
}
|
||||
|
||||
@Test func intValueDoesNotPrimeFanOutCache() {
|
||||
// Calling intValue then calculatedSamples in the same segment must produce a correct
|
||||
// ramp — intValue must not consume the fan-out budget (no calculatedSamples side-effect).
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let blockCount: Int = 4
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
node.update(4.0)
|
||||
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
// intValue call — must be a direct atomic read, no _computeSamples invocation.
|
||||
_ = node.intValue(graph: graph)
|
||||
// calculatedSamples in the same segment must still compute the correct ramp.
|
||||
let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount)
|
||||
// Ramp 0→4 in 4 steps: [1.0, 2.0, 3.0, 4.0]
|
||||
#expect(result[0][blockCount - 1] == 4.0,
|
||||
"calculatedSamples after intValue must still produce the correct ramp target")
|
||||
}
|
||||
|
||||
@Test func needsInterpolationIsTrue() {
|
||||
let node: UIParamNode = UIParamNode(1.0)
|
||||
#expect(node.needsInterpolation() == true)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Atomic update round-trip
|
||||
|
||||
@Suite("UIParamNode atomic update") struct UIParamNodeAtomicTests {
|
||||
|
||||
@Test func updateSetsObservableValue() {
|
||||
let node: UIParamNode = UIParamNode(100.0)
|
||||
node.update(200.0)
|
||||
#expect(node.value == 200.0)
|
||||
}
|
||||
|
||||
@Test func updateReflectsInFloatValue() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(100.0)
|
||||
node.update(880.0)
|
||||
#expect(node.floatValue(graph: graph) == 880.0)
|
||||
}
|
||||
|
||||
@Test func multipleUpdatesRetainLatestValue() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
node.update(1.0)
|
||||
node.update(2.0)
|
||||
node.update(3.0)
|
||||
#expect(node.floatValue(graph: graph) == 3.0)
|
||||
#expect(node.value == 3.0)
|
||||
}
|
||||
|
||||
@Test func zeroRoundTrip() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(1.0)
|
||||
node.update(0.0)
|
||||
#expect(node.floatValue(graph: graph) == 0.0)
|
||||
}
|
||||
|
||||
@Test func negativeValueRoundTrip() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
node.update(-1.5)
|
||||
#expect(node.floatValue(graph: graph) == -1.5)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - calculatedSamples interpolation
|
||||
|
||||
@Suite("UIParamNode calculatedSamples") struct UIParamNodeCalcTests {
|
||||
|
||||
@Test func flatBlockWhenValueUnchanged() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let blockCount: Int = 4
|
||||
let node: UIParamNode = UIParamNode(1.0)
|
||||
let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount)
|
||||
#expect(result.count == 1)
|
||||
// Buffer is pre-allocated to lyrebirdDefaultResultBufferSize — never resized.
|
||||
// Only the first blockCount elements are meaningful.
|
||||
#expect(result[0].count >= blockCount)
|
||||
// No change — step = 0 — the first blockCount samples should all equal 1.0
|
||||
for sample: Double in result[0].prefix(blockCount) {
|
||||
#expect(sample == 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func rampReachesTargetAtLastSample() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let blockCount: Int = 4
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
node.update(4.0)
|
||||
let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount)
|
||||
// The meaningful range is [0..<blockCount]; index blockCount-1 is the last ramp sample.
|
||||
#expect(result[0][blockCount - 1] == 4.0)
|
||||
}
|
||||
|
||||
@Test func rampIsLinear() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
node.update(4.0)
|
||||
let result: [[Double]] = node.calculatedSamples(graph: graph, count: 4)
|
||||
// Ramp from 0→4 in 4 steps: expected [1.0, 2.0, 3.0, 4.0] at indices 0–3
|
||||
#expect(result[0][0] == 1.0)
|
||||
#expect(result[0][1] == 2.0)
|
||||
#expect(result[0][2] == 3.0)
|
||||
#expect(result[0][3] == 4.0)
|
||||
}
|
||||
|
||||
@Test func subsequentBlockIsFlatAtTarget() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let blockCount: Int = 4
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
node.update(2.0)
|
||||
// Block 1 — ramp from 0 to 2.0.
|
||||
let graph1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
_ = node.calculatedSamples(graph: graph1, count: blockCount)
|
||||
// Advance to the next segment so the fan-out cache is invalidated.
|
||||
advanceUIBlock(engine)
|
||||
// Block 2 — value unchanged, should be flat at 2.0.
|
||||
let graph2: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let second: [[Double]] = node.calculatedSamples(graph: graph2, count: blockCount)
|
||||
// Only the first blockCount elements are meaningful after this call.
|
||||
for sample: Double in second[0].prefix(blockCount) {
|
||||
#expect(sample == 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func sampleBlockMatchesFirstChannel() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
// Use separate nodes with identical initial state so each call starts fresh.
|
||||
let node2D: UIParamNode = UIParamNode(0.0)
|
||||
node2D.update(4.0)
|
||||
let via2D: [Double] = node2D.calculatedSamples(graph: graph, count: 4).first ?? []
|
||||
|
||||
let node1D: UIParamNode = UIParamNode(0.0)
|
||||
node1D.update(4.0)
|
||||
let via1D: [Double] = node1D.sampleBlock(graph: graph, count: 4, previousValue: 0.0)
|
||||
|
||||
#expect(via2D == via1D)
|
||||
}
|
||||
|
||||
@Test func bufferStaysAtPreAllocatedSize() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let node: UIParamNode = UIParamNode(1.0)
|
||||
let first: [[Double]] = node.calculatedSamples(graph: graph, count: 8)
|
||||
let second: [[Double]] = node.calculatedSamples(graph: graph, count: 8)
|
||||
// Buffer is fixed at lyrebirdDefaultResultBufferSize — never resized regardless of count.
|
||||
#expect(first[0].count == lyrebirdDefaultResultBufferSize)
|
||||
#expect(second[0].count == lyrebirdDefaultResultBufferSize)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ValidGeneratorInput wiring
|
||||
|
||||
@Suite("UIParamNode as ValidGeneratorInput") struct UIParamNodeWiringTests {
|
||||
|
||||
@Test func usableAsSourceParameter() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
try engine.start()
|
||||
let node: UIParamNode = UIParamNode(0.5)
|
||||
|
||||
class ParamSynth: SynthesizerBase {
|
||||
let node: UIParamNode
|
||||
init(node: UIParamNode) { self.node = node; super.init() }
|
||||
override func build() {
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: node))
|
||||
}
|
||||
}
|
||||
|
||||
let synth: ParamSynth = ParamSynth(node: node)
|
||||
try engine.add(synth)
|
||||
engine.processBlock(inputSamples: [])
|
||||
|
||||
let bus0: [Float] = engine.audioBuses[0]
|
||||
// Out accumulates, so bus 0 should be non-zero
|
||||
let anyNonZero: Bool = bus0.contains { $0 != 0.0 }
|
||||
#expect(anyNonZero)
|
||||
}
|
||||
|
||||
@Test func updateReflectsInNextBlock() throws {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
try engine.start()
|
||||
let node: UIParamNode = UIParamNode(0.0)
|
||||
|
||||
class ParamSynth: SynthesizerBase {
|
||||
let node: UIParamNode
|
||||
init(node: UIParamNode) { self.node = node; super.init() }
|
||||
override func build() {
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: node))
|
||||
}
|
||||
}
|
||||
|
||||
let synth: ParamSynth = ParamSynth(node: node)
|
||||
try engine.add(synth)
|
||||
engine.processBlock(inputSamples: [])
|
||||
|
||||
// Bus 0 should be zero (node value was 0)
|
||||
let allZero: Bool = engine.audioBuses[0].allSatisfy { $0 == 0.0 }
|
||||
#expect(allZero)
|
||||
|
||||
// Update to 1.0 — next block ramps from 0 to 1.0; last sample reaches target.
|
||||
node.update(1.0)
|
||||
engine.processBlock(inputSamples: [])
|
||||
let lastSample: Float = engine.audioBuses[0][7]
|
||||
#expect(abs(lastSample - 1.0) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Source-swap
|
||||
|
||||
@Suite("UIParamNode source swap") struct UIParamNodeSourceSwapTests {
|
||||
|
||||
@Test func sourceAssignmentRoutesThroughCalculatedSamples() {
|
||||
// Verifies that UIParamNode.calculatedSamples forwards to
|
||||
// _node.calculatedSamples — actually drains the source queue and
|
||||
// renders the source's output, not the scalar ramp.
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let blockCount: Int = 8
|
||||
let node: UIParamNode = UIParamNode(100.0)
|
||||
node.update(100.0)
|
||||
|
||||
// Warm-up block to settle the scalar ramp at 100.
|
||||
let graph0: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
_ = node.calculatedSamples(graph: graph0, count: blockCount)
|
||||
advanceUIBlock(engine)
|
||||
|
||||
// Install a constant-value source generator that outputs 770.
|
||||
node.source = ConstantTestGenerator(770.0)
|
||||
|
||||
// Next block should reflect the source's output, not the scalar 100.
|
||||
let graph1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let result: [[Double]] = node.calculatedSamples(graph: graph1, count: blockCount)
|
||||
|
||||
// The source is a control-rate constant — calculatedSamples returns
|
||||
// a one-row buffer containing the constant value.
|
||||
#expect(result[0][blockCount - 1] == 770.0,
|
||||
"UIParamNode.source must drive output via _node.calculatedSamples")
|
||||
}
|
||||
|
||||
@Test func sourceNilRevertsToScalarRamp() {
|
||||
let engine: SynthesisEngine = makeUITestEngine()
|
||||
let blockCount: Int = 8
|
||||
let node: UIParamNode = UIParamNode(100.0)
|
||||
node.update(100.0)
|
||||
|
||||
let graph0: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
_ = node.calculatedSamples(graph: graph0, count: blockCount)
|
||||
advanceUIBlock(engine)
|
||||
|
||||
node.source = ConstantTestGenerator(770.0)
|
||||
let graph1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
_ = node.calculatedSamples(graph: graph1, count: blockCount)
|
||||
advanceUIBlock(engine)
|
||||
|
||||
// Revert. After enough blocks the scalar ramp should resettle at 100.
|
||||
node.source = nil
|
||||
for _ in 0..<8 {
|
||||
let g: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
_ = node.calculatedSamples(graph: g, count: blockCount)
|
||||
advanceUIBlock(engine)
|
||||
}
|
||||
let gFinal: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
||||
let final: [[Double]] = node.calculatedSamples(graph: gFinal, count: blockCount)
|
||||
#expect(abs(final[0][blockCount - 1] - 100.0) < 1e-9,
|
||||
"UIParamNode.source = nil must revert to scalar")
|
||||
}
|
||||
}
|
||||
|
||||
/// Minimal ControlGenerator emitting a constant value, used to test that
|
||||
/// `UIParamNode.source` actually routes through `_node.calculatedSamples`
|
||||
/// and not the scalar ramp.
|
||||
private final class ConstantTestGenerator: GeneratorBase, ControlGenerator, @unchecked Sendable {
|
||||
private let _value: Double
|
||||
private var _buf: [[Double]] = [[0.0]]
|
||||
var previousOutputValue: Double
|
||||
init(_ value: Double) {
|
||||
_value = value
|
||||
previousOutputValue = value
|
||||
_buf = [[Double](repeating: value, count: lyrebirdDefaultResultBufferSize)]
|
||||
}
|
||||
func floatValue(graph: SynthGraph) -> Double { _value }
|
||||
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||||
return _buf
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Test helpers
|
||||
|
||||
/// Advances the fan-out cache serial by one, simulating a block boundary.
|
||||
/// Mirror of `advanceBlock` in `LyrebirdTests/TestHelpers.swift` for use in UI tests.
|
||||
private func advanceUIBlock(_ engine: SynthesisEngine) {
|
||||
engine.advanceSegmentSerial()
|
||||
}
|
||||
|
||||
private func makeUITestEngine() -> SynthesisEngine {
|
||||
SynthesisEngine(
|
||||
blockSize: 8,
|
||||
sampleRate: 44100,
|
||||
numOutputChannels: 2,
|
||||
numInputChannels: 0,
|
||||
numAudioBuses: 8,
|
||||
numControlBuses: 8,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
// UIParamTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable import LyrebirdUI
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - @UIParam property wrapper
|
||||
|
||||
@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()
|
||||
#expect(s.$freq 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
|
||||
|
||||
@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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user