1.0.0 release
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
// FlangerTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("Flanger")
|
||||
struct FlangerTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
let testSampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Deterministic first sample
|
||||
|
||||
@Test func firstSampleDeterministic() {
|
||||
// input=0.5, depth=0, rate=0, feedback=0, mix=0.5 → first sample always 0.25.
|
||||
// Delay line starts all-zero; wet signal = 0; output = dry*(1-0.5) + 0*0.5 = 0.25.
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(0.5),
|
||||
delayTime: 0.005,
|
||||
depth: 0.0,
|
||||
rate: 0.0,
|
||||
feedback: 0.0,
|
||||
mix: 0.5,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let out: [[Double]] = flanger.calculatedSamples(graph: graph, count: 1)
|
||||
let expected: Double = 0.25
|
||||
#expect(abs(out[0][0] - expected) < 1e-10, "First sample must be \(expected), got \(out[0][0])")
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: 0.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
#expect(flanger.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(0.5),
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let out: [[Double]] = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
#expect(out.count >= 1)
|
||||
#expect(out[0].count >= 512)
|
||||
}
|
||||
|
||||
// MARK: - Silent input → output near zero (with mix 0.5, dry is 0 so output is 0)
|
||||
|
||||
@Test func silentInputProducesNearZeroOutput() {
|
||||
// With input = 0 and no feedback, delay line stays zero.
|
||||
// Output = 0 * (1 - mix) + 0 * mix = 0.
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: 0.0,
|
||||
mix: 0.5,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let out: [[Double]] = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
let allNearZero: Bool = samples.allSatisfy { abs($0) < 1e-12 }
|
||||
#expect(allNearZero, "Zero input with no feedback must produce near-zero output")
|
||||
}
|
||||
|
||||
// MARK: - Non-silent input → output differs from dry (modulation is happening)
|
||||
|
||||
@Test func nonSilentInputDiffersFromDry() {
|
||||
// After some blocks the modulated delay creates a different signal from dry.
|
||||
let amplitude: Double = 0.5
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(amplitude),
|
||||
delayTime: 0.005,
|
||||
depth: 0.5,
|
||||
rate: 2.0,
|
||||
feedback: 0.0,
|
||||
mix: 0.5,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
|
||||
// Run several blocks to let delay line fill.
|
||||
for _ in 0..<5 {
|
||||
_ = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let out: [[Double]] = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
// With constant input the delayed signal is also constant, so mix just attenuates.
|
||||
// At mix=0.5: output = 0.5 * amplitude + 0.5 * amplitude = amplitude.
|
||||
// Verify output is non-zero and finite.
|
||||
let rms: Double = rmsValue(samples)
|
||||
#expect(rms > 0, "Non-silent input must produce non-zero output")
|
||||
let allFinite: Bool = samples.allSatisfy { $0.isFinite }
|
||||
#expect(allFinite, "All output samples must be finite")
|
||||
}
|
||||
|
||||
// MARK: - Fan-out (same segment returns identical result)
|
||||
|
||||
@Test func fanOutSingleEvaluation() {
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(0.3),
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let out1: [[Double]] = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
let out2: [[Double]] = flanger.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 once per block
|
||||
|
||||
@Test func stateAdvancesOncePerBlock() {
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(0.3),
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
_ = flanger.calculatedSamples(graph: graph, count: 512) // cache hit
|
||||
advanceBlock(engine)
|
||||
let block2: [[Double]] = flanger.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||||
let allFinite: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite }
|
||||
#expect(allFinite, "All output samples after block advance must be finite")
|
||||
}
|
||||
|
||||
// MARK: - mulAdd applied
|
||||
|
||||
@Test func mulAddApplied() {
|
||||
// With zero input: output = 0 * (1 - mix) + 0 * mix = 0.
|
||||
// After mulAdd(mul: 2.0, add: 1.0): output = 0 * 2 + 1 = 1.
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: 0.0,
|
||||
mix: 0.5,
|
||||
sampleRate: testSampleRate,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let out: [[Double]] = flanger.calculatedSamples(graph: graph, count: 512)
|
||||
let samples: [Double] = Array(out[0].prefix(512))
|
||||
let allNearOne: Bool = samples.allSatisfy { abs($0 - 1.0) < 1e-10 }
|
||||
#expect(allNearOne, "mulAdd(mul:2, add:1) on zero input must produce output of 1.0")
|
||||
}
|
||||
|
||||
// MARK: - Feedback affects output amplitude
|
||||
|
||||
@Test func feedbackOutputIsFinite() {
|
||||
// With feedback the delayed signal feeds back into the delay line.
|
||||
// Verify both variants (feedback=0 and feedback=0.8) produce finite output.
|
||||
let flangerNoFb: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(0.2),
|
||||
delayTime: 0.005,
|
||||
depth: 0.3,
|
||||
rate: 0.5,
|
||||
feedback: 0.0,
|
||||
mix: 0.5,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let flangerFb: Flanger.Audio = Flanger.Audio(
|
||||
input: ConstantGeneratorInput(0.2),
|
||||
delayTime: 0.005,
|
||||
depth: 0.3,
|
||||
rate: 0.5,
|
||||
feedback: 0.8,
|
||||
mix: 0.5,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
|
||||
for _ in 0..<10 {
|
||||
_ = flangerNoFb.calculatedSamples(graph: graph, count: 512)
|
||||
_ = flangerFb.calculatedSamples(graph: graph, count: 512)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let outNoFb: [Double] = Array(flangerNoFb.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)[0].prefix(512))
|
||||
let outFb: [Double] = Array(flangerFb.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)[0].prefix(512))
|
||||
let rmsNoFb: Double = rmsValue(outNoFb)
|
||||
let rmsFb: Double = rmsValue(outFb)
|
||||
// Feedback with constant input is stable (clamped to 0.9). Both must be finite.
|
||||
#expect(rmsNoFb.isFinite, "No-feedback output must be finite")
|
||||
#expect(rmsFb.isFinite, "Feedback output must be finite")
|
||||
}
|
||||
|
||||
// MARK: - Integration with engine
|
||||
|
||||
@Test func integrationWithEngine() throws {
|
||||
final class FlangerSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let flanger: Flanger.Audio = Flanger.Audio(
|
||||
input: OscSin.Audio(freq: 220.0),
|
||||
delayTime: 0.005,
|
||||
depth: 0.5,
|
||||
rate: 0.5,
|
||||
feedback: 0.2,
|
||||
mix: 0.5,
|
||||
sampleRate: 44100
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: flanger))
|
||||
}
|
||||
}
|
||||
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try intEngine.start()
|
||||
let synth: FlangerSynth = FlangerSynth()
|
||||
try intEngine.add(synth)
|
||||
for _ in 0..<4 {
|
||||
intEngine.processBlock(inputSamples: [])
|
||||
}
|
||||
intEngine.stop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user