129 lines
4.1 KiB
Swift
129 lines
4.1 KiB
Swift
// LeakDCTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("LeakDC")
|
|
struct LeakDCTests {
|
|
|
|
let engine: SynthesisEngine
|
|
let blockSize: Int = 64
|
|
let sampleRate: Double = 44100
|
|
|
|
init() throws {
|
|
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
|
try engine.start()
|
|
}
|
|
|
|
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
|
|
|
// MARK: 1. Rate
|
|
|
|
@Test func audioRate() {
|
|
let gen: LeakDC.Audio = LeakDC.Audio(input: 1.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
@Test func controlRate() {
|
|
let gen: LeakDC.Control = LeakDC.Control(input: 1.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
// MARK: 2. Default coefficients
|
|
|
|
@Test func audioDefaultCoef() {
|
|
// Verify the public constant has the expected value.
|
|
#expect(abs(leakDCDefaultAudioCoef - 0.995) < 1e-12)
|
|
}
|
|
|
|
@Test func controlDefaultCoef() {
|
|
#expect(abs(leakDCDefaultControlCoef - 0.9) < 1e-12)
|
|
}
|
|
|
|
// MARK: 3. Output length
|
|
|
|
@Test func audioOutputLength() {
|
|
let gen: LeakDC.Audio = LeakDC.Audio(input: 1.0)
|
|
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(result.count == 1)
|
|
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
|
|
}
|
|
|
|
// MARK: 4. DC removal — constant input approaches zero output
|
|
|
|
@Test func audioDCRemoval() {
|
|
// Feed constant 1.0 for many blocks; output should shrink toward 0.
|
|
let gen: LeakDC.Audio = LeakDC.Audio(input: 1.0)
|
|
var last: Double = 1.0
|
|
for _ in 0..<500 {
|
|
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
|
last = result[0][blockSize - 1]
|
|
advanceBlock(engine)
|
|
}
|
|
#expect(abs(last) < 0.01)
|
|
}
|
|
|
|
@Test func controlDCRemoval() {
|
|
let gen: LeakDC.Control = LeakDC.Control(input: 1.0)
|
|
var last: Double = 1.0
|
|
for _ in 0..<100 {
|
|
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
|
last = result[0][blockSize - 1]
|
|
advanceBlock(engine)
|
|
}
|
|
#expect(abs(last) < 0.01)
|
|
}
|
|
|
|
// MARK: 5. AC signals pass through (approximately)
|
|
|
|
@Test func acSignalPassesThrough() {
|
|
// A signal that alternates sign each sample (Nyquist) should not be attenuated much.
|
|
// We approximate by checking the first output sample is nonzero.
|
|
let gen: LeakDC.Audio = LeakDC.Audio(input: 1.0)
|
|
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// First sample: x[0]=1, x[-1]=0, y[-1]=0 → y[0] = 1 - 0 + 0.995*0 = 1
|
|
#expect(abs(result[0][0] - 1.0) < 1e-10)
|
|
}
|
|
|
|
// MARK: 6. First sample equals input (x[-1]=0, y[-1]=0)
|
|
|
|
@Test func firstSampleEqualsInput() {
|
|
let gen: LeakDC.Audio = LeakDC.Audio(input: 0.5)
|
|
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// y[0] = 0.5 - 0 + 0.995*0 = 0.5
|
|
#expect(abs(result[0][0] - 0.5) < 1e-10)
|
|
}
|
|
|
|
// MARK: 7. Control previousOutputValue
|
|
|
|
@Test func previousOutputValueUpdates() {
|
|
let gen: LeakDC.Control = LeakDC.Control(input: 1.0)
|
|
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.previousOutputValue == result[0][blockSize - 1])
|
|
}
|
|
|
|
// MARK: 8. Fan-out caching
|
|
|
|
@Test func audioFanOut() {
|
|
let gen: LeakDC.Audio = LeakDC.Audio(input: 1.0)
|
|
let g: SynthGraph = graph
|
|
let r1: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
|
let r2: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
|
#expect(r1[0][0] == r2[0][0])
|
|
}
|
|
|
|
// MARK: 9. GeneratorEnum
|
|
|
|
@Test func generatorEnumAudioType() {
|
|
let _: LeakDC.AudioType = LeakDC.Audio(input: 1.0)
|
|
}
|
|
|
|
@Test func generatorEnumControlType() {
|
|
let _: LeakDC.ControlType = LeakDC.Control(input: 1.0)
|
|
}
|
|
}
|