prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// DelayLineCubicTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - DelayLineCubicTests
|
||||
|
||||
@MainActor
|
||||
@Suite("DelayLine cubic")
|
||||
struct DelayLineCubicTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
let sr: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// A unit impulse through a fractional pure delay reproduces the interpolation
|
||||
/// kernel's impulse response directly (no feedback to entangle the math).
|
||||
/// Delay = 3.5 samples → integer part 3, frac 0.5. The impulse is written at
|
||||
/// buffer index 0 (frame 0); reads happen before writes, so it surfaces across
|
||||
/// the four cubic taps at frames 2…5.
|
||||
|
||||
@Test func cubicImpulseResponseMatchesCatmullRomAtHalfSample() {
|
||||
// catmullRomCubic basis at t = 0.5: pM1 → -0.0625, p0 → 0.5625,
|
||||
// p1 → 0.5625, p2 → -0.0625. As the lone impulse passes through taps
|
||||
// (i0,i1,i2,i3) = (writePos-intD+1 … -2), output[2..5] trace that basis.
|
||||
let dl: DelayLine = DelayLine(input: Impulse.Audio(freq: 1.0),
|
||||
maxDelay: 16.0 / sr, delay: 3.5 / sr,
|
||||
sampleRate: sr, interpolation: .cubic)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s: [Double] = dl.calculatedSamples(graph: g, count: engine.blockSize)[0]
|
||||
|
||||
#expect(abs(s[0]) < 1e-12)
|
||||
#expect(abs(s[1]) < 1e-12)
|
||||
#expect(abs(s[2] - (-0.0625)) < 1e-12)
|
||||
#expect(abs(s[3] - 0.5625) < 1e-12)
|
||||
#expect(abs(s[4] - 0.5625) < 1e-12)
|
||||
#expect(abs(s[5] - (-0.0625)) < 1e-12)
|
||||
for i: Int in 6..<engine.blockSize {
|
||||
#expect(abs(s[i]) < 1e-12, "sample \(i) should be silent")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func linearImpulseResponseAtHalfSample() {
|
||||
// Linear interp of the same impulse: 0.5 at the two integer taps bracketing
|
||||
// the 3.5-sample read (frames 3 and 4).
|
||||
let dl: DelayLine = DelayLine(input: Impulse.Audio(freq: 1.0),
|
||||
maxDelay: 16.0 / sr, delay: 3.5 / sr,
|
||||
sampleRate: sr, interpolation: .linear)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s: [Double] = dl.calculatedSamples(graph: g, count: engine.blockSize)[0]
|
||||
#expect(abs(s[3] - 0.5) < 1e-12)
|
||||
#expect(abs(s[4] - 0.5) < 1e-12)
|
||||
#expect(abs(s[2]) < 1e-12)
|
||||
#expect(abs(s[5]) < 1e-12)
|
||||
}
|
||||
|
||||
@Test func cubicReproducesLinearRampExactly() throws {
|
||||
// Catmull-Rom is exact on collinear data, so a unit-per-sample ramp delayed
|
||||
// by a fractional amount yields the same output under .cubic and .linear.
|
||||
let rampCubic: Phasor.Audio = Phasor.Audio(rate: 1.0, start: 0.0, end: 1.0e9)
|
||||
let dlCubic: DelayLine = DelayLine(input: rampCubic, maxDelay: 16.0 / sr,
|
||||
delay: 4.25 / sr, sampleRate: sr, interpolation: .cubic)
|
||||
let sCubic: [Double] = collectBlocks(dlCubic, engine: engine, blocks: 4)
|
||||
|
||||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
|
||||
try engine2.start()
|
||||
let rampLinear: Phasor.Audio = Phasor.Audio(rate: 1.0, start: 0.0, end: 1.0e9)
|
||||
let dlLinear: DelayLine = DelayLine(input: rampLinear, maxDelay: 16.0 / sr,
|
||||
delay: 4.25 / sr, sampleRate: sr, interpolation: .linear)
|
||||
let sLinear: [Double] = collectBlocks(dlLinear, engine: engine2, blocks: 4)
|
||||
|
||||
// Skip the warm-up edge (first ~6 samples) where the ramp history is incomplete.
|
||||
for i: Int in 8..<sCubic.count {
|
||||
#expect(abs(sCubic[i] - sLinear[i]) < 1e-9, "cubic vs linear differ at \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func cubicFiniteUnderNoise() {
|
||||
let dl: DelayLine = DelayLine(input: WhiteNoise.Audio(), maxDelay: 0.05,
|
||||
delay: 0.013, sampleRate: sr, interpolation: .cubic)
|
||||
let s: [Double] = collectBlocks(dl, engine: engine, blocks: 20)
|
||||
#expect(s.allSatisfy { $0.isFinite })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// DelayLineFeedbackCubicTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - DelayLineFeedbackCubicTests
|
||||
|
||||
@MainActor
|
||||
@Suite("DelayLineFeedback cubic")
|
||||
struct DelayLineFeedbackCubicTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
let sr: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// The wet output of the feedback delay is the cubic-read delayed signal.
|
||||
/// For a unit impulse and a 3.5-sample delay, the first two non-zero outputs
|
||||
/// (frames 2 and 3) read only the original impulse — feedback has not yet
|
||||
/// recirculated into those taps — so they equal the Catmull-Rom basis at
|
||||
/// t = 0.5 (-0.0625, then 0.5625) regardless of the feedback coefficient.
|
||||
@Test func cubicFirstEchoMatchesCatmullRomBeforeRecirculation() {
|
||||
let dlf: DelayLineFeedback = DelayLineFeedback(input: Impulse.Audio(freq: 1.0),
|
||||
maxDelay: 16.0 / sr, delay: 3.5 / sr,
|
||||
decayTime: 1.0, sampleRate: sr,
|
||||
interpolation: .cubic)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s: [Double] = dlf.calculatedSamples(graph: g, count: engine.blockSize)[0]
|
||||
|
||||
#expect(abs(s[0]) < 1e-12)
|
||||
#expect(abs(s[1]) < 1e-12)
|
||||
#expect(abs(s[2] - (-0.0625)) < 1e-12)
|
||||
#expect(abs(s[3] - 0.5625) < 1e-12)
|
||||
#expect(s.allSatisfy { $0.isFinite })
|
||||
}
|
||||
|
||||
@Test func cubicFiniteUnderNoise() {
|
||||
let dlf: DelayLineFeedback = DelayLineFeedback(input: WhiteNoise.Audio(),
|
||||
maxDelay: 0.05, delay: 0.013,
|
||||
decayTime: 0.3, sampleRate: sr,
|
||||
interpolation: .cubic)
|
||||
let s: [Double] = collectBlocks(dlf, engine: engine, blocks: 20)
|
||||
#expect(s.allSatisfy { $0.isFinite })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// DelayLineFeedbackParameterRampingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DelayLineFeedback parameter ramping")
|
||||
struct DelayLineFeedbackParameterRampingTests {
|
||||
|
||||
@Test func decayTimeGeneratorInputDoesNotCrash() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let decayNode: ParameterNode = ParameterNode(2.0)
|
||||
let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.3)
|
||||
let delay: DelayLineFeedback = DelayLineFeedback(
|
||||
input: signal,
|
||||
maxDelay: 0.5,
|
||||
delay: 0.1,
|
||||
decayTime: decayNode,
|
||||
targetAmp: 0.001,
|
||||
sampleRate: 44100
|
||||
)
|
||||
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = delay.calculatedSamples(graph: g, count: 64)
|
||||
let samples: [Double] = Array((out.first ?? []).prefix(64))
|
||||
#expect(samples.allSatisfy { $0.isFinite },
|
||||
"DelayLineFeedback output must be finite with ParameterNode decayTime")
|
||||
}
|
||||
|
||||
@Test func targetAmpGeneratorInputDoesNotCrash() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let ampNode: ParameterNode = ParameterNode(0.001)
|
||||
let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.3)
|
||||
let delay: DelayLineFeedback = DelayLineFeedback(
|
||||
input: signal,
|
||||
maxDelay: 0.5,
|
||||
delay: 0.1,
|
||||
decayTime: 2.0,
|
||||
targetAmp: ampNode,
|
||||
sampleRate: 44100
|
||||
)
|
||||
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = delay.calculatedSamples(graph: g, count: 64)
|
||||
let samples: [Double] = Array((out.first ?? []).prefix(64))
|
||||
#expect(samples.allSatisfy { $0.isFinite },
|
||||
"DelayLineFeedback output must be finite with ParameterNode targetAmp")
|
||||
}
|
||||
|
||||
@Test func decayTimeRampChangesOutputEnergy() throws {
|
||||
// Block 1: decayTime=5.0 (long, lots of feedback). Block 2: ramp to 0.01 (short, almost no feedback).
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let decayNode: ParameterNode = ParameterNode(5.0)
|
||||
let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.3)
|
||||
let delay: DelayLineFeedback = DelayLineFeedback(
|
||||
input: signal,
|
||||
maxDelay: 0.5,
|
||||
delay: 0.1,
|
||||
decayTime: decayNode,
|
||||
targetAmp: 0.001,
|
||||
sampleRate: 44100
|
||||
)
|
||||
|
||||
// Warm up several blocks to build feedback.
|
||||
for _ in 0..<10 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = delay.calculatedSamples(graph: g, count: 64)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
// Block: long decay — measure output energy.
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [[Double]] = delay.calculatedSamples(graph: g1, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Now ramp to very short decay.
|
||||
decayNode.value = 0.001
|
||||
// Run several more blocks so feedback decays.
|
||||
for _ in 0..<5 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = delay.calculatedSamples(graph: g, count: 64)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = delay.calculatedSamples(graph: g2, count: 64)
|
||||
|
||||
let energy1: Double = rmsValue(Array((out1.first ?? []).prefix(64)))
|
||||
let energy2: Double = rmsValue(Array((out2.first ?? []).prefix(64)))
|
||||
|
||||
// After short decay, output should have less energy than after long decay.
|
||||
#expect(energy2 < energy1 + 0.01,
|
||||
"DelayLineFeedback: after decayTime ramp to near-zero, output energy should drop: \(energy2) vs \(energy1)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user