Files
Lyrebird/Tests/LyrebirdTests/Generators/Routing/SelectControlTests.swift
T
2026-06-21 11:45:58 -07:00

168 lines
6.7 KiB
Swift

// SelectControlTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("Select.Control")
struct SelectControlTests {
let engine: SynthesisEngine
var graph: SynthGraph { makeSynthGraph(engine: engine) }
let blockSize: Int = 64
let sampleRate: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
// MARK: - Rate
@Test func rateIsControl() {
let gen: Select.Control = Select.Control(which: 0, inputs: [1.0])
#expect(gen.rate == .control)
}
// MARK: - Output count
@Test func outputCountMatchesRequest() {
let gen: Select.Control = Select.Control(which: 0, inputs: [1.0, 2.0, 3.0])
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
}
// MARK: - Index selection
/// which = 0 picks inputs[0].
@Test func whichZeroPicksFirstInput() {
let gen: Select.Control = Select.Control(which: 0, inputs: [0.1, 0.2, 0.3])
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.1) < 1e-12,
"which=0 target should be 0.1, got \(gen.previousOutputValue)")
}
/// which = 1 picks inputs[1].
@Test func whichOnePicksSecondInput() {
let gen: Select.Control = Select.Control(which: 1, inputs: [0.1, 0.2, 0.3])
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12,
"which=1 target should be 0.2, got \(gen.previousOutputValue)")
}
// MARK: - Truncation and clamping
/// Non-integer which truncates: which = 1.9 → index 1.
@Test func nonIntegerWhichTruncates() {
let gen: Select.Control = Select.Control(which: 1.9, inputs: [0.1, 0.2, 0.3])
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12,
"which=1.9 (→ index 1) target should be 0.2, got \(gen.previousOutputValue)")
}
/// Negative which clamps to index 0.
@Test func negativeWhichClampsToZero() {
let gen: Select.Control = Select.Control(which: -5.0, inputs: [0.1, 0.2, 0.3])
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.1) < 1e-12,
"which=-5 (clamp→0) target should be 0.1, got \(gen.previousOutputValue)")
}
/// which >= N clamps to N-1.
@Test func overflowWhichClampsToLast() {
let gen: Select.Control = Select.Control(which: 99.0, inputs: [0.1, 0.2, 0.3])
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.3) < 1e-12,
"which=99 (clamp→2) target should be 0.3, got \(gen.previousOutputValue)")
}
// MARK: - Ramp
/// Last sample of first block equals the selected target.
@Test func rampsToTarget() {
let gen: Select.Control = Select.Control(which: 2, inputs: [0.1, 0.5, 0.9])
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
let lastSample: Double = out[0][blockSize - 1]
#expect(abs(lastSample - 0.9) < 1e-12,
"last sample of ramp should equal target 0.9, got \(lastSample)")
}
// MARK: - mul/add
@Test func mulAddApplied() {
// which=0, inputs[0]=0.5 → base target = 0.5
// mul=2, add=1 → scaled target = 0.5*2+1 = 2.0
let gen: Select.Control = Select.Control(which: 0, inputs: [0.5, 0.8], mul: 2.0, add: 1.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 2.0) < 1e-12,
"mulAdd: expected 2.0, got \(gen.previousOutputValue)")
}
// MARK: - Fan-out
@Test func fanOutSameSegment() {
let gen: Select.Control = Select.Control(which: 1, inputs: [0.3, 0.7])
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
for i: Int in 0..<blockSize {
#expect(out1[0][i] == out2[0][i],
"fan-out: sample \(i) differs between calls on same segment")
}
}
/// floatValue returns previousOutputValue.
@Test func floatValueReturnsPreviousOutputValue() {
let gen: Select.Control = Select.Control(which: 0, inputs: [0.4])
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
}
// MARK: - All inputs advance every block
/// At control rate: pulling floatValue on non-selected inputs advances their state.
@Test func allInputsAdvanceEveryBlock() {
// input[1] returns 0.0 on first block, 99.0 on second block.
let firstSerial: UInt64 = engine.segmentSerial
let input1: Closure = Closure { graph, _ in
graph.engine.segmentSerial == firstSerial ? 0.0 : 99.0
}
let gen: Select.Control = Select.Control(which: 0, inputs: [1.0, input1])
// Block 1: select input[0] = 1.0.
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
"block 1: expected 1.0, got \(gen.previousOutputValue)")
// Block 2: switch to input[1] to confirm it has been called.
advanceBlock(engine)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let gen2: Select.Control = Select.Control(which: 1.0, inputs: [1.0, input1])
_ = gen2.calculatedSamples(graph: g2, count: blockSize)
// Since this is a fresh generator, input[1] on block 2's serial returns 99.0.
#expect(abs(gen2.previousOutputValue - 99.0) < 1e-12,
"block 2 (select input[1]): expected 99.0, got \(gen2.previousOutputValue)")
}
// MARK: - Integration
@Test func integrationWithEngine() throws {
final class SelectControlSynth: SynthesizerBase {
override func build() {
let gen: Select.Control = Select.Control(which: 1, inputs: [0.1, 0.2, 0.3])
addOutputGenerator(Out.Audio(bus: 0, source: gen))
}
}
let synth: SelectControlSynth = SelectControlSynth()
do {
try engine.add(synth)
} catch {
Issue.record("engine.add failed: \(error)")
}
advanceBlock(engine)
}
}