Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/SplitterTests.swift
T
2026-06-21 12:25:18 -07:00

189 lines
7.6 KiB
Swift

// SplitterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("Splitter")
struct SplitterTests {
// MARK: - Helpers
/// Number of audio buses allocated in the test engine.
private let numAudioBuses: Int = 64
/// Render duration in seconds.
private let renderDuration: Double = 0.2
// MARK: - Non-silent output
@Test func splitterPassesThroughBothBranchesByDefault() throws {
// Both branches enabled at 1.0 — both output buses should carry signal.
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busA: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busB: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(busA)
engine.busAllocator.releaseAudioBus(busB)
}
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
let splitter: Splitter = Splitter(
inputBus: srcBus.index,
outputBusA: busA.index,
outputBusB: busB.index
)
// Route bus A to the hardware output.
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: busA.index)
try engine.add(source)
try engine.add(splitter, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(splitter))
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
#expect(!samples.isEmpty, "Splitter produced no samples")
#expect(hasNonSilentOutput(samples), "Splitter branch A produced silence with enable=1")
}
// MARK: - Branch muting
@Test func branchBDisabledMutesBranchBBus() throws {
// Wire only bus B to the output. Set branchBEnabled = 0. Expect silence.
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busA: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busB: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(busA)
engine.busAllocator.releaseAudioBus(busB)
}
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
let splitter: Splitter = Splitter(
inputBus: srcBus.index,
outputBusA: busA.index,
outputBusB: busB.index
)
// Route only bus B to the hardware output — branch A is ignored here.
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: busB.index)
// Mute branch B before adding.
splitter.branchBEnabled = 0.0
try engine.add(source)
try engine.add(splitter, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(splitter))
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
// Allow for the 20 ms lag: silence expectation applies once the lag settles.
// Because branchBEnabled is set to 0 before the splitter is added, the very
// first block already ramps toward 0 — the overall render should be silent or
// very nearly so. Use a tight threshold to allow for the lag ramp.
let lagSettleSamples: Int = 2048
guard samples.count > lagSettleSamples else { return }
let settled: [Float] = Array(samples[lagSettleSamples...])
#expect(!hasNonSilentOutput(settled, threshold: 1e-4), "Branch B should be muted")
}
@Test func branchADisabledMutesBranchABus() throws {
// Wire only bus A to the output. Set branchAEnabled = 0. Expect silence.
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busA: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busB: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(busA)
engine.busAllocator.releaseAudioBus(busB)
}
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
let splitter: Splitter = Splitter(
inputBus: srcBus.index,
outputBusA: busA.index,
outputBusB: busB.index
)
// Route only bus A to the hardware output.
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: busA.index)
// Mute branch A before adding.
splitter.branchAEnabled = 0.0
try engine.add(source)
try engine.add(splitter, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(splitter))
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
let lagSettleSamples: Int = 2048
guard samples.count > lagSettleSamples else { return }
let settled: [Float] = Array(samples[lagSettleSamples...])
#expect(!hasNonSilentOutput(settled, threshold: 1e-4), "Branch A should be muted")
}
// MARK: - Mode convenience
@Test func modeSetterConfiguresBranchEnables() throws {
// Test the mode convenience setter in isolation (no engine needed).
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.01)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busA: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let busB: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(busA)
engine.busAllocator.releaseAudioBus(busB)
}
let splitter: Splitter = Splitter(
inputBus: srcBus.index,
outputBusA: busA.index,
outputBusB: busB.index
)
splitter.mode = .a
#expect(splitter.branchAEnabled == 1.0, "mode .a should set branchAEnabled=1")
#expect(splitter.branchBEnabled == 0.0, "mode .a should set branchBEnabled=0")
#expect(splitter.mode == .a)
splitter.mode = .b
#expect(splitter.branchAEnabled == 0.0, "mode .b should set branchAEnabled=0")
#expect(splitter.branchBEnabled == 1.0, "mode .b should set branchBEnabled=1")
#expect(splitter.mode == .b)
splitter.mode = .both
#expect(splitter.branchAEnabled == 1.0, "mode .both should set branchAEnabled=1")
#expect(splitter.branchBEnabled == 1.0, "mode .both should set branchBEnabled=1")
#expect(splitter.mode == .both)
}
}