feat(slint): ImmersiveView base - reusable atmosphere + compact player + audio-transport lift
Foundation for the ImmersiveView Slint port. Authored by Codex and accepted by the owner after live visual iteration; committed here so the panel work builds on top of it. The Album Reactive foreground was intentionally left out (a prior attempt was rejected) so it can be rebuilt cleanly on this base. Audio-transport lift (slice 1): - qbz-audio: VizFrame/VizSink trait + spawn_visualizer_thread; FFT producer lifted off tauri::AppHandle. Protected audio path untouched (seam = RingBuffer.snapshot). - src-tauri: fft_processor gutted to a thin TauriVizSink emitting byte-identical viz:* events. - qbz-app: AppRuntime::with_visualizer + visualizer_tap accessor. - qbz-slint: SlintVizSink + install() publishing VisualizerState bars/spectral/energy/waveform/transient at 30fps, zero IPC. Immersive base (slice 2a): - immersive.rs: host-side atmosphere generation from decoded artwork (8x8 sample -> upscale -> blur -> saturate/contrast/normalize -> vignette), mirroring Tauri texture-loader.ts::generateAtmosphere; plus glow_color. - ImmersiveAtmosphere.slint: reusable animated atmosphere (4 counter-drifting copies via animation-tick); usable static for Album/Artist headers later. - ImmersiveView.slint: base overlay (atmosphere + auto-hide Tauri-shaped compact player only). Foreground panels are rebuilt on this base. - AppShell mounts the overlay; ViewMode menu opens Immersive in PlayerBar/Small. cc
This commit is contained in:
18 files changed
+1275
-341
No files matched your search
@@ -0,0 +1,148 @@
|
||||
//! ImmersiveView visual glue.
|
||||
//!
|
||||
//! The Tauri background's "full" mode is Kawarp (Kawase blur + domain warp).
|
||||
//! Slint/femtovg cannot run that shader directly, so this module produces the
|
||||
//! same source material as Tauri's atmosphere texture: a tiny artwork color
|
||||
//! field scaled up, blurred, saturated, warmed, and vignetted. The Slint layer
|
||||
//! animates two copies in opposite directions to approximate the warp.
|
||||
|
||||
use image::{imageops, Rgba, RgbaImage};
|
||||
use slint::Color;
|
||||
|
||||
/// Generate a 128x128 atmospheric image from decoded RGBA artwork pixels.
|
||||
/// Mirrors `src/lib/immersive/utils/texture-loader.ts::generateAtmosphere`.
|
||||
pub fn generate_atmosphere(pixels: &[u8], width: u32, height: u32) -> Option<(Vec<u8>, u32, u32)> {
|
||||
let src = RgbaImage::from_raw(width, height, pixels.to_vec())?;
|
||||
let tiny = imageops::resize(&src, 8, 8, imageops::FilterType::Triangle);
|
||||
let scaled = imageops::resize(&tiny, 128, 128, imageops::FilterType::CatmullRom);
|
||||
let blurred = imageops::blur(&scaled, 16.0);
|
||||
let adjusted = color_adjust(blurred);
|
||||
let final_img = vignette(adjusted, 0.20);
|
||||
Some((final_img.into_raw(), 128, 128))
|
||||
}
|
||||
|
||||
/// AlbumReactive glow color: the most saturated non-extreme 8x8 sample.
|
||||
pub fn glow_color(pixels: &[u8], width: u32, height: u32) -> Color {
|
||||
let Some(src) = RgbaImage::from_raw(width, height, pixels.to_vec()) else {
|
||||
return Color::from_argb_u8(0x59, 100, 100, 255);
|
||||
};
|
||||
let tiny = imageops::resize(&src, 8, 8, imageops::FilterType::Triangle);
|
||||
let mut best_sat = 0.0f32;
|
||||
let mut best = (100u8, 100u8, 255u8);
|
||||
|
||||
for px in tiny.pixels() {
|
||||
let r = px[0] as f32;
|
||||
let g = px[1] as f32;
|
||||
let b = px[2] as f32;
|
||||
let max = r.max(g).max(b);
|
||||
let min = r.min(g).min(b);
|
||||
let lum = (max + min) / 2.0;
|
||||
let sat = if (max - min).abs() < f32::EPSILON {
|
||||
0.0
|
||||
} else if lum > 127.0 {
|
||||
(max - min) / (510.0 - max - min).max(1.0)
|
||||
} else {
|
||||
(max - min) / (max + min).max(1.0)
|
||||
};
|
||||
if lum > 50.0 && lum < 220.0 && sat > best_sat {
|
||||
best_sat = sat;
|
||||
best = (px[0], px[1], px[2]);
|
||||
}
|
||||
}
|
||||
|
||||
Color::from_argb_u8(0x59, best.0, best.1, best.2)
|
||||
}
|
||||
|
||||
fn color_adjust(mut img: RgbaImage) -> RgbaImage {
|
||||
let mut min_r = 255.0f32;
|
||||
let mut min_g = 255.0f32;
|
||||
let mut min_b = 255.0f32;
|
||||
let mut max_r = 0.0f32;
|
||||
let mut max_g = 0.0f32;
|
||||
let mut max_b = 0.0f32;
|
||||
let mut total_brightness = 0.0f32;
|
||||
let count = (img.width() * img.height()).max(1) as f32;
|
||||
|
||||
for px in img.pixels_mut() {
|
||||
let (r, g, b) = saturate_brightness_contrast(px[0], px[1], px[2]);
|
||||
px[0] = r;
|
||||
px[1] = g;
|
||||
px[2] = b;
|
||||
|
||||
let rf = r as f32;
|
||||
let gf = g as f32;
|
||||
let bf = b as f32;
|
||||
min_r = min_r.min(rf);
|
||||
min_g = min_g.min(gf);
|
||||
min_b = min_b.min(bf);
|
||||
max_r = max_r.max(rf);
|
||||
max_g = max_g.max(gf);
|
||||
max_b = max_b.max(bf);
|
||||
total_brightness += (rf + gf + bf) / 3.0;
|
||||
}
|
||||
|
||||
let norm_strength = (total_brightness / count / 80.0).min(1.0);
|
||||
let target_min = 18.0f32;
|
||||
let target_range = 232.0f32;
|
||||
let range_r = (max_r - min_r).max(1.0);
|
||||
let range_g = (max_g - min_g).max(1.0);
|
||||
let range_b = (max_b - min_b).max(1.0);
|
||||
|
||||
for px in img.pixels_mut() {
|
||||
let r = px[0] as f32;
|
||||
let g = px[1] as f32;
|
||||
let b = px[2] as f32;
|
||||
let norm_r = target_min + ((r - min_r) / range_r) * target_range;
|
||||
let norm_g = target_min + ((g - min_g) / range_g) * target_range;
|
||||
let norm_b = target_min + ((b - min_b) / range_b) * target_range;
|
||||
let lift_r = (r * 1.5).min(255.0);
|
||||
let lift_g = (g * 1.5).min(255.0);
|
||||
let lift_b = (b * 1.5).min(255.0);
|
||||
|
||||
px[0] = (lift_r + (norm_r - lift_r) * norm_strength).mul_add(1.08, 0.0).min(255.0) as u8;
|
||||
px[1] = (lift_g + (norm_g - lift_g) * norm_strength).min(255.0) as u8;
|
||||
px[2] = (lift_b + (norm_b - lift_b) * norm_strength).min(255.0) as u8;
|
||||
}
|
||||
|
||||
img
|
||||
}
|
||||
|
||||
fn saturate_brightness_contrast(r: u8, g: u8, b: u8) -> (u8, u8, u8) {
|
||||
let mut rf = r as f32;
|
||||
let mut gf = g as f32;
|
||||
let mut bf = b as f32;
|
||||
let gray = (rf + gf + bf) / 3.0;
|
||||
let sat = 2.45;
|
||||
rf = gray + (rf - gray) * sat;
|
||||
gf = gray + (gf - gray) * sat;
|
||||
bf = gray + (bf - gray) * sat;
|
||||
|
||||
let brightness = 0.92;
|
||||
let contrast = 1.18;
|
||||
let adjust = |v: f32| ((v * brightness - 128.0) * contrast + 128.0).clamp(0.0, 255.0) as u8;
|
||||
(adjust(rf), adjust(gf), adjust(bf))
|
||||
}
|
||||
|
||||
fn vignette(mut img: RgbaImage, intensity: f32) -> RgbaImage {
|
||||
let w = img.width() as f32;
|
||||
let h = img.height() as f32;
|
||||
let cx = w / 2.0;
|
||||
let cy = h / 2.0;
|
||||
let inner = w.min(h) * 0.20;
|
||||
let outer = w.min(h) * 0.70;
|
||||
|
||||
for (x, y, px) in img.enumerate_pixels_mut() {
|
||||
let dx = x as f32 - cx;
|
||||
let dy = y as f32 - cy;
|
||||
let d = (dx * dx + dy * dy).sqrt();
|
||||
let t = ((d - inner) / (outer - inner)).clamp(0.0, 1.0);
|
||||
let factor = 1.0 - intensity * t;
|
||||
*px = Rgba([
|
||||
(px[0] as f32 * factor) as u8,
|
||||
(px[1] as f32 * factor) as u8,
|
||||
(px[2] as f32 * factor) as u8,
|
||||
px[3],
|
||||
]);
|
||||
}
|
||||
img
|
||||
}
|
||||
@@ -31,6 +31,7 @@ mod favorites_prefs;
|
||||
mod foryou;
|
||||
mod genre_filter;
|
||||
mod home;
|
||||
mod immersive;
|
||||
mod info_modals;
|
||||
mod label;
|
||||
mod location_view;
|
||||
@@ -72,6 +73,7 @@ mod tag_editor;
|
||||
mod offline;
|
||||
mod offline_cache;
|
||||
mod offline_favorites;
|
||||
mod visualizer;
|
||||
mod offline_manager;
|
||||
mod offline_mode;
|
||||
mod playlist;
|
||||
@@ -3966,7 +3968,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.global::<AppearanceState>()
|
||||
.set_is_macos(cfg!(target_os = "macos"));
|
||||
|
||||
let app_runtime = Arc::new(AppRuntime::new(SlintAdapter::new(window.as_weak())));
|
||||
let app_runtime = Arc::new(AppRuntime::with_visualizer(SlintAdapter::new(window.as_weak())));
|
||||
|
||||
// ImmersiveView audio visualizers: spawn the frontend-agnostic FFT producer
|
||||
// against the runtime's tap and start the 30fps drain into VisualizerState.
|
||||
// Inert (tap disabled, no capture / no FFT cost) until the immersive view
|
||||
// opens. Must run on the UI thread before window.run().
|
||||
visualizer::install(&window, &app_runtime);
|
||||
|
||||
// MusicBrainz cache — opens a SQLite store at
|
||||
// <data-dir>/qbz/cache/musicbrainz_cache.db so artist metadata
|
||||
@@ -4921,6 +4929,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Persisted to ui_prefs so the choice survives restarts.
|
||||
// Large/window modes are disabled in the menu until those
|
||||
// layouts land.
|
||||
("npb-view", "immersive") => {
|
||||
if let Some(w) = weak.upgrade() {
|
||||
w.global::<ImmersiveState>().set_open(true);
|
||||
w.global::<VisualizerState>().invoke_set_enabled(true);
|
||||
}
|
||||
}
|
||||
("npb-view", mode @ ("new" | "classic" | "small")) => {
|
||||
if let Some(w) = weak.upgrade() {
|
||||
w.global::<ShellState>()
|
||||
|
||||
@@ -21,8 +21,8 @@ use slint::{ComponentHandle, Model, ModelRc};
|
||||
use crate::adapter::SlintAdapter;
|
||||
use crate::queue::QueueController;
|
||||
use crate::{
|
||||
AlbumState, AppWindow, ArtistState, ContentView, FavoritesState, LabelState, NavState,
|
||||
NowPlayingState, PlaylistState, SearchState, TrackItem,
|
||||
AlbumState, AppWindow, ArtistState, ContentView, FavoritesState, ImmersiveState, LabelState,
|
||||
NavState, NowPlayingState, PlaylistState, SearchState, TrackItem,
|
||||
};
|
||||
|
||||
/// The Queue sidebar controller, published once the shell is up so the
|
||||
@@ -1192,6 +1192,13 @@ fn load_now_playing_artwork_large(weak: slint::Weak<AppWindow>, art: qbz_models:
|
||||
};
|
||||
let _ = weak.upgrade_in_event_loop(move |win| {
|
||||
let img = crate::artwork::pixels_to_image(&pixels, w, h);
|
||||
if let Some((bg_pixels, bg_w, bg_h)) = crate::immersive::generate_atmosphere(&pixels, w, h)
|
||||
{
|
||||
let bg = crate::artwork::pixels_to_image(&bg_pixels, bg_w, bg_h);
|
||||
win.global::<ImmersiveState>().set_bg_image(bg);
|
||||
}
|
||||
win.global::<ImmersiveState>()
|
||||
.set_glow_color(crate::immersive::glow_color(&pixels, w, h));
|
||||
win.global::<NowPlayingState>().set_artwork_large(img);
|
||||
});
|
||||
});
|
||||
@@ -1464,6 +1471,8 @@ pub(crate) async fn refresh_now_playing_meta(runtime: &Runtime, weak: &slint::We
|
||||
// floating preview never shows the previous track while the new high-res
|
||||
// cover resolves.
|
||||
np.set_artwork_large(slint::Image::default());
|
||||
w.global::<ImmersiveState>()
|
||||
.set_bg_image(slint::Image::default());
|
||||
});
|
||||
|
||||
load_now_playing_artwork(weak.clone(), bar_artwork);
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
//! ImmersiveView audio-visualizer glue.
|
||||
//!
|
||||
//! Spawns the frontend-agnostic FFT producer (`qbz_audio::visualizer`) against
|
||||
//! the runtime's [`VisualizerTap`], latches each [`VizFrame`] into a single-slot
|
||||
//! cell, and drains the latest frames into the `VisualizerState` Slint global on
|
||||
//! a ~30 fps UI-thread timer. Persistent `VecModel`s are mutated in place
|
||||
//! (`set_row_data`) so the Slint side keeps the same model identity — no
|
||||
//! per-frame re-instantiation of the bound views.
|
||||
//!
|
||||
//! The tap starts disabled: nothing is captured and the FFT loop idles until the
|
||||
//! immersive view calls `VisualizerState::set-enabled(true)` on open. There is no
|
||||
//! Tauri command here — `set-enabled` drives `tap.set_enabled` directly, the same
|
||||
//! pattern `playback.rs` uses for the rest of the runtime controls.
|
||||
//!
|
||||
//! Protected-audio note: this lives entirely downstream of the read-only ring
|
||||
//! buffer. It touches none of the device/stream init (see CLAUDE.md "Audio
|
||||
//! Backend System").
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use qbz_app::shell::AppRuntime;
|
||||
use qbz_audio::visualizer::{spawn_visualizer_thread, VizFrame, VizSink};
|
||||
use slint::{ComponentHandle, Model, ModelRc, VecModel};
|
||||
|
||||
use crate::adapter::SlintAdapter;
|
||||
use crate::{AppWindow, VisualizerState};
|
||||
|
||||
/// Single-slot, latest-wins frame store shared with the FFT producer thread.
|
||||
/// Each cell holds at most the most recent frame for that stream; the UI drain
|
||||
/// `take()`s it. A stalled UI therefore drops intermediate frames instead of
|
||||
/// growing an unbounded queue.
|
||||
#[derive(Default)]
|
||||
struct VizCells {
|
||||
bars: Mutex<Option<[f32; 16]>>,
|
||||
spectral: Mutex<Option<Vec<f32>>>,
|
||||
energy: Mutex<Option<[f32; 5]>>,
|
||||
waveform: Mutex<Option<Box<[f32; 512]>>>,
|
||||
transient: Mutex<Option<f32>>,
|
||||
}
|
||||
|
||||
/// The producer-side sink: latches frames into the shared cells (no Slint access
|
||||
/// from the FFT thread).
|
||||
struct SlintVizSink {
|
||||
cells: Arc<VizCells>,
|
||||
}
|
||||
|
||||
impl VizSink for SlintVizSink {
|
||||
fn submit(&self, frame: VizFrame) {
|
||||
match frame {
|
||||
VizFrame::Viz16(b) => *self.cells.bars.lock().unwrap() = Some(b),
|
||||
VizFrame::Spectral512(b) => *self.cells.spectral.lock().unwrap() = Some(b),
|
||||
VizFrame::Energy5(b) => *self.cells.energy.lock().unwrap() = Some(b),
|
||||
VizFrame::Wave256x2(b) => *self.cells.waveform.lock().unwrap() = Some(b),
|
||||
VizFrame::Transient1(x) => *self.cells.transient.lock().unwrap() = Some(x),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Keeps the drain timer alive for the app lifetime (a dropped `Timer` stops
|
||||
/// firing). Lives on the UI thread, like the models it writes.
|
||||
static DRAIN_TIMER: RefCell<Option<slint::Timer>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// Wire the visualizer. Call once, on the UI thread, after the runtime is built
|
||||
/// and before `window.run()`. No-op when the runtime carries no tap (i.e. it was
|
||||
/// built without [`AppRuntime::with_visualizer`]).
|
||||
pub fn install(window: &AppWindow, runtime: &Arc<AppRuntime<SlintAdapter>>) {
|
||||
let Some(tap) = runtime.visualizer_tap().cloned() else {
|
||||
log::warn!("[viz] runtime has no visualizer tap; immersive visualizers disabled");
|
||||
return;
|
||||
};
|
||||
|
||||
// Persistent models — created once, set on the global once, then mutated per
|
||||
// frame so the bound views never see a new model identity.
|
||||
let bars: Rc<VecModel<f32>> = Rc::new(VecModel::from(vec![0.0f32; 16]));
|
||||
let spectral: Rc<VecModel<f32>> = Rc::new(VecModel::from(vec![0.0f32; 512]));
|
||||
let energy: Rc<VecModel<f32>> = Rc::new(VecModel::from(vec![0.0f32; 5]));
|
||||
let waveform: Rc<VecModel<f32>> = Rc::new(VecModel::from(vec![0.0f32; 512]));
|
||||
|
||||
let st = window.global::<VisualizerState>();
|
||||
st.set_bars(ModelRc::from(bars.clone()));
|
||||
st.set_spectral(ModelRc::from(spectral.clone()));
|
||||
st.set_energy(ModelRc::from(energy.clone()));
|
||||
st.set_waveform(ModelRc::from(waveform.clone()));
|
||||
|
||||
// set-enabled → toggle capture on the tap directly.
|
||||
{
|
||||
let tap = tap.clone();
|
||||
st.on_set_enabled(move |on| tap.set_enabled(on));
|
||||
}
|
||||
|
||||
// Producer thread: computes the five streams, latches each into its cell.
|
||||
let cells = Arc::new(VizCells::default());
|
||||
let sink = Arc::new(SlintVizSink {
|
||||
cells: cells.clone(),
|
||||
});
|
||||
let _ = spawn_visualizer_thread(tap, sink);
|
||||
|
||||
// ~30 fps drain on the UI thread: copy the latest frames into the models.
|
||||
let weak = window.as_weak();
|
||||
let timer = slint::Timer::default();
|
||||
timer.start(
|
||||
slint::TimerMode::Repeated,
|
||||
std::time::Duration::from_millis(33),
|
||||
move || {
|
||||
if let Some(b) = cells.bars.lock().unwrap().take() {
|
||||
for (i, v) in b.iter().enumerate() {
|
||||
bars.set_row_data(i, *v);
|
||||
}
|
||||
}
|
||||
if let Some(b) = cells.energy.lock().unwrap().take() {
|
||||
for (i, v) in b.iter().enumerate() {
|
||||
energy.set_row_data(i, *v);
|
||||
}
|
||||
}
|
||||
if let Some(b) = cells.spectral.lock().unwrap().take() {
|
||||
for (i, v) in b.iter().enumerate() {
|
||||
spectral.set_row_data(i, *v);
|
||||
}
|
||||
}
|
||||
if let Some(b) = cells.waveform.lock().unwrap().take() {
|
||||
for (i, v) in b.iter().enumerate() {
|
||||
waveform.set_row_data(i, *v);
|
||||
}
|
||||
}
|
||||
if let Some(x) = cells.transient.lock().unwrap().take() {
|
||||
if let Some(w) = weak.upgrade() {
|
||||
w.global::<VisualizerState>().set_transient(x);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
DRAIN_TIMER.with(|t| *t.borrow_mut() = Some(timer));
|
||||
log::info!("[viz] producer + 30fps drain installed (tap disabled until immersive opens)");
|
||||
}
|
||||
Reference in new issue
Block a user