1.0.0 release
This commit is contained in:
@@ -0,0 +1,356 @@
|
||||
// SpectralChainTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("SpectralChain.Audio — basic shape") struct SpectralChainBasicTests {
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(0.0), fftSize: 64, hopSize: 32)
|
||||
#expect(chain.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func defaultOutputIsMono() {
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(0.0), fftSize: 64, hopSize: 32)
|
||||
#expect(chain.outputChannelCount == 1)
|
||||
}
|
||||
|
||||
@Test func fanOutControlsChannelCount() {
|
||||
let diffuser: SpectralStereoDiffuse = SpectralStereoDiffuse(amount: 0.5, seed: 1)
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.0),
|
||||
fftSize: 64, hopSize: 32,
|
||||
fanOut: diffuser
|
||||
)
|
||||
#expect(chain.outputChannelCount == 2)
|
||||
}
|
||||
|
||||
@Test func analyzeOnlyForcesMono() {
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.0),
|
||||
fftSize: 64, hopSize: 32,
|
||||
analyzeOnly: true
|
||||
)
|
||||
#expect(chain.outputChannelCount == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("SpectralChain.Audio — analysis hop") struct SpectralChainAnalysisHopTests {
|
||||
|
||||
@Test func dcBinDominatesForConstantInput() throws {
|
||||
let fftSize: Int = 128
|
||||
let hopSize: Int = 64
|
||||
let blockSize: Int = hopSize
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(1.0),
|
||||
fftSize: fftSize, hopSize: hopSize
|
||||
)
|
||||
|
||||
// Run 4 blocks to reach steady state (ring fully filled with DC).
|
||||
for _ in 0..<4 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
let dcMag: Float = chain.spectralBuffer.magnitude(at: 0)
|
||||
#expect(dcMag > 0, "DC magnitude must be positive for constant input")
|
||||
|
||||
let halfSize: Int = fftSize / 2
|
||||
for bin: Int in 1..<halfSize {
|
||||
let binMag: Float = chain.spectralBuffer.magnitude(at: bin)
|
||||
#expect(dcMag > binMag,
|
||||
"DC magnitude \(dcMag) should exceed bin \(bin) magnitude \(binMag)")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func analyzeOnlyOutputIsSilence() throws {
|
||||
let blockSize: Int = 32
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(1.0),
|
||||
fftSize: 64, hopSize: 32,
|
||||
analyzeOnly: true
|
||||
)
|
||||
let out: [[Double]] = chain.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == 0.0, "analyzeOnly chain should output silence")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func processorsCalledInOrder() throws {
|
||||
final class TaggingProcessor: SpectralProcessor {
|
||||
let id: Int
|
||||
let log: TagLog
|
||||
init(id: Int, log: TagLog) { self.id = id; self.log = log }
|
||||
func process(spectral: SpectralBuffer, engine: SynthesisEngine) { log.append(id) }
|
||||
}
|
||||
final class TagLog { var ids: [Int] = []; func append(_ id: Int) { ids.append(id) } }
|
||||
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let log: TagLog = TagLog()
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(1.0),
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
stages: [
|
||||
.mono(TaggingProcessor(id: 1, log: log)),
|
||||
.mono(TaggingProcessor(id: 2, log: log)),
|
||||
.mono(TaggingProcessor(id: 3, log: log)),
|
||||
]
|
||||
)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
#expect(log.ids == [1, 2, 3])
|
||||
}
|
||||
|
||||
@Test func sideChainProcessorReceivesBothBuffers() throws {
|
||||
final class CrossProcessor: SpectralProcessor2 {
|
||||
var seen: (Int, Int)?
|
||||
func process(spectral: SpectralBuffer,
|
||||
sideChain: SpectralBuffer, engine: SynthesisEngine) {
|
||||
seen = (spectral.fftSize, sideChain.fftSize)
|
||||
}
|
||||
}
|
||||
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let sideChain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.5),
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
analyzeOnly: true
|
||||
)
|
||||
let cross: CrossProcessor = CrossProcessor()
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(1.0),
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
stages: [.dual(cross, sideChain: sideChain)]
|
||||
)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
#expect(cross.seen != nil)
|
||||
#expect(cross.seen?.0 == fftSize)
|
||||
#expect(cross.seen?.1 == fftSize)
|
||||
}
|
||||
|
||||
@Test func mixedMonoDualStagesDispatchInInsertionOrder() throws {
|
||||
// The new `stages` slot lets `.mono` and `.dual` interleave. The prior
|
||||
// two-array layout fixed the order to "all monos first, then all duals";
|
||||
// this test pins the new contract that order is whatever the user passed.
|
||||
|
||||
final class TagLog { var ids: [String] = []; func append(_ id: String) { ids.append(id) } }
|
||||
|
||||
final class TaggingMono: SpectralProcessor {
|
||||
let id: String
|
||||
let log: TagLog
|
||||
init(id: String, log: TagLog) { self.id = id; self.log = log }
|
||||
func process(spectral: SpectralBuffer, engine: SynthesisEngine) { log.append(id) }
|
||||
}
|
||||
|
||||
final class TaggingDual: SpectralProcessor2 {
|
||||
let id: String
|
||||
let log: TagLog
|
||||
init(id: String, log: TagLog) { self.id = id; self.log = log }
|
||||
func process(spectral: SpectralBuffer,
|
||||
sideChain: SpectralBuffer, engine: SynthesisEngine) {
|
||||
log.append(id)
|
||||
}
|
||||
}
|
||||
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let log: TagLog = TagLog()
|
||||
let sideChain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.5),
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
analyzeOnly: true
|
||||
)
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(1.0),
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
stages: [
|
||||
.mono(TaggingMono(id: "a", log: log)),
|
||||
.dual(TaggingDual(id: "b", log: log), sideChain: sideChain),
|
||||
.mono(TaggingMono(id: "c", log: log)),
|
||||
.dual(TaggingDual(id: "d", log: log), sideChain: sideChain),
|
||||
]
|
||||
)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
#expect(log.ids == ["a", "b", "c", "d"])
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("SpectralChain.Audio — resynthesis") struct SpectralChainResynthesisTests {
|
||||
|
||||
@Test func monoResynthesisReachesUnityGainForDC() throws {
|
||||
// Constant DC input through the consolidated chain: after WOLA
|
||||
// warmup the resynthesised output should match the input within
|
||||
// round-trip float tolerance.
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let dcInput: Double = 0.3
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(input: dcInput,
|
||||
fftSize: fftSize, hopSize: hopSize)
|
||||
|
||||
let warmUpBlocks: Int = 4
|
||||
let measureBlocks: Int = 4
|
||||
var sum: Double = 0
|
||||
var n: Int = 0
|
||||
for block: Int in 0..<(warmUpBlocks + measureBlocks) {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
if block >= warmUpBlocks {
|
||||
for i: Int in 0..<blockSize { sum += out[0][i]; n += 1 }
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let mean: Double = sum / Double(n)
|
||||
#expect(abs(mean - dcInput) < 0.05,
|
||||
"Round-trip DC mean \(mean) should be ≈ \(dcInput)")
|
||||
}
|
||||
|
||||
@Test func fanOutProducesNRows() throws {
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let diffuser: SpectralStereoDiffuse = SpectralStereoDiffuse(amount: 0.0, seed: 42)
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.3),
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
fanOut: diffuser
|
||||
)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
#expect(out.count == 2)
|
||||
#expect(out[0].count >= blockSize)
|
||||
#expect(out[1].count >= blockSize)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("SpectralChain.Audio — synthOnly mode") struct SpectralChainSynthOnlyTests {
|
||||
|
||||
/// Build a frame buffer with a constant non-zero DC magnitude on every
|
||||
/// frame so playback through synthOnly produces a non-silent signal.
|
||||
private func makeDCFrameBuffer(fftSize: Int, hopSize: Int, frames: Int)
|
||||
-> SpectralFrameBuffer
|
||||
{
|
||||
let fb: SpectralFrameBuffer = SpectralFrameBuffer(fftSize: fftSize, hopSize: hopSize,
|
||||
frameCount: frames, sampleRate: 48_000)
|
||||
let scratch: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
scratch.dcValue = 100.0 // strong DC tone
|
||||
for f: Int in 0..<frames {
|
||||
fb.writeFrame(from: scratch, at: f)
|
||||
}
|
||||
return fb
|
||||
}
|
||||
|
||||
@Test func synthOnlyResynthesisProducesNonZeroAudio() throws {
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let frames: SpectralFrameBuffer = makeDCFrameBuffer(fftSize: fftSize, hopSize: hopSize, frames: 64)
|
||||
let player: SpectralPlayBuffer = SpectralPlayBuffer(frameBuffer: frames,
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
rate: 1.0, loop: true)
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(spectralSource: player,
|
||||
fftSize: fftSize, hopSize: hopSize)
|
||||
|
||||
// Warm up so the IFFT bank's one-hop latency clears.
|
||||
var anyNonZero: Bool = false
|
||||
for _ in 0..<8 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
for s: Double in out[0].prefix(blockSize) where s != 0 {
|
||||
anyNonZero = true
|
||||
break
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(anyNonZero,
|
||||
"SpectralChain.Audio(spectralSource:) should produce non-zero audio for a constant-DC frame buffer")
|
||||
}
|
||||
|
||||
/// Confirm that registered single-input processors run on the aliased
|
||||
/// source spectralBuffer in synthOnly mode.
|
||||
private final class CountingProcessor: SpectralProcessor {
|
||||
var callCount: Int = 0
|
||||
var lastSeenDC: Float = 0
|
||||
func process(spectral: SpectralBuffer, engine: SynthesisEngine) {
|
||||
callCount += 1
|
||||
lastSeenDC = spectral.dcValue
|
||||
}
|
||||
}
|
||||
|
||||
@Test func synthOnlyDispatchesProcessorsOnAliasedBuffer() throws {
|
||||
let fftSize: Int = 64
|
||||
let hopSize: Int = 32
|
||||
let blockSize: Int = hopSize
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let frames: SpectralFrameBuffer = makeDCFrameBuffer(fftSize: fftSize, hopSize: hopSize, frames: 64)
|
||||
let player: SpectralPlayBuffer = SpectralPlayBuffer(frameBuffer: frames,
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
rate: 1.0, loop: true)
|
||||
let counter: SpectralChainSynthOnlyTests.CountingProcessor = CountingProcessor()
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(spectralSource: player,
|
||||
fftSize: fftSize, hopSize: hopSize,
|
||||
stages: [.mono(counter)])
|
||||
|
||||
for _ in 0..<8 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = chain.calculatedSamples(graph: g, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(counter.callCount > 0,
|
||||
"Processor must be dispatched on the aliased source spectralBuffer")
|
||||
#expect(counter.lastSeenDC == 100.0,
|
||||
"Processor should see the DC the source wrote into the shared buffer")
|
||||
#expect(chain.spectralBuffer === player.spectralBuffer,
|
||||
"SynthOnly chain.spectralBuffer must alias spectralSource.spectralBuffer")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user