237 lines
8.5 KiB
Swift
237 lines
8.5 KiB
Swift
// GatedKarplusStrongTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@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
|
||
}
|
||
}
|