feat(audio): headless query_dac_capabilities for the Slint wizard (Slice 2)
Extract the DAC capability assembly out of the Tauri-only `v2_query_dac_capabilities` into `qbz-audio::dac_capabilities` so the Slint wizard can probe a DAC's real supported sample rates + pretty description without a Tauri command. The detection itself already lived in this crate (`PipeWireBackend::get_sink_supported_rates` -> /proc/asound, with the `get_device_supported_rates` ALSA-direct fallback); this just assembles the DTO frontend-agnostically and benefits from the Slice-0 pw-dump-robust enumeration for the description. Formats stay nominal (S16/S24/F32) and are documented as NOT probed — honest port of the prior behavior, not a fake measurement. Pure `assemble` helper + 3 unit tests. Read-only. cc
This commit is contained in:
2 files changed
+107
No files matched your search
@@ -0,0 +1,105 @@
|
||||
//! Headless DAC capability probe (HiFi wizard Slice 2).
|
||||
//!
|
||||
//! Mirrors the old Tauri `v2_query_dac_capabilities` so the Slint wizard can
|
||||
//! query a DAC's real supported sample rates + pretty description WITHOUT a
|
||||
//! Tauri command. The actual detection already lived in this crate
|
||||
//! (`PipeWireBackend::get_sink_supported_rates`, `get_device_supported_rates`);
|
||||
//! this module just assembles the DTO frontend-agnostically. Read-only.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// DAC capability descriptor surfaced to the wizard.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DacCapabilities {
|
||||
pub node_name: String,
|
||||
pub sample_rates: Vec<u32>,
|
||||
pub formats: Vec<String>,
|
||||
pub channels: Option<u32>,
|
||||
pub description: Option<String>,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// The common rate set used when real detection fails (continuous-range DACs,
|
||||
/// non-USB devices, missing `/proc/asound` stream files).
|
||||
const FALLBACK_RATES: [u32; 6] = [44100, 48000, 88200, 96000, 176400, 192000];
|
||||
|
||||
/// Nominal format list. NOTE: these are NOT probed — ALSA does not expose a
|
||||
/// clean per-node format capability here, so we report the common set honestly
|
||||
/// as nominal (matches the prior Tauri behavior; do not present it as measured).
|
||||
fn nominal_formats() -> Vec<String> {
|
||||
vec!["S16LE".to_string(), "S24LE".to_string(), "F32LE".to_string()]
|
||||
}
|
||||
|
||||
/// Pure assembly (testable): combine detected rates + description into the DTO,
|
||||
/// falling back to the common rate set when detection yielded nothing.
|
||||
fn assemble(
|
||||
node_name: &str,
|
||||
detected_rates: Option<Vec<u32>>,
|
||||
description: Option<String>,
|
||||
) -> DacCapabilities {
|
||||
let sample_rates = detected_rates
|
||||
.filter(|r| !r.is_empty())
|
||||
.unwrap_or_else(|| FALLBACK_RATES.to_vec());
|
||||
DacCapabilities {
|
||||
node_name: node_name.to_string(),
|
||||
sample_rates,
|
||||
formats: nominal_formats(),
|
||||
channels: Some(2),
|
||||
description,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Detect a DAC's real supported sample rates + pretty description, headlessly.
|
||||
/// Read-only — reads `/proc/asound` and runs `pw-dump`; never opens a stream.
|
||||
pub fn query_dac_capabilities(node_name: &str) -> DacCapabilities {
|
||||
// Pretty description from robust (pw-dump-backed, Slice 0) enumeration.
|
||||
let description = crate::backend::BackendManager::create_backend(
|
||||
crate::backend::AudioBackendType::PipeWire,
|
||||
)
|
||||
.ok()
|
||||
.and_then(|b| b.enumerate_devices().ok())
|
||||
.and_then(|devs| {
|
||||
devs.into_iter()
|
||||
.find(|d| d.id == node_name || d.name == node_name)
|
||||
})
|
||||
.map(|d| d.description.unwrap_or(d.name));
|
||||
|
||||
// Real sample rates: PipeWire sink -> ALSA card -> /proc/asound, with an
|
||||
// ALSA-direct fallback. Hardware-only; non-Linux gets the fallback set.
|
||||
#[cfg(target_os = "linux")]
|
||||
let detected = crate::pipewire_backend::PipeWireBackend::get_sink_supported_rates(node_name)
|
||||
.or_else(|| crate::alsa_backend::get_device_supported_rates(node_name));
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let detected: Option<Vec<u32>> = None;
|
||||
|
||||
assemble(node_name, detected, description)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn uses_detected_rates_when_present() {
|
||||
let caps = assemble("alsa_output.usb-x", Some(vec![44100, 96000, 192000]), Some("My DAC".into()));
|
||||
assert_eq!(caps.sample_rates, vec![44100, 96000, 192000]);
|
||||
assert_eq!(caps.description.as_deref(), Some("My DAC"));
|
||||
assert_eq!(caps.channels, Some(2));
|
||||
assert!(caps.error.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn falls_back_when_detection_empty_or_missing() {
|
||||
let none = assemble("x", None, None);
|
||||
assert_eq!(none.sample_rates, FALLBACK_RATES.to_vec());
|
||||
let empty = assemble("x", Some(vec![]), None);
|
||||
assert_eq!(empty.sample_rates, FALLBACK_RATES.to_vec());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_are_nominal_not_empty() {
|
||||
let caps = assemble("x", None, None);
|
||||
assert!(caps.formats.contains(&"S24LE".to_string()));
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ pub mod pulse_backend;
|
||||
pub mod jack_backend;
|
||||
pub mod alsa_direct;
|
||||
pub mod coreaudio_direct;
|
||||
pub mod dac_capabilities;
|
||||
pub mod dac_probe;
|
||||
pub mod analysis;
|
||||
pub mod analyzer_tap;
|
||||
@@ -70,6 +71,7 @@ pub use backend::{
|
||||
BackendManager, BackendResult, BitPerfectMode,
|
||||
};
|
||||
pub use coreaudio_direct::CoreAudioExclusiveGuard;
|
||||
pub use dac_capabilities::{query_dac_capabilities, DacCapabilities};
|
||||
pub use dac_probe::{negotiated_stream_rate, NegotiatedRate};
|
||||
pub use device_reservation::{DeviceReservation, ReservationError};
|
||||
pub use diagnostic::{AudioDiagnostic, BitDepthResult, DiagnosticSource};
|
||||
|
||||
Reference in new issue
Block a user