227 lines
7.9 KiB
Swift
227 lines
7.9 KiB
Swift
// FluteTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Flute")
|
||
struct FluteTests {
|
||
|
||
// MARK: - Test fixture
|
||
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
let testSampleRate: Double = 44100
|
||
let testMaxDelayTime: Double = 0.02
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
// MARK: - Rate
|
||
|
||
@Test func rateIsAudio() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
#expect(flute.rate == .audio)
|
||
}
|
||
|
||
// MARK: - Output count
|
||
|
||
@Test func outputCountMatchesRequest() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let out: [[Double]] = flute.calculatedSamples(graph: graph, count: 512)
|
||
#expect(out.count >= 1)
|
||
#expect(out[0].count >= 512)
|
||
}
|
||
|
||
// MARK: - Silent at zero excitation
|
||
|
||
@Test func silentAtZeroExcitation() {
|
||
// With breathPressure = 0 and noiseGain = 0, no energy enters the waveguide.
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
breathPressure: 0.0,
|
||
noiseGain: 0.0,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let out: [[Double]] = flute.calculatedSamples(graph: graph, count: 512)
|
||
let samples: [Double] = Array(out[0].prefix(512))
|
||
let allNearZero: Bool = samples.allSatisfy { abs($0) < 1e-6 }
|
||
#expect(allNearZero, "Output must be near zero with breathPressure = 0 and noiseGain = 0")
|
||
}
|
||
|
||
// MARK: - Non-silent with excitation
|
||
|
||
@Test func nonSilentWithExcitation() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: 0.5,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
var lastSamples: [Double] = []
|
||
for _ in 0..<10 {
|
||
let out: [[Double]] = flute.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||
lastSamples = Array(out[0].prefix(512))
|
||
advanceBlock(engine)
|
||
}
|
||
let rms: Double = rmsValue(lastSamples)
|
||
#expect(rms > 0, "Flute must produce non-zero output with breathPressure > 0")
|
||
#expect(lastSamples.allSatisfy { $0.isFinite }, "All samples must be finite")
|
||
}
|
||
|
||
// MARK: - Fan-out single evaluation
|
||
|
||
@Test func fanOutSingleEvaluation() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let out1: [[Double]] = flute.calculatedSamples(graph: graph, count: 512)
|
||
let out2: [[Double]] = flute.calculatedSamples(graph: graph, count: 512)
|
||
let s1: [Double] = Array(out1[0].prefix(512))
|
||
let s2: [Double] = Array(out2[0].prefix(512))
|
||
#expect(s1 == s2, "Fan-out: two calculatedSamples calls in same segment must return identical data")
|
||
}
|
||
|
||
// MARK: - State advances across blocks
|
||
|
||
@Test func stateAdvancesAcrossBlocks() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
breathPressure: 0.5,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
_ = flute.calculatedSamples(graph: graph, count: 512)
|
||
_ = flute.calculatedSamples(graph: graph, count: 512) // cache hit
|
||
advanceBlock(engine)
|
||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||
let block2: [[Double]] = flute.calculatedSamples(graph: graph2, count: 512)
|
||
let allFinite: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite }
|
||
#expect(allFinite, "All output samples must be finite after block advance")
|
||
}
|
||
|
||
// MARK: - mulAdd applied
|
||
|
||
@Test func mulAddApplied() {
|
||
let flute1: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: ConstantGeneratorInput(0.5),
|
||
noiseGain: ConstantGeneratorInput(0.0),
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let flute2: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: ConstantGeneratorInput(0.5),
|
||
noiseGain: ConstantGeneratorInput(0.0),
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime,
|
||
mul: 2.0,
|
||
add: 1.0
|
||
)
|
||
let out1: [Double] = Array(flute1.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||
let out2: [Double] = Array(flute2.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||
var relationshipHolds: Bool = true
|
||
for i: Int in 0..<512 {
|
||
let expected: Double = out1[i] * 2.0 + 1.0
|
||
if abs(out2[i] - expected) > 1e-10 {
|
||
relationshipHolds = false
|
||
break
|
||
}
|
||
}
|
||
#expect(relationshipHolds, "mul:2.0 add:1.0 must produce output × 2 + 1")
|
||
}
|
||
|
||
// MARK: - Integration with engine
|
||
|
||
@Test func integrationWithEngine() throws {
|
||
final class FluteSynth: SynthesizerBase {
|
||
override func build() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: 0.5,
|
||
sampleRate: 44100,
|
||
maxDelayTime: 0.02
|
||
)
|
||
addOutputGenerator(Out.Audio(bus: 0, source: flute))
|
||
}
|
||
}
|
||
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||
try intEngine.start()
|
||
let synth: FluteSynth = FluteSynth()
|
||
try intEngine.add(synth)
|
||
for _ in 0..<4 {
|
||
intEngine.processBlock(inputSamples: [])
|
||
}
|
||
intEngine.stop()
|
||
}
|
||
|
||
// MARK: - Finite after many blocks
|
||
|
||
@Test func finiteAfterManyBlocks() {
|
||
let flute: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: 0.5,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let blockCount: Int = 200
|
||
var allFinite: Bool = true
|
||
for _ in 0..<blockCount {
|
||
let out: [[Double]] = flute.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||
let samples: [Double] = Array(out[0].prefix(512))
|
||
if !samples.allSatisfy({ $0.isFinite }) {
|
||
allFinite = false
|
||
break
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(allFinite, "Flute must produce finite output for 200 blocks")
|
||
}
|
||
|
||
// MARK: - Jet ratio affects tone
|
||
|
||
@Test func jetRatioAffectsTone() {
|
||
let flute1: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: 0.5,
|
||
jetRatio: 0.1,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let flute2: Flute.Audio = Flute.Audio(
|
||
freq: 440.0,
|
||
breathPressure: 0.5,
|
||
jetRatio: 0.6,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
|
||
let warmupBlocks: Int = 20
|
||
for b: Int in 0..<warmupBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = flute1.calculatedSamples(graph: g, count: 512)
|
||
_ = flute2.calculatedSamples(graph: g, count: 512)
|
||
advanceBlock(engine)
|
||
_ = b
|
||
}
|
||
let gFinal: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out1: [Double] = Array(flute1.calculatedSamples(graph: gFinal, count: 512)[0].prefix(512))
|
||
let out2: [Double] = Array(flute2.calculatedSamples(graph: gFinal, count: 512)[0].prefix(512))
|
||
let identical: Bool = out1 == out2
|
||
#expect(!identical, "Different jetRatio values must produce different output buffers")
|
||
}
|
||
}
|