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
+1267
-333
No files matched your search
Generated
+34
-1
@@ -2001,6 +2001,15 @@ version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
|
||||
|
||||
[[package]]
|
||||
name = "float-cmp"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flume"
|
||||
version = "0.11.1"
|
||||
@@ -3868,6 +3877,17 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "microfft"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2b6673eb0cc536241d6734c2ca45abfdbf90e9e7791c66a36a7ba3c315b76cf"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"num-complex",
|
||||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
@@ -5191,6 +5211,7 @@ dependencies = [
|
||||
"rustfft",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"spectrum-analyzer",
|
||||
"symphonia",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
@@ -6892,6 +6913,18 @@ dependencies = [
|
||||
"windows 0.44.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spectrum-analyzer"
|
||||
version = "1.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f5353405212be77e7f9e95aa77934f0aafb0f80c586571680b9c20ce5bae758"
|
||||
dependencies = [
|
||||
"float-cmp 0.10.0",
|
||||
"libm",
|
||||
"microfft",
|
||||
"paste",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
@@ -6946,7 +6979,7 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
|
||||
dependencies = [
|
||||
"float-cmp",
|
||||
"float-cmp 0.9.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -50,6 +50,10 @@ pub struct AppRuntime<A: FrontendAdapter + Send + Sync + 'static> {
|
||||
runtime: Arc<RuntimeManager>,
|
||||
user_paths: UserDataPaths,
|
||||
session: Mutex<Option<ActiveSession>>,
|
||||
/// The visualizer tap handed to the player, retained so the shell can start
|
||||
/// the FFT producer and toggle capture. `None` for shells that do not drive
|
||||
/// audio visualization (the default `new`/`with_audio_settings` path).
|
||||
visualizer_tap: Option<VisualizerTap>,
|
||||
}
|
||||
|
||||
impl<A: FrontendAdapter + Send + Sync + 'static> AppRuntime<A> {
|
||||
@@ -71,6 +75,7 @@ impl<A: FrontendAdapter + Send + Sync + 'static> AppRuntime<A> {
|
||||
runtime: Arc::new(RuntimeManager::new()),
|
||||
user_paths: UserDataPaths::new(),
|
||||
session: Mutex::new(None),
|
||||
visualizer_tap: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +100,37 @@ impl<A: FrontendAdapter + Send + Sync + 'static> AppRuntime<A> {
|
||||
Self::with_audio_settings(adapter, device_name, audio_settings, None)
|
||||
}
|
||||
|
||||
/// Build like [`AppRuntime::new`], but also wire a [`VisualizerTap`] into the
|
||||
/// player and retain it so the shell can start the frontend-agnostic FFT
|
||||
/// producer ([`qbz_audio::visualizer::spawn_visualizer_thread`]) and toggle
|
||||
/// capture via the tap's `set_enabled`. Used by the Slint shell for the
|
||||
/// ImmersiveView audio visualizers. The tap starts disabled, so it adds no
|
||||
/// runtime cost until the immersive view enables it.
|
||||
pub fn with_visualizer(adapter: A) -> Self {
|
||||
let (device_name, audio_settings) = AudioSettingsStore::new()
|
||||
.ok()
|
||||
.and_then(|store| {
|
||||
store
|
||||
.get_settings()
|
||||
.ok()
|
||||
.map(|settings| (settings.output_device.clone(), settings))
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
log::info!("[AppRuntime] No saved audio settings, using defaults");
|
||||
(None, AudioSettings::default())
|
||||
});
|
||||
let tap = VisualizerTap::new();
|
||||
let mut rt =
|
||||
Self::with_audio_settings(adapter, device_name, audio_settings, Some(tap.clone()));
|
||||
rt.visualizer_tap = Some(tap);
|
||||
rt
|
||||
}
|
||||
|
||||
/// The visualizer tap, if this runtime was built with [`AppRuntime::with_visualizer`].
|
||||
pub fn visualizer_tap(&self) -> Option<&VisualizerTap> {
|
||||
self.visualizer_tap.as_ref()
|
||||
}
|
||||
|
||||
/// Initialize the core (extracts Qobuz bundle tokens).
|
||||
///
|
||||
/// Best-effort and offline-tolerant: a network failure here leaves the
|
||||
|
||||
@@ -29,6 +29,9 @@ rodio = { version = "0.22", features = ["symphonia-all"] }
|
||||
symphonia = { workspace = true }
|
||||
ebur128 = "0.1"
|
||||
rustfft = "6.4"
|
||||
# FFT spectrum helpers for the frontend-agnostic visualizer producer
|
||||
# (Hann window, log-bar mapping). Kept in lockstep with the src-tauri dep.
|
||||
spectrum-analyzer = "1.5"
|
||||
|
||||
# Database for loudness cache
|
||||
rusqlite = { version = "0.31", features = ["bundled"] }
|
||||
|
||||
@@ -10,9 +10,11 @@
|
||||
//!
|
||||
//! The Tauri-specific FFT thread and event emission remain in qbz-nix.
|
||||
|
||||
mod processor;
|
||||
mod ring_buffer;
|
||||
mod tapped_source;
|
||||
|
||||
pub use processor::{spawn_visualizer_thread, VizFrame, VizSink};
|
||||
pub use ring_buffer::RingBuffer;
|
||||
pub use tapped_source::TappedSource;
|
||||
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
//! Frontend-agnostic FFT/visualizer producer.
|
||||
//!
|
||||
//! All DSP — Hann window, FFT, log-bars, energy bands, transient detection,
|
||||
//! waveform downsample, and the spectral ribbon — lives here so any frontend
|
||||
//! (Tauri, Slint, headless) can consume the same five typed streams without a
|
||||
//! framework dependency. A [`VizSink`] is the only seam: the producer thread
|
||||
//! computes a [`VizFrame`] and hands it to `sink.submit(...)`.
|
||||
//!
|
||||
//! This is strictly downstream of the lockless [`RingBuffer`](super::RingBuffer)
|
||||
//! (the read-only tap on the bit-perfect stream); it touches none of the
|
||||
//! protected audio device/stream path. See
|
||||
//! `qbz-nix-docs/immersive-slint-handoff/recon/source/audio-transport.md`.
|
||||
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use spectrum_analyzer::scaling::divide_by_N_sqrt;
|
||||
use spectrum_analyzer::windows::hann_window;
|
||||
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
|
||||
|
||||
use crate::SpectralAnalyzer;
|
||||
|
||||
use super::{VisualizerTap, FFT_SIZE, NUM_BARS, TARGET_FPS};
|
||||
|
||||
/// Number of energy bands for the Energy Bands visualizer
|
||||
const NUM_ENERGY_BANDS: usize = 5;
|
||||
const NUM_SPECTRAL_BANDS: usize = 512;
|
||||
const SPECTRAL_UPDATE_RATE_HZ: u32 = 58;
|
||||
const SPECTRAL_SMOOTHING: f32 = 0.30;
|
||||
|
||||
/// Energy band frequency ranges (Hz):
|
||||
/// Sub-bass (20-60), Bass (60-250), Mids (250-2k), Presence (2k-6k), Air (6k-20k)
|
||||
const ENERGY_BAND_RANGES: [(f32, f32); NUM_ENERGY_BANDS] = [
|
||||
(20.0, 60.0),
|
||||
(60.0, 250.0),
|
||||
(250.0, 2000.0),
|
||||
(2000.0, 6000.0),
|
||||
(6000.0, 20000.0),
|
||||
];
|
||||
|
||||
/// One frame of computed visualization data. Each variant corresponds to one of
|
||||
/// the five streams the Tauri build historically emitted as `viz:*` events; the
|
||||
/// payload is the decoded magnitudes, not the LE-f32 byte blob (the Tauri
|
||||
/// adapter re-serializes those for backward compatibility).
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum VizFrame {
|
||||
/// 16 log-spaced FFT bars (`viz:data`).
|
||||
Viz16([f32; 16]),
|
||||
/// 256 L samples followed by 256 R samples (`viz:waveform`).
|
||||
Wave256x2(Box<[f32; 512]>),
|
||||
/// `NUM_SPECTRAL_BANDS` spectral bands (`viz:spectral`).
|
||||
Spectral512(Vec<f32>),
|
||||
/// 5 energy bands (`viz:energy`).
|
||||
Energy5([f32; 5]),
|
||||
/// A single transient intensity, submitted only on detection (`viz:transient`).
|
||||
Transient1(f32),
|
||||
}
|
||||
|
||||
/// Frontend-agnostic consumer of visualization frames. Implemented by the Tauri
|
||||
/// adapter (re-emits the `viz:*` events) and the Slint adapter (latches frames
|
||||
/// for the UI-thread drain).
|
||||
pub trait VizSink: Send + Sync {
|
||||
fn submit(&self, frame: VizFrame);
|
||||
}
|
||||
|
||||
/// Spawn the FFT processing thread. Idempotency is the caller's concern (the
|
||||
/// `Visualizer`/shell guards against a double start). Returns the join handle;
|
||||
/// callers that run for the app lifetime can drop it.
|
||||
pub fn spawn_visualizer_thread(tap: VisualizerTap, sink: Arc<dyn VizSink>) -> JoinHandle<()> {
|
||||
std::thread::Builder::new()
|
||||
.name("visualizer-fft".to_string())
|
||||
.spawn(move || {
|
||||
run_fft_loop(tap, sink);
|
||||
})
|
||||
.expect("Failed to spawn visualizer thread")
|
||||
}
|
||||
|
||||
/// Main FFT processing loop. Reads samples from the tap's ring buffer, computes
|
||||
/// all five streams at `TARGET_FPS`, and submits them to the sink. Pacing,
|
||||
/// `enabled`/`sample_rate` reads, and the `SpectralAnalyzer` cadence are
|
||||
/// byte-for-byte identical to the historical Tauri loop.
|
||||
fn run_fft_loop(tap: VisualizerTap, sink: Arc<dyn VizSink>) {
|
||||
// Pre-allocate all buffers to avoid allocations in the hot path
|
||||
let mut samples = vec![0.0f32; FFT_SIZE];
|
||||
let mut windowed = vec![0.0f32; FFT_SIZE];
|
||||
let mut output = vec![0.0f32; NUM_BARS];
|
||||
let mut smoothed = vec![0.0f32; NUM_BARS];
|
||||
|
||||
// Waveform buffer: 256 L + 256 R = 512 floats
|
||||
const WAVEFORM_POINTS: usize = 256;
|
||||
let mut waveform_buf = vec![0.0f32; WAVEFORM_POINTS * 2];
|
||||
|
||||
// Energy bands state
|
||||
let mut energy_bands = [0.0f32; NUM_ENERGY_BANDS];
|
||||
let mut smoothed_energy = [0.0f32; NUM_ENERGY_BANDS];
|
||||
let mut spectral_analyzer = SpectralAnalyzer::new(
|
||||
tap.sample_rate.load(Ordering::Relaxed),
|
||||
FFT_SIZE,
|
||||
NUM_SPECTRAL_BANDS,
|
||||
SPECTRAL_UPDATE_RATE_HZ,
|
||||
SPECTRAL_SMOOTHING,
|
||||
);
|
||||
|
||||
// Transient detection state
|
||||
let mut prev_rms = 0.0f32;
|
||||
let mut transient_cooldown = 0u32; // frames remaining in cooldown
|
||||
const TRANSIENT_THRESHOLD: f32 = 0.04; // RMS jump threshold (sensitive)
|
||||
const TRANSIENT_COOLDOWN_FRAMES: u32 = 3; // ~100ms at 30fps
|
||||
|
||||
// Smoothing factor: 0 = no smoothing, higher = more smoothing
|
||||
const SMOOTHING: f32 = 0.65;
|
||||
|
||||
let frame_duration = Duration::from_micros(1_000_000 / TARGET_FPS);
|
||||
|
||||
loop {
|
||||
let frame_start = Instant::now();
|
||||
|
||||
if tap.enabled.load(Ordering::Relaxed) {
|
||||
let sample_rate = tap.sample_rate.load(Ordering::Relaxed);
|
||||
|
||||
// Get samples from ring buffer
|
||||
tap.ring_buffer.snapshot(&mut samples);
|
||||
|
||||
// Compact, progressive spectrogram bands for the Spectral Ribbon,
|
||||
// gated on the analyzer's own update cadence.
|
||||
if spectral_analyzer.process_audio_frame(&samples, sample_rate) {
|
||||
let spectral = spectral_analyzer.get_latest_bands();
|
||||
sink.submit(VizFrame::Spectral512(spectral.to_vec()));
|
||||
}
|
||||
|
||||
// Apply Hann window to reduce spectral leakage
|
||||
let window = hann_window(&samples);
|
||||
for (i, (sample, win)) in samples.iter().zip(window.iter()).enumerate() {
|
||||
windowed[i] = sample * win;
|
||||
}
|
||||
|
||||
// Compute FFT spectrum
|
||||
match samples_fft_to_spectrum(
|
||||
&windowed,
|
||||
sample_rate,
|
||||
FrequencyLimit::Range(20.0, 20000.0),
|
||||
Some(÷_by_N_sqrt),
|
||||
) {
|
||||
Ok(spectrum) => {
|
||||
// Map spectrum to logarithmic frequency bars
|
||||
map_to_log_bars(&spectrum, &mut output);
|
||||
|
||||
// Apply smoothing for visual continuity
|
||||
for i in 0..NUM_BARS {
|
||||
let new = output[i];
|
||||
// Faster attack, slower decay for punchy visuals
|
||||
if new > smoothed[i] {
|
||||
smoothed[i] = smoothed[i] * 0.3 + new * 0.7; // Fast attack
|
||||
} else {
|
||||
smoothed[i] = smoothed[i] * SMOOTHING + new * (1.0 - SMOOTHING);
|
||||
// Slow decay
|
||||
}
|
||||
output[i] = smoothed[i];
|
||||
}
|
||||
|
||||
let mut bars = [0.0f32; 16];
|
||||
bars.copy_from_slice(&output);
|
||||
sink.submit(VizFrame::Viz16(bars));
|
||||
|
||||
// --- Energy Bands: compute RMS per frequency band from spectrum ---
|
||||
let data = spectrum.data();
|
||||
for (band_idx, &(lo, hi)) in ENERGY_BAND_RANGES.iter().enumerate() {
|
||||
let mut sum_sq = 0.0f32;
|
||||
let mut count = 0u32;
|
||||
for (freq, magnitude) in data.iter() {
|
||||
let f = freq.val();
|
||||
if f >= lo && f < hi {
|
||||
let mag = magnitude.val();
|
||||
sum_sq += mag * mag;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
let rms = if count > 0 {
|
||||
(sum_sq / count as f32).sqrt()
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
// Compress and normalize
|
||||
let compressed = (rms * 6.0).powf(0.5).clamp(0.0, 1.0);
|
||||
// Smooth: fast attack, slow decay
|
||||
if compressed > smoothed_energy[band_idx] {
|
||||
smoothed_energy[band_idx] =
|
||||
smoothed_energy[band_idx] * 0.2 + compressed * 0.8;
|
||||
} else {
|
||||
smoothed_energy[band_idx] =
|
||||
smoothed_energy[band_idx] * 0.85 + compressed * 0.15;
|
||||
}
|
||||
energy_bands[band_idx] = smoothed_energy[band_idx];
|
||||
}
|
||||
sink.submit(VizFrame::Energy5(energy_bands));
|
||||
|
||||
// --- Transient Detection: detect sharp RMS jumps ---
|
||||
// Use raw (pre-smoothed) RMS for transient sensitivity.
|
||||
// Weight bass/sub-bass more heavily for beat detection.
|
||||
let raw_rms = {
|
||||
let mut raw_sum = 0.0f32;
|
||||
for (band_idx, &(lo, hi)) in ENERGY_BAND_RANGES.iter().enumerate() {
|
||||
let mut sum_sq = 0.0f32;
|
||||
let mut cnt = 0u32;
|
||||
for (freq, magnitude) in data.iter() {
|
||||
let f = freq.val();
|
||||
if f >= lo && f < hi {
|
||||
let mag = magnitude.val();
|
||||
sum_sq += mag * mag;
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
let band_rms = if cnt > 0 {
|
||||
(sum_sq / cnt as f32).sqrt()
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
// Bass/sub-bass weighted 2x for beat detection
|
||||
let weight = if band_idx < 2 { 2.0 } else { 1.0 };
|
||||
raw_sum += (band_rms * 6.0).powf(0.5).clamp(0.0, 1.0) * weight;
|
||||
}
|
||||
raw_sum / (NUM_ENERGY_BANDS as f32 + 2.0) // account for extra bass weight
|
||||
};
|
||||
let rms_delta = raw_rms - prev_rms;
|
||||
|
||||
if transient_cooldown > 0 {
|
||||
transient_cooldown -= 1;
|
||||
}
|
||||
|
||||
if rms_delta > TRANSIENT_THRESHOLD && transient_cooldown == 0 {
|
||||
// Transient detected! Submit intensity (0.0 - 1.0)
|
||||
let intensity = (rms_delta * 5.0).clamp(0.0, 1.0);
|
||||
sink.submit(VizFrame::Transient1(intensity));
|
||||
transient_cooldown = TRANSIENT_COOLDOWN_FRAMES;
|
||||
}
|
||||
|
||||
prev_rms = raw_rms;
|
||||
}
|
||||
Err(e) => {
|
||||
log::debug!("FFT error: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Raw waveform data for the oscilloscope (stereo L/R).
|
||||
// samples[] is interleaved: L0, R0, L1, R1, ...
|
||||
// 1024 samples = 512 stereo pairs → downsample to 256 per channel
|
||||
let stereo_pairs = FFT_SIZE / 2; // 512
|
||||
let step = stereo_pairs / WAVEFORM_POINTS; // 512/256 = 2
|
||||
for i in 0..WAVEFORM_POINTS {
|
||||
let base = i * step * 2; // index into interleaved buffer
|
||||
waveform_buf[i] = samples[base]; // L
|
||||
waveform_buf[WAVEFORM_POINTS + i] = samples[base + 1]; // R
|
||||
}
|
||||
let mut wave = Box::new([0.0f32; 512]);
|
||||
wave.copy_from_slice(&waveform_buf);
|
||||
sink.submit(VizFrame::Wave256x2(wave));
|
||||
}
|
||||
|
||||
// Maintain target FPS
|
||||
let elapsed = frame_start.elapsed();
|
||||
if elapsed < frame_duration {
|
||||
std::thread::sleep(frame_duration - elapsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Map spectrum data to logarithmically-spaced frequency bars.
|
||||
///
|
||||
/// Human hearing is logarithmic, so we use log-spaced bars to match how we
|
||||
/// perceive frequency. This gives equal visual weight to bass, mids, and treble.
|
||||
fn map_to_log_bars(spectrum: &spectrum_analyzer::FrequencySpectrum, output: &mut [f32]) {
|
||||
let num_bars = output.len();
|
||||
|
||||
// Frequency range (Hz)
|
||||
const MIN_FREQ: f32 = 20.0;
|
||||
const MAX_FREQ: f32 = 20000.0;
|
||||
|
||||
let min_log = MIN_FREQ.ln();
|
||||
let max_log = MAX_FREQ.ln();
|
||||
|
||||
// Get spectrum data
|
||||
let data = spectrum.data();
|
||||
|
||||
for (i, bar) in output.iter_mut().enumerate() {
|
||||
// Calculate logarithmic frequency bounds for this bar
|
||||
let t_low = i as f32 / num_bars as f32;
|
||||
let t_high = (i + 1) as f32 / num_bars as f32;
|
||||
|
||||
let freq_low = (min_log + (max_log - min_log) * t_low).exp();
|
||||
let freq_high = (min_log + (max_log - min_log) * t_high).exp();
|
||||
|
||||
// Find all frequency bins that fall within this bar's range
|
||||
let mut sum = 0.0f32;
|
||||
let mut count = 0u32;
|
||||
|
||||
for (freq, magnitude) in data.iter() {
|
||||
let f = freq.val();
|
||||
if f >= freq_low && f < freq_high {
|
||||
// Apply perceptual weighting (boost bass slightly)
|
||||
let weight = if f < 200.0 {
|
||||
1.5 // Bass boost
|
||||
} else if f < 2000.0 {
|
||||
1.0 // Mids
|
||||
} else {
|
||||
0.8 // Reduce harsh highs
|
||||
};
|
||||
|
||||
sum += magnitude.val() * weight;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Average magnitude for this bar
|
||||
let avg = if count > 0 { sum / count as f32 } else { 0.0 };
|
||||
|
||||
// Apply dynamic range compression and normalize.
|
||||
// This makes quiet passages more visible while preventing clipping.
|
||||
let compressed = (avg * 4.0).powf(0.6);
|
||||
*bar = compressed.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_log_frequency_distribution() {
|
||||
// Verify that frequency bars are logarithmically distributed
|
||||
let num_bars = NUM_BARS; // Use the actual constant
|
||||
let min_log = 20.0_f32.ln();
|
||||
let max_log = 20000.0_f32.ln();
|
||||
|
||||
let mut freqs = Vec::new();
|
||||
for i in 0..num_bars {
|
||||
let t = i as f32 / num_bars as f32;
|
||||
let freq = (min_log + (max_log - min_log) * t).exp();
|
||||
freqs.push(freq);
|
||||
}
|
||||
|
||||
// First bar should be around 20Hz
|
||||
assert!(freqs[0] > 19.0 && freqs[0] < 25.0);
|
||||
|
||||
// Middle bar (~16 for 32 bars) should be around 630Hz (geometric mean of 20 and 20000)
|
||||
let mid = num_bars / 2;
|
||||
assert!(freqs[mid] > 500.0 && freqs[mid] < 800.0);
|
||||
|
||||
// Last bar should approach 20000Hz (but won't reach it since t < 1.0)
|
||||
assert!(freqs[num_bars - 1] > 10000.0);
|
||||
}
|
||||
}
|
||||
@@ -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)");
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import { Toast } from "primitives/Toast.slint";
|
||||
import { ShellState } from "state.slint";
|
||||
|
||||
// Re-export the state globals so the Rust layer can populate them.
|
||||
export { HomeState, HomeActions, DiscoverBrowseState, DiscoverBrowseActions, ForYouState, MixState, GenreFilterState, GenreFilterActions, AlbumState, ArtistState, NavState, ShellState, SessionState, SettingsState, AlbumActions, ArtistActions, ArtworkActions, NowPlayingState, QueueState, LyricsState, LyricsLineItem, SearchState, SearchActions, NetworkSidebarState, NetworkSidebarActions, MusicianState, MusicianActions, LabelState, LabelActions, LocationViewState, LocationViewActions, FavoritesState, FavoritesActions, PlaylistPickerState, PlaylistPickerActions, DuplicateConfirmState, DuplicateConfirmActions, PlaylistState, PlaylistActions, SidebarState, SidebarActions, SidebarFolderPopupState, CreatePlaylistState, CreatePlaylistActions, EditPlaylistState, EditPlaylistActions, CreateFolderState, CreateFolderActions, MyQbzCreateState, MyQbzCreateActions, DragState, DragActions, PlaylistManagerState, PlaylistManagerActions, OfflineManagerState, OfflineManagerActions, MyQbzState, MyQbzActions, MixtapeCardItem, MyQbzAddState, MyQbzAddActions, MyQbzAddRow, MyQbzDetailState, MyQbzDetailActions, MixtapeDetailItem, MyQbzEditState, MyQbzEditActions, MyQbzMixState, MyQbzMixActions, DiscoBuilderState, DiscoBuilderActions, DiscoGroup, DiscoCandidate, LocalLibraryState, LocalLibraryActions, LibraryFoldersState, LibFolderEditState, LibraryManageActions, LibraryScanState, LibAlbumFilterState, LocalAlbumState, LocalAlbumActions, TagEditorState, TagEditorActions, FolderEditState, FolderEditActions, ToastState, TextUtil, QconnectDevState, QconnectDevice, AppearanceState, MyQbzBrandingState, EphemeralPlayChoiceState, EphemeralPlayChoiceActions, PlexSettingsState, PlexAuthActions, PlexSectionItem, ScrobbleState, ScrobbleActions, OfflineState, OfflineModeActions, OfflineFavoritesState, OfflineFavoritesActions, ImportLogEntry, PlaylistImportState, PlaylistImportActions, InfoCreditRow, InfoCreditPair, AlbumCreditPerformer, AlbumCreditTrack, TrackInfoState, TrackInfoActions, AlbumInfoState, AlbumInfoActions } from "state.slint";
|
||||
export { HomeState, HomeActions, DiscoverBrowseState, DiscoverBrowseActions, ForYouState, MixState, GenreFilterState, GenreFilterActions, AlbumState, ArtistState, NavState, ShellState, SessionState, SettingsState, AlbumActions, ArtistActions, ArtworkActions, NowPlayingState, QueueState, LyricsState, LyricsLineItem, SearchState, SearchActions, NetworkSidebarState, NetworkSidebarActions, MusicianState, MusicianActions, LabelState, LabelActions, LocationViewState, LocationViewActions, FavoritesState, FavoritesActions, PlaylistPickerState, PlaylistPickerActions, DuplicateConfirmState, DuplicateConfirmActions, PlaylistState, PlaylistActions, SidebarState, SidebarActions, SidebarFolderPopupState, CreatePlaylistState, CreatePlaylistActions, EditPlaylistState, EditPlaylistActions, CreateFolderState, CreateFolderActions, MyQbzCreateState, MyQbzCreateActions, DragState, DragActions, PlaylistManagerState, PlaylistManagerActions, OfflineManagerState, OfflineManagerActions, MyQbzState, MyQbzActions, MixtapeCardItem, MyQbzAddState, MyQbzAddActions, MyQbzAddRow, MyQbzDetailState, MyQbzDetailActions, MixtapeDetailItem, MyQbzEditState, MyQbzEditActions, MyQbzMixState, MyQbzMixActions, DiscoBuilderState, DiscoBuilderActions, DiscoGroup, DiscoCandidate, LocalLibraryState, LocalLibraryActions, LibraryFoldersState, LibFolderEditState, LibraryManageActions, LibraryScanState, LibAlbumFilterState, LocalAlbumState, LocalAlbumActions, TagEditorState, TagEditorActions, FolderEditState, FolderEditActions, ToastState, TextUtil, QconnectDevState, QconnectDevice, AppearanceState, MyQbzBrandingState, EphemeralPlayChoiceState, EphemeralPlayChoiceActions, PlexSettingsState, PlexAuthActions, PlexSectionItem, ScrobbleState, ScrobbleActions, OfflineState, OfflineModeActions, OfflineFavoritesState, OfflineFavoritesActions, ImportLogEntry, PlaylistImportState, PlaylistImportActions, InfoCreditRow, InfoCreditPair, AlbumCreditPerformer, AlbumCreditTrack, TrackInfoState, TrackInfoActions, AlbumInfoState, AlbumInfoActions, VisualizerState, ImmersiveState } from "state.slint";
|
||||
|
||||
// Which top-level screen is shown. The app starts on `splash` while it
|
||||
// restores a saved session, then resolves to `shell` or `login`.
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// Reusable Kawarp-like atmosphere background.
|
||||
//
|
||||
// Source image is expected to be the host-generated 128x128 atmosphere bitmap
|
||||
// (tiny artwork sample -> blur -> color adjust -> vignette). This component
|
||||
// only animates/composites it, so it can be reused outside ImmersiveView later.
|
||||
// Immersive must keep `animated:true`; static album/artist headers can opt out.
|
||||
|
||||
export component ImmersiveAtmosphere inherits Rectangle {
|
||||
in property <image> source-image;
|
||||
in property <image> fallback-image;
|
||||
in property <float> dim: 0.15;
|
||||
in property <float> base-opacity: 0.95;
|
||||
in property <float> warp-opacity: 0.48;
|
||||
in property <bool> animated: true;
|
||||
|
||||
property <float> tick: root.animated ? animation-tick() / 1ms : 0.0;
|
||||
|
||||
background: #0a0a0b;
|
||||
clip: true;
|
||||
|
||||
if root.source-image.width > 0: Image {
|
||||
property <float> sx: Math.sin((root.tick / 7600.0) * 6.283185 * 1rad)
|
||||
+ 0.35 * Math.sin((root.tick / 3300.0 + 1.9) * 6.283185 * 1rad);
|
||||
property <float> sy: Math.sin((root.tick / 11200.0 + 1.1) * 6.283185 * 1rad);
|
||||
source: root.source-image;
|
||||
image-fit: cover;
|
||||
width: root.width + 520px;
|
||||
height: root.height + 420px;
|
||||
x: -260px + self.sx * 132px;
|
||||
y: -210px + self.sy * 58px;
|
||||
opacity: root.base-opacity;
|
||||
}
|
||||
if root.source-image.width > 0: Image {
|
||||
property <float> sx: Math.sin((root.tick / 9200.0 + 0.4) * 6.283185 * 1rad)
|
||||
+ 0.42 * Math.sin((root.tick / 4100.0 + 2.6) * 6.283185 * 1rad);
|
||||
property <float> sy: Math.sin((root.tick / 14800.0 + 1.7) * 6.283185 * 1rad);
|
||||
source: root.source-image;
|
||||
image-fit: cover;
|
||||
width: root.width + 620px;
|
||||
height: root.height + 500px;
|
||||
x: -310px - self.sx * 156px;
|
||||
y: -250px - self.sy * 74px;
|
||||
opacity: root.warp-opacity;
|
||||
}
|
||||
if root.source-image.width > 0: Image {
|
||||
property <float> sx: Math.sin((root.tick / 5700.0 + 1.6) * 6.283185 * 1rad);
|
||||
property <float> sy: Math.sin((root.tick / 12300.0 + 0.2) * 6.283185 * 1rad)
|
||||
+ 0.28 * Math.sin((root.tick / 3600.0 + 2.8) * 6.283185 * 1rad);
|
||||
source: root.source-image;
|
||||
image-fit: cover;
|
||||
width: root.width + 700px;
|
||||
height: root.height + 540px;
|
||||
x: -350px + self.sx * 118px;
|
||||
y: -270px + self.sy * 92px;
|
||||
opacity: 0.24;
|
||||
}
|
||||
if root.source-image.width > 0: Image {
|
||||
property <float> sx: Math.sin((root.tick / 15100.0 + 2.4) * 6.283185 * 1rad);
|
||||
property <float> sy: Math.sin((root.tick / 6900.0 + 1.4) * 6.283185 * 1rad);
|
||||
source: root.source-image;
|
||||
image-fit: cover;
|
||||
width: root.width + 560px;
|
||||
height: root.height + 620px;
|
||||
x: -280px - self.sx * 102px;
|
||||
y: -310px + self.sy * 128px;
|
||||
opacity: 0.18;
|
||||
}
|
||||
if root.source-image.width <= 0: Image {
|
||||
source: root.fallback-image;
|
||||
image-fit: cover;
|
||||
width: root.width + 220px;
|
||||
height: root.height + 220px;
|
||||
x: -110px;
|
||||
y: -110px;
|
||||
opacity: 0.35;
|
||||
}
|
||||
Rectangle {
|
||||
background: #000000.with-alpha(root.dim);
|
||||
}
|
||||
Rectangle {
|
||||
background: @linear-gradient(180deg, #00000066 0%, transparent 35%, #00000080 100%);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
// ImmersiveView base: reusable Kawarp-like atmosphere background +
|
||||
// Tauri-shaped compact player with auto-hide. Foreground panels will be
|
||||
// rebuilt on top of this base after the background/player are approved.
|
||||
|
||||
import { Radius } from "../foundation/radius.slint";
|
||||
import { QbzIcon } from "../primitives/QbzIcon.slint";
|
||||
import { NowPlayingState, VisualizerState, ImmersiveState } from "../state.slint";
|
||||
import { ImmersiveAtmosphere } from "ImmersiveAtmosphere.slint";
|
||||
|
||||
component GhostButton inherits Rectangle {
|
||||
in property <image> icon;
|
||||
in property <bool> active: false;
|
||||
in property <bool> enabled: true;
|
||||
in property <length> icon-size: 12px;
|
||||
callback clicked();
|
||||
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 14px;
|
||||
opacity: root.enabled ? 1.0 : 0.4;
|
||||
background: root.active ? #ffffff40 : transparent;
|
||||
|
||||
QbzIcon {
|
||||
source: root.icon;
|
||||
width: root.icon-size;
|
||||
height: root.icon-size;
|
||||
x: Math.round((parent.width - self.width) / 2 / 1px) * 1px;
|
||||
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
|
||||
tint: root.active || ta.has-hover ? #ffffff : #ffffffa6;
|
||||
}
|
||||
|
||||
ta := TouchArea {
|
||||
mouse-cursor: root.enabled ? pointer : default;
|
||||
clicked => {
|
||||
if (root.enabled) {
|
||||
root.clicked();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
component NavButton inherits Rectangle {
|
||||
in property <image> icon;
|
||||
callback clicked();
|
||||
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 14px;
|
||||
background: ta.has-hover ? #ffffff40 : #ffffff26;
|
||||
|
||||
QbzIcon {
|
||||
source: root.icon;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
x: Math.round((parent.width - self.width) / 2 / 1px) * 1px;
|
||||
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
|
||||
tint: #ffffff;
|
||||
}
|
||||
|
||||
ta := TouchArea {
|
||||
mouse-cursor: pointer;
|
||||
clicked => { root.clicked(); }
|
||||
}
|
||||
}
|
||||
|
||||
component PlayDisc inherits Rectangle {
|
||||
callback clicked();
|
||||
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 24px;
|
||||
background: ta.has-hover ? #ffffffe6 : #ffffff;
|
||||
drop-shadow-blur: 12px;
|
||||
drop-shadow-color: #0000004d;
|
||||
drop-shadow-offset-y: 4px;
|
||||
|
||||
QbzIcon {
|
||||
source: NowPlayingState.playing
|
||||
? @image-url("../assets/icons/pause.svg")
|
||||
: @image-url("../assets/icons/play-fill.svg");
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
x: Math.round((parent.width - self.width) / 2 / 1px) * 1px
|
||||
+ (NowPlayingState.playing ? 0px : 1px);
|
||||
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
|
||||
tint: #000000;
|
||||
}
|
||||
|
||||
ta := TouchArea {
|
||||
mouse-cursor: pointer;
|
||||
clicked => { root.clicked(); }
|
||||
}
|
||||
}
|
||||
|
||||
component TinyBar inherits Rectangle {
|
||||
in property <float> value: 0.0;
|
||||
in property <color> fill: #ffffff;
|
||||
callback changed(float);
|
||||
|
||||
height: 10px;
|
||||
background: transparent;
|
||||
|
||||
Rectangle {
|
||||
x: 0px;
|
||||
width: root.width;
|
||||
y: 4px;
|
||||
height: 2px;
|
||||
border-radius: 1px;
|
||||
background: #ffffff2e;
|
||||
}
|
||||
Rectangle {
|
||||
x: 0px;
|
||||
y: 4px;
|
||||
height: 2px;
|
||||
width: parent.width * Math.max(0.0, Math.min(1.0, root.value));
|
||||
border-radius: 1px;
|
||||
background: root.fill;
|
||||
}
|
||||
ta := TouchArea {
|
||||
mouse-cursor: pointer;
|
||||
clicked => {
|
||||
root.changed(Math.max(0.0, Math.min(1.0, self.mouse-x / self.width)));
|
||||
}
|
||||
moved => {
|
||||
if (self.pressed) {
|
||||
root.changed(Math.max(0.0, Math.min(1.0, self.mouse-x / self.width)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
component ImmersivePlayerBar inherits Rectangle {
|
||||
in property <length> viewport-width: 720px;
|
||||
callback media-action(string, string, string);
|
||||
callback wake();
|
||||
|
||||
width: Math.min(root.viewport-width - 32px, 720px);
|
||||
height: 90px;
|
||||
border-radius: 16px;
|
||||
background: #00000080;
|
||||
border-width: 1px;
|
||||
border-color: #ffffff2e;
|
||||
drop-shadow-blur: 32px;
|
||||
drop-shadow-color: #00000066;
|
||||
drop-shadow-offset-y: 8px;
|
||||
|
||||
// Left utility group.
|
||||
GhostButton {
|
||||
x: 24px;
|
||||
y: 22px;
|
||||
icon: @image-url("../assets/icons/heart.svg");
|
||||
active: false;
|
||||
enabled: NowPlayingState.has-track && !NowPlayingState.is-ephemeral;
|
||||
clicked => { root.media-action("track", NowPlayingState.track-id, "favorite"); }
|
||||
}
|
||||
GhostButton {
|
||||
x: 56px;
|
||||
y: 22px;
|
||||
icon: @image-url("../assets/icons/shuffle.svg");
|
||||
active: NowPlayingState.shuffle;
|
||||
clicked => { NowPlayingState.toggle-shuffle(); }
|
||||
}
|
||||
GhostButton {
|
||||
x: 88px;
|
||||
y: 22px;
|
||||
icon: @image-url("../assets/icons/repeat.svg");
|
||||
active: NowPlayingState.repeat-mode != 0;
|
||||
clicked => { NowPlayingState.cycle-repeat(); }
|
||||
}
|
||||
GhostButton {
|
||||
x: 120px;
|
||||
y: 22px;
|
||||
icon: @image-url("../assets/icons/infinity.svg");
|
||||
active: false;
|
||||
enabled: false;
|
||||
}
|
||||
|
||||
// Playback group: positioned from the play disc so play is exactly centered.
|
||||
Text {
|
||||
x: root.width / 2 - 108px;
|
||||
y: 31px;
|
||||
width: 42px;
|
||||
text: NowPlayingState.elapsed;
|
||||
color: #ffffffbf;
|
||||
font-size: 11px;
|
||||
horizontal-alignment: right;
|
||||
}
|
||||
NavButton {
|
||||
x: root.width / 2 - 64px;
|
||||
y: 22px;
|
||||
icon: @image-url("../assets/icons/skip-back.svg");
|
||||
clicked => { NowPlayingState.previous(); }
|
||||
}
|
||||
PlayDisc {
|
||||
x: root.width / 2 - 24px;
|
||||
y: 12px;
|
||||
clicked => { NowPlayingState.toggle-play(); }
|
||||
}
|
||||
NavButton {
|
||||
x: root.width / 2 + 36px;
|
||||
y: 22px;
|
||||
icon: @image-url("../assets/icons/skip-forward.svg");
|
||||
clicked => { NowPlayingState.next(); }
|
||||
}
|
||||
Text {
|
||||
x: root.width / 2 + 76px;
|
||||
y: 31px;
|
||||
width: 46px;
|
||||
text: NowPlayingState.remaining;
|
||||
color: #ffffffbf;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
// Right volume/menu group.
|
||||
GhostButton {
|
||||
x: root.width - 156px;
|
||||
y: 22px;
|
||||
icon: NowPlayingState.muted || NowPlayingState.volume == 0
|
||||
? @image-url("../assets/icons/volume-x.svg")
|
||||
: @image-url("../assets/icons/volume-2.svg");
|
||||
enabled: !NowPlayingState.volume-locked;
|
||||
clicked => { NowPlayingState.toggle-mute(); }
|
||||
}
|
||||
TinyBar {
|
||||
x: root.width - 112px;
|
||||
y: 31px;
|
||||
width: 64px;
|
||||
value: NowPlayingState.volume;
|
||||
changed(v) => { NowPlayingState.set-volume(v); }
|
||||
}
|
||||
Rectangle {
|
||||
x: root.width - 42px;
|
||||
y: 22px;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 14px;
|
||||
background: menu-ta.has-hover ? #ffffff26 : transparent;
|
||||
QbzIcon {
|
||||
source: @image-url("../assets/icons/ellipsis.svg");
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
x: Math.round((parent.width - self.width) / 2 / 1px) * 1px;
|
||||
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
|
||||
tint: #ffffffa6;
|
||||
}
|
||||
menu-ta := TouchArea {
|
||||
mouse-cursor: pointer;
|
||||
clicked => {
|
||||
root.wake();
|
||||
player-menu.show();
|
||||
}
|
||||
}
|
||||
player-menu := PopupWindow {
|
||||
x: -172px;
|
||||
y: -58px;
|
||||
width: 200px;
|
||||
height: 46px;
|
||||
close-policy: close-on-click;
|
||||
Rectangle {
|
||||
background: #000000cc;
|
||||
border-radius: 12px;
|
||||
border-width: 1px;
|
||||
border-color: #ffffff2e;
|
||||
Rectangle {
|
||||
x: 6px;
|
||||
y: 6px;
|
||||
width: parent.width - 12px;
|
||||
height: 34px;
|
||||
border-radius: Radius.sm;
|
||||
background: exit-ta.has-hover ? #ffffff1f : transparent;
|
||||
exit-ta := TouchArea {
|
||||
mouse-cursor: pointer;
|
||||
clicked => {
|
||||
ImmersiveState.open = false;
|
||||
VisualizerState.set-enabled(false);
|
||||
}
|
||||
}
|
||||
QbzIcon {
|
||||
source: @image-url("../assets/icons/x.svg");
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
x: 10px;
|
||||
y: Math.round((parent.height - self.height) / 2 / 1px) * 1px;
|
||||
tint: #ffffffcc;
|
||||
}
|
||||
Text {
|
||||
x: 34px;
|
||||
y: 0px;
|
||||
height: parent.height;
|
||||
width: parent.width - 44px;
|
||||
text: @tr("Exit Immersive");
|
||||
color: #ffffffcc;
|
||||
font-size: 13px;
|
||||
vertical-alignment: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TinyBar {
|
||||
x: 16px;
|
||||
y: 71px;
|
||||
height: 10px;
|
||||
width: root.width - 32px;
|
||||
value: NowPlayingState.progress;
|
||||
changed(v) => { NowPlayingState.seek(Math.min(v, NowPlayingState.seekable-max)); }
|
||||
}
|
||||
}
|
||||
|
||||
export component ImmersiveView inherits Rectangle {
|
||||
callback media-action(string, string, string);
|
||||
|
||||
property <bool> chrome-visible: true;
|
||||
|
||||
background: #0a0a0b;
|
||||
clip: true;
|
||||
|
||||
function wake() {
|
||||
root.chrome-visible = true;
|
||||
hide-timer.running = false;
|
||||
hide-timer.running = true;
|
||||
}
|
||||
|
||||
keys := FocusScope {
|
||||
key-pressed(event) => {
|
||||
if (event.text == Key.Escape) {
|
||||
ImmersiveState.open = false;
|
||||
VisualizerState.set-enabled(false);
|
||||
return accept;
|
||||
}
|
||||
return reject;
|
||||
}
|
||||
focus-timer := Timer {
|
||||
interval: 30ms;
|
||||
running: true;
|
||||
triggered => {
|
||||
self.running = false;
|
||||
keys.focus();
|
||||
root.wake();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hide-timer := Timer {
|
||||
interval: 4000ms;
|
||||
triggered => {
|
||||
root.chrome-visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Pointer movement wakes the chrome. `TouchArea.moved` only fires while
|
||||
// pressed in Slint, so watch mouse-x/mouse-y changes instead.
|
||||
hover := TouchArea {
|
||||
changed mouse-x => { root.wake(); }
|
||||
changed mouse-y => { root.wake(); }
|
||||
}
|
||||
|
||||
ImmersiveAtmosphere {
|
||||
source-image: ImmersiveState.bg-image;
|
||||
fallback-image: NowPlayingState.artwork-large.width > 0
|
||||
? NowPlayingState.artwork-large
|
||||
: NowPlayingState.artwork;
|
||||
dim: 0.15;
|
||||
}
|
||||
|
||||
ImmersivePlayerBar {
|
||||
viewport-width: root.width;
|
||||
x: Math.round((root.width - self.width) / 2 / 1px) * 1px;
|
||||
y: root.height - self.height - 24px;
|
||||
opacity: root.chrome-visible ? 1.0 : 0.0;
|
||||
animate opacity { duration: 300ms; }
|
||||
wake => { root.wake(); }
|
||||
media-action(kind, id, action) => { root.media-action(kind, id, action); }
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
import { Theme } from "../foundation/semantic-colors.slint";
|
||||
import { Layout } from "../foundation/layout.slint";
|
||||
import { Radius } from "../foundation/radius.slint";
|
||||
import { NavState, ContentView, ShellState, OfflineState, PlaylistState } from "../state.slint";
|
||||
import { NavState, ContentView, ShellState, OfflineState, PlaylistState, ImmersiveState } from "../state.slint";
|
||||
import { Sidebar } from "Sidebar.slint";
|
||||
import { HeaderBar } from "HeaderBar.slint";
|
||||
import { HeaderMenuOverlay } from "HeaderMenuOverlay.slint";
|
||||
@@ -33,6 +33,7 @@ import { TagEditorModal } from "../primitives/TagEditorModal.slint";
|
||||
import { PlaylistImportModal } from "../primitives/PlaylistImportModal.slint";
|
||||
import { TrackInfoModal } from "../album/TrackInfoModal.slint";
|
||||
import { AlbumCreditsModal } from "../album/AlbumCreditsModal.slint";
|
||||
import { ImmersiveView } from "../immersive/ImmersiveView.slint";
|
||||
import { QconnectDevModal } from "QconnectDevModal.slint";
|
||||
import { DragGhost } from "../primitives/DragGhost.slint";
|
||||
import { Toast } from "../primitives/Toast.slint";
|
||||
@@ -509,6 +510,14 @@ export component AppShell inherits Rectangle {
|
||||
// Floating track-drag ghost (follows the cursor, on top of all).
|
||||
DragGhost { }
|
||||
|
||||
if ImmersiveState.open: ImmersiveView {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
media-action(kind, id, action) => {
|
||||
root.media-action(kind, id, action);
|
||||
}
|
||||
}
|
||||
|
||||
// In-app toast notifications — bottom-center, above everything.
|
||||
Toast { }
|
||||
|
||||
|
||||
@@ -807,7 +807,6 @@ export component PlayerBar inherits Rectangle {
|
||||
ContextMenuItem {
|
||||
icon: @image-url("../assets/icons/maximize-2.svg");
|
||||
label: @tr("Immersive");
|
||||
enabled: false;
|
||||
clicked => {
|
||||
root.media-action("npb-view", "", "immersive");
|
||||
}
|
||||
|
||||
@@ -542,7 +542,6 @@ export component PlayerBarSmall inherits Rectangle {
|
||||
ContextMenuItem {
|
||||
icon: @image-url("../assets/icons/maximize-2.svg");
|
||||
label: @tr("Immersive");
|
||||
enabled: false;
|
||||
clicked => {
|
||||
root.media-action("npb-view", "", "immersive");
|
||||
}
|
||||
|
||||
@@ -2944,6 +2944,28 @@ export struct QueueItem {
|
||||
is-ephemeral: bool,
|
||||
}
|
||||
|
||||
// ImmersiveView audio visualizers. Driven at ~30fps by the Rust drain
|
||||
// (crate::visualizer) from the frontend-agnostic FFT producer in qbz-audio.
|
||||
// Magnitudes are normalized 0..1. The tap stays disabled (no capture / no FFT
|
||||
// cost) until the immersive view calls set-enabled(true) on open.
|
||||
export global VisualizerState {
|
||||
in property <bool> enabled: false;
|
||||
in property <[float]> bars; // 16 log-spaced FFT bars (viz:data)
|
||||
in property <[float]> spectral; // 512 spectral-ribbon bands (viz:spectral)
|
||||
in property <[float]> energy; // 5 energy bands: sub-bass..air (viz:energy)
|
||||
in property <[float]> waveform; // 256 L then 256 R samples (viz:waveform)
|
||||
in property <float> transient: 0; // last transient intensity; UI decays it
|
||||
callback set-enabled(bool);
|
||||
}
|
||||
|
||||
// Full-window immersive now-playing overlay. The first Slint slice ships the
|
||||
// Album Reactive focus view only; broader panels can hang off this same state.
|
||||
export global ImmersiveState {
|
||||
in-out property <bool> open: false;
|
||||
in property <image> bg-image;
|
||||
in property <color> glow-color: #6464ff59;
|
||||
}
|
||||
|
||||
// Now-playing / transport state. The Rust playback poll loop pushes the
|
||||
// live values here; the PlayerBar and SeekBar bind to it directly.
|
||||
export global NowPlayingState {
|
||||
|
||||
@@ -1,333 +1,46 @@
|
||||
//! FFT Processing for Audio Visualization
|
||||
//! Tauri adapter for the frontend-agnostic visualizer producer.
|
||||
//!
|
||||
//! Runs on a dedicated thread, completely separate from audio playback.
|
||||
//! Uses spectrum-analyzer crate for efficient FFT computation.
|
||||
//! The DSP now lives in `qbz_audio::visualizer::processor`. This file only maps
|
||||
//! each computed [`VizFrame`] back to the historical `viz:*` Tauri events, with a
|
||||
//! byte-identical little-endian `f32` payload, so the Svelte frontend listeners
|
||||
//! are unchanged.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use qbz_audio::SpectralAnalyzer;
|
||||
use spectrum_analyzer::scaling::divide_by_N_sqrt;
|
||||
use spectrum_analyzer::windows::hann_window;
|
||||
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
|
||||
use qbz_audio::visualizer::{VizFrame, VizSink};
|
||||
use tauri::{AppHandle, Emitter};
|
||||
|
||||
use super::{RingBuffer, FFT_SIZE, NUM_BARS, TARGET_FPS};
|
||||
|
||||
/// Shared state for the visualizer thread
|
||||
pub struct VisualizerState {
|
||||
pub ring_buffer: Arc<RingBuffer>,
|
||||
pub enabled: Arc<AtomicBool>,
|
||||
pub sample_rate: Arc<AtomicU32>,
|
||||
/// Re-emits visualization frames as the legacy binary `viz:*` Tauri events.
|
||||
pub struct TauriVizSink {
|
||||
app: AppHandle,
|
||||
}
|
||||
|
||||
/// Start the FFT processing thread
|
||||
pub fn start_visualizer_thread(state: VisualizerState, app_handle: AppHandle) {
|
||||
std::thread::Builder::new()
|
||||
.name("visualizer-fft".to_string())
|
||||
.spawn(move || {
|
||||
run_fft_loop(state, app_handle);
|
||||
})
|
||||
.expect("Failed to spawn visualizer thread");
|
||||
|
||||
log::info!("Visualizer FFT thread started");
|
||||
}
|
||||
|
||||
/// Number of energy bands for the Energy Bands visualizer
|
||||
const NUM_ENERGY_BANDS: usize = 5;
|
||||
const NUM_SPECTRAL_BANDS: usize = 512;
|
||||
const SPECTRAL_UPDATE_RATE_HZ: u32 = 58;
|
||||
const SPECTRAL_SMOOTHING: f32 = 0.30;
|
||||
|
||||
/// Energy band frequency ranges (Hz):
|
||||
/// Sub-bass (20-60), Bass (60-250), Mids (250-2k), Presence (2k-6k), Air (6k-20k)
|
||||
const ENERGY_BAND_RANGES: [(f32, f32); NUM_ENERGY_BANDS] = [
|
||||
(20.0, 60.0),
|
||||
(60.0, 250.0),
|
||||
(250.0, 2000.0),
|
||||
(2000.0, 6000.0),
|
||||
(6000.0, 20000.0),
|
||||
];
|
||||
|
||||
/// Main FFT processing loop
|
||||
fn run_fft_loop(state: VisualizerState, app_handle: AppHandle) {
|
||||
// Pre-allocate all buffers to avoid allocations in the hot path
|
||||
let mut samples = vec![0.0f32; FFT_SIZE];
|
||||
let mut windowed = vec![0.0f32; FFT_SIZE];
|
||||
let mut output = vec![0.0f32; NUM_BARS];
|
||||
let mut smoothed = vec![0.0f32; NUM_BARS];
|
||||
|
||||
// Waveform buffer: 256 L + 256 R = 512 floats
|
||||
const WAVEFORM_POINTS: usize = 256;
|
||||
let mut waveform_buf = vec![0.0f32; WAVEFORM_POINTS * 2];
|
||||
|
||||
// Energy bands state
|
||||
let mut energy_bands = [0.0f32; NUM_ENERGY_BANDS];
|
||||
let mut smoothed_energy = [0.0f32; NUM_ENERGY_BANDS];
|
||||
let mut spectral_analyzer = SpectralAnalyzer::new(
|
||||
state.sample_rate.load(Ordering::Relaxed),
|
||||
FFT_SIZE,
|
||||
NUM_SPECTRAL_BANDS,
|
||||
SPECTRAL_UPDATE_RATE_HZ,
|
||||
SPECTRAL_SMOOTHING,
|
||||
);
|
||||
let mut spectral_bytes = vec![0u8; NUM_SPECTRAL_BANDS * std::mem::size_of::<f32>()];
|
||||
|
||||
// Transient detection state
|
||||
let mut prev_rms = 0.0f32;
|
||||
let mut transient_cooldown = 0u32; // frames remaining in cooldown
|
||||
const TRANSIENT_THRESHOLD: f32 = 0.04; // RMS jump threshold (sensitive)
|
||||
const TRANSIENT_COOLDOWN_FRAMES: u32 = 3; // ~100ms at 30fps
|
||||
|
||||
// Smoothing factor: 0 = no smoothing, higher = more smoothing
|
||||
const SMOOTHING: f32 = 0.65;
|
||||
|
||||
let frame_duration = Duration::from_micros(1_000_000 / TARGET_FPS);
|
||||
|
||||
loop {
|
||||
let frame_start = Instant::now();
|
||||
|
||||
if state.enabled.load(Ordering::Relaxed) {
|
||||
let sample_rate = state.sample_rate.load(Ordering::Relaxed);
|
||||
|
||||
// Get samples from ring buffer
|
||||
state.ring_buffer.snapshot(&mut samples);
|
||||
|
||||
// Emit compact, progressive spectrogram bands for Spectral Ribbon.
|
||||
if spectral_analyzer.process_audio_frame(&samples, sample_rate) {
|
||||
let spectral = spectral_analyzer.get_latest_bands();
|
||||
for (idx, value) in spectral.iter().enumerate() {
|
||||
let offset = idx * 4;
|
||||
spectral_bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
let _ = app_handle.emit("viz:spectral", spectral_bytes.clone());
|
||||
}
|
||||
|
||||
// Apply Hann window to reduce spectral leakage
|
||||
let window = hann_window(&samples);
|
||||
for (i, (sample, win)) in samples.iter().zip(window.iter()).enumerate() {
|
||||
windowed[i] = sample * win;
|
||||
}
|
||||
|
||||
// Compute FFT spectrum
|
||||
match samples_fft_to_spectrum(
|
||||
&windowed,
|
||||
sample_rate,
|
||||
FrequencyLimit::Range(20.0, 20000.0),
|
||||
Some(÷_by_N_sqrt),
|
||||
) {
|
||||
Ok(spectrum) => {
|
||||
// Map spectrum to logarithmic frequency bars
|
||||
map_to_log_bars(&spectrum, &mut output);
|
||||
|
||||
// Apply smoothing for visual continuity
|
||||
for i in 0..NUM_BARS {
|
||||
let new = output[i];
|
||||
// Faster attack, slower decay for punchy visuals
|
||||
if new > smoothed[i] {
|
||||
smoothed[i] = smoothed[i] * 0.3 + new * 0.7; // Fast attack
|
||||
} else {
|
||||
smoothed[i] = smoothed[i] * SMOOTHING + new * (1.0 - SMOOTHING);
|
||||
// Slow decay
|
||||
}
|
||||
output[i] = smoothed[i];
|
||||
}
|
||||
|
||||
// Send to frontend as binary data
|
||||
let bytes: Vec<u8> = output.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
|
||||
let _ = app_handle.emit("viz:data", bytes);
|
||||
|
||||
// --- Energy Bands: compute RMS per frequency band from spectrum ---
|
||||
let data = spectrum.data();
|
||||
for (band_idx, &(lo, hi)) in ENERGY_BAND_RANGES.iter().enumerate() {
|
||||
let mut sum_sq = 0.0f32;
|
||||
let mut count = 0u32;
|
||||
for (freq, magnitude) in data.iter() {
|
||||
let f = freq.val();
|
||||
if f >= lo && f < hi {
|
||||
let mag = magnitude.val();
|
||||
sum_sq += mag * mag;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
let rms = if count > 0 {
|
||||
(sum_sq / count as f32).sqrt()
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
// Compress and normalize
|
||||
let compressed = (rms * 6.0).powf(0.5).clamp(0.0, 1.0);
|
||||
// Smooth: fast attack, slow decay
|
||||
if compressed > smoothed_energy[band_idx] {
|
||||
smoothed_energy[band_idx] =
|
||||
smoothed_energy[band_idx] * 0.2 + compressed * 0.8;
|
||||
} else {
|
||||
smoothed_energy[band_idx] =
|
||||
smoothed_energy[band_idx] * 0.85 + compressed * 0.15;
|
||||
}
|
||||
energy_bands[band_idx] = smoothed_energy[band_idx];
|
||||
}
|
||||
|
||||
let energy_bytes: Vec<u8> =
|
||||
energy_bands.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
let _ = app_handle.emit("viz:energy", energy_bytes);
|
||||
|
||||
// --- Transient Detection: detect sharp RMS jumps ---
|
||||
// Use raw (pre-smoothed) RMS for transient sensitivity
|
||||
// Weight bass/sub-bass more heavily for beat detection
|
||||
let raw_rms = {
|
||||
let mut raw_sum = 0.0f32;
|
||||
for (band_idx, &(lo, hi)) in ENERGY_BAND_RANGES.iter().enumerate() {
|
||||
let mut sum_sq = 0.0f32;
|
||||
let mut cnt = 0u32;
|
||||
for (freq, magnitude) in data.iter() {
|
||||
let f = freq.val();
|
||||
if f >= lo && f < hi {
|
||||
let mag = magnitude.val();
|
||||
sum_sq += mag * mag;
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
let band_rms = if cnt > 0 {
|
||||
(sum_sq / cnt as f32).sqrt()
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
// Bass/sub-bass weighted 2x for beat detection
|
||||
let weight = if band_idx < 2 { 2.0 } else { 1.0 };
|
||||
raw_sum += (band_rms * 6.0).powf(0.5).clamp(0.0, 1.0) * weight;
|
||||
}
|
||||
raw_sum / (NUM_ENERGY_BANDS as f32 + 2.0) // account for extra bass weight
|
||||
};
|
||||
let rms_delta = raw_rms - prev_rms;
|
||||
|
||||
if transient_cooldown > 0 {
|
||||
transient_cooldown -= 1;
|
||||
}
|
||||
|
||||
if rms_delta > TRANSIENT_THRESHOLD && transient_cooldown == 0 {
|
||||
// Transient detected! Emit intensity (0.0 - 1.0)
|
||||
let intensity = (rms_delta * 5.0).clamp(0.0, 1.0);
|
||||
let transient_bytes: Vec<u8> = intensity.to_le_bytes().to_vec();
|
||||
let _ = app_handle.emit("viz:transient", transient_bytes);
|
||||
transient_cooldown = TRANSIENT_COOLDOWN_FRAMES;
|
||||
}
|
||||
|
||||
prev_rms = raw_rms;
|
||||
}
|
||||
Err(e) => {
|
||||
log::debug!("FFT error: {:?}", e);
|
||||
impl TauriVizSink {
|
||||
pub fn new(app: AppHandle) -> Self {
|
||||
Self { app }
|
||||
}
|
||||
}
|
||||
|
||||
// Emit raw waveform data for oscilloscope (stereo L/R)
|
||||
// samples[] is interleaved: L0, R0, L1, R1, ...
|
||||
// 1024 samples = 512 stereo pairs → downsample to 256 per channel
|
||||
let stereo_pairs = FFT_SIZE / 2; // 512
|
||||
let step = stereo_pairs / WAVEFORM_POINTS; // 512/256 = 2
|
||||
for i in 0..WAVEFORM_POINTS {
|
||||
let base = i * step * 2; // index into interleaved buffer
|
||||
waveform_buf[i] = samples[base]; // L
|
||||
waveform_buf[WAVEFORM_POINTS + i] = samples[base + 1]; // R
|
||||
impl VizSink for TauriVizSink {
|
||||
fn submit(&self, frame: VizFrame) {
|
||||
match frame {
|
||||
VizFrame::Viz16(bars) => {
|
||||
let bytes: Vec<u8> = bars.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
let _ = self.app.emit("viz:data", bytes);
|
||||
}
|
||||
let waveform_bytes: Vec<u8> =
|
||||
waveform_buf.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
let _ = app_handle.emit("viz:waveform", waveform_bytes);
|
||||
VizFrame::Wave256x2(wave) => {
|
||||
let bytes: Vec<u8> = wave.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
let _ = self.app.emit("viz:waveform", bytes);
|
||||
}
|
||||
|
||||
// Maintain target FPS
|
||||
let elapsed = frame_start.elapsed();
|
||||
if elapsed < frame_duration {
|
||||
std::thread::sleep(frame_duration - elapsed);
|
||||
VizFrame::Spectral512(bands) => {
|
||||
let bytes: Vec<u8> = bands.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
let _ = self.app.emit("viz:spectral", bytes);
|
||||
}
|
||||
VizFrame::Energy5(energy) => {
|
||||
let bytes: Vec<u8> = energy.iter().flat_map(|f| f.to_le_bytes()).collect();
|
||||
let _ = self.app.emit("viz:energy", bytes);
|
||||
}
|
||||
VizFrame::Transient1(intensity) => {
|
||||
let _ = self.app.emit("viz:transient", intensity.to_le_bytes().to_vec());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Map spectrum data to logarithmically-spaced frequency bars
|
||||
///
|
||||
/// Human hearing is logarithmic, so we use log-spaced bars to match
|
||||
/// how we perceive frequency. This gives equal visual weight to
|
||||
/// bass, mids, and treble.
|
||||
fn map_to_log_bars(spectrum: &spectrum_analyzer::FrequencySpectrum, output: &mut [f32]) {
|
||||
let num_bars = output.len();
|
||||
|
||||
// Frequency range (Hz)
|
||||
const MIN_FREQ: f32 = 20.0;
|
||||
const MAX_FREQ: f32 = 20000.0;
|
||||
|
||||
let min_log = MIN_FREQ.ln();
|
||||
let max_log = MAX_FREQ.ln();
|
||||
|
||||
// Get spectrum data
|
||||
let data = spectrum.data();
|
||||
|
||||
for (i, bar) in output.iter_mut().enumerate() {
|
||||
// Calculate logarithmic frequency bounds for this bar
|
||||
let t_low = i as f32 / num_bars as f32;
|
||||
let t_high = (i + 1) as f32 / num_bars as f32;
|
||||
|
||||
let freq_low = (min_log + (max_log - min_log) * t_low).exp();
|
||||
let freq_high = (min_log + (max_log - min_log) * t_high).exp();
|
||||
|
||||
// Find all frequency bins that fall within this bar's range
|
||||
let mut sum = 0.0f32;
|
||||
let mut count = 0u32;
|
||||
|
||||
for (freq, magnitude) in data.iter() {
|
||||
let f = freq.val();
|
||||
if f >= freq_low && f < freq_high {
|
||||
// Apply perceptual weighting (boost bass slightly)
|
||||
let weight = if f < 200.0 {
|
||||
1.5 // Bass boost
|
||||
} else if f < 2000.0 {
|
||||
1.0 // Mids
|
||||
} else {
|
||||
0.8 // Reduce harsh highs
|
||||
};
|
||||
|
||||
sum += magnitude.val() * weight;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Average magnitude for this bar
|
||||
let avg = if count > 0 { sum / count as f32 } else { 0.0 };
|
||||
|
||||
// Apply dynamic range compression and normalize
|
||||
// This makes quiet passages more visible while preventing clipping
|
||||
let compressed = (avg * 4.0).powf(0.6);
|
||||
*bar = compressed.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_log_frequency_distribution() {
|
||||
// Verify that frequency bars are logarithmically distributed
|
||||
let num_bars = NUM_BARS; // Use the actual constant
|
||||
let min_log = 20.0_f32.ln();
|
||||
let max_log = 20000.0_f32.ln();
|
||||
|
||||
let mut freqs = Vec::new();
|
||||
for i in 0..num_bars {
|
||||
let t = i as f32 / num_bars as f32;
|
||||
let freq = (min_log + (max_log - min_log) * t).exp();
|
||||
freqs.push(freq);
|
||||
}
|
||||
|
||||
// First bar should be around 20Hz
|
||||
assert!(freqs[0] > 19.0 && freqs[0] < 25.0);
|
||||
|
||||
// Middle bar (~16 for 32 bars) should be around 630Hz (geometric mean of 20 and 20000)
|
||||
let mid = num_bars / 2;
|
||||
assert!(freqs[mid] > 500.0 && freqs[mid] < 800.0);
|
||||
|
||||
// Last bar should approach 20000Hz (but won't reach it since t < 1.0)
|
||||
assert!(freqs[num_bars - 1] > 10000.0);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
//! on a dedicated thread.
|
||||
|
||||
mod fft_processor;
|
||||
pub use fft_processor::{start_visualizer_thread, VisualizerState};
|
||||
use fft_processor::TauriVizSink;
|
||||
pub use qbz_audio::{RingBuffer, TappedSource, VisualizerTap};
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@@ -49,12 +49,9 @@ impl Visualizer {
|
||||
log::debug!("Visualizer FFT thread already started, skipping");
|
||||
return;
|
||||
}
|
||||
let state = VisualizerState {
|
||||
ring_buffer: self.tap.ring_buffer.clone(),
|
||||
enabled: self.tap.enabled.clone(),
|
||||
sample_rate: self.tap.sample_rate.clone(),
|
||||
};
|
||||
start_visualizer_thread(state, app_handle);
|
||||
let sink = std::sync::Arc::new(TauriVizSink::new(app_handle));
|
||||
qbz_audio::visualizer::spawn_visualizer_thread(self.tap.clone(), sink);
|
||||
log::info!("Visualizer FFT thread started");
|
||||
}
|
||||
|
||||
/// Enable or disable visualization
|
||||
|
||||
Reference in new issue
Block a user