// BlockRateControlGeneratorTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("BlockRateControlGenerator") struct BlockRateControlGeneratorTests { /// A `BlockRateControlGenerator` must resolve `calculatedSamples` to the block-rate /// gateway (the marker's protocol-extension default), NOT the `ControlGenerator` /// sub-block path — even when `controlBlockSize < blockSize`. These generators drop /// the `_setupSubBlockBuffer()` allocation, so if the sub-block path were wrongly /// selected it would trap on the empty `_subBlockBuffer`. No trap + correct output /// proves the marker default won the `ValidGeneratorInput.calculatedSamples` witness. @Test func markedControlDoesNotEnterSubBlockPath() throws { let engine: SynthesisEngine = SynthesisEngine( blockSize: 512, controlBlockSize: 64, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 2, numAudioBuses: 128, numControlBuses: 4096, driver: OfflineDriver()) try engine.start() defer { engine.stop() } #expect(engine.controlBlockSize < engine.blockSize) // Lag with time 0 is transparent; a unit input settles to ~1.0 within the block. let lag: Lag.Control = Lag.Control(input: ConstantAudioGen(1.0), time: 0.0) let g: SynthGraph = makeSynthGraph(engine: engine) let out: [Double] = lag.calculatedSamples(graph: g, count: engine.blockSize)[0] #expect(out.count >= engine.blockSize) #expect(out.prefix(engine.blockSize).allSatisfy { $0.isFinite }) #expect(abs(out[engine.blockSize - 1] - 1.0) < 1e-6, "got \(out[engine.blockSize - 1])") // The real graph calls through `any ValidGeneratorInput`, which resolves via the // witness table (not concrete-type overload ranking). Exercise that path too: a // wrong witness (the ControlGenerator sub-block gateway) would still trap here. let erased: any ValidGeneratorInput = lag let g2: SynthGraph = makeSynthGraph(engine: engine) advanceBlock(engine) let out2: [Double] = erased.calculatedSamples(graph: g2, count: engine.blockSize)[0] #expect(out2.prefix(engine.blockSize).allSatisfy { $0.isFinite }) #expect(abs(out2[engine.blockSize - 1] - 1.0) < 1e-6, "got \(out2[engine.blockSize - 1])") } /// Same guarantee through a `BlockRateControlGenerator` conformer that INHERITS the /// conformance from a non-final base (`SpectralFeatureControlBase`) rather than /// declaring it directly — the witness must still resolve to the block-rate default. @Test func inheritedMarkerConformanceDoesNotSubBlock() throws { let engine: SynthesisEngine = SynthesisEngine( blockSize: 512, controlBlockSize: 64, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 2, numAudioBuses: 128, numControlBuses: 4096, driver: OfflineDriver()) try engine.start() defer { engine.stop() } let buffer: SpectralBuffer = SpectralBuffer(fftSize: 1024) let centroid: any ValidGeneratorInput = SpectralCentroid.Control(spectral: buffer, sampleRate: 44100) let g: SynthGraph = makeSynthGraph(engine: engine) let out: [Double] = centroid.calculatedSamples(graph: g, count: engine.blockSize)[0] #expect(out.prefix(engine.blockSize).allSatisfy { $0.isFinite }) } }