b6fdee8bfc
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
157 lines
6.2 KiB
Swift
157 lines
6.2 KiB
Swift
// ALSAAudioDriverTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Linux-only: `ALSAAudioDriver` exists solely where libasound does. These tests
|
|
// run headless — a CI container has no sound device — so they cover the paths
|
|
// that must behave correctly *without* hardware: refusing to start unconfigured,
|
|
// failing cleanly on a device that cannot be opened, and surviving redundant
|
|
// stops. Their other job is to prove the driver was actually compiled into the
|
|
// module: if the CALSA dependency ever stops resolving, the whole file compiles
|
|
// away to nothing and everything else would still pass.
|
|
//
|
|
// Guarded on `canImport(CALSA)` rather than `os(Linux)` so the file tracks the
|
|
// driver's own condition exactly: a Linux build with `LYREBIRD_DISABLE_ALSA=1`
|
|
// has no driver, and an `os(Linux)` guard would leave these tests referencing a
|
|
// type that is not there.
|
|
#if canImport(CALSA)
|
|
|
|
import Testing
|
|
@_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("ALSAAudioDriver (headless)")
|
|
struct ALSAAudioDriverTests {
|
|
|
|
/// A name no ALSA configuration will resolve, so `snd_pcm_open` fails on
|
|
/// any machine — with or without a sound card.
|
|
private static let unopenableDevice: String = "lyrebird-nonexistent-pcm"
|
|
|
|
@Test func startWithoutSetupThrowsNotConfigured() {
|
|
let driver: ALSAAudioDriver = ALSAAudioDriver()
|
|
#expect(throws: ALSAError.notConfigured) {
|
|
try driver.start()
|
|
}
|
|
}
|
|
|
|
@Test func startOnUnopenableDeviceThrowsAndDoesNotRun() throws {
|
|
let driver: ALSAAudioDriver = ALSAAudioDriver()
|
|
driver.device = Self.unopenableDevice
|
|
let engine: SynthesisEngine = SynthesisEngine(
|
|
blockSize: 64,
|
|
sampleRate: 44100,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 0,
|
|
driver: driver
|
|
)
|
|
try driver.setup(engine: engine)
|
|
|
|
var thrown: ALSAError?
|
|
do {
|
|
try driver.start()
|
|
} catch let error as ALSAError {
|
|
thrown = error
|
|
}
|
|
guard case .playbackDeviceOpenFailed(let device, _)? = thrown else {
|
|
Issue.record("expected playbackDeviceOpenFailed, got \(String(describing: thrown))")
|
|
return
|
|
}
|
|
#expect(device == Self.unopenableDevice)
|
|
#expect(driver.lastError == nil, "no render thread ran, so nothing should have failed inside one")
|
|
}
|
|
|
|
@Test func failedStartLeavesNoWedgedState() throws {
|
|
// A start that throws must not leave the running flag set or a device
|
|
// half-open, or every later start would fail for the wrong reason.
|
|
let driver: ALSAAudioDriver = ALSAAudioDriver()
|
|
driver.device = Self.unopenableDevice
|
|
let engine: SynthesisEngine = SynthesisEngine(
|
|
blockSize: 64,
|
|
sampleRate: 44100,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 0,
|
|
driver: driver
|
|
)
|
|
try driver.setup(engine: engine)
|
|
|
|
for attempt: Int in 0..<3 {
|
|
var thrown: ALSAError?
|
|
do {
|
|
try driver.start()
|
|
} catch let error as ALSAError {
|
|
thrown = error
|
|
}
|
|
guard case .playbackDeviceOpenFailed? = thrown else {
|
|
Issue.record("attempt \(attempt): expected playbackDeviceOpenFailed, got \(String(describing: thrown))")
|
|
return
|
|
}
|
|
driver.stop()
|
|
}
|
|
}
|
|
|
|
@Test func engineRetainRoundTripBalances() throws {
|
|
// The render thread pins the engine with a retain handed over as a bit
|
|
// pattern, released by the loop's `defer`. Nothing hardware is involved
|
|
// in that balance, so pin it here rather than leaving it to run only on
|
|
// a real device: a missing release leaks the engine, a double release
|
|
// is a use-after-free.
|
|
weak var weakEngine: SynthesisEngine?
|
|
var bits: UInt = 0
|
|
do {
|
|
let engine: SynthesisEngine = SynthesisEngine(
|
|
blockSize: 64,
|
|
sampleRate: 44100,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 0,
|
|
driver: OfflineDriver()
|
|
)
|
|
weakEngine = engine
|
|
bits = ALSAAudioDriver.retainForRenderThread(Unmanaged.passUnretained(engine))
|
|
}
|
|
// The local strong reference is gone, but the render-thread retain is
|
|
// still holding the engine alive.
|
|
#expect(weakEngine != nil, "retainForRenderThread must keep the engine alive")
|
|
|
|
ALSAAudioDriver.releaseEngine(bits: bits)
|
|
#expect(weakEngine == nil, "releaseEngine must balance the retain exactly")
|
|
}
|
|
|
|
@Test func stallBudgetGivesUpOnlyAfterTheLimit() {
|
|
// The loop that consults this only runs against real hardware, so the
|
|
// boundary is pinned here: one attempt early kills a stream that was
|
|
// merely slow, one late means a wedged device spins forever at
|
|
// SCHED_FIFO.
|
|
#expect(ALSAAudioDriver.stallBudgetExhausted(consecutiveStalls: 1) == false)
|
|
#expect(ALSAAudioDriver.stallBudgetExhausted(
|
|
consecutiveStalls: ALSAAudioDriver.maxConsecutiveStalls) == false)
|
|
#expect(ALSAAudioDriver.stallBudgetExhausted(
|
|
consecutiveStalls: ALSAAudioDriver.maxConsecutiveStalls + 1) == true)
|
|
}
|
|
|
|
@Test func stopIsSafeBeforeStartAndWhenRepeated() {
|
|
let driver: ALSAAudioDriver = ALSAAudioDriver()
|
|
driver.stop()
|
|
driver.stop()
|
|
}
|
|
|
|
@Test func defaultsTargetTheUserConfiguredDevice() {
|
|
let driver: ALSAAudioDriver = ALSAAudioDriver()
|
|
// "default" honours ~/.asoundrc and dmix; addressing hw:N,M directly
|
|
// would lock out every other application on the machine.
|
|
#expect(driver.device == "default")
|
|
#expect(driver.captureDevice == "default")
|
|
#expect(driver.allowSoftwareResampling == false)
|
|
#expect(driver.linkedStreams == false)
|
|
#expect(driver.hasRealtimePriority == false)
|
|
}
|
|
|
|
@Test func suggestsTheSameControlBlockSizeAsOtherLiveDrivers() {
|
|
let driver: ALSAAudioDriver = ALSAAudioDriver()
|
|
#expect(driver.suggestedControlBlockSize == 64)
|
|
}
|
|
}
|
|
|
|
#endif
|