prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
// CrossoverDistortionAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("CrossoverDistortion.Audio")
|
||||
struct CrossoverDistortionAudioTests {
|
||||
|
||||
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: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// amp=0 means no dead zone → output equals input (passthrough).
|
||||
/// Tests three constant inputs to verify the passthrough at amp=0.
|
||||
@Test func ampZeroIsPassthrough() {
|
||||
// Positive input
|
||||
let genPos: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.5, amp: 0.0, smooth: 0.0)
|
||||
let outPos: [[Double]] = genPos.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(outPos[0][i] - 0.5) < 1e-12,
|
||||
"passthrough +0.5 sample \(i): expected 0.5 got \(outPos[0][i])")
|
||||
}
|
||||
// Negative input
|
||||
advanceBlock(engine)
|
||||
let genNeg: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: -0.5, amp: 0.0, smooth: 0.0)
|
||||
let outNeg: [[Double]] = genNeg.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(outNeg[0][i] - (-0.5)) < 1e-12,
|
||||
"passthrough -0.5 sample \(i): expected -0.5 got \(outNeg[0][i])")
|
||||
}
|
||||
// Zero input
|
||||
advanceBlock(engine)
|
||||
let genZero: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.0, amp: 0.0, smooth: 0.0)
|
||||
let outZero: [[Double]] = genZero.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(outZero[0][i] == 0.0, "passthrough 0 sample \(i): expected 0 got \(outZero[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// amp=0.5, smooth=0: hard knee dead zone [−0.5, 0.5] → zero; x=0.3→0; x=0.7→0.2; x=-0.7→-0.2.
|
||||
@Test func hardKneeDeadZoneAndLinearRegion() {
|
||||
// Within dead zone (x=0.3, amp=0.5): output = 0
|
||||
let genDeadZone: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.3, amp: 0.5, smooth: 0.0)
|
||||
let outDeadZone: [[Double]] = genDeadZone.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(outDeadZone[0][0] == 0.0, "dead zone: expected 0 got \(outDeadZone[0][0])")
|
||||
|
||||
// Positive linear region (x=0.7, amp=0.5): over=0.2, out = 0.2
|
||||
advanceBlock(engine)
|
||||
let genPos: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.7, amp: 0.5, smooth: 0.0)
|
||||
let outPos: [[Double]] = genPos.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(outPos[0][0] - 0.2) < 1e-12, "positive linear: expected 0.2 got \(outPos[0][0])")
|
||||
|
||||
// Negative linear region (x=-0.7, amp=0.5): out = -0.2
|
||||
advanceBlock(engine)
|
||||
let genNeg: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: -0.7, amp: 0.5, smooth: 0.0)
|
||||
let outNeg: [[Double]] = genNeg.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(outNeg[0][0] - (-0.2)) < 1e-12, "negative linear: expected -0.2 got \(outNeg[0][0])")
|
||||
|
||||
// Dead zone at threshold (x=0.5, amp=0.5): exactly at boundary, out = 0
|
||||
advanceBlock(engine)
|
||||
let genEdge: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.5, amp: 0.5, smooth: 0.0)
|
||||
let outEdge: [[Double]] = genEdge.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(outEdge[0][0] == 0.0, "at threshold: expected 0 got \(outEdge[0][0])")
|
||||
}
|
||||
|
||||
/// smooth > 0: output just above the threshold is smaller than the hard-knee value (C1 ease-in).
|
||||
@Test func smoothKneeIsLessThanHardKneeNearThreshold() {
|
||||
// amp=0.5, smooth=1.0, x=0.55: over=0.05, kneeWidth=0.5.
|
||||
// hard knee: out = 0.05
|
||||
// smooth ease-in: out = (0.05^2)/(2*0.5) = 0.0025/1.0 = 0.0025
|
||||
advanceBlock(engine)
|
||||
let genHard: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.55, amp: 0.5, smooth: 0.0)
|
||||
let genSmooth: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.55, amp: 0.5, smooth: 1.0)
|
||||
let outHard: [[Double]] = genHard.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let outSmooth: [[Double]] = genSmooth.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Smooth output should be smaller than hard-knee output (ease-in starts slower).
|
||||
#expect(outSmooth[0][0] < outHard[0][0],
|
||||
"smooth should be less than hard at knee entry: smooth=\(outSmooth[0][0]) hard=\(outHard[0][0])")
|
||||
// Both should be positive.
|
||||
#expect(outSmooth[0][0] > 0.0, "smooth output should be positive above threshold")
|
||||
// Verify exact value: over^2/(2*kneeWidth) = 0.05^2 / (2*0.5) = 0.0025
|
||||
#expect(abs(outSmooth[0][0] - 0.0025) < 1e-12, "smooth ease-in: expected 0.0025 got \(outSmooth[0][0])")
|
||||
}
|
||||
|
||||
/// mul/add: with mul=2.0 add=1.0, output is base×2+1.
|
||||
@Test func mulAddScalesOutput() {
|
||||
// Use amp=0 (passthrough) so base output = input.
|
||||
let inputVal: Double = 0.4
|
||||
let genBase: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: inputVal, amp: 0.0)
|
||||
let genScaled: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: inputVal, amp: 0.0, 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: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.3, amp: 0.5)
|
||||
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 — state does not double-advance.
|
||||
@Test func cacheHitDoesNotDoubleAdvance() {
|
||||
let gen: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.7, amp: 0.5)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||||
for i: Int in 0..<blockSize {
|
||||
// amp=0.5, x=0.7: out = 0.2 for all samples.
|
||||
#expect(abs(out1[0][i] - 0.2) < 1e-12)
|
||||
}
|
||||
}
|
||||
|
||||
/// Integration: runs inside a Synthesizer without crash.
|
||||
@Test func integrationWithEngine() throws {
|
||||
class CrossoverSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let dist: CrossoverDistortion.Audio = CrossoverDistortion.Audio(
|
||||
input: OscSin.Audio(freq: 440.0),
|
||||
amp: 0.3,
|
||||
smooth: 0.5
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: dist))
|
||||
}
|
||||
}
|
||||
let synth: CrossoverSynth = CrossoverSynth()
|
||||
try engine.add(synth)
|
||||
for _: Int in 0..<4 {
|
||||
engine.processBlock(inputSamples: [])
|
||||
}
|
||||
engine.remove(synth)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// CrossoverDistortionControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("CrossoverDistortion.Control")
|
||||
struct CrossoverDistortionControlTests {
|
||||
|
||||
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 rateIsControl() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// amp=0, input=0.5: ramp target equals 0.5 (passthrough at control rate).
|
||||
@Test func ampZeroIsPassthrough() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.5, amp: 0.0, smooth: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 0.5) < 1e-12, "passthrough ramp target: expected 0.5 got \(lastSample)")
|
||||
}
|
||||
|
||||
/// amp=0.5, smooth=0, input=0.7: ramp target ≈ 0.2.
|
||||
@Test func hardKneeLinearRegionTarget() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, amp: 0.5, smooth: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 0.2) < 1e-12, "hard-knee target: expected 0.2 got \(lastSample)")
|
||||
}
|
||||
|
||||
/// amp=0.5, smooth=0, input=0.3: ramp target ≈ 0 (dead zone).
|
||||
@Test func deadZoneTargetIsZero() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.3, amp: 0.5, smooth: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(lastSample == 0.0, "dead zone target: expected 0 got \(lastSample)")
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, amp: 0.5, smooth: 0.0)
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.floatValue(graph: graph) - 0.2) < 1e-12)
|
||||
}
|
||||
|
||||
/// mul/add: target × mul + add is folded before ramping.
|
||||
@Test func mulAddFoldsIntoTarget() {
|
||||
// amp=0 (passthrough), input=0.3: base target = 0.3; ×2+1 = 1.6
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.3, amp: 0.0, mul: 2.0, add: 1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 1.6) < 1e-12, "mulAdd target: expected 1.6 got \(lastSample)")
|
||||
}
|
||||
|
||||
/// Fan-out: two calculatedSamples calls on the same graph return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, amp: 0.5)
|
||||
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 is a cache hit — no double advance.
|
||||
@Test func cacheHitDoesNotDoubleAdvance() {
|
||||
let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, amp: 0.5, smooth: 0.0)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||||
let lastSample: Double = out1[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 0.2) < 1e-12)
|
||||
}
|
||||
|
||||
/// Integration: runs inside a Synthesizer without crash.
|
||||
@Test func integrationWithEngine() throws {
|
||||
class CrossoverControlSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let dist: CrossoverDistortion.Control = CrossoverDistortion.Control(
|
||||
input: 0.7,
|
||||
amp: 0.5,
|
||||
smooth: 0.5
|
||||
)
|
||||
addOutputGenerator(Out.Control(bus: 0, source: dist))
|
||||
}
|
||||
}
|
||||
let synth: CrossoverControlSynth = CrossoverControlSynth()
|
||||
try engine.add(synth)
|
||||
for _: Int in 0..<4 {
|
||||
engine.processBlock(inputSamples: [])
|
||||
}
|
||||
engine.remove(synth)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
// WaveshaperAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Waveshaper.Audio")
|
||||
struct WaveshaperAudioTests {
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
// MARK: - Build helpers
|
||||
|
||||
/// Identity transfer curve: table maps x ∈ [-1,1] → x.
|
||||
func makeIdentityTable(n: Int = 1024) -> LyrebirdBuffer {
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: n)
|
||||
table.fill { i, count in Double(i) / Double(count - 1) * 2.0 - 1.0 }
|
||||
return table
|
||||
}
|
||||
|
||||
/// Inverting transfer curve: table maps x ∈ [-1,1] → -x.
|
||||
func makeInvertingTable(n: Int = 1024) -> LyrebirdBuffer {
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: n)
|
||||
table.fill { i, count in -(Double(i) / Double(count - 1) * 2.0 - 1.0) }
|
||||
return table
|
||||
}
|
||||
|
||||
/// Absolute-value transfer curve: table maps x ∈ [-1,1] → |x|.
|
||||
func makeAbsTable(n: Int = 1024) -> LyrebirdBuffer {
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: n)
|
||||
table.fill { i, count in
|
||||
let x: Double = Double(i) / Double(count - 1) * 2.0 - 1.0
|
||||
return abs(x)
|
||||
}
|
||||
return table
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// Identity table with constant input 0.3: output should ≈ 0.3 for all samples.
|
||||
@Test func identityTableScalarInput() {
|
||||
let tableSize: Int = 1024
|
||||
let table: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 0.3)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.3) < 1e-10,
|
||||
"identity sample \(i): expected ~0.3 got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Identity table with generator input (OscSin produces per-sample variation):
|
||||
/// waveshaper output must track the generator's per-sample values.
|
||||
@Test func identityTableGeneratorInput() {
|
||||
let tableSize: Int = 1024
|
||||
let table: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
// Use a constant at a known value via ConstantGeneratorInput to stay in [-1,1].
|
||||
let inputGen: ConstantGeneratorInput = ConstantGeneratorInput(0.5)
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: inputGen)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.5) < 1e-10,
|
||||
"generator input sample \(i): expected ~0.5 got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverting table with negative input: output ≈ -input.
|
||||
@Test func invertingTableNegatesInput() {
|
||||
let tableSize: Int = 1024
|
||||
let table: LyrebirdBuffer = makeInvertingTable(n: tableSize)
|
||||
let inputVal: Double = 0.6
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: inputVal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - (-inputVal)) < 1e-10,
|
||||
"inverting sample \(i): expected ~\(-inputVal) got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Absolute-value table: negative and positive inputs of the same magnitude produce
|
||||
/// the same output.
|
||||
@Test func absTableSymmetry() {
|
||||
let tableSize: Int = 1024
|
||||
let tablePos: LyrebirdBuffer = makeAbsTable(n: tableSize)
|
||||
let tableNeg: LyrebirdBuffer = makeAbsTable(n: tableSize)
|
||||
let genPos: Waveshaper.Audio = Waveshaper.Audio(table: tablePos, input: 0.4)
|
||||
let genNeg: Waveshaper.Audio = Waveshaper.Audio(table: tableNeg, input: -0.4)
|
||||
let outPos: [[Double]] = genPos.calculatedSamples(graph: graph, count: blockSize)
|
||||
let outNeg: [[Double]] = genNeg.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(outPos[0][i] - 0.4) < 1e-10,
|
||||
"abs(+0.4) sample \(i): expected ~0.4 got \(outPos[0][i])")
|
||||
#expect(abs(outNeg[0][i] - 0.4) < 1e-10,
|
||||
"abs(-0.4) sample \(i): expected ~0.4 got \(outNeg[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Input > 1.0 is clamped to table[last] (saturation at +1.0).
|
||||
@Test func inputAboveOneClampedToTableEnd() {
|
||||
let tableSize: Int = 1024
|
||||
let table: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 2.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 1.0) < 1e-10,
|
||||
"clamp-high sample \(i): expected ~1.0 got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Input < -1.0 is clamped to table[0] (saturation at -1.0).
|
||||
@Test func inputBelowMinusOneClampedToTableStart() {
|
||||
let tableSize: Int = 1024
|
||||
let table: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: -2.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - (-1.0)) < 1e-10,
|
||||
"clamp-low sample \(i): expected ~-1.0 got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Invalidated table produces silence (all zeros).
|
||||
@Test func invalidTableProducesSilence() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 0.5)
|
||||
table.invalidate()
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == 0.0, "silenced sample \(i) not zero: \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// mul/add: with mul=2.0 add=1.0, output is base×2+1.
|
||||
@Test func mulAddScalesOutput() {
|
||||
let tableSize: Int = 1024
|
||||
let table1: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
let table2: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
let genBase: Waveshaper.Audio = Waveshaper.Audio(table: table1, input: 0.3)
|
||||
let genScaled: Waveshaper.Audio = Waveshaper.Audio(table: table2, input: 0.3, 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-10,
|
||||
"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 table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 0.3)
|
||||
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 — state does not double-advance.
|
||||
@Test func cacheHitDoesNotDoubleAdvance() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Audio = Waveshaper.Audio(table: table, input: 0.5)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out1[0][i] - 0.5) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
/// Integration: Waveshaper.Audio runs inside a Synthesizer wired to the engine without crash.
|
||||
@Test func integrationWithEngine() throws {
|
||||
let tableForSynth: LyrebirdBuffer = makeIdentityTable()
|
||||
class WaveshaperSynth: SynthesizerBase {
|
||||
let table: LyrebirdBuffer
|
||||
init(table: LyrebirdBuffer) {
|
||||
self.table = table
|
||||
super.init()
|
||||
}
|
||||
override func build() {
|
||||
let shaper: Waveshaper.Audio = Waveshaper.Audio(
|
||||
table: table,
|
||||
input: OscSin.Audio(freq: 440.0) * 0.5
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: shaper))
|
||||
}
|
||||
}
|
||||
let synth: WaveshaperSynth = WaveshaperSynth(table: tableForSynth)
|
||||
try engine.add(synth)
|
||||
for _: Int in 0..<4 {
|
||||
engine.processBlock(inputSamples: [])
|
||||
}
|
||||
engine.remove(synth)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// WaveshaperControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Waveshaper.Control")
|
||||
struct WaveshaperControlTests {
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func makeIdentityTable(n: Int = 1024) -> LyrebirdBuffer {
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: n)
|
||||
table.fill { i, count in Double(i) / Double(count - 1) * 2.0 - 1.0 }
|
||||
return table
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// Identity table with constant input 0.5: ramp reaches ~0.5 by last sample.
|
||||
@Test func identityTableConstantInput() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Control ramp: last sample of first block equals target ≈ 0.5.
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 0.5) < 1e-10, "ramp target: expected ~0.5 got \(lastSample)")
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue (the control-rate target).
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.7)
|
||||
// After first block, previousOutputValue should be ~0.7.
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.floatValue(graph: graph) - 0.7) < 1e-10)
|
||||
}
|
||||
|
||||
/// Invalidated table produces silence (target = 0 → ramp toward 0).
|
||||
@Test func invalidTableProduceSilence() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.5)
|
||||
table.invalidate()
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// With invalid table target = 0; initial previousOutputValue = 0, so output is flat zero.
|
||||
#expect(out[0][blockSize - 1] == 0.0)
|
||||
}
|
||||
|
||||
/// mul/add: with mul=2.0 add=1.0, ramp target is approximately original×2+1.
|
||||
@Test func mulAddFoldsIntoTarget() {
|
||||
let tableSize: Int = 1024
|
||||
let table: LyrebirdBuffer = makeIdentityTable(n: tableSize)
|
||||
// identity table at 0.25 → waveshaper output ≈ 0.25; ×2 +1 = 1.5
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.25, mul: 2.0, add: 1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 1.5) < 1e-10, "mulAdd target: expected ~1.5 got \(lastSample)")
|
||||
}
|
||||
|
||||
/// Fan-out: two calculatedSamples calls on the same graph return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.3)
|
||||
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 — state does not double-advance.
|
||||
@Test func cacheHitDoesNotDoubleAdvance() {
|
||||
let table: LyrebirdBuffer = makeIdentityTable()
|
||||
let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.5)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||||
// out1 last sample should equal target ≈ 0.5.
|
||||
#expect(abs(out1[0][blockSize - 1] - 0.5) < 1e-10)
|
||||
}
|
||||
|
||||
/// Integration: runs inside a Synthesizer without crash.
|
||||
@Test func integrationWithEngine() throws {
|
||||
let tableForSynth: LyrebirdBuffer = makeIdentityTable()
|
||||
class WaveshaperControlSynth: SynthesizerBase {
|
||||
let table: LyrebirdBuffer
|
||||
init(table: LyrebirdBuffer) {
|
||||
self.table = table
|
||||
super.init()
|
||||
}
|
||||
override func build() {
|
||||
let shaper: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.3)
|
||||
addOutputGenerator(Out.Control(bus: 0, source: shaper))
|
||||
}
|
||||
}
|
||||
let synth: WaveshaperControlSynth = WaveshaperControlSynth(table: tableForSynth)
|
||||
try engine.add(synth)
|
||||
for _: Int in 0..<4 {
|
||||
engine.processBlock(inputSamples: [])
|
||||
}
|
||||
engine.remove(synth)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user