78 lines
2.8 KiB
Swift
78 lines
2.8 KiB
Swift
// LyrebirdAUPresetRegistryTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import AudioToolbox
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - Fixture preset-codable generator
|
|
|
|
private final class FixturePresetGen: GeneratorBase, AudioGenerator, LyrebirdAUPresetCodable {
|
|
|
|
static let presetTypeID: String = "test.fixturePresetGen"
|
|
|
|
private let _amp: Double
|
|
private let _freq: Double
|
|
|
|
init(amp: Double, freq: Double) {
|
|
self._amp = amp
|
|
self._freq = freq
|
|
super.init()
|
|
}
|
|
|
|
var presetEncodedParameters: [String: AUValue] {
|
|
["amp": AUValue(_amp), "freq": AUValue(_freq)]
|
|
}
|
|
|
|
static func presetMake(from parameters: [String: AUValue]) -> any Generator {
|
|
FixturePresetGen(
|
|
amp: Double(parameters["amp"] ?? 0),
|
|
freq: Double(parameters["freq"] ?? 0)
|
|
)
|
|
}
|
|
|
|
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
|
precondition(count <= _result[0].count)
|
|
for i: Int in 0..<count { _result[0][i] = _amp }
|
|
return _result
|
|
}
|
|
|
|
func floatValue(graph: SynthGraph) -> Double { _amp }
|
|
}
|
|
|
|
@MainActor
|
|
@Suite("LyrebirdAUPresetSourceRegistry") struct LyrebirdAUPresetRegistryTests {
|
|
|
|
@Test func registerAndMakeRoundTrip() {
|
|
defer { LyrebirdAUPresetSourceRegistry.unregister(typeID: FixturePresetGen.presetTypeID) }
|
|
LyrebirdAUPresetSourceRegistry.register(FixturePresetGen.self)
|
|
let descriptor: LyrebirdAUPresetSourceDescriptor = LyrebirdAUPresetSourceDescriptor(
|
|
typeID: FixturePresetGen.presetTypeID,
|
|
parameters: ["amp": 0.5, "freq": 440]
|
|
)
|
|
let made: (any Generator)? = LyrebirdAUPresetSourceRegistry.makeGenerator(from: descriptor)
|
|
guard let typed: FixturePresetGen = made as? FixturePresetGen else {
|
|
Issue.record("made-generator is not FixturePresetGen — got \(type(of: made))")
|
|
return
|
|
}
|
|
#expect(typed.presetEncodedParameters["amp"] == AUValue(0.5))
|
|
#expect(typed.presetEncodedParameters["freq"] == AUValue(440))
|
|
}
|
|
|
|
@Test func unknownTypeIDReturnsNil() {
|
|
let descriptor: LyrebirdAUPresetSourceDescriptor = LyrebirdAUPresetSourceDescriptor(typeID: "test.unknown")
|
|
let made: (any Generator)? = LyrebirdAUPresetSourceRegistry.makeGenerator(from: descriptor)
|
|
#expect(made == nil)
|
|
}
|
|
|
|
@Test func unregisterRemovesType() {
|
|
LyrebirdAUPresetSourceRegistry.register(FixturePresetGen.self)
|
|
#expect(LyrebirdAUPresetSourceRegistry.registeredTypeIDs.contains(FixturePresetGen.presetTypeID))
|
|
LyrebirdAUPresetSourceRegistry.unregister(typeID: FixturePresetGen.presetTypeID)
|
|
#expect(!LyrebirdAUPresetSourceRegistry.registeredTypeIDs.contains(FixturePresetGen.presetTypeID))
|
|
}
|
|
}
|