Files
2026-06-21 11:45:58 -07:00

220 lines
9.4 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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)
}
}