172 lines
6.2 KiB
Swift
172 lines
6.2 KiB
Swift
// GeneratorProtocolTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// File-scope helpers used across generator tests
|
|
|
|
final class ConstantAudioGen: GeneratorBase, AudioGenerator {
|
|
let value: Double
|
|
init(_ v: Double) { value = v }
|
|
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
|
[[Double](repeating: value, count: count)]
|
|
}
|
|
func floatValue(graph: SynthGraph) -> Double { value }
|
|
}
|
|
|
|
/// A ControlGenerator with a fixed target value, used to test the default
|
|
/// interpolated-ramp behaviour.
|
|
final class ConstantControlGen: GeneratorBase, ControlGenerator {
|
|
let value: Double
|
|
/// Required by ControlGenerator — ramp start for the next block. Starts at 0.
|
|
var previousOutputValue: Double = 0.0
|
|
|
|
init(_ v: Double) {
|
|
value = v
|
|
super.init()
|
|
_setupSubBlockBuffer()
|
|
}
|
|
func floatValue(graph: SynthGraph) -> Double { value }
|
|
// _computeSamples uses the default ControlGenerator extension (interpolated ramp).
|
|
}
|
|
|
|
/// A ControlGenerator whose target can be changed between blocks, used to test that
|
|
/// previousOutputValue carries the ramp state correctly across block boundaries.
|
|
final class MutableControlGen: GeneratorBase, ControlGenerator {
|
|
var target: Double
|
|
var previousOutputValue: Double = 0.0
|
|
init(_ v: Double) {
|
|
target = v
|
|
super.init()
|
|
_setupSubBlockBuffer()
|
|
}
|
|
func floatValue(graph: SynthGraph) -> Double { target }
|
|
}
|
|
|
|
@MainActor
|
|
@Suite("ProcessingRate")
|
|
struct ProcessingRateTests {
|
|
@Test func audioRateExists() {
|
|
let r: ProcessingRate = .audio
|
|
#expect(r == .audio)
|
|
}
|
|
|
|
@Test func controlRateExists() {
|
|
let r: ProcessingRate = .control
|
|
#expect(r == .control)
|
|
}
|
|
|
|
@Test func spectralRateExists() {
|
|
let r: ProcessingRate = .spectral
|
|
#expect(r == .spectral)
|
|
}
|
|
|
|
@Test func demandRateExists() {
|
|
let r: ProcessingRate = .demand
|
|
#expect(r == .demand)
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite("Generator protocol")
|
|
struct GeneratorProtocolTests {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
|
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
|
|
|
@Test func audioGeneratorDefaultRateIsAudio() {
|
|
let gen: ConstantAudioGen = ConstantAudioGen(1.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
@Test func controlGeneratorDefaultRateIsControl() {
|
|
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
@Test func generatorConformsToValidGeneratorInput() throws {
|
|
let gen: ConstantAudioGen = ConstantAudioGen(2.5)
|
|
let input: any ValidGeneratorInput = gen
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
|
try engine.start()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
#expect(input.floatValue(graph: graph) == 2.5)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - ControlGenerator interpolation
|
|
|
|
@Test func controlGeneratorPreviousValueInitiallyZero() {
|
|
// All ControlGenerator conformers must initialise previousOutputValue to 0.
|
|
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
|
#expect(gen.previousOutputValue == 0.0)
|
|
}
|
|
|
|
@Test func controlGeneratorCalculatedSamplesRampsToTarget() {
|
|
// previousOutputValue=0, target=1, count=4 → step = 0.25
|
|
// samples: [0.25, 0.50, 0.75, 1.00] — reaches target at last sample
|
|
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
|
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 4)
|
|
#expect(samples[0] == [0.25, 0.5, 0.75, 1.0])
|
|
}
|
|
|
|
@Test func controlGeneratorReachesTargetAtLastSample() {
|
|
let target: Double = 2.5
|
|
let gen: ConstantControlGen = ConstantControlGen(target)
|
|
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
|
#expect(samples[0].last == target)
|
|
}
|
|
|
|
@Test func controlGeneratorFirstSampleIsOneStepAbovePrevious() {
|
|
// first sample = prev + step = prev + (target - prev) / count
|
|
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
|
let count: Int = 4
|
|
let step: Double = (1.0 - 0.0) / Double(count)
|
|
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
|
#expect(samples[0][0] == step)
|
|
}
|
|
|
|
@Test func controlGeneratorUpdatesPreviousOutputValueAfterBlock() {
|
|
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: 4)
|
|
#expect(gen.previousOutputValue == 1.0)
|
|
}
|
|
|
|
@Test func controlGeneratorSecondBlockStartsFromPreviousTarget() {
|
|
// Block 1: 0→1 → [0.25, 0.50, 0.75, 1.00], previousOutputValue = 1.0
|
|
// Block 2: 1→1 → [1.00, 1.00, 1.00, 1.00] (step = 0, constant at target)
|
|
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: 4)
|
|
advanceBlock(engine)
|
|
let block2: [[Double]] = gen.calculatedSamples(graph: graph, count: 4)
|
|
#expect(block2[0] == [1.0, 1.0, 1.0, 1.0])
|
|
}
|
|
|
|
@Test func controlGeneratorRampsDownWhenTargetDecreases() {
|
|
// Block 1: 0→2 → previousOutputValue = 2
|
|
// Block 2: 2→0 → [1.5, 1.0, 0.5, 0.0]
|
|
let gen: MutableControlGen = MutableControlGen(2.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: 4)
|
|
advanceBlock(engine)
|
|
gen.target = 0.0
|
|
let block2: [[Double]] = gen.calculatedSamples(graph: graph, count: 4)
|
|
#expect(block2[0] == [1.5, 1.0, 0.5, 0.0])
|
|
}
|
|
|
|
@Test func controlGeneratorRampCarriesAcrossConsecutiveBlocks() {
|
|
// Two-block ramp: 0→1 then 1→3
|
|
// Block 1: step=(1-0)/4=0.25 → [0.25, 0.50, 0.75, 1.00]
|
|
// Block 2: step=(3-1)/4=0.50 → [1.50, 2.00, 2.50, 3.00]
|
|
let gen: MutableControlGen = MutableControlGen(1.0)
|
|
let block1: [[Double]] = gen.calculatedSamples(graph: graph, count: 4)
|
|
advanceBlock(engine)
|
|
gen.target = 3.0
|
|
let block2: [[Double]] = gen.calculatedSamples(graph: graph, count: 4)
|
|
#expect(block1[0] == [0.25, 0.5, 0.75, 1.0])
|
|
#expect(block2[0] == [1.5, 2.0, 2.5, 3.0])
|
|
}
|
|
}
|