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

111 lines
4.7 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.
// LFGaussAudioTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - LFGaussAudioTests
@MainActor
@Suite("LFGauss.Audio")
struct LFGaussAudioTests {
let engine: SynthesisEngine
let sr: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func outputWithinUnitRange() {
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.15)
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 50)
#expect(samples.allSatisfy { $0 > 0.0 && $0 <= 1.0 + 1e-12 })
#expect(samples.allSatisfy { $0.isFinite })
}
@Test func peakReachesOneAtMidPeriod() {
// One full period long enough to land a sample near phase 0.5 (peak).
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.05, width: 0.2)
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 40)
let maxVal: Double = samples.max() ?? 0
#expect(maxVal > 0.999)
}
@Test func narrowerWidthSpendsMoreTimeNearZero() throws {
let wide: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.4)
let sWide: [Double] = collectBlocks(wide, engine: engine, blocks: 60)
let meanWide: Double = sWide.reduce(0, +) / Double(sWide.count)
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
try engine2.start()
let narrow: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.05)
let sNarrow: [Double] = collectBlocks(narrow, engine: engine2, blocks: 60)
let meanNarrow: Double = sNarrow.reduce(0, +) / Double(sNarrow.count)
// Narrower bell → lower average level (more of the cycle near zero).
#expect(meanNarrow < meanWide)
}
@Test func deterministicValueAtPeakPhase() {
// phase 0.5 → x = 0 → exp(0) = 1.0 on the first sample.
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.1, width: 0.2, phase: 0.5)
let g: SynthGraph = makeSynthGraph(engine: engine)
let samples: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
#expect(abs(samples[0][0] - 1.0) < 1e-12)
}
@Test func deterministicValueAtTroughPhase() {
// phase 0 → x = -1 → exp(-0.5 · (1/width)²).
let width: Double = 0.2
let a: Double = -1.0 / width
let expected: Double = exp(-0.5 * a * a)
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.1, width: width, phase: 0.0)
let g: SynthGraph = makeSynthGraph(engine: engine)
let samples: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
#expect(abs(samples[0][0] - expected) < 1e-12)
}
@Test func audioRatePeriodModNoCrash() {
let lfo: OscSin.Audio = OscSin.Audio(freq: 3.0)
let periodGen: BinaryOpGenerator = lfo * 0.005 + 0.02 // sweeps 15–25 ms
let gauss: LFGauss.Audio = LFGauss.Audio(period: periodGen, width: 0.2)
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 20)
#expect(samples.allSatisfy { $0.isFinite })
#expect(samples.allSatisfy { $0 > 0.0 && $0 <= 1.0 + 1e-12 })
}
@Test func audioRateWidthModNoCrash() {
let lfo: OscSin.Audio = OscSin.Audio(freq: 0.5)
let widthGen: BinaryOpGenerator = lfo * 0.1 + 0.2
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: widthGen)
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 20)
#expect(samples.allSatisfy { $0.isFinite })
}
@Test func participatesInFanOutCache() {
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.2)
let g: SynthGraph = makeSynthGraph(engine: engine)
let r1: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
let r2: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
}
@Test func mulAddScalesAndOffsets() {
let plain: LFGauss.Audio = LFGauss.Audio(period: 0.05, width: 0.2, phase: 0.5)
let g1: SynthGraph = makeSynthGraph(engine: engine)
let p: Double = plain.calculatedSamples(graph: g1, count: engine.blockSize)[0][0]
let scaled: LFGauss.Audio = LFGauss.Audio(period: 0.05, width: 0.2, phase: 0.5,
mul: 2.0, add: 1.0)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let s: Double = scaled.calculatedSamples(graph: g2, count: engine.blockSize)[0][0]
#expect(abs(s - (p * 2.0 + 1.0)) < 1e-12)
}
}