105 lines
4.1 KiB
Swift
105 lines
4.1 KiB
Swift
// PulseCountControlTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("PulseCount.Control")
|
|
struct PulseCountControlTests {
|
|
|
|
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: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
/// Without any trigger, previousOutputValue = 0.
|
|
@Test func noTriggerCountStaysZero() {
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.previousOutputValue == 0.0)
|
|
}
|
|
|
|
/// Rising edges increment the count; block-final count is previousOutputValue.
|
|
@Test func edgesIncrementCount() {
|
|
let trig: Closure = Closure { _, i in
|
|
(i == 10 || i == 20 || i == 30) ? 1.0 : 0.0
|
|
}
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: trig)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(gen.previousOutputValue - 3.0) < 1e-12,
|
|
"expected count 3.0 got \(gen.previousOutputValue)")
|
|
}
|
|
|
|
/// Reset clears count to 0.
|
|
@Test func resetClearsCount() {
|
|
let trig: Closure = Closure { _, i in (i == 5 || i == 15 || i == 25) ? 1.0 : 0.0 }
|
|
let reset: Closure = Closure { _, i in i == 40 ? 1.0 : 0.0 }
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: trig, reset: reset)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// Reset fires after trig edges; block-final count = 0.
|
|
#expect(gen.previousOutputValue == 0.0,
|
|
"after reset expected 0.0 got \(gen.previousOutputValue)")
|
|
}
|
|
|
|
/// Output block is flat (discrete).
|
|
@Test func outputBlockIsFlat() {
|
|
let trig: Closure = Closure { _, i in i == 10 ? 1.0 : 0.0 }
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: trig)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
let block: [Double] = Array(out[0].prefix(blockSize))
|
|
let finalValue: Double = gen.previousOutputValue
|
|
for i: Int in 0..<blockSize {
|
|
#expect(block[i] == finalValue,
|
|
"sample \(i) expected \(finalValue) got \(block[i])")
|
|
}
|
|
}
|
|
|
|
/// floatValue returns previousOutputValue.
|
|
@Test func floatValueReturnsPreviousOutputValue() {
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
|
#expect(gen.floatValue(graph: graph) == 0.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
|
}
|
|
|
|
/// Count persists across blocks.
|
|
@Test func countPersistsAcrossBlocks() {
|
|
let firstSerial: UInt64 = engine.segmentSerial
|
|
let trig: Closure = Closure { graph, i in
|
|
graph.engine.segmentSerial == firstSerial && (i == 5 || i == 15) ? 1.0 : 0.0
|
|
}
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: trig)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(gen.previousOutputValue - 2.0) < 1e-12)
|
|
|
|
advanceBlock(engine)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(gen.previousOutputValue - 2.0) < 1e-12,
|
|
"block-2 count expected 2.0 got \(gen.previousOutputValue)")
|
|
}
|
|
|
|
/// Fan-out: two calls in same segment return identical channel.
|
|
@Test func fanOutReturnsSameChannel() {
|
|
let gen: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
|
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
|
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out1[i] == out2[i])
|
|
}
|
|
}
|
|
}
|