Files
Lyrebird/Tests/LyrebirdTests/LFStepNoiseAudioGenFreqTests.swift
T
2026-06-01 18:27:40 -07:00

46 lines
2.0 KiB
Swift

// LFStepNoiseAudioGenFreqTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("LFStepNoise.Audio generator freq")
struct LFStepNoiseAudioGenFreqTests {
@Test func generatorFreqDoesNotCrash() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
try engine.start()
defer { engine.stop() }
let freqNode: ParameterNode = ParameterNode(4.0)
let noise: LFStepNoise.Audio = LFStepNoise.Audio(freq: freqNode)
let g: SynthGraph = makeSynthGraph(engine: engine)
let out: [[Double]] = noise.calculatedSamples(graph: g, count: 64)
let samples: [Double] = Array((out.first ?? []).prefix(64))
#expect(samples.allSatisfy { $0.isFinite }, "LFStepNoise.Audio output must be finite")
#expect(samples.allSatisfy { abs($0) <= 1.0 + 1e-6 }, "LFStepNoise.Audio output must be in [-1, 1]")
}
@Test func generatorFreqProducesMultipleStepsWhenHigh() throws {
// Use a constant high freq (not ramp) wired as a Generator to verify the generator path.
// At 5000 Hz with blockSize=64 and sampleRate=44100, there should be ~7 steps per block.
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
defer { engine.stop() }
// ParameterNode pre-primed to 5000 Hz (no ramp: both atomic and previousOutputValue = 5000).
let freqNode: ParameterNode = ParameterNode(5000.0)
let noise: LFStepNoise.Audio = LFStepNoise.Audio(freq: freqNode, seed: 42)
let g: SynthGraph = makeSynthGraph(engine: engine)
let out: [[Double]] = noise.calculatedSamples(graph: g, count: 64)
let samples: [Double] = Array((out.first ?? []).prefix(64))
let uniqueCount: Int = Set(samples).count
#expect(uniqueCount > 1, "LFStepNoise.Audio with generator freq=5000Hz should produce multiple values per block (got \(uniqueCount) unique values)")
}
}