# Beat-synced dancing stick figure for alku.mp3, rendered with ggplot2 and # encoded with av. Run tempo.R first to produce tempo.rds. library(ggplot2) library(av) library(parallel) tempo <- readRDS("tempo.rds") bpm <- tempo$bpm bps <- bpm / 60 offset <- tempo$offset fps <- 25 n_frames <- ceiling(tempo$duration * fps) frame_dir <- Sys.getenv("FRAME_DIR", file.path(tempdir(), "dance_frames")) dir.create(frame_dir, showWarnings = FALSE, recursive = TRUE) beats_per_move <- 8 move_names <- c("bounce", "raise the roof", "disco point", "jumping jacks") # Pose parameters for one move at beat phase p (0 = on the beat) of beat b. # Arm angles are measured from straight down, positive = away from the body. pose_for <- function(move, b, p) { dip <- 0.5 * (1 + cos(2 * pi * p)) # 1 on the beat, 0 halfway between sway <- cos(pi * (b + p)) # +-1 on alternating beats switch(move, # 1: bounce with forearms swinging up list(la1 = 0.5 + 0.3 * dip, la2 = 2.1 + 0.3 * dip, ra1 = 0.5 + 0.3 * (1 - dip), ra2 = 2.1 + 0.3 * (1 - dip), lean = 0.08 * sway, shift = 0.1 * sway, spread = 0.3, jump = 0, dip = dip), # 2: raise the roof list(la1 = 1.9 + 0.35 * (1 - dip), la2 = 3.05, ra1 = 1.9 + 0.35 * (1 - dip), ra2 = 3.05, lean = 0.05 * sway, shift = 0, spread = 0.35, jump = 0, dip = dip), # 3: disco point, left hand on hip { up <- (sway + 1) / 2 list(la1 = 0.8, la2 = -0.9, ra1 = -0.5 + 3.1 * up, ra2 = -0.5 + 3.1 * up, lean = -0.15 * sway, shift = 0.2 * sway, spread = 0.3, jump = 0, dip = dip) }, # 4: jumping jacks, land on every beat { air <- sin(pi * p) list(la1 = 0.25 + 2.6 * air, la2 = 0.25 + 2.6 * air, ra1 = 0.25 + 2.6 * air, ra2 = 0.25 + 2.6 * air, lean = 0, shift = 0, spread = 0.12 + 0.4 * air, jump = 0.3 * air, dip = dip) } ) } # Two-link inverse kinematics: knee position between hip and foot. knee_pos <- function(hip, foot, len, side) { d <- foot - hip dist <- sqrt(sum(d^2)) mid <- hip + d / 2 h <- sqrt(max(len^2 - (dist / 2)^2, 0)) perp <- c(-d[2], d[1]) / dist if (sign(perp[1]) != side) perp <- -perp # knees bend outward mid + h * perp } skeleton <- function(t) { beat <- max((t - offset) * bps, 0) b <- floor(beat) p <- beat - b seg <- b %/% beats_per_move move <- seg %% 4 + 1 prev_move <- (seg - 1) %% 4 + 1 pose <- pose_for(move, b, p) # Blend from the previous move over the first half beat of a new one into_seg <- beat - seg * beats_per_move if (seg > 0 && into_seg < 0.5) { w <- into_seg / 0.5 old <- pose_for(prev_move, b, p) pose <- Map(function(a, o) w * a + (1 - w) * o, pose, old) } hip <- c(pose$shift, 1.1 - 0.14 * pose$dip + pose$jump) dir <- c(sin(pose$lean), cos(pose$lean)) neck <- hip + 1.0 * dir head <- neck + 0.33 * dir shoulder <- neck - 0.08 * dir arm <- function(side, a1, a2) { elbow <- shoulder + 0.5 * c(side * sin(a1), -cos(a1)) hand <- elbow + 0.48 * c(side * sin(a2), -cos(a2)) list(elbow = elbow, hand = hand) } la <- arm(-1, pose$la1, pose$la2) ra <- arm(1, pose$ra1, pose$ra2) lfoot <- c(pose$shift * 0.3 - pose$spread, pose$jump) rfoot <- c(pose$shift * 0.3 + pose$spread, pose$jump) lknee <- knee_pos(hip, lfoot, 0.62, -1) rknee <- knee_pos(hip, rfoot, 0.62, 1) bones <- rbind( c(hip, neck), c(shoulder, la$elbow), c(la$elbow, la$hand), c(shoulder, ra$elbow), c(ra$elbow, ra$hand), c(hip, lknee), c(lknee, lfoot), c(hip, rknee), c(rknee, rfoot) ) list( bones = setNames(as.data.frame(bones), c("x", "y", "xend", "yend")), head = head, dip = pose$dip, jump = pose$jump, move = move_names[move], beat = b ) } circle <- function(center, r, n = 60) { a <- seq(0, 2 * pi, length.out = n) data.frame(x = center[1] + r * cos(a), y = center[2] + r * sin(a)) } draw_frame <- function(i) { t <- (i - 1) / fps s <- skeleton(t) flash <- if (t >= offset) s$dip^4 else 0 bg <- grDevices::rgb(0.07 + 0.10 * flash, 0.06 + 0.03 * flash, 0.16 + 0.14 * flash) shadow_w <- 0.55 - 0.25 * s$jump ggplot() + annotate("segment", x = -2.4, xend = 2.4, y = 0, yend = 0, colour = "grey45", linewidth = 0.8) + geom_polygon(aes(x, y), data = transform(circle(c(0, 0), 1), x = x * shadow_w, y = y * 0.06), fill = "black", alpha = 0.35) + geom_polygon(aes(x, y), data = circle(c(0, 0.03), 1) |> transform(x = x * (0.9 + 0.5 * flash), y = y * 0.08 * (1 + flash)), fill = "#ff5fa2", alpha = 0.25 * flash) + geom_segment(aes(x, y, xend = xend, yend = yend), data = s$bones, colour = "white", linewidth = 3.2, lineend = "round") + geom_polygon(aes(x, y), data = circle(s$head, 0.22), fill = bg, colour = "white", linewidth = 3.2) + annotate("text", x = -2.3, y = 2.75, hjust = 0, colour = "white", size = 6, label = sprintf("%.1f BPM", bpm), fontface = "bold") + annotate("text", x = -2.3, y = 2.5, hjust = 0, colour = "grey70", size = 4.5, label = s$move) + annotate("text", x = 2.3, y = 2.75, hjust = 1, colour = "#ff5fa2", size = 6, label = if (t >= offset) paste(rep("●", s$beat %% 4 + 1), collapse = " ") else "") + coord_fixed(xlim = c(-2.4, 2.4), ylim = c(-0.25, 2.9), expand = FALSE) + theme_void() + theme(plot.background = element_rect(fill = bg, colour = NA)) } files <- file.path(frame_dir, sprintf("frame_%05d.png", seq_len(n_frames))) invisible(mclapply(seq_len(n_frames), function(i) { ragg::agg_png(files[i], width = 1280, height = 840, res = 144) print(draw_frame(i)) dev.off() }, mc.cores = max(1, detectCores() - 1))) av_encode_video(files, output = "dance.mp4", framerate = fps, audio = "alku.mp3", vfilter = "format=yuv420p") cat("wrote dance.mp4\n")