prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
// KarplusStrongTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("KarplusStrong")
|
||||
struct KarplusStrongTests {
|
||||
|
||||
// MARK: - Test fixture
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
// Shared structural params for tests.
|
||||
let testSampleRate: Double = 44100
|
||||
let testMaxDelayTime: Double = 0.05
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
#expect(ks.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Silent before trigger
|
||||
|
||||
@Test func silentBeforeTrigger() {
|
||||
// With trigger held at 0 and delay line zero-initialised, output must be zero.
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = ks.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
let allZero: Bool = samples.allSatisfy { abs($0) < 1e-12 }
|
||||
#expect(allZero, "Output must be zero before any trigger fires")
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = ks.calculatedSamples(graph: graph, count: 512)
|
||||
#expect(out.count >= 1)
|
||||
#expect(out[0].count >= 512)
|
||||
}
|
||||
|
||||
// MARK: - Fan-out
|
||||
|
||||
@Test func fanOutSingleEvaluation() {
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out1: [[Double]] = ks.calculatedSamples(graph: graph, count: 512)
|
||||
let out2: [[Double]] = ks.calculatedSamples(graph: graph, count: 512)
|
||||
// Same segment serial — second call must return cached result.
|
||||
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 once per block
|
||||
|
||||
@Test func stateAdvancesOncePerBlock() {
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
_ = ks.calculatedSamples(graph: graph, count: 512)
|
||||
_ = ks.calculatedSamples(graph: graph, count: 512) // cache hit
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let block2: [[Double]] = ks.calculatedSamples(graph: graph2, count: 512)
|
||||
// Both blocks are zero (no trigger); just confirm no crash and output is finite.
|
||||
let allFinite: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite }
|
||||
#expect(allFinite, "All output samples must be finite")
|
||||
}
|
||||
|
||||
// MARK: - Non-silent after trigger
|
||||
|
||||
@Test func nonSilentAfterTrigger() throws {
|
||||
// Use an ImpulseSource-style trigger that fires at sample 0.
|
||||
let trigger: OneShotTrigger = OneShotTrigger()
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: trigger,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
|
||||
// Block 0: trigger fires → delay line starts filling with exciter.
|
||||
let out: [[Double]] = ks.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
let rms: Double = rmsValue(samples)
|
||||
// After trigger, the delay line is being filled with noise; output should be non-zero.
|
||||
#expect(rms > 0, "RMS must be non-zero after trigger fires")
|
||||
}
|
||||
|
||||
// MARK: - Decay over time
|
||||
|
||||
@Test func decayOverTime() throws {
|
||||
let trigger: OneShotTrigger = OneShotTrigger()
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: trigger,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
|
||||
// Block 0: fire trigger, capture early RMS.
|
||||
let earlyOut: [[Double]] = ks.calculatedSamples(graph: graph, count: 512)
|
||||
let earlyRms: Double = rmsValue(Array(earlyOut[0].prefix(512)))
|
||||
advanceBlock(engine)
|
||||
|
||||
// Advance many blocks to allow decay.
|
||||
let decayBlocks: Int = 100
|
||||
var lateOut: [[Double]] = [[]]
|
||||
for _ in 0..<decayBlocks {
|
||||
lateOut = ks.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let lateRms: Double = rmsValue(Array(lateOut[0].prefix(512)))
|
||||
|
||||
#expect(earlyRms > 0, "Early RMS must be non-zero")
|
||||
#expect(lateRms < earlyRms, "KarplusStrong output must decay over time (lateRms \(lateRms) < earlyRms \(earlyRms))")
|
||||
}
|
||||
|
||||
// MARK: - mulAdd
|
||||
|
||||
@Test func mulAddApplied() {
|
||||
// Trigger at block 0 so output is non-zero, then compare with/without mulAdd.
|
||||
let trigger1: OneShotTrigger = OneShotTrigger()
|
||||
let trigger2: OneShotTrigger = OneShotTrigger()
|
||||
|
||||
let ksBase: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: ConstantGeneratorInput(0.5),
|
||||
trigger: trigger1,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let ksMul: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: ConstantGeneratorInput(0.5),
|
||||
trigger: trigger2,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
|
||||
let outBase: [Double] = Array(ksBase.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||||
let outMul: [Double] = Array(ksMul.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||||
|
||||
// The mul/add relationship only holds for samples where both computed the same exciter.
|
||||
// Since exciter is constant (0.5), the delay line fill is identical and the relationship
|
||||
// must hold. Verify a few early samples.
|
||||
var relationshipHolds: Bool = true
|
||||
for i: Int in 0..<512 {
|
||||
let expected: Double = outBase[i] * 2.0 + 1.0
|
||||
if abs(outMul[i] - expected) > 1e-10 {
|
||||
relationshipHolds = false
|
||||
break
|
||||
}
|
||||
}
|
||||
#expect(relationshipHolds, "mul:2.0 add:1.0 must produce output × 2 + 1")
|
||||
}
|
||||
|
||||
// MARK: - Integration
|
||||
|
||||
@Test func integrationWithEngine() throws {
|
||||
final class KSSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let ks: KarplusStrong.Audio = KarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
trigger: Impulse.Audio(freq: 4.0),
|
||||
freq: 220.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: 44100,
|
||||
maxDelayTime: 0.05
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: ks))
|
||||
}
|
||||
}
|
||||
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try intEngine.start()
|
||||
let synth: KSSynth = KSSynth()
|
||||
try intEngine.add(synth)
|
||||
// Render a few blocks without crashing.
|
||||
for _ in 0..<4 {
|
||||
intEngine.processBlock(inputSamples: [])
|
||||
}
|
||||
intEngine.stop()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Local test helpers
|
||||
|
||||
/// Emits 1.0 at sample 0 of the first `calculatedSamples` call, then 0.0 forever after.
|
||||
/// This simulates a one-shot rising edge at the very first sample.
|
||||
private final class OneShotTrigger: GeneratorBase, AudioGenerator {
|
||||
private var _fired: Bool = false
|
||||
|
||||
public func floatValue(graph: SynthGraph) -> Double { 0.0 }
|
||||
|
||||
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||||
precondition(count <= _result[0].count,
|
||||
"OneShotTrigger: count \(count) exceeds buffer size \(_result[0].count)")
|
||||
_result[0][0] = _fired ? 0.0 : 1.0
|
||||
for i: Int in 1..<count { _result[0][i] = 0.0 }
|
||||
_fired = true
|
||||
return _result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user