Files
Lyrebird/Tests/LyrebirdTests/MultiChannelTests.swift
T

394 lines
17 KiB
Swift

// MultiChannelTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("Multi-channel expansion")
struct MultiChannelTests {
// MARK: - GeneratorArray basics
@Test func generatorArrayWrapsGenerators() {
let a: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
let b: ConstantGeneratorInput = ConstantGeneratorInput(2.0)
let arr: GeneratorArray = GeneratorArray([a, b])
#expect(arr.generators.count == 2)
}
@Test func generatorArrayComputesNChannels() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(1.0), ConstantGeneratorInput(2.0)])
let result: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
#expect(result.count == 2)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 1.0) < 1e-9 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 2.0) < 1e-9 })
}
@Test func generatorArrayFloatValueReturnsFirstGenerator() {
let engine: SynthesisEngine = makeTestEngine()
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(3.0), ConstantGeneratorInput(7.0)])
#expect(abs(arr.floatValue(graph: graph) - 3.0) < 1e-9)
}
@Test func emptyGeneratorArrayReturnsEmpty() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([])
let result: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
#expect(result.isEmpty)
}
@Test func generatorArrayRateDerivedFromFirstGenerator() {
let a: OscSin.Audio = OscSin.Audio(freq: 440.0) // AudioGenerator
let arr: GeneratorArray = GeneratorArray([a])
#expect(arr.rate == .audio)
}
@Test func emptyGeneratorArrayDefaultsToAudioRate() {
let arr: GeneratorArray = GeneratorArray([])
#expect(arr.rate == .audio)
}
// MARK: - Fan-out cache
@Test func generatorArrayCacheHitWithinSegment() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let osc: OscSin.Audio = OscSin.Audio(freq: 440.0)
let arr: GeneratorArray = GeneratorArray([osc])
let r1: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
let r2: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
// Same backing storage — cache hit means no re-evaluation.
for i: Int in 0..<4 { #expect(r1[0][i] == r2[0][i]) }
}
@Test func generatorArrayCacheInvalidatedAcrossBlocks() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let osc: OscSin.Audio = OscSin.Audio(freq: 440.0)
let arr: GeneratorArray = GeneratorArray([osc])
let r1: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
advanceBlock(engine)
let r2: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
let identical: Bool = (0..<4).allSatisfy { r1[0][$0] == r2[0][$0] }
#expect(!identical)
}
// MARK: - expand() helper
@Test func expandCreatesCorrectGeneratorCount() {
let arr: GeneratorArray = expand([440.0, 880.0, 1320.0]) { OscSin.Audio(freq: $0) }
#expect(arr.generators.count == 3)
}
@Test func expandProducesDistinctGenerators() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// ConstantGeneratorInput with different values — channels should differ.
let arr: GeneratorArray = expand([1.0, 2.0] as [any ValidGeneratorInput]) { ConstantGeneratorInput($0.floatValue(graph: graph)) }
let result: [[Double]] = arr.calculatedSamples(graph: graph, count: 4)
#expect(result.count == 2)
#expect(result[0][0] != result[1][0])
}
// MARK: - Element-wise operators (GeneratorArray op scalar)
@Test func generatorArrayTimesScalar() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(4.0), ConstantGeneratorInput(8.0)])
let scaled: GeneratorArray = arr * 0.5
let result: [[Double]] = scaled.calculatedSamples(graph: graph, count: 4)
#expect(result.count == 2)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 2.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 4.0) < 1e-6 })
}
@Test func scalarTimesGeneratorArray() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(3.0), ConstantGeneratorInput(6.0)])
let scaled: GeneratorArray = 2.0 * arr
let result: [[Double]] = scaled.calculatedSamples(graph: graph, count: 4)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 6.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 12.0) < 1e-6 })
}
@Test func generatorArrayPlusScalar() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(1.0), ConstantGeneratorInput(2.0)])
let shifted: GeneratorArray = arr + 10.0
let result: [[Double]] = shifted.calculatedSamples(graph: graph, count: 4)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 11.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 12.0) < 1e-6 })
}
@Test func generatorArrayMinusScalar() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(5.0), ConstantGeneratorInput(3.0)])
let result: [[Double]] = (arr - 1.0).calculatedSamples(graph: graph, count: 4)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 4.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 2.0) < 1e-6 })
}
@Test func generatorArrayDivideScalar() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(6.0), ConstantGeneratorInput(4.0)])
let result: [[Double]] = (arr / 2.0).calculatedSamples(graph: graph, count: 4)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 3.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 2.0) < 1e-6 })
}
// MARK: - Element-wise operators (GeneratorArray op GeneratorArray)
@Test func generatorArrayPlusGeneratorArray() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let a: GeneratorArray = GeneratorArray([ConstantGeneratorInput(1.0), ConstantGeneratorInput(2.0)])
let b: GeneratorArray = GeneratorArray([ConstantGeneratorInput(10.0), ConstantGeneratorInput(20.0)])
let result: [[Double]] = (a + b).calculatedSamples(graph: graph, count: 4)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 11.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 22.0) < 1e-6 })
}
@Test func generatorArrayTimesGeneratorArray() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let a: GeneratorArray = GeneratorArray([ConstantGeneratorInput(3.0), ConstantGeneratorInput(5.0)])
let b: GeneratorArray = GeneratorArray([ConstantGeneratorInput(2.0), ConstantGeneratorInput(4.0)])
let result: [[Double]] = (a * b).calculatedSamples(graph: graph, count: 4)
#expect(result[0].prefix(4).allSatisfy { abs($0 - 6.0) < 1e-6 })
#expect(result[1].prefix(4).allSatisfy { abs($0 - 20.0) < 1e-6 })
}
// MARK: - Multi-channel Out
@Test func monoOutStillWritesSingleBus() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 8)
try engine.start()
let synth: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 0)
try engine.add(synth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.audioBuses[0][0] - 1.0) < 1e-6)
// Bus 1 untouched — mono source wrote only bus 0.
#expect(abs(engine.audioBuses[1][0]) < 1e-9)
engine.stop()
}
@Test func twoChannelSourceWritesConsecutiveBuses() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 8)
try engine.start()
final class TwoChannelSynth: SynthesizerBase {
override func build() {
// GeneratorArray of two different constants → should write bus 2 and bus 3.
let arr: GeneratorArray = GeneratorArray([ConstantGeneratorInput(3.0), ConstantGeneratorInput(7.0)])
output(bus: 2, source: arr)
}
}
let synth: TwoChannelSynth = TwoChannelSynth()
try engine.add(synth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.audioBuses[2][0] - 3.0) < 1e-6)
#expect(abs(engine.audioBuses[3][0] - 7.0) < 1e-6)
// Bus 4 untouched.
#expect(abs(engine.audioBuses[4][0]) < 1e-9)
engine.stop()
}
@Test func pan2WritesToConsecutiveBusesViaSynthesizerBase() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 8)
try engine.start()
final class Pan2Synth: SynthesizerBase {
override func build() {
let src: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
output(bus: 0, source: Pan2(input: src, pan: 0.0))
}
}
let synth: Pan2Synth = Pan2Synth()
try engine.add(synth)
engine.processBlock(inputSamples: [])
// Center pan: left ≈ right ≈ cos(π/4) ≈ 0.7071
let expectedGain: Double = Double(cos(Double.pi / 4.0))
#expect(abs(engine.audioBuses[0][0] - Float(expectedGain)) < 1e-5)
#expect(abs(engine.audioBuses[1][0] - Float(expectedGain)) < 1e-5)
engine.stop()
}
// MARK: - Pan2 correctness
@Test func pan2CenterBalance() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let panner: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: 0.0)
let result: [[Double]] = panner.calculatedSamples(graph: graph, count: 4)
#expect(result.count == 2)
let left: Double = result[0][0]
let right: Double = result[1][0]
#expect(abs(left - right) < 1e-6)
}
@Test func pan2HardLeft() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let panner: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -1.0)
let result: [[Double]] = panner.calculatedSamples(graph: graph, count: 4)
#expect(abs(result[0][0] - 1.0) < 1e-6) // left ≈ 1
#expect(abs(result[1][0]) < 1e-6) // right ≈ 0
}
@Test func pan2HardRight() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let panner: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: 1.0)
let result: [[Double]] = panner.calculatedSamples(graph: graph, count: 4)
#expect(abs(result[0][0]) < 1e-6) // left ≈ 0
#expect(abs(result[1][0] - 1.0) < 1e-6) // right ≈ 1
}
@Test func pan2EqualPowerInvariant() {
// left² + right² should equal input² at all pan positions.
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let panPositions: [Double] = [-1.0, -0.5, 0.0, 0.5, 1.0]
for pan: Double in panPositions {
let panner: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: pan)
advanceBlock(engine)
let result: [[Double]] = panner.calculatedSamples(graph: graph, count: 4)
let left: Double = result[0][0]
let right: Double = result[1][0]
let power: Double = left * left + right * right
#expect(abs(power - 1.0) < 1e-6, "Equal power failed for pan=\(pan): L²+R²=\(power)")
}
}
@Test func pan2ClampsPanOutOfRange() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// Values outside [-1, 1] should be clamped, not crash.
let panner: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: 5.0)
let result: [[Double]] = panner.calculatedSamples(graph: graph, count: 4)
#expect(result.count == 2)
// Clamped to 1.0 = hard right: left ≈ 0, right ≈ 1
#expect(abs(result[0][0]) < 1e-6)
#expect(abs(result[1][0] - 1.0) < 1e-6)
}
@Test func pan2FloatValuePrimesCacheAndReturnsFirstChannel() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let panner: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -1.0)
// floatValue routes through calculatedSamples(count:1), priming cache.
let fv: Double = panner.floatValue(graph: graph)
// Hard left: first channel (left) = 1.0
#expect(abs(fv - 1.0) < 1e-6)
}
// MARK: - In.multi / multiInput
@Test func inMultiReadsConsecutiveBuses() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 16)
try engine.start()
// Write distinct values to buses 4 and 5.
final class WriteSynth: SynthesizerBase {
override func build() {
output(bus: 4, source: ConstantGeneratorInput(0.25))
output(bus: 5, source: ConstantGeneratorInput(0.75))
}
}
final class ReadSynth: SynthesizerBase {
override func build() {
// Read buses 4 and 5, pass through to buses 0 and 1.
let stereoIn: GeneratorArray = In.Audio.multi(bus: 4, channels: 2)
output(bus: 0, source: stereoIn)
}
}
// engine.add uses addToHead: last-added evaluates first.
// Add reader first (lands at tail), then writer (takes head) so writer evaluates before reader.
try engine.add(ReadSynth()) // reader first → tail
try engine.add(WriteSynth()) // writer last → head (evaluates before ReadSynth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.audioBuses[0][0] - 0.25) < 1e-5)
#expect(abs(engine.audioBuses[1][0] - 0.75) < 1e-5)
engine.stop()
}
@Test func synthesizerBaseMultiInputMatchesInMulti() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 16)
try engine.start()
final class WriteSynth: SynthesizerBase {
override func build() {
output(bus: 6, source: ConstantGeneratorInput(0.3))
output(bus: 7, source: ConstantGeneratorInput(0.6))
}
}
final class ReadSynth: SynthesizerBase {
override func build() {
output(bus: 0, source: input(bus: 6, channels: 2))
}
}
// engine.add uses addToHead: last-added evaluates first.
// Add reader first (tail), then writer (head) so writer evaluates before reader.
try engine.add(ReadSynth()) // reader first → tail
try engine.add(WriteSynth()) // writer last → head (evaluates before ReadSynth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.audioBuses[0][0] - 0.3) < 1e-5)
#expect(abs(engine.audioBuses[1][0] - 0.6) < 1e-5)
engine.stop()
}
// MARK: - MCE end-to-end
@Test func expandTwoOscillatorsStereoOut() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16, numAudioBuses: 8)
try engine.start()
final class StereoOscSynth: SynthesizerBase {
override func build() {
// Two independent DC oscillators (freq=0 → constant at sin(π/2)=1)
let oscs: GeneratorArray = expand([0.0, 0.0] as [any ValidGeneratorInput]) {
OscSin.Audio(freq: $0, phase: 0.25)
}
// Scale each channel differently so we can tell them apart.
let scaled: GeneratorArray = GeneratorArray([
BinaryOpGenerator(lhs: oscs.generators[0], rhs: 0.4, op: *),
BinaryOpGenerator(lhs: oscs.generators[1], rhs: 0.8, op: *)
])
output(bus: 0, source: scaled)
}
}
try engine.add(StereoOscSynth())
engine.processBlock(inputSamples: [])
#expect(abs(engine.audioBuses[0][0] - 0.4) < 1e-4)
#expect(abs(engine.audioBuses[1][0] - 0.8) < 1e-4)
engine.stop()
}
}