165 lines
4.9 KiB
Swift
165 lines
4.9 KiB
Swift
// ValidGeneratorInputTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Double as ValidGeneratorInput")
|
||
struct DoubleValidGeneratorInputTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func doubleFloatValue() {
|
||
let v: Double = 0.5
|
||
#expect(v.floatValue(graph: graph) == 0.5)
|
||
}
|
||
|
||
@Test func doubleIntValue() {
|
||
let v: Double = 3.7
|
||
#expect(v.intValue(graph: graph) == 3)
|
||
}
|
||
|
||
@Test func doubleCalculatedSamplesCount() {
|
||
let v: Double = 1.0
|
||
let samples: [[Double]] = v.calculatedSamples(graph: graph, count: 64)
|
||
#expect(samples[0].count == 64)
|
||
}
|
||
|
||
@Test func doubleCalculatedSamplesValue() {
|
||
let v: Double = 2.5
|
||
let samples: [[Double]] = v.calculatedSamples(graph: graph, count: 8)
|
||
#expect(samples[0].allSatisfy { $0 == 2.5 })
|
||
}
|
||
|
||
@Test func doubleNeedsNoInterpolation() {
|
||
let v: Double = 1.0
|
||
#expect(v.needsInterpolation() == false)
|
||
}
|
||
|
||
@Test func doubleSampleBlockIsConstant() {
|
||
let v: Double = 3.0
|
||
let block: [Double] = v.sampleBlock(graph: graph, count: 8, previousValue: 0.0)
|
||
#expect(block.allSatisfy { $0 == 3.0 })
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
@Suite("Int as ValidGeneratorInput")
|
||
struct IntValidGeneratorInputTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func intFloatValue() {
|
||
let v: Int = 3
|
||
#expect(v.floatValue(graph: graph) == 3.0)
|
||
}
|
||
|
||
@Test func intCalculatedSamplesValue() {
|
||
let v: Int = 5
|
||
let samples: [[Double]] = v.calculatedSamples(graph: graph, count: 4)
|
||
#expect(samples[0].allSatisfy { $0 == Double(5) })
|
||
}
|
||
|
||
@Test func intNeedsNoInterpolation() {
|
||
let v: Int = 1
|
||
#expect(v.needsInterpolation() == false)
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
@Suite("Closure as ValidGeneratorInput")
|
||
struct ClosureTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func closureFloatValue() {
|
||
let c: Closure = Closure { _, _ in 7.0 }
|
||
#expect(c.floatValue(graph: graph) == 7.0)
|
||
}
|
||
|
||
@Test func closureCalculatedSamples() {
|
||
let c: Closure = Closure { _, i in Double(i) }
|
||
let samples: [[Double]] = c.calculatedSamples(graph: graph, count: 4)
|
||
#expect(samples[0] == [0.0, 1.0, 2.0, 3.0])
|
||
}
|
||
|
||
@Test func closureNeedsInterpolation() {
|
||
let c: Closure = Closure { _, _ in 0.0 }
|
||
#expect(c.needsInterpolation() == true)
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
@Suite("Generator as ValidGeneratorInput")
|
||
struct GeneratorAsValidGeneratorInputTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func generatorFloatValue() {
|
||
let gen: ConstantAudioGen = ConstantAudioGen(1.5)
|
||
#expect(gen.floatValue(graph: graph) == 1.5)
|
||
}
|
||
|
||
@Test func generatorCalculatedSamples() {
|
||
let gen: ConstantAudioGen = ConstantAudioGen(2.0)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||
#expect(samples[0] == [Double](repeating: 2.0, count: 8))
|
||
}
|
||
|
||
@Test func audioGeneratorNeedsNoInterpolation() {
|
||
let gen: ConstantAudioGen = ConstantAudioGen(1.0)
|
||
#expect(gen.needsInterpolation() == false)
|
||
}
|
||
|
||
@Test func controlGeneratorNeedsInterpolation() {
|
||
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
||
#expect(gen.needsInterpolation() == true)
|
||
}
|
||
|
||
@Test func controlGeneratorSampleBlockInterpolates() {
|
||
let gen: ConstantControlGen = ConstantControlGen(1.0)
|
||
let block: [Double] = gen.sampleBlock(graph: graph, count: 4, previousValue: 0.0)
|
||
// sampleBlock delegates to calculatedSamples for Generator conformers.
|
||
// ControlGenerator default: ramp from previousOutputValue (0.0) to target (1.0),
|
||
// reaching the target exactly at the last sample.
|
||
//
|
||
// Formula: previousOutputValue + step × (i+1), step = (target − prev) / count
|
||
// i=0: 0 + 0.25×1 = 0.25
|
||
// i=1: 0 + 0.25×2 = 0.50
|
||
// i=2: 0 + 0.25×3 = 0.75
|
||
// i=3: 0 + 0.25×4 = 1.00 ← reaches target at last sample
|
||
//
|
||
// Note: the `previousValue` parameter is unused for Generator conformers — state is
|
||
// tracked internally via `previousOutputValue`.
|
||
#expect(block == [0.25, 0.5, 0.75, 1.0])
|
||
}
|
||
}
|