From eab57dc4cdb6a6389ed57c08ba98e455f3bee5df Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Tue, 20 Jan 2026 15:04:38 -0600 Subject: [PATCH] feat(radio): add deterministic next-track selection cx --- src-tauri/src/radio_engine/engine.rs | 105 +++++++++++++++++++++++++++ src-tauri/src/radio_engine/mod.rs | 2 + 2 files changed, 107 insertions(+) create mode 100644 src-tauri/src/radio_engine/engine.rs diff --git a/src-tauri/src/radio_engine/engine.rs b/src-tauri/src/radio_engine/engine.rs new file mode 100644 index 000000000..d91b47002 --- /dev/null +++ b/src-tauri/src/radio_engine/engine.rs @@ -0,0 +1,105 @@ +use super::db::{RadioDb, RadioTrackRef}; + +pub struct RadioEngine { + db: RadioDb, +} + +impl RadioEngine { + pub fn new(db: RadioDb) -> Self { + Self { db } + } + + pub fn db(&self) -> &RadioDb { + &self.db + } + + fn splitmix64(mut x: u64) -> u64 { + x = x.wrapping_add(0x9E3779B97F4A7C15); + let mut z = x; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB); + z ^ (z >> 31) + } + + fn base_weight(distance: u8) -> u32 { + match distance { + 0 => 3, + 1 => 3, + 2 => 1, + _ => 0, + } + } + + fn effective_weight(track: &RadioTrackRef, reseed_tick: bool) -> u32 { + let mut w = Self::base_weight(track.distance); + if w == 0 { + return 0; + } + + if reseed_tick { + if track.distance == 0 { + w = w.saturating_add(6); + } + if track.source == "curated_playlist" { + w = w.saturating_add(2); + } + } + w + } + + pub fn next_track(&self, session_id: &str) -> Result { + let session = self.db.load_session(session_id)?; + + let mut spacing = session.artist_spacing; + let candidates = loop { + let recent_artists = self.db.get_recent_artist_ids(session_id, spacing)?; + let cands = self.db.get_unused_candidates(session_id, &recent_artists)?; + if !cands.is_empty() || spacing == 0 { + break cands; + } + spacing = spacing.saturating_sub(1); + }; + + if candidates.is_empty() { + return Err("Radio session exhausted: no eligible tracks left".to_string()); + } + + let next_index = session.selection_count.saturating_add(1); + let reseed_tick = session.reseed_every > 0 && (next_index % session.reseed_every == 0); + + let mut total_weight: u64 = 0; + let mut weights: Vec = Vec::with_capacity(candidates.len()); + for t in &candidates { + let w = Self::effective_weight(t, reseed_tick); + weights.push(w); + total_weight = total_weight.saturating_add(w as u64); + } + + if total_weight == 0 { + return Err("Radio session exhausted: all candidate weights are zero".to_string()); + } + + let r = Self::splitmix64(session.rng_seed ^ (next_index as u64)); + let mut pick = (r % total_weight) as u64; + + let mut chosen_idx = 0usize; + for (idx, w) in weights.iter().enumerate() { + let w = *w as u64; + if pick < w { + chosen_idx = idx; + break; + } + pick = pick.saturating_sub(w); + } + + let chosen = candidates + .get(chosen_idx) + .cloned() + .ok_or_else(|| "Radio selection failed: chosen candidate missing".to_string())?; + + self.db + .mark_played(session_id, session.selection_count, &chosen)?; + + Ok(chosen) + } +} diff --git a/src-tauri/src/radio_engine/mod.rs b/src-tauri/src/radio_engine/mod.rs index 3fa372dd6..17fe82174 100644 --- a/src-tauri/src/radio_engine/mod.rs +++ b/src-tauri/src/radio_engine/mod.rs @@ -1,3 +1,5 @@ pub mod db; +pub mod engine; pub use db::{RadioDb, RadioSeed, RadioSession, RadioTrackRef}; +pub use engine::RadioEngine;