Music card at 20 fps storing only changed pixels; link button on the panel

Frames now hold just the pixels that moved, so the GIF is smoother and
smaller. A new button shows the current song's link privately to whoever
pressed it, which also evens out the panel's second row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-06 21:18:50 -06:00
commit 37e707069a
3 changed files with 18 additions and 7 deletions

View file

@ -105,6 +105,6 @@ Plan (backward compatible with the existing file):
- Dashboard controls live in `web/dashboard/static/controls.js` (faders, switches, segments, searchable pickers, colour picker). Every control fires a bubbling `input`; sections go through one save bar.
- The server page has only been looked at with faked API answers (`--web-dev` has no Discord cache). Not checked on the live bot yet.
- Fixed: role menus published from the dashboard no longer claim roles they didn't make (taking one out used to delete a server's own role); zero ids no longer panic `D!ban`; announcement channels show in the pickers; static files revalidate after an update.
- Music panel: an animated GIF drawn by `commands/music/card.rs`, laid out like the website's player: cover and title, moving bars, clock and progress. Each GIF is 30 s at about 6 fps (0.55 s to make on the Celeron) and is replaced about every 20 s while people listen, its clock started ahead by the measured upload time; a new panel posts a still JPEG first, and paused stays still. Bar heights follow the song's real loudness (`source::peaks`), their shape is drawn, not a real spectrum. Bar colours come from the album cover (greys for a cover with no colour). Typed searches go to YouTube Music for the real artist, album, year and square art, falling back to plain YouTube; links keep their own details. Without Attach Files the panel falls back to the text card.
- Music panel: an animated GIF drawn by `commands/music/card.rs`, laid out like the website's player: cover and title, moving bars, clock and progress. Each GIF is 30 s at 20 fps, storing only changed pixels and is replaced about every 20 s while people listen, its clock started ahead by the measured upload time; a new panel posts a still JPEG first, and paused stays still. Bar heights follow the song's real loudness (`source::peaks`), their shape is drawn, not a real spectrum. Bar colours come from the album cover (greys for a cover with no colour). Typed searches go to YouTube Music for the real artist, album, year and square art, falling back to plain YouTube; links keep their own details. Without Attach Files the panel falls back to the text card. The 🔗 button shows the song's link to whoever pressed it, privately.
- Voice: a failed join now forgets the call, and a song whose position stands still for 45 s tells the channel and leaves. Joins failed on 2026-11-06 because the Chromebook's network gives an IPv6 address with no IPv6 route; /etc/gai.conf now prefers IPv4 (backup /root/gai.conf.bak-2026-11-06). A running bot only picks that up after a restart.
- Known gap, unchanged: on a stage channel the bot can't unsuppress itself (twilight's `suppress()` only sets true).

View file

@ -28,9 +28,11 @@ const TRACK_RIGHT: i32 = WIDTH as i32 - 104;
/// Everything below this line changes from frame to frame.
const MOVING_TOP: i32 = BAR_TOP - 4;
/// Six frames a second for 30 seconds. GIF delays are in hundredths of a second.
const FRAME_DELAY: u16 = 16;
const FRAMES: usize = 188;
/// Twenty frames a second for 30 seconds. GIF delays are in hundredths of a second.
const FRAME_DELAY: u16 = 5;
const FRAMES: usize = 600;
/// A palette index no colour uses: in a frame it means "this pixel hasn't changed".
const UNCHANGED: u8 = 255;
/// How much of the palette the cover gets; the rest is kept for the interface's own colours.
const COVER_COLOURS: usize = 200;
@ -237,7 +239,7 @@ impl Palette {
}
colours.extend([SLOT, TRACK]);
colours.extend(bars);
colours.truncate(256);
colours.truncate(UNCHANGED as usize);
Self { cover, colours, known: HashMap::new() }
}
@ -300,6 +302,7 @@ pub fn moving(view: &View, lead: f64) -> Vec<u8> {
moment(&mut first, view, &bars, at(0));
let in_cover = |x: u32, y: u32| (PAD as u32..PAD as u32 + COVER).contains(&x) && (PAD as u32..PAD as u32 + COVER).contains(&y);
let buffer: Vec<u8> = first.enumerate_pixels().map(|(x, y, p)| if in_cover(x, y) { palette.index_cover([p[0], p[1], p[2]]) } else { palette.index([p[0], p[1], p[2]]) }).collect();
let mut previous = buffer[MOVING_TOP as usize * Strip::WIDTH..].to_vec();
if encoder.write_frame(&gif::Frame { width: WIDTH as u16, height: HEIGHT as u16, buffer: buffer.into(), delay: FRAME_DELAY, ..Default::default() }).is_err() {
return still(view);
}
@ -333,7 +336,10 @@ pub fn moving(view: &View, lead: f64) -> Vec<u8> {
let target = (clock_y + row) * Strip::WIDTH + clock_x;
strip.pixels[target..target + clock_w].copy_from_slice(&digits[row * clock_w..(row + 1) * clock_w]);
}
let frame = gif::Frame { top: MOVING_TOP as u16, width: WIDTH as u16, height: (HEIGHT as i32 - MOVING_TOP) as u16, buffer: strip.pixels.into(), delay: FRAME_DELAY, ..Default::default() };
// only what changed goes in the file: at this frame rate that's most of what keeps it small
let changes: Vec<u8> = strip.pixels.iter().zip(&previous).map(|(now, before)| if now == before { UNCHANGED } else { *now }).collect();
previous = strip.pixels;
let frame = gif::Frame { top: MOVING_TOP as u16, width: WIDTH as u16, height: (HEIGHT as i32 - MOVING_TOP) as u16, buffer: changes.into(), delay: FRAME_DELAY, transparent: Some(UNCHANGED), ..Default::default() };
if encoder.write_frame(&frame).is_err() {
return still(view);
}
@ -369,7 +375,7 @@ mod tests {
assert_eq!(frames.len(), FRAMES);
assert_eq!(frames[0].buffer().dimensions(), (WIDTH, HEIGHT));
assert_ne!(frames[0].buffer().as_raw(), frames[40].buffer().as_raw(), "the bars have to move");
assert!(bytes.len() < 2_000_000, "{} bytes", bytes.len());
assert!(bytes.len() < 3_000_000, "{} bytes", bytes.len());
}
#[test]

View file

@ -627,6 +627,7 @@ fn panel_card(p: &Player, image: Option<&str>) -> Option<Component> {
emoji_button(&id("queue"), None, "📜", false),
emoji_button(&id("voldown"), None, "🔉", false),
emoji_button(&id("volup"), None, "🔊", false),
emoji_button(&id("link"), None, "🔗", false),
style(ButtonStyle::Danger, emoji_button(&id("leave"), None, "👋", false)),
]);
Some(card.build())
@ -805,6 +806,10 @@ pub async fn on_component(state: &Arc<State>, i: &Interaction) {
let first = pager::register(user.id.get(), queue_pages(player.as_ref()), Duration::from_secs(120));
return interact::private_card(state, i, first).await;
}
if action == "link" {
let url = player.as_ref().and_then(|p| p.lock().current.as_ref().map(|c| c.track.url.clone())).filter(|u| !u.is_empty());
return interact::private_text(state, i, &url.map_or("Nothing is playing right now.".to_string(), |u| format!("🔗 {u}"))).await;
}
let playing = player.as_ref().is_some_and(|p| p.lock().current.is_some());
let Some(p) = player.filter(|_| playing) else {
if action == "leave" && bot_channel(state, guild).await.is_some() {