154 lines
5.9 KiB
Swift
154 lines
5.9 KiB
Swift
// SumChannelsAudioTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Darwin
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("SumChannels.Audio")
|
|
struct SumChannelsAudioTests {
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 32)
|
|
|
|
init() throws {
|
|
try engine.start()
|
|
}
|
|
|
|
/// Stereo input → mono output equal to channel0 + channel1.
|
|
@Test func stereoFoldDownSumsChannels() {
|
|
let stereo: GeneratorArray = GeneratorArray([
|
|
SumChannelsFixtureGen(0.25),
|
|
SumChannelsFixtureGen(-0.75)
|
|
])
|
|
let sum: SumChannels.Audio = SumChannels.Audio(input: stereo)
|
|
#expect(sum.outputChannelCount == 1)
|
|
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = sum.calculatedSamples(graph: graph, count: engine.blockSize)
|
|
|
|
#expect(out.count == 1)
|
|
let expected: Double = 0.25 + (-0.75) // -0.5
|
|
for i: Int in 0..<engine.blockSize {
|
|
#expect(abs(out[0][i] - expected) < 1e-9,
|
|
"frame \(i): expected \(expected), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Quad input → mono output equal to sum of all four channels.
|
|
@Test func quadFoldDownSumsAllChannels() {
|
|
let quad: GeneratorArray = GeneratorArray([
|
|
SumChannelsFixtureGen(0.1),
|
|
SumChannelsFixtureGen(0.2),
|
|
SumChannelsFixtureGen(0.3),
|
|
SumChannelsFixtureGen(0.4)
|
|
])
|
|
let sum: SumChannels.Audio = SumChannels.Audio(input: quad)
|
|
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = sum.calculatedSamples(graph: graph, count: engine.blockSize)
|
|
|
|
let expected: Double = 1.0 // 0.1 + 0.2 + 0.3 + 0.4
|
|
for i: Int in 0..<engine.blockSize {
|
|
#expect(abs(out[0][i] - expected) < 1e-9, "frame \(i)")
|
|
}
|
|
}
|
|
|
|
/// Mono input passes through unchanged (no double-counting).
|
|
@Test func monoInputPassesThrough() {
|
|
let mono: SumChannelsFixtureGen = SumChannelsFixtureGen(0.7)
|
|
let sum: SumChannels.Audio = SumChannels.Audio(input: mono)
|
|
#expect(sum.outputChannelCount == 1)
|
|
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = sum.calculatedSamples(graph: graph, count: engine.blockSize)
|
|
|
|
for i: Int in 0..<engine.blockSize {
|
|
#expect(abs(out[0][i] - 0.7) < 1e-9, "frame \(i)")
|
|
}
|
|
}
|
|
|
|
/// Scalar input broadcasts to the single output row.
|
|
@Test func scalarInputBroadcasts() {
|
|
let sum: SumChannels.Audio = SumChannels.Audio(input: 0.42)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = sum.calculatedSamples(graph: graph, count: engine.blockSize)
|
|
for i: Int in 0..<engine.blockSize {
|
|
#expect(abs(out[0][i] - 0.42) < 1e-9, "frame \(i)")
|
|
}
|
|
}
|
|
|
|
/// MulAdd applied after summation.
|
|
@Test func mulAddAppliesScalar() {
|
|
let stereo: GeneratorArray = GeneratorArray([
|
|
SumChannelsFixtureGen(1.0),
|
|
SumChannelsFixtureGen(1.0)
|
|
])
|
|
let sum: SumChannels.Audio = SumChannels.Audio(input: stereo, mul: 0.5, add: 0.1)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = sum.calculatedSamples(graph: graph, count: engine.blockSize)
|
|
// (1 + 1) * 0.5 + 0.1 = 1.1
|
|
for i: Int in 0..<engine.blockSize {
|
|
#expect(abs(out[0][i] - 1.1) < 1e-9, "frame \(i)")
|
|
}
|
|
}
|
|
|
|
/// Parity vs. a manual per-sample `_result[0][i] = sum_ch input[ch][i]`
|
|
/// reference computed from the same fixture rows.
|
|
@Test func parityVsReferenceFold() {
|
|
let rows: [[Double]] = [
|
|
(0..<engine.blockSize).map { Double($0) * 0.01 },
|
|
(0..<engine.blockSize).map { Double($0) * 0.02 - 0.5 },
|
|
(0..<engine.blockSize).map { sin(Double($0) * 0.1) }
|
|
]
|
|
let gens: [SumChannelsRowFixtureGen] = rows.map { SumChannelsRowFixtureGen($0) }
|
|
let multi: GeneratorArray = GeneratorArray(gens)
|
|
let sum: SumChannels.Audio = SumChannels.Audio(input: multi)
|
|
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = sum.calculatedSamples(graph: graph, count: engine.blockSize)
|
|
|
|
for i: Int in 0..<engine.blockSize {
|
|
let expected: Double = rows[0][i] + rows[1][i] + rows[2][i]
|
|
#expect(abs(out[0][i] - expected) < 1e-9,
|
|
"frame \(i): expected \(expected), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
@Test func outputChannelCountAlwaysOne() {
|
|
let scalar: SumChannels.Audio = SumChannels.Audio(input: 0.0)
|
|
#expect(scalar.outputChannelCount == 1)
|
|
let mono: SumChannels.Audio = SumChannels.Audio(input: SumChannelsFixtureGen(0.0))
|
|
#expect(mono.outputChannelCount == 1)
|
|
let stereo: SumChannels.Audio = SumChannels.Audio(input: GeneratorArray([
|
|
SumChannelsFixtureGen(0.0), SumChannelsFixtureGen(0.0)
|
|
]))
|
|
#expect(stereo.outputChannelCount == 1)
|
|
}
|
|
}
|
|
|
|
// Test-only mono generator fixture. Allocation in `_computeSamples` is
|
|
// acceptable for test fixtures only.
|
|
private final class SumChannelsFixtureGen: GeneratorBase, AudioGenerator {
|
|
let value: Double
|
|
init(_ v: Double) { value = v }
|
|
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
|
_checkCount(count)
|
|
return [[Double](repeating: value, count: count)]
|
|
}
|
|
func floatValue(graph: SynthGraph) -> Double { value }
|
|
}
|
|
|
|
// Test-only mono generator that emits a caller-supplied row.
|
|
private final class SumChannelsRowFixtureGen: GeneratorBase, AudioGenerator {
|
|
let row: [Double]
|
|
init(_ r: [Double]) { row = r }
|
|
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
|
_checkCount(count)
|
|
return [Array(row.prefix(count))]
|
|
}
|
|
func floatValue(graph: SynthGraph) -> Double { row.first ?? 0.0 }
|
|
}
|