diff --git a/crates/qbz-slint/src/dac_wizard.rs b/crates/qbz-slint/src/dac_wizard.rs index d85b4678f..6a465552e 100644 --- a/crates/qbz-slint/src/dac_wizard.rs +++ b/crates/qbz-slint/src/dac_wizard.rs @@ -459,3 +459,114 @@ mod slice7_tests { assert_eq!(format_rates(&[]), ""); } } + +// ── Slice 9: self-service playback test (N6 read-back) ───────────────────── + +/// One curated test track (owner-provided). Resolved by id-hint first, then by +/// "artist title" search if the id 404s (a pulled license) — never raw-id-only. +pub struct TestSeed { + pub depth: u32, + pub rate: f64, + pub id_hint: u64, + pub artist: &'static str, + pub title: &'static str, +} + +pub const TEST_SEEDS: [TestSeed; 4] = [ + TestSeed { depth: 16, rate: 44100.0, id_hint: 19301386, artist: "George Harrison", title: "My Sweet Lord" }, + TestSeed { depth: 24, rate: 44100.0, id_hint: 266725027, artist: "Billie Eilish", title: "LUNCH" }, + TestSeed { depth: 24, rate: 96000.0, id_hint: 126886854, artist: "Iron Maiden", title: "Stratego" }, + TestSeed { depth: 24, rate: 192000.0, id_hint: 52265, artist: "Toto", title: "Africa" }, +]; + +/// The DAC node.name the test is currently watching (N6 probe target). +static TEST_NODE: Mutex> = Mutex::new(None); + +/// True if a resolved track matches this seed's family (rate + bit depth — the +/// two 44.1 seeds only differ by depth). +pub fn track_matches_seed(track: &qbz_models::Track, seed: &TestSeed) -> bool { + let rate_ok = track + .maximum_sampling_rate + .map(|r| (r * 1000.0 - seed.rate).abs() < 1.0 || (r - seed.rate).abs() < 1.0) + .unwrap_or(false); + let depth_ok = track.maximum_bit_depth.map(|d| d == seed.depth).unwrap_or(false); + rate_ok && depth_ok +} + +/// The node.name of the first checked candidate (the DAC being configured), or +/// the manual node.name from the escape hatch. +pub fn first_selected_node(window: &AppWindow) -> Option { + let st = window.global::(); + let model = st.get_candidates(); + for i in 0..model.row_count() { + if let Some(r) = model.row_data(i) { + if r.checked { + return Some(r.id.to_string()); + } + } + } + let manual = st.get_manual_node_name().to_string(); + if !manual.trim().is_empty() && st.get_manual_valid() { + Some(manual) + } else { + None + } +} + +/// Start the test: stash the watched node + show the "playing" state. +pub fn begin_test(window: &AppWindow, node: Option) { + *TEST_NODE.lock().unwrap() = node; + let st = window.global::(); + st.set_test_playing(true); + st.set_test_rate_matched(false); + st.set_test_requested_label("Starting…".into()); + st.set_test_negotiated_label("".into()); +} + +pub fn end_test(window: &AppWindow) { + window.global::().set_test_playing(false); +} + +/// The node the poll loop should probe (read on a worker thread). +pub fn test_node() -> Option { + TEST_NODE.lock().unwrap().clone() +} + +/// Apply one poll: the rate QBZ requested vs the DAC's real negotiated rate (N6). +pub fn apply_poll( + window: &AppWindow, + requested_rate: u32, + requested_bits: u32, + negotiated: Option, +) { + let st = window.global::(); + st.set_test_requested_label(if requested_rate > 0 { + format!( + "QBZ requesting {} · {}-bit", + khz(requested_rate), + requested_bits + ) + .into() + } else { + "Nothing playing".into() + }); + match negotiated { + Some(n) => { + st.set_test_negotiated_label(format!("DAC running at {}", khz(n.sample_rate)).into()); + // Truth signal (N6): the DAC's real clock matches what QBZ asked for. + st.set_test_rate_matched(requested_rate > 0 && n.sample_rate == requested_rate); + } + None => { + st.set_test_negotiated_label("DAC idle — set QBZ output to this DAC".into()); + st.set_test_rate_matched(false); + } + } +} + +fn khz(hz: u32) -> String { + if hz % 1000 == 0 { + format!("{} kHz", hz / 1000) + } else { + format!("{:.1} kHz", hz as f64 / 1000.0) + } +} diff --git a/crates/qbz-slint/src/main.rs b/crates/qbz-slint/src/main.rs index 6fa4a7ee1..b5b8da1a2 100644 --- a/crates/qbz-slint/src/main.rs +++ b/crates/qbz-slint/src/main.rs @@ -11382,6 +11382,90 @@ fn main() -> Result<(), Box> { } }); } + { + // Self-service playback test (Slice 9): resolve the 4 curated tracks + // (id-hint then "artist title" search), route output to the DAC under + // test, and play them. The N6 read-back is driven by on_poll_test. + let runtime = app_runtime.clone(); + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window.global::().on_start_test(move || { + let Some(w) = weak.upgrade() else { + return; + }; + let node = dac_wizard::first_selected_node(&w); + dac_wizard::begin_test(&w, node.clone()); + let runtime = runtime.clone(); + let weak2 = w.as_weak(); + let play_handle = handle.clone(); + handle.spawn(async move { + let mut tracks: Vec = Vec::new(); + for seed in dac_wizard::TEST_SEEDS.iter() { + let mut chosen = match runtime.core().get_track(seed.id_hint).await { + Ok(t) if dac_wizard::track_matches_seed(&t, seed) => Some(t), + _ => None, + }; + if chosen.is_none() { + let q = format!("{} {}", seed.artist, seed.title); + if let Ok(page) = runtime.core().search_tracks(&q, 10, 0, None).await { + chosen = page + .items + .into_iter() + .find(|t| dac_wizard::track_matches_seed(t, seed)); + } + } + if let Some(t) = chosen { + tracks.push(t); + } + } + if let Some(n) = node.clone() { + let _ = runtime.core().player().reinit_device(Some(n)); + } + let runtime2 = runtime.clone(); + let _ = weak2.upgrade_in_event_loop(move |w| { + if tracks.is_empty() { + w.global::() + .set_test_requested_label("Couldn't load the test tracks (offline?)".into()); + return; + } + crate::playback::play_tracks(runtime2, w.as_weak(), play_handle, tracks, 0); + }); + }); + }); + } + { + // Poll the requested vs negotiated rate while the test plays. + let runtime = app_runtime.clone(); + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window.global::().on_poll_test(move || { + if weak.upgrade().is_none() { + return; + } + let runtime = runtime.clone(); + let weak2 = weak.clone(); + handle.spawn_blocking(move || { + let player = runtime.core().player(); + let req_rate = player.get_sample_rate(); + let req_bits = player.get_bit_depth(); + let negotiated = dac_wizard::test_node() + .and_then(|n| qbz_audio::negotiated_stream_rate(&n)); + let _ = weak2.upgrade_in_event_loop(move |w| { + dac_wizard::apply_poll(&w, req_rate, req_bits, negotiated); + }); + }); + }); + } + { + let runtime = app_runtime.clone(); + let weak = window.as_weak(); + window.global::().on_stop_test(move || { + let _ = runtime.core().pause(); + if let Some(w) = weak.upgrade() { + dac_wizard::end_test(&w); + } + }); + } { window .global::() diff --git a/crates/qbz-slint/ui/primitives/DacWizardModal.slint b/crates/qbz-slint/ui/primitives/DacWizardModal.slint index 4986bc8e6..cd682931e 100644 --- a/crates/qbz-slint/ui/primitives/DacWizardModal.slint +++ b/crates/qbz-slint/ui/primitives/DacWizardModal.slint @@ -49,6 +49,14 @@ export component DacWizardModal inherits Rectangle { // enumeration found devices; forced open when it found none. property show-manual: false; + // The four owner-curated test tracks (display only; resolution is Rust-side). + property <[string]> test-tracks: [ + @tr("16-bit / 44.1 kHz — George Harrison · My Sweet Lord"), + @tr("24-bit / 44.1 kHz — Billie Eilish · LUNCH"), + @tr("24-bit / 96 kHz — Iron Maiden · Stratego"), + @tr("24-bit / 192 kHz — Toto · Africa"), + ]; + // Step labels for the header rail (index = DacWizardState.step). property <[string]> steps: [ @tr("Welcome"), @@ -455,9 +463,97 @@ export component DacWizardModal inherits Rectangle { title: @tr("Review & Apply"); note: @tr("Backup, per-DAC sample rates, and the copy-paste config files (Slice 10)."); } - if DacWizardState.step == 4: StepPlaceholder { - title: @tr("Test Playback"); - note: @tr("Plays tracks at 16/44.1, 24/44.1, 24/96 and 24/192 and reads the DAC's real negotiated rate (Slice 9)."); + // ── Step 4: self-service playback test ─────────── + if DacWizardState.step == 4: VerticalLayout { + spacing: 14px; + Text { + text: @tr("Test Playback"); + color: Theme.text-primary; + font-size: Typography.heading; + font-weight: Typography.semibold; + } + Text { + text: @tr("Plays four tracks at different bit-depths and sample rates through the selected DAC. Watch your DAC's display switch rates — and confirm it below."); + color: Theme.text-secondary; + font-size: Typography.body; + wrap: word-wrap; + } + // The four tracks that will play. + VerticalLayout { + spacing: 6px; + for line in root.test-tracks: HorizontalLayout { + spacing: 8px; + alignment: start; + Text { + text: "♪"; + color: Theme.text-muted; + font-size: Typography.legal; + } + Text { + text: line; + color: Theme.text-secondary; + font-size: Typography.legal; + overflow: elide; + } + } + } + // Play / Stop. + HorizontalLayout { + alignment: start; + QbzPrimaryButton { + height: 34px; + label: DacWizardState.test-playing ? @tr("Stop") : @tr("Play test"); + clicked => { + if (DacWizardState.test-playing) { + DacWizardActions.stop-test(); + } else { + DacWizardActions.start-test(); + } + } + } + } + // Live read-back: requested (honest) + DAC negotiated (N6). + if DacWizardState.test-playing || DacWizardState.test-requested-label != "": Rectangle { + border-radius: Radius.sm; + background: Theme.surface-elevated; + border-width: 1px; + border-color: Theme.border-subtle; + height: readout.preferred-height; + readout := VerticalLayout { + padding: 12px; + spacing: 6px; + Text { + text: DacWizardState.test-requested-label; + color: Theme.text-primary; + font-size: Typography.body; + font-weight: Typography.medium; + } + HorizontalLayout { + spacing: 8px; + alignment: start; + if DacWizardState.test-rate-matched: Text { + text: "✓"; + color: #3fae6a; + font-size: Typography.body; + font-weight: Typography.bold; + } + Text { + text: DacWizardState.test-negotiated-label; + color: DacWizardState.test-rate-matched ? #3fae6a : Theme.text-secondary; + font-size: Typography.body; + vertical-alignment: center; + } + } + } + } + // Drives the read-back while the test plays. + Timer { + interval: 1.5s; + running: DacWizardState.test-playing; + triggered => { + DacWizardActions.poll-test(); + } + } } if DacWizardState.step == 5: StepPlaceholder { title: @tr("All set"); diff --git a/crates/qbz-slint/ui/state.slint b/crates/qbz-slint/ui/state.slint index 004aad920..e4731cd3b 100644 --- a/crates/qbz-slint/ui/state.slint +++ b/crates/qbz-slint/ui/state.slint @@ -3925,5 +3925,6 @@ export global DacWizardActions { callback toggle-dac(int); // flip one candidate's checked state callback validate-manual(string);// validate a pasted node.name (escape hatch) callback start-test(); // queue + play the 4 test tracks (Slice 9) + callback poll-test(); // refresh the requested-vs-negotiated read-back callback stop-test(); }