prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
// ClarinetTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Clarinet")
|
||||
struct ClarinetTests {
|
||||
|
||||
// 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.05
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let clar: Clarinet.Audio = Clarinet.Audio(
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
#expect(clar.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let clar: Clarinet.Audio = Clarinet.Audio(
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = clar.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 bore.
|
||||
let clar: Clarinet.Audio = Clarinet.Audio(
|
||||
breathPressure: 0.0,
|
||||
noiseGain: 0.0,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = clar.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 clar: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: 0.6,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
var lastSamples: [Double] = []
|
||||
for _ in 0..<10 {
|
||||
let out: [[Double]] = clar.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||||
lastSamples = Array(out[0].prefix(512))
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let rms: Double = rmsValue(lastSamples)
|
||||
#expect(rms > 0, "Clarinet 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 clar: Clarinet.Audio = Clarinet.Audio(
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out1: [[Double]] = clar.calculatedSamples(graph: graph, count: 512)
|
||||
let out2: [[Double]] = clar.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 clar: Clarinet.Audio = Clarinet.Audio(
|
||||
breathPressure: 0.6,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
_ = clar.calculatedSamples(graph: graph, count: 512)
|
||||
_ = clar.calculatedSamples(graph: graph, count: 512) // cache hit
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let block2: [[Double]] = clar.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 clar1: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: ConstantGeneratorInput(0.6),
|
||||
noiseGain: ConstantGeneratorInput(0.0),
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let clar2: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: ConstantGeneratorInput(0.6),
|
||||
noiseGain: ConstantGeneratorInput(0.0),
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let out1: [Double] = Array(clar1.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||||
let out2: [Double] = Array(clar2.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 ClarinetSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let clar: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: 0.6,
|
||||
sampleRate: 44100,
|
||||
maxDelayTime: 0.05
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: clar))
|
||||
}
|
||||
}
|
||||
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try intEngine.start()
|
||||
let synth: ClarinetSynth = ClarinetSynth()
|
||||
try intEngine.add(synth)
|
||||
for _ in 0..<4 {
|
||||
intEngine.processBlock(inputSamples: [])
|
||||
}
|
||||
intEngine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Finite after many blocks
|
||||
|
||||
@Test func finiteAfterManyBlocks() {
|
||||
let clar: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: 0.6,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let blockCount: Int = 200
|
||||
var allFinite: Bool = true
|
||||
for _ in 0..<blockCount {
|
||||
let out: [[Double]] = clar.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, "Clarinet must produce finite output for 200 blocks")
|
||||
}
|
||||
|
||||
// MARK: - Reed stiffness affects tone
|
||||
|
||||
@Test func reedStiffnessAffectsTone() {
|
||||
let clar1: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: 0.6,
|
||||
reedStiffness: -0.2,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let clar2: Clarinet.Audio = Clarinet.Audio(
|
||||
freq: 220.0,
|
||||
breathPressure: 0.6,
|
||||
reedStiffness: -0.8,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
|
||||
let warmupBlocks: Int = 20
|
||||
for b: Int in 0..<warmupBlocks {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = clar1.calculatedSamples(graph: g, count: 512)
|
||||
_ = clar2.calculatedSamples(graph: g, count: 512)
|
||||
advanceBlock(engine)
|
||||
_ = b
|
||||
}
|
||||
let gFinal: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [Double] = Array(clar1.calculatedSamples(graph: gFinal, count: 512)[0].prefix(512))
|
||||
let out2: [Double] = Array(clar2.calculatedSamples(graph: gFinal, count: 512)[0].prefix(512))
|
||||
let identical: Bool = out1 == out2
|
||||
#expect(!identical, "Different reedStiffness values must produce different output buffers")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user