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

120 lines
4.6 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.
// GeneratorArrayTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("GeneratorArray")
struct GeneratorArrayTests {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
var graph: SynthGraph {
makeSynthGraph(engine: engine)
}
init() throws {
try engine.start()
}
@Test("expand factory builds N-row GeneratorArray")
func expandFactory() {
let arr: GeneratorArray = expand([1.0, 2.0, 3.0]) { ConstantAudioGen($0.floatValue(graph: makeSynthGraph(engine: engine))) }
#expect(arr.outputChannelCount == 3)
}
@Test("GeneratorArray * scalar scales every channel")
func arrayTimesScalar() {
let count: Int = 8
let arr: GeneratorArray = GeneratorArray([
ConstantAudioGen(1.0),
ConstantAudioGen(2.0),
ConstantAudioGen(3.0)
])
let scaled: GeneratorArray = arr * (10.0 as Double)
#expect(scaled.outputChannelCount == 3)
let samples: [[Double]] = scaled.calculatedSamples(graph: graph, count: count)
#expect(samples[0].prefix(count).allSatisfy { $0 == 10.0 })
#expect(samples[1].prefix(count).allSatisfy { $0 == 20.0 })
#expect(samples[2].prefix(count).allSatisfy { $0 == 30.0 })
}
@Test("scalar * GeneratorArray scales every channel")
func scalarTimesArray() {
let count: Int = 8
let arr: GeneratorArray = GeneratorArray([
ConstantAudioGen(1.0),
ConstantAudioGen(2.0)
])
let scaled: GeneratorArray = (5.0 as Double) * arr
let samples: [[Double]] = scaled.calculatedSamples(graph: graph, count: count)
#expect(samples[0].prefix(count).allSatisfy { $0 == 5.0 })
#expect(samples[1].prefix(count).allSatisfy { $0 == 10.0 })
}
@Test("GeneratorArray + GeneratorArray is element-wise")
func arrayPlusArrayElementWise() {
let count: Int = 8
let a: GeneratorArray = GeneratorArray([
ConstantAudioGen(1.0),
ConstantAudioGen(2.0)
])
let b: GeneratorArray = GeneratorArray([
ConstantAudioGen(10.0),
ConstantAudioGen(20.0)
])
let sum: GeneratorArray = a + b
let samples: [[Double]] = sum.calculatedSamples(graph: graph, count: count)
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
#expect(samples[1].prefix(count).allSatisfy { $0 == 22.0 })
}
@Test("GeneratorArray subscript returns ChannelSelect for that channel")
func subscriptChannel() {
let count: Int = 8
let arr: GeneratorArray = GeneratorArray([
ConstantAudioGen(1.0),
ConstantAudioGen(2.0),
ConstantAudioGen(3.0)
])
let mid: ChannelSelect.Audio = arr[1]
let samples: [[Double]] = mid.calculatedSamples(graph: graph, count: count)
#expect(samples[0].prefix(count).allSatisfy { $0 == 2.0 })
}
@Test("outputChannelCount matches contained generators")
func outputChannelCountMatchesGenerators() {
let arr: GeneratorArray = GeneratorArray([
ConstantAudioGen(1.0),
ConstantAudioGen(2.0),
ConstantAudioGen(3.0),
ConstantAudioGen(4.0)
])
#expect(arr.outputChannelCount == 4)
}
@Test("GeneratorArray * mono generator passes (other operand is 1-channel)")
func arrayTimesMonoGenerator() {
let count: Int = 8
let arr: GeneratorArray = GeneratorArray([
ConstantAudioGen(1.0),
ConstantAudioGen(2.0)
])
let mono: ConstantAudioGen = ConstantAudioGen(10.0)
let scaled: GeneratorArray = arr * mono
#expect(scaled.outputChannelCount == 2)
let samples: [[Double]] = scaled.calculatedSamples(graph: graph, count: count)
#expect(samples[0].prefix(count).allSatisfy { $0 == 10.0 })
#expect(samples[1].prefix(count).allSatisfy { $0 == 20.0 })
}
// GeneratorArray × multi-row-generator (e.g. `arr * Pan2(...)`) is intentionally rejected
// at construction time via `precondition` in `_generatorArrayCheckOtherOperandMono` —
// GeneratorArray reads only row 0 of each inner generator, so multi-row operands would
// silently drop channels 1..N-1. The trap is documented but not testable via Swift Testing
// (`precondition` terminates the process rather than throwing). The construction
// `arr * Pan2(input: ...)` would trap with a message naming the rhs channel count and
// pointing the caller at `ChannelSelect` / per-channel composition.
}