184 lines
7.1 KiB
Swift
184 lines
7.1 KiB
Swift
// SignalChangedTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("SignalChanged.Audio")
|
|
struct SignalChangedAudioTests {
|
|
|
|
let engine: SynthesisEngine
|
|
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
|
let blockSize: Int = 64
|
|
|
|
init() throws {
|
|
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func rateIsAudio() {
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: 0.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
/// Constant input → no per-sample change → output all zeros.
|
|
@Test func constantInputProducesZeros() {
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: 0.5)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
// First sample: prev=0, current=0.5 → diff=0.5 > 0 → 1.0 (initial transition)
|
|
#expect(out[0] == 1.0)
|
|
// Subsequent samples: 0.5 == 0.5 → 0.0
|
|
for i: Int in 1..<blockSize {
|
|
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
|
}
|
|
}
|
|
|
|
/// Step input fires `1.0` only on the change sample.
|
|
@Test func stepInputFiresOnChangeSample() {
|
|
// 0.0 for samples 0..<32, then 1.0 for samples 32..<64.
|
|
let stepInput: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: stepInput)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
for i: Int in 0..<blockSize {
|
|
if i == 32 {
|
|
#expect(out[i] == 1.0, "expected change fire at sample 32")
|
|
} else {
|
|
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Threshold gates small changes.
|
|
@Test func thresholdSuppressesSubthresholdChanges() {
|
|
// Tiny step inside the threshold band.
|
|
let smallStep: Closure = Closure { _, i in i < 32 ? 0.0 : 0.05 }
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: smallStep, threshold: 0.1)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[i] == 0.0, "sample \(i) should not fire below threshold")
|
|
}
|
|
}
|
|
|
|
@Test func thresholdAllowsAboveThresholdChanges() {
|
|
let bigStep: Closure = Closure { _, i in i < 32 ? 0.0 : 0.5 }
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: bigStep, threshold: 0.1)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
#expect(out[32] == 1.0)
|
|
for i: Int in 0..<blockSize where i != 32 {
|
|
#expect(out[i] == 0.0)
|
|
}
|
|
}
|
|
|
|
/// Negative-going changes are detected via abs().
|
|
@Test func negativeChangeFires() {
|
|
let drop: Closure = Closure { _, i in i < 32 ? 1.0 : 0.0 }
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: drop)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
// First sample: prev=0, current=1.0 → fires.
|
|
#expect(out[0] == 1.0)
|
|
// Sample 32: 1.0 → 0.0, change=1.0 > 0 → fires.
|
|
#expect(out[32] == 1.0)
|
|
}
|
|
|
|
/// State persists across blocks: a sample equal to the previous block's
|
|
/// last sample produces no change at the start of the next block.
|
|
@Test func prevSampleCarriesAcrossBlocks() {
|
|
// Per-block-discriminated closure: block 1 = 1.0, block 2 = 1.0
|
|
// (continuous), so no change anywhere in block 2.
|
|
let firstSerial: UInt64 = engine.segmentSerial
|
|
let constInput: Closure = Closure { graph, i in
|
|
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 1.0
|
|
}
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: constInput)
|
|
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
// First sample of block 1: prev=0, current=1.0 → fires.
|
|
#expect(out1[0] == 1.0)
|
|
for i: Int in 1..<blockSize { #expect(out1[i] == 0.0) }
|
|
|
|
advanceBlock(engine)
|
|
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out2[i] == 0.0, "block 2 sample \(i) should be 0.0 (no change)")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Suite("SignalChanged.Control")
|
|
struct SignalChangedControlTests {
|
|
|
|
let engine: SynthesisEngine
|
|
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
|
let blockSize: Int = 64
|
|
|
|
init() throws {
|
|
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func rateIsControl() {
|
|
let gen: SignalChanged.Control = SignalChanged.Control(input: 0.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
@Test func constantInputProducesZeroAfterFirstBlock() {
|
|
let gen: SignalChanged.Control = SignalChanged.Control(input: 0.5)
|
|
// First block: 0 → 0.5 transition → fires.
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.previousOutputValue == 1.0)
|
|
|
|
// Second block: 0.5 → 0.5 → no change.
|
|
advanceBlock(engine)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[i] == 0.0)
|
|
}
|
|
#expect(gen.previousOutputValue == 0.0)
|
|
}
|
|
|
|
@Test func stepInputFiresOnceForBlock() {
|
|
let step: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
|
let gen: SignalChanged.Control = SignalChanged.Control(input: step)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
// Block-constant 1.0: any change in the block fires for the whole block.
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[i] == 1.0)
|
|
}
|
|
}
|
|
|
|
@Test func floatValueReturnsPreviousOutputValue() {
|
|
let gen: SignalChanged.Control = SignalChanged.Control(input: 0.5)
|
|
#expect(gen.floatValue(graph: graph) == 0.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
|
}
|
|
}
|
|
|
|
@Suite("SignalChanged generator-driven threshold")
|
|
struct SignalChangedGeneratorThresholdTests {
|
|
|
|
let engine: SynthesisEngine
|
|
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
|
let blockSize: Int = 64
|
|
|
|
init() throws {
|
|
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
|
try engine.start()
|
|
}
|
|
|
|
/// A live ParameterNode threshold is read per-sample.
|
|
@Test func parameterNodeThresholdIsHonoured() {
|
|
let thresh: ParameterNode = ParameterNode(0.5)
|
|
let step: Closure = Closure { _, i in i < 32 ? 0.0 : 0.3 }
|
|
let gen: SignalChanged.Audio = SignalChanged.Audio(input: step, threshold: thresh)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
// With threshold 0.5, a 0.3 step does NOT exceed.
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[i] == 0.0, "sample \(i) should be 0.0 with threshold 0.5")
|
|
}
|
|
}
|
|
}
|