[PR #5102] [MERGED] Audio: list output devices only when the ALSA cards or Core Audio devices change #6072

Closed
opened 2026-11-03 06:11:25 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/5102
Author: @hauserdew-ui
Created: 10/31/2026
Status: ✅ Merged
Merged: 10/31/2026
Merged by: @rdbtCVS

Base: main ← Head: fix/alsa-device-polling


📝 Commits (2)

  • da185c6 Audio: list output devices only when the ALSA cards change
  • 8961611 Audio: list output devices only when the Core Audio devices change

📊 Changes

1 file changed (+121 additions, -23 deletions)

View changed files

📝 crates/horizon-client-render/src/audio.rs (+121 -23)

📄 Description

🤖 Opened by hauserdew-ui's AI agent (opencode, Big Pickle), not by hauserdew-ui personally.

What and why

Audio::should_change_device calls cpal's output_devices() every 20 ticks to notice a device change. Enumerating a list you do not need is expensive on both of the desktop systems where it runs, for different reasons, and in both cases the list can only change when the system's devices change.

Linux. cpal 0.16's ALSA Devices iterator opens a hardcoded list of PCMs (default, pipewire, pulse, jack, oss) for playback and capture. Every plugin this system lacks prints two ALSA errors, so a PipeWire machine without pulse/jack/oss got six Unknown PCM lines per second and a 5–9 ms stall (spiking to ~70 ms) — the log spam and stutter in #5098. /proc/asound/cards (measured ~19 µs) says when to look again.

macOS. cpal checks each Core Audio device's output by opening it (supports_output → supported_configs → AudioComponentInstanceNew, AudioUnitSetProperty, AudioDeviceCreateIOProcID, all round trips to coreaudiod). With 19 devices (12 outputs, mostly virtual: RØDE Unify, Wave Link, Background Music) that takes about 90 ms on the render thread, so the game froze once a second (#5192). Naming every device from host.devices() (inputs included) opens nothing and takes 2–3 ms.

So card_fingerprint() returns the ALSA cards on Linux and the Core Audio device names on macOS; the output list is looked at only when it changes, and the default output — cheap, and what actually plays — is still checked every second, so switching the system output still reopens audio. Audio::open() tries the default output first and enumerates only when it cannot open. Elsewhere (no fingerprint) the list is checked every time, and it is seeded at startup as before.

The macOS branch is @vivescene's work, from their PR #5193, folded in here with their agreement so both platforms are one change; see that PR for their measurements and their own pr_ready run.

Fixes #5098. Fixes #5192.

Measured on the two machines:

Build Linux: Unknown PCM lines macOS: frame times
nightly 9d979ca 102 in ~17 s a 85–120 ms slow frame every second
this branch 0 in 12–15 s 2 in ~40 s, both while the world loaded; 0 in ~1 min of play

Audio still opens as audio output default (44100 Hz, 2 channels, f32), and a unit test covers the fingerprint gate (output_devices_are_looked_at_again_only_when_the_cards_change).

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

None — this is Horizon's own audio backend, not vanilla gameplay. Nothing player-visible changes: the same default output opens, and other devices are still tried when it cannot.

Other projects' code

none

Self-check

**pr_ready** (`tools/pr_ready.py --quick` on localhost, against origin/main at c3905131b, 1 min 18 s)

| Check | Result |
| --- | --- |
| Scope | ✅ 1 files, +121 −23 |
| Cargo.lock | ✅ unchanged |
| Public tree | ✅ |
| Python tests | ✅ 495 passed (37 s) |
| rustfmt | ✅ (6 s) |
| clippy | ✅ (21 s) |
| tests: horizon-client-render | ✅ 708 passed (13 s) |
| doc tests | ✅ 0 passed (1 s) |
| content root tests | ➖ content root untouched |
| tokens.css up to date | ➖ theme untouched |
| launcher rustfmt | ➖ launcher untouched |
| launcher clippy | ➖ launcher untouched |
| launcher tests | ➖ launcher untouched |
| Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses |
| Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) |

This run is the Linux one, so it compiles and tests the macOS branch out. #5193 ran the same checks on macOS against this commit, where the Core Audio branch is the one that compiles.

🤖 Generated with opencode (Big Pickle)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/5102 **Author:** [@hauserdew-ui](https://github.com/hauserdew-ui) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@rdbtCVS](https://github.com/rdbtCVS) **Base:** `main` ← **Head:** `fix/alsa-device-polling` --- ### 📝 Commits (2) - [`da185c6`](https://github.com/horizon-rust/horizon/commit/da185c6128aedfa7309346ff9947a5b553f07441) Audio: list output devices only when the ALSA cards change - [`8961611`](https://github.com/horizon-rust/horizon/commit/8961611cbee2c9164bc525543f4ad74da964ff7e) Audio: list output devices only when the Core Audio devices change ### 📊 Changes **1 file changed** (+121 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/audio.rs` (+121 -23) </details> ### 📄 Description > 🤖 Opened by hauserdew-ui's AI agent (opencode, Big Pickle), not by hauserdew-ui personally. ## What and why `Audio::should_change_device` calls cpal's `output_devices()` every 20 ticks to notice a device change. Enumerating a list you do not need is expensive on both of the desktop systems where it runs, for different reasons, and in both cases the list can only change when the system's devices change. **Linux.** cpal 0.16's ALSA `Devices` iterator opens a hardcoded list of PCMs (`default`, `pipewire`, `pulse`, `jack`, `oss`) for playback and capture. Every plugin this system lacks prints two ALSA errors, so a PipeWire machine without `pulse`/`jack`/`oss` got six `Unknown PCM` lines per second and a 5–9 ms stall (spiking to ~70 ms) — the log spam and stutter in #5098. `/proc/asound/cards` (measured ~19 µs) says when to look again. **macOS.** cpal checks each Core Audio device's output by opening it (`supports_output` → `supported_configs` → `AudioComponentInstanceNew`, `AudioUnitSetProperty`, `AudioDeviceCreateIOProcID`, all round trips to coreaudiod). With 19 devices (12 outputs, mostly virtual: RØDE Unify, Wave Link, Background Music) that takes about 90 ms on the render thread, so the game froze once a second (#5192). Naming every device from `host.devices()` (inputs included) opens nothing and takes 2–3 ms. So `card_fingerprint()` returns the ALSA cards on Linux and the Core Audio device names on macOS; the output list is looked at only when it changes, and the default output — cheap, and what actually plays — is still checked every second, so switching the system output still reopens audio. `Audio::open()` tries the default output first and enumerates only when it cannot open. Elsewhere (no fingerprint) the list is checked every time, and it is seeded at startup as before. The macOS branch is @vivescene's work, from their PR #5193, folded in here with their agreement so both platforms are one change; see that PR for their measurements and their own pr_ready run. `Fixes #5098. Fixes #5192.` Measured on the two machines: | Build | Linux: `Unknown PCM` lines | macOS: frame times | | --- | --- | --- | | nightly `9d979ca` | 102 in ~17 s | a 85–120 ms slow frame every second | | this branch | 0 in 12–15 s | 2 in ~40 s, both while the world loaded; 0 in ~1 min of play | Audio still opens as `audio output default (44100 Hz, 2 channels, f32)`, and a unit test covers the fingerprint gate (`output_devices_are_looked_at_again_only_when_the_cards_change`). ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [x] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference None — this is Horizon's own audio backend, not vanilla gameplay. Nothing player-visible changes: the same default output opens, and other devices are still tried when it cannot. ## Other projects' code none ## Self-check ``` **pr_ready** (`tools/pr_ready.py --quick` on localhost, against origin/main at c3905131b, 1 min 18 s) | Check | Result | | --- | --- | | Scope | ✅ 1 files, +121 −23 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 495 passed (37 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (21 s) | | tests: horizon-client-render | ✅ 708 passed (13 s) | | doc tests | ✅ 0 passed (1 s) | | content root tests | ➖ content root untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | ``` This run is the Linux one, so it compiles and tests the macOS branch out. #5193 ran the same checks on macOS against this commit, where the Core Audio branch is the one that compiles. 🤖 Generated with [opencode](https://opencode.ai) (Big Pickle) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:11:25 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#6072
No description provided.