135 lines
5.0 KiB
Swift
135 lines
5.0 KiB
Swift
// GateTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Gate.Audio")
|
|
struct GateAudioTests {
|
|
|
|
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: Gate.Audio = Gate.Audio(input: 0.0, trig: 0.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
/// Constant trig=1 → pure passthrough.
|
|
@Test func openGatePassesInput() {
|
|
let input: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let gen: Gate.Audio = Gate.Audio(input: input, trig: 1.0)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[i] - Double(i) * 0.01) < 1e-12,
|
|
"sample \(i): expected \(Double(i) * 0.01), got \(out[i])")
|
|
}
|
|
}
|
|
|
|
/// Constant trig=0 → output stays at initial 0.0 regardless of input.
|
|
@Test func closedGateHoldsZero() {
|
|
let gen: Gate.Audio = Gate.Audio(input: 0.7, trig: 0.0)
|
|
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])")
|
|
}
|
|
}
|
|
|
|
/// Gate falling-edge holds the last input seen during the high phase.
|
|
@Test func fallingEdgeHoldsLastInput() {
|
|
// Input ramps from 0 to 0.63 across samples 0..63 (step 0.01).
|
|
let input: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
// Trigger high for samples 0..31, low for 32..63.
|
|
let trig: Closure = Closure { _, i in i < 32 ? 1.0 : 0.0 }
|
|
let gen: Gate.Audio = Gate.Audio(input: input, trig: trig)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
|
|
// Samples 0..31 — passthrough.
|
|
for i: Int in 0..<32 {
|
|
#expect(abs(out[i] - Double(i) * 0.01) < 1e-12)
|
|
}
|
|
// Samples 32..63 — held at last passed sample (i=31, value=0.31).
|
|
for i: Int in 32..<blockSize {
|
|
#expect(abs(out[i] - 0.31) < 1e-12,
|
|
"sample \(i) expected held=0.31, got \(out[i])")
|
|
}
|
|
}
|
|
|
|
/// Held value persists across block boundaries.
|
|
@Test func heldValuePersistsAcrossBlocks() {
|
|
// Block 1: input ramps, gate high then low; held = 0.31 at end.
|
|
// Block 2 (segmentSerial != block 1's): gate stays low.
|
|
let firstSerial: UInt64 = engine.segmentSerial
|
|
let input: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let trig: Closure = Closure { graph, i in
|
|
graph.engine.segmentSerial == firstSerial
|
|
? (i < 32 ? 1.0 : 0.0)
|
|
: 0.0
|
|
}
|
|
let gen: Gate.Audio = Gate.Audio(input: input, trig: trig)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
|
|
advanceBlock(engine)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[i] - 0.31) < 1e-12,
|
|
"block 2 sample \(i) expected held=0.31, got \(out[i])")
|
|
}
|
|
}
|
|
}
|
|
|
|
@Suite("Gate.Control")
|
|
struct GateControlTests {
|
|
|
|
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: Gate.Control = Gate.Control(input: 0.0, trig: 0.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
/// Open gate, constant input → control output ramps from 0 to input value.
|
|
@Test func openGateRampsToInput() {
|
|
let gen: Gate.Control = Gate.Control(input: 0.5, trig: 1.0)
|
|
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
|
// First block — ramp from rampStart=0.0 to 0.5; last sample should be 0.5.
|
|
#expect(abs(out[blockSize - 1] - 0.5) < 1e-12)
|
|
// Step linear: each sample = step * (i+1), step = 0.5/64.
|
|
let step: Double = 0.5 / Double(blockSize)
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[i] - step * Double(i + 1)) < 1e-12)
|
|
}
|
|
#expect(abs(gen.previousOutputValue - 0.5) < 1e-12)
|
|
}
|
|
|
|
@Test func closedGateHoldsZero() {
|
|
let gen: Gate.Control = Gate.Control(input: 0.5, trig: 0.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.previousOutputValue == 0.0)
|
|
}
|
|
|
|
@Test func floatValueReturnsPreviousOutputValue() {
|
|
let gen: Gate.Control = Gate.Control(input: 0.5, trig: 1.0)
|
|
#expect(gen.floatValue(graph: graph) == 0.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
|
}
|
|
}
|