Files
Lyrebird/Tests/LyrebirdTests/AUv3/LyrebirdAudioUnitPresetTests.swift
T

83 lines
3.0 KiB
Swift

// LyrebirdAudioUnitPresetTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import AudioToolbox
import AVFoundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
private final class PresetSynth: SynthesizerBase {
@LiveParam var freq: Double = 440
@LiveParam var amp: Double = 0.5
override func build() { output(bus: 0, source: $freq * $amp) }
}
private final class PresetAU: LyrebirdAudioUnit {
public override class func makeSynthesizer() -> SynthesizerBase { MainActor.assumeIsolated { PresetSynth() } }
public override class func factoryPresetsForLyrebird() -> [LyrebirdAUFactoryPreset] {
[
.init(name: "Bright", scalarValues: ["freq": 880, "amp": 0.8]),
.init(name: "Quiet", scalarValues: ["freq": 220, "amp": 0.1]),
]
}
}
private func makeDesc() -> AudioComponentDescription {
AudioComponentDescription(
componentType: kAudioUnitType_MusicDevice,
componentSubType: 0x50525345,
componentManufacturer: 0x524C5A53,
componentFlags: 0,
componentFlagsMask: 0
)
}
@MainActor
@Suite("LyrebirdAudioUnit factory presets") struct LyrebirdAudioUnitFactoryPresetsTests {
@Test func factoryPresetsExposeAllNames() throws {
let au: PresetAU = try PresetAU(componentDescription: makeDesc())
let presets: [AUAudioUnitPreset] = try #require(au.factoryPresets)
#expect(presets.map(\.name) == ["Bright", "Quiet"])
#expect(presets.map(\.number) == [0, 1])
}
@Test func selectingCurrentPresetAppliesScalars() throws {
let au: PresetAU = try PresetAU(componentDescription: makeDesc())
let synth: PresetSynth = try #require(au.synthesizer as? PresetSynth)
let p0: AUAudioUnitPreset? = au.factoryPresets?.first
au.currentPreset = p0
// Preset scalars are AUValue (Float) — round-trip through Double will
// pick up a small precision delta, so tolerance is applied.
#expect(synth.freq == 880)
#expect(abs(synth.amp - 0.8) < 1e-6)
let p1: AUAudioUnitPreset? = au.factoryPresets?.last
au.currentPreset = p1
#expect(synth.freq == 220)
#expect(abs(synth.amp - 0.1) < 1e-6)
}
@Test func currentPresetSetterIsIdempotent() throws {
let au: PresetAU = try PresetAU(componentDescription: makeDesc())
let p0: AUAudioUnitPreset? = au.factoryPresets?.first
au.currentPreset = p0
au.currentPreset = p0
let synth: PresetSynth = try #require(au.synthesizer as? PresetSynth)
#expect(synth.freq == 880)
}
@Test func nilCurrentPresetIsNoop() throws {
let au: PresetAU = try PresetAU(componentDescription: makeDesc())
let synth: PresetSynth = try #require(au.synthesizer as? PresetSynth)
synth.freq = 1234
au.currentPreset = nil
// Nil should not overwrite the user-set scalar.
#expect(synth.freq == 1234)
}
}