Files
Lyrebird/Sources/Lyrebird/AudioDrivers/ALSARecoveryAction.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
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>
2026-09-10 21:09:11 -07:00

69 lines
3.0 KiB
Swift

// ALSARecoveryAction.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// What an ALSA PCM transfer's return value tells the render loop to do next.
///
/// `snd_pcm_writei` / `snd_pcm_readi` return a frame count on success and a
/// negative errno on failure, and the correct response differs per error: an
/// underrun is routine and recoverable, a signal interruption just needs the
/// same call again, and anything else means the device is gone. Getting this
/// wrong is how a driver either spins forever or dies on the first xrun. The
/// two transient cases are kept apart deliberately: a signal is not a device
/// problem and must not consume the patience budget that a device which never
/// becomes ready does.
///
/// The classification is pure arithmetic over the return value, so it is
/// tested on every platform even though the driver that consumes it
/// (`ALSAAudioDriver`) only compiles on Linux.
public enum ALSARecoveryAction: Equatable, Sendable {
/// Transfer succeeded. The associated value is the number of frames moved,
/// which may be fewer than requested — the caller must resume from there.
case transferred(frames: Int)
/// A signal interrupted the call (`-EINTR`). Reissue immediately: nothing
/// is wrong with the device, and a profiler or interval timer can deliver
/// signals indefinitely without the stream being unhealthy.
case interrupted
/// The device is not ready (`-EAGAIN`). Wait for it, and give up if it
/// never becomes ready — this one *is* evidence of a sick stream.
case wouldBlock
/// Underrun/overrun (`-EPIPE`) or suspend (`-ESTRPIPE`). Call
/// `snd_pcm_recover` (or `snd_pcm_prepare`), then reissue.
case recover
/// Unrecoverable. Surface the error and stop the render thread.
case fail
// Linux errno values, spelled out rather than imported. ALSA is a Linux
// API, so these are the numbers it returns wherever this code is compiled;
// taking them from the host's `errno.h` instead would make the same input
// classify differently on a non-Linux host (`EAGAIN` is 11 on Linux and 35
// on Darwin), which would make the tests lie.
private static let eintr: Int = 4
private static let eagain: Int = 11
private static let epipe: Int = 32
private static let estrpipe: Int = 86
/// Classifies the return value of an ALSA PCM transfer call.
///
/// - Parameter result: Value returned by `snd_pcm_writei` / `snd_pcm_readi`
/// — frames transferred when non-negative, negative errno otherwise.
/// - Returns: The action the render loop should take.
public static func classify(_ result: Int) -> ALSARecoveryAction {
if result >= 0 {
return .transferred(frames: result)
}
switch -result {
case eintr:
return .interrupted
case eagain:
return .wouldBlock
case epipe, estrpipe:
return .recover
default:
return .fail
}
}
}