136 lines
5.4 KiB
Swift
136 lines
5.4 KiB
Swift
// Trig1AudioTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Trig1.Audio")
|
||
struct Trig1AudioTests {
|
||
|
||
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: Trig1.Audio = Trig1.Audio(input: 0.0, dur: 0.1)
|
||
#expect(gen.rate == .audio)
|
||
}
|
||
|
||
/// Constant-zero input produces no rising edge; output stays at 0.
|
||
@Test func noTriggerOutputsZero() {
|
||
let gen: Trig1.Audio = Trig1.Audio(input: 0.0, dur: 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) expected 0.0 got \(out[i])")
|
||
}
|
||
}
|
||
|
||
/// Rising edge at sample 16 outputs 1.0 (NOT the input level) for dur samples.
|
||
/// Trig1 always outputs unity regardless of the input amplitude.
|
||
@Test func risingEdgeOutputsUnity() {
|
||
let durSamples: Int = 10
|
||
let dur: Double = Double(durSamples) / 44100.0
|
||
// Input = 0 until sample 16, then 0.99 (non-unity) to confirm Trig1 outputs 1.0 not 0.99.
|
||
let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.99 }
|
||
let gen: Trig1.Audio = Trig1.Audio(input: input, dur: dur)
|
||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
|
||
// Before edge: 0.
|
||
for i: Int in 0..<16 {
|
||
#expect(out[i] == 0.0, "pre-edge sample \(i) expected 0.0 got \(out[i])")
|
||
}
|
||
// Pulse: exactly 1.0 regardless of input level.
|
||
for i: Int in 16..<(16 + durSamples) {
|
||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||
"pulse sample \(i) expected 1.0 got \(out[i])")
|
||
}
|
||
// After pulse: 0.
|
||
for i: Int in (16 + durSamples)..<blockSize {
|
||
#expect(out[i] == 0.0,
|
||
"post-pulse sample \(i) expected 0.0 got \(out[i])")
|
||
}
|
||
}
|
||
|
||
/// Sustained input > 0 (no falling edge) produces only one pulse.
|
||
@Test func sustainedTrigProducesOnePulse() {
|
||
let durSamples: Int = 8
|
||
let dur: Double = Double(durSamples) / 44100.0
|
||
// Input stays at 0.5 from sample 10 onwards (no falling edge → no second pulse).
|
||
let input: Closure = Closure { _, i in i >= 10 ? 0.5 : 0.0 }
|
||
let gen: Trig1.Audio = Trig1.Audio(input: input, dur: dur)
|
||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
|
||
// Pulse samples: 10..(10+durSamples-1) = 1.0.
|
||
for i: Int in 10..<(10 + durSamples) {
|
||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||
"pulse sample \(i) expected 1.0 got \(out[i])")
|
||
}
|
||
// After pulse: 0 (trig still high but no new edge — no re-fire).
|
||
for i: Int in (10 + durSamples)..<blockSize {
|
||
#expect(out[i] == 0.0,
|
||
"post-pulse sample \(i) expected 0.0 got \(out[i])")
|
||
}
|
||
}
|
||
|
||
/// Input drops back to 0 then rises again — new rising edge restarts timer at 1.0.
|
||
@Test func newEdgeRestartsTimer() {
|
||
let durSamples: Int = 20
|
||
let dur: Double = Double(durSamples) / 44100.0
|
||
// First edge at 5, drops to 0 at 10, second edge at 15.
|
||
let input: Closure = Closure { _, i in
|
||
if i < 5 { return 0.0 }
|
||
if i < 10 { return 0.5 }
|
||
if i < 15 { return 0.0 }
|
||
return 0.5
|
||
}
|
||
let gen: Trig1.Audio = Trig1.Audio(input: input, dur: dur)
|
||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
|
||
// From 15 onwards (re-triggered) all samples in the 20-sample window = 1.0.
|
||
for i: Int in 15..<(15 + durSamples) {
|
||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||
"re-trigger sample \(i) expected 1.0 got \(out[i])")
|
||
}
|
||
}
|
||
|
||
/// Fan-out: two calls in same segment return identical first channel.
|
||
@Test func fanOutReturnsSameChannel() {
|
||
let gen: Trig1.Audio = Trig1.Audio(input: 0.0, dur: 0.01)
|
||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for i: Int in 0..<blockSize {
|
||
#expect(out1[i] == out2[i])
|
||
}
|
||
}
|
||
|
||
/// mulAdd: pulse output × 2 + 1 = 3.0 during hold.
|
||
@Test func mulAddApplied() {
|
||
let durSamples: Int = 20
|
||
let dur: Double = Double(durSamples) / 44100.0
|
||
// Input goes from 0 to 0.5 at sample 0 (fires immediately).
|
||
let input: Closure = Closure { _, _ in 0.5 }
|
||
let genMulAdd: Trig1.Audio = Trig1.Audio(input: input, dur: dur, mul: 2.0, add: 1.0)
|
||
let out: [Double] = genMulAdd.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// 1.0 * 2 + 1 = 3.0 during pulse.
|
||
for i: Int in 0..<durSamples {
|
||
#expect(abs(out[i] - 3.0) < 1e-12,
|
||
"mul/add sample \(i) expected 3.0 got \(out[i])")
|
||
}
|
||
// After pulse: 0.0 * 2 + 1 = 1.0.
|
||
for i: Int in durSamples..<blockSize {
|
||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||
"post-pulse mul/add sample \(i) expected 1.0 got \(out[i])")
|
||
}
|
||
}
|
||
}
|