173 lines
8.1 KiB
Swift
173 lines
8.1 KiB
Swift
// DisintegratorAudioTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Disintegrator.Audio")
|
||
struct DisintegratorAudioTests {
|
||
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
let blockSize: Int = 64
|
||
let sampleRate: Double = 44100
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsAudio() {
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: 0.5, seed: 1)
|
||
#expect(gen.rate == .audio)
|
||
}
|
||
|
||
@Test func outputCountMatchesRequest() {
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: 0.5, seed: 1)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||
}
|
||
|
||
/// probability=0 → output equals input for every sample.
|
||
@Test func probabilityZeroIsPassthrough() {
|
||
let inputVal: Double = 0.7
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.0, multiplier: 0.0, seed: 1)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(out[0][i] == inputVal,
|
||
"prob=0 sample \(i): expected \(inputVal) got \(out[0][i])")
|
||
}
|
||
}
|
||
|
||
/// probability=1, multiplier=0 → all samples dropped to 0.
|
||
@Test func probabilityOneMultiplierZeroDropsAll() {
|
||
let inputVal: Double = 0.7
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 1.0, multiplier: 0.0, seed: 1)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(out[0][i] == 0.0,
|
||
"prob=1 mult=0 sample \(i): expected 0.0 got \(out[0][i])")
|
||
}
|
||
}
|
||
|
||
/// probability=1, multiplier=0.5 → all samples become input×0.5.
|
||
@Test func probabilityOneMultiplierHalfScalesAll() {
|
||
let inputVal: Double = 0.8
|
||
let multiplierVal: Double = 0.5
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 1.0, multiplier: multiplierVal, seed: 1)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(abs(out[0][i] - inputVal * multiplierVal) < 1e-12,
|
||
"prob=1 mult=0.5 sample \(i): expected \(inputVal * multiplierVal) got \(out[0][i])")
|
||
}
|
||
}
|
||
|
||
/// probability=0.5 with fixed seed: every output is either input or input×multiplier.
|
||
@Test func probabilityHalfOutputIsMembershipConsistent() {
|
||
let inputVal: Double = 0.6
|
||
let multiplierVal: Double = 0.0
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.5, multiplier: multiplierVal, seed: 42)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
let isOriginal: Bool = abs(out[0][i] - inputVal) < 1e-12
|
||
let isDisintegrated: Bool = abs(out[0][i] - inputVal * multiplierVal) < 1e-12
|
||
#expect(isOriginal || isDisintegrated,
|
||
"sample \(i) = \(out[0][i]) is neither input nor disintegrated")
|
||
}
|
||
}
|
||
|
||
/// probability=0.5, fixed seed: result is not all-identical (some disintegrated).
|
||
@Test func probabilityHalfProducesVariety() {
|
||
// With seed=42 and 64 samples at probability=0.5, we expect a mix.
|
||
let inputVal: Double = 0.6
|
||
let multiplierVal: Double = 0.0
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.5, multiplier: multiplierVal, seed: 42)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
let countZero: Int = out[0].prefix(blockSize).filter { $0 == 0.0 }.count
|
||
let countInput: Int = out[0].prefix(blockSize).filter { abs($0 - inputVal) < 1e-12 }.count
|
||
// With 64 samples at p=0.5 and a fixed seed, we should get some of each.
|
||
#expect(countZero > 0, "expected some disintegrated (zero) samples")
|
||
#expect(countInput > 0, "expected some passthrough samples")
|
||
}
|
||
|
||
/// Determinism: same seed → identical output across two independent instances.
|
||
@Test func sameSeedException() {
|
||
let inputVal: Double = 0.5
|
||
let gen1: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.5, multiplier: 0.0, seed: 123)
|
||
let gen2: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.5, multiplier: 0.0, seed: 123)
|
||
let out1: [[Double]] = gen1.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let out2: [[Double]] = gen2.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(out1[0][i] == out2[0][i],
|
||
"same seed: sample \(i) differs: \(out1[0][i]) vs \(out2[0][i])")
|
||
}
|
||
}
|
||
|
||
/// mul/add: with mul=2.0 add=1.0, output is original_output×2+1.
|
||
@Test func mulAddScalesOutput() {
|
||
// probability=0 so base output = input; mul=2 add=1.
|
||
let inputVal: Double = 0.4
|
||
let genBase: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.0, multiplier: 0.0, seed: 1)
|
||
let genScaled: Disintegrator.Audio = Disintegrator.Audio(input: inputVal, probability: 0.0, multiplier: 0.0, seed: 1, mul: 2.0, add: 1.0)
|
||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||
}
|
||
}
|
||
|
||
/// Fan-out: two calculatedSamples calls on the same graph return identical first channel.
|
||
@Test func fanOut() {
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: 0.5, probability: 0.5, seed: 7)
|
||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(out1[0][i] == out2[0][i])
|
||
}
|
||
}
|
||
|
||
/// Second call in same segment is a cache hit — PRNG does not double-advance.
|
||
@Test func cacheHitDoesNotDoubleAdvancePRNG() {
|
||
let gen: Disintegrator.Audio = Disintegrator.Audio(input: 0.5, probability: 0.5, seed: 99)
|
||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||
advanceBlock(engine)
|
||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
// out2 should differ from out1 (PRNG advanced by exactly one block, not two).
|
||
var differs: Bool = false
|
||
for i: Int in 0..<blockSize {
|
||
if out1[0][i] != out2[0][i] { differs = true; break }
|
||
}
|
||
#expect(differs, "block 2 output should differ from block 1 with probability=0.5")
|
||
}
|
||
|
||
/// Integration: runs inside a Synthesizer without crash.
|
||
@Test func integrationWithEngine() throws {
|
||
class DisintegratorSynth: SynthesizerBase {
|
||
override func build() {
|
||
let dist: Disintegrator.Audio = Disintegrator.Audio(
|
||
input: OscSin.Audio(freq: 440.0),
|
||
probability: 0.3,
|
||
multiplier: 0.0,
|
||
seed: 1
|
||
)
|
||
addOutputGenerator(Out.Audio(bus: 0, source: dist))
|
||
}
|
||
}
|
||
let synth: DisintegratorSynth = DisintegratorSynth()
|
||
try engine.add(synth)
|
||
for _: Int in 0..<4 {
|
||
engine.processBlock(inputSamples: [])
|
||
}
|
||
engine.remove(synth)
|
||
}
|
||
}
|