From 6b2ef2f66a01f885d3d4ec794090e9c11c2b3a1d Mon Sep 17 00:00:00 2001 From: Devan <68340659+DevanMetz@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:36:42 -0500 Subject: [PATCH] Fix ALSA monitor defaults and filter silent devices --- PARITY.md | 2 +- src/engine.rs | 89 ++++++++++++++++++++++++++++++++++++++++------- src/gui/routes.rs | 6 ++++ 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/PARITY.md b/PARITY.md index c61085f..2757764 100644 --- a/PARITY.md +++ b/PARITY.md @@ -6,7 +6,7 @@ Status of each Windows user-facing feature on Linux in this tree: `working`, `pa |---|---|---| | DeepFilterNet 3 and RNNoise, voice gate, rumble filter, gains, level gate, bypass, mute, presets | working | `src/denoise.rs`, `src/dsp.rs`, `src/gui/mic_tab.rs` | | Live scope and the voice rows | working | `src/gui/mic_tab.rs`, `src/viz.rs` | -| Microphone, processed output, and headphone monitor routing, with live changes and reconnect | working | `src/engine.rs`, `src/gui/routes.rs` | +| Microphone, processed output, and headphone monitor routing, with live changes and reconnect | working | `src/engine.rs`, `src/gui/routes.rs` — ALSA pickers exclude silent null PCMs and prefer default/PipeWire/system routes before hardware | | Devices opened at their native format and resampled to the 48 kHz DSP core | working | `src/engine.rs`, `src/resample.rs` | | 10 ms PulseAudio buffers and clock-drift correction | working | `src/engine.rs` | | Real-time priority for audio threads | working | `src/rt.rs`, `src/engine.rs` — requests RealtimeKit priority and logs refusals | diff --git a/src/engine.rs b/src/engine.rs index b97eb27..1448bbc 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -79,8 +79,8 @@ pub struct StatsSnapshot { /// One device as the host reports it. `id` is the backend id (`DeviceId::id`), /// empty when the host cannot provide one. PulseAudio monitor sources use -/// `{sink}.monitor` here; their display names are translated. Only the -/// non-Windows monitor matching reads it, so Windows builds do not carry it. +/// `{sink}.monitor` here; their display names are translated. Linux also +/// uses it to rank ALSA routes. Windows builds do not carry it. pub struct ListedDevice { pub name: String, #[cfg(not(windows))] @@ -98,11 +98,11 @@ pub fn list_device_details(host: &Host, output: bool) -> Vec { if let Ok(devices) = iter { for device in devices { #[cfg(target_os = "linux")] - if !output && host.id() == cpal::HostId::Alsa { + if host.id() == cpal::HostId::Alsa { let id = device.id().map(|id| id.id().to_owned()).unwrap_or_default(); let description = device.description().ok(); let name = device.to_string(); - if is_noop_alsa_input( + if is_noop_alsa_device( &id, std::iter::once(name.as_str()).chain( description @@ -120,34 +120,97 @@ pub fn list_device_details(host: &Host, output: bool) -> Vec { }); } } + #[cfg(target_os = "linux")] + if host.id() == cpal::HostId::Alsa { + // Stable ordering keeps the backend's order within each preference. + listed.sort_by_key(|device| alsa_device_rank(&device.id)); + } listed } +/// Prefer configured routing over direct hardware, then other ALSA plugins. +/// CPAL's ALSA default device is the PCM named `default`. +#[cfg(target_os = "linux")] +fn alsa_device_rank(id: &str) -> u8 { + match id.split(':').next().unwrap_or(id).trim() { + "default" => 0, + "pipewire" => 1, + "sysdefault" => 2, + "plughw" => 3, + "hw" => 4, + _ => 5, + } +} + /// ALSA's null PCM can be exposed under an alias, so inspect its description too. #[cfg(target_os = "linux")] -fn is_noop_alsa_input<'a>(id: &str, descriptions: impl IntoIterator) -> bool { - id.trim().eq_ignore_ascii_case("null") +pub(crate) fn is_noop_alsa_device<'a>( + id: &str, + descriptions: impl IntoIterator, +) -> bool { + id.split(':') + .next() + .unwrap_or(id) + .trim() + .eq_ignore_ascii_case("null") || descriptions.into_iter().any(|description| { let text = description.trim().to_ascii_lowercase(); text == "null" - || (text.contains("discard all samples") && text.contains("generate zero samples")) + || text.contains("discard all samples") + || text.contains("generate zero samples") }) } #[cfg(all(test, target_os = "linux"))] #[test] -fn filters_alsa_noop_inputs_by_id_or_description() { - assert!(is_noop_alsa_input("null", ["Anything"])); - assert!(is_noop_alsa_input("alias", ["Null"])); - assert!(is_noop_alsa_input( +fn filters_alsa_noop_devices_by_id_or_description() { + assert!(is_noop_alsa_device("null", ["Anything"])); + assert!(is_noop_alsa_device("null:CARD=0", ["Anything"])); + assert!(is_noop_alsa_device( + "alias", + ["Discard all samples (playback)"] + )); + assert!(is_noop_alsa_device( + "alias", + ["Generate zero samples (capture)"] + )); + assert!(is_noop_alsa_device("alias", ["Null"])); + assert!(is_noop_alsa_device( "alias", [ "Dummy", "Discard all samples (playback) or generate zero samples (capture)" ] )); - assert!(!is_noop_alsa_input("hw:0", ["USB microphone"])); - assert!(!is_noop_alsa_input("hw:1", ["Null Audio USB microphone"])); + assert!(!is_noop_alsa_device("hw:0", ["USB microphone"])); + assert!(!is_noop_alsa_device("hw:1", ["Null Audio USB microphone"])); +} + +#[cfg(all(test, target_os = "linux"))] +#[test] +fn alsa_defaults_precede_hardware_and_other_plugins() { + let mut ids = [ + "rate", + "hw:0", + "plughw:1", + "sysdefault:CARD=USB", + "pipewire", + "default", + "hw:1", + ]; + ids.sort_by_key(|id| alsa_device_rank(id)); + assert_eq!( + ids, + [ + "default", + "pipewire", + "sysdefault:CARD=USB", + "plughw:1", + "hw:0", + "hw:1", + "rate" + ] + ); } /// Deduped friendly names for a direction. The first device keeps the name. diff --git a/src/gui/routes.rs b/src/gui/routes.rs index e582380..37668e3 100644 --- a/src/gui/routes.rs +++ b/src/gui/routes.rs @@ -89,6 +89,12 @@ pub fn choose_routes_with_monitors( monitors: &[String], ) { let needs = |current: &String, pool: &[String]| { + #[cfg(target_os = "linux")] + if crate::engine::is_noop_alsa_device(current, [current.as_str()]) + && cpal::default_host().id() == cpal::HostId::Alsa + { + return true; + } current.is_empty() || (replace_missing && !pool.contains(current)) }; if needs(&s.microphone, inputs) {