76 lines
2.4 KiB
Swift
76 lines
2.4 KiB
Swift
// ClosureSynthesizerTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable import LyrebirdSynth
|
|
|
|
/// Reference box so a closure can record how many times it ran.
|
|
private final class CallCounter {
|
|
var count: Int = 0
|
|
}
|
|
|
|
@MainActor
|
|
@Suite("ClosureSynthesizer")
|
|
struct ClosureSynthesizerTests {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
|
|
|
init() throws {
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func closureGraphWritesToBusZero() throws {
|
|
let synth: ClosureSynthesizer = ClosureSynthesizer { 0.5 }
|
|
try engine.add(synth)
|
|
engine.processBlock(inputSamples: [])
|
|
for i: Int in 0..<engine.blockSize {
|
|
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
|
}
|
|
}
|
|
|
|
@Test func busParameterSelectsOutputBus() throws {
|
|
let synth: ClosureSynthesizer = ClosureSynthesizer(bus: 2) { 0.7 }
|
|
try engine.add(synth)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(abs(engine.audioBuses[2][0] - 0.7) < 1e-6)
|
|
#expect(engine.audioBuses[0][0] == 0.0)
|
|
}
|
|
|
|
@Test func closureRunsExactlyOncePerInstanceAtAdd() throws {
|
|
let counter: CallCounter = CallCounter()
|
|
let synth: ClosureSynthesizer = ClosureSynthesizer {
|
|
counter.count += 1
|
|
return 0.25
|
|
}
|
|
// build() — and thus the closure — runs synchronously inside add.
|
|
try engine.add(synth)
|
|
#expect(counter.count == 1)
|
|
engine.processBlock(inputSamples: [])
|
|
// Rendering does not re-run the closure.
|
|
#expect(counter.count == 1)
|
|
}
|
|
|
|
@Test func bareOscillatorGraphProducesAudio() throws {
|
|
let synth: ClosureSynthesizer = ClosureSynthesizer {
|
|
OscSin.Audio(freq: 440, mul: 0.1)
|
|
}
|
|
try engine.add(synth)
|
|
engine.processBlock(inputSamples: [])
|
|
let anyNonZero: Bool = (0..<engine.blockSize).contains { engine.audioBuses[0][$0] != 0.0 }
|
|
#expect(anyNonZero)
|
|
}
|
|
|
|
@Test func multiChannelSourceFansAcrossBuses() throws {
|
|
// A stereo Pan2 source writes bus 0 and bus 1.
|
|
let synth: ClosureSynthesizer = ClosureSynthesizer {
|
|
Pan2(input: 0.5, pan: 0)
|
|
}
|
|
try engine.add(synth)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.audioBuses[0][0] != 0.0)
|
|
#expect(engine.audioBuses[1][0] != 0.0)
|
|
}
|
|
}
|