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:
parent
7a7974d6bb
commit
37e707069a
2 changed files with 18 additions and 7 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue