Files
Lyrebird/Tests/LyrebirdTests/Generators/Waveshaping/WaveshaperControlTests.swift
T
2026-06-21 11:45:58 -07:00

127 lines
5.4 KiB
Swift
Raw 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.
// 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)
}
}