prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
// GatedKarplusStrongTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("GatedKarplusStrong")
|
||||
struct GatedKarplusStrongTests {
|
||||
|
||||
// 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 gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
gate: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
#expect(gks.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
gate: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = gks.calculatedSamples(graph: graph, count: 512)
|
||||
#expect(out.count >= 1)
|
||||
#expect(out[0].count >= 512)
|
||||
}
|
||||
|
||||
// MARK: - Fan-out
|
||||
|
||||
@Test func fanOutSingleEvaluation() {
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
gate: 0.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out1: [[Double]] = gks.calculatedSamples(graph: graph, count: 512)
|
||||
let out2: [[Double]] = gks.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: - Gate off — delay line stays zero
|
||||
|
||||
@Test func gateOffOutputNearZero() {
|
||||
// With gate = 0 and delay line zero-initialised, the feedback loop recirculates zeros.
|
||||
// The one-pole filter produces (0 + 0) * 0.5 * feedback = 0 → output stays zero.
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
gate: 0.0, // gate always off
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = gks.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
let allZero: Bool = samples.allSatisfy { abs($0) < 1e-12 }
|
||||
#expect(allZero, "With gate off and zero delay line, output must be zero")
|
||||
}
|
||||
|
||||
// MARK: - Gate on injects exciter into delay line
|
||||
|
||||
@Test func gateOnInjectsExciter() {
|
||||
// Gate = 1.0 → exciter constantly written into delay line.
|
||||
// After one block the delay line is full of exciter energy; output must be non-zero.
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: ConstantGeneratorInput(0.5),
|
||||
gate: 1.0, // gate always on
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let out: [[Double]] = gks.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
let rms: Double = rmsValue(samples)
|
||||
#expect(rms > 0, "With gate on and non-zero exciter, output RMS must be non-zero")
|
||||
}
|
||||
|
||||
// MARK: - Gate on then off — resonance continues
|
||||
|
||||
@Test func gateOnThenOffResonancesContinue() {
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
gate: GatedBlockToggle(), // on for first block, off thereafter
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
|
||||
// Block 0: gate on → delay line fills with noise.
|
||||
_ = gks.calculatedSamples(graph: graph, count: 512)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 1: gate off → should resonate (delay line now has energy).
|
||||
let resonanceOut: [[Double]] = gks.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||||
let rms: Double = rmsValue(Array(resonanceOut[0].prefix(512)))
|
||||
#expect(rms > 0, "After gate-off, resonance should continue from filled delay line (rms=\(rms))")
|
||||
}
|
||||
|
||||
// MARK: - Continuous gate sustain
|
||||
|
||||
@Test func continuousGateSustain() {
|
||||
// With gate always on and constant exciter, output should stay non-zero across blocks.
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: ConstantGeneratorInput(0.3),
|
||||
gate: 1.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
|
||||
var allBlocksNonZero: Bool = true
|
||||
for _ in 0..<10 {
|
||||
let out: [[Double]] = gks.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||||
let rms: Double = rmsValue(Array(out[0].prefix(512)))
|
||||
if rms <= 0 { allBlocksNonZero = false }
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(allBlocksNonZero, "Continuous gate must sustain non-zero output across all blocks")
|
||||
}
|
||||
|
||||
// MARK: - mulAdd
|
||||
|
||||
@Test func mulAddApplied() {
|
||||
let gksBase: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: ConstantGeneratorInput(0.5),
|
||||
gate: 1.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime
|
||||
)
|
||||
let gksMul: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: ConstantGeneratorInput(0.5),
|
||||
gate: 1.0,
|
||||
freq: 440.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: testSampleRate,
|
||||
maxDelayTime: testMaxDelayTime,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
|
||||
let outBase: [Double] = Array(gksBase.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||||
let outMul: [Double] = Array(gksMul.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||||
|
||||
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 GKSSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let gks: GatedKarplusStrong.Audio = GatedKarplusStrong.Audio(
|
||||
exciter: WhiteNoise.Audio(),
|
||||
gate: LFSquare.Audio(freq: 4.0),
|
||||
freq: 220.0,
|
||||
feedback: 0.99,
|
||||
sampleRate: 44100,
|
||||
maxDelayTime: 0.05
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: gks))
|
||||
}
|
||||
}
|
||||
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try intEngine.start()
|
||||
let synth: GKSSynth = GKSSynth()
|
||||
try intEngine.add(synth)
|
||||
for _ in 0..<4 {
|
||||
intEngine.processBlock(inputSamples: [])
|
||||
}
|
||||
intEngine.stop()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Local test helpers
|
||||
|
||||
/// Gate source that outputs 1.0 on the first `calculatedSamples` call, then 0.0 forever.
|
||||
/// Simulates a gate that is open for exactly one audio block.
|
||||
private final class GatedBlockToggle: GeneratorBase, AudioGenerator {
|
||||
private var _firstBlock: Bool = true
|
||||
|
||||
public func floatValue(graph: SynthGraph) -> Double { _firstBlock ? 1.0 : 0.0 }
|
||||
|
||||
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||||
precondition(count <= _result[0].count,
|
||||
"GatedBlockToggle: count \(count) exceeds buffer size \(_result[0].count)")
|
||||
let value: Double = _firstBlock ? 1.0 : 0.0
|
||||
for i: Int in 0..<count { _result[0][i] = value }
|
||||
_firstBlock = false
|
||||
return _result
|
||||
}
|
||||
}
|
||||
@@ -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