# Dancing stick figure for the whole song (kappale.mp3), rendered with ggplot2 and encoded with av. # # Run rakenne.R first (tempo_kappale.rds, laulu_kayra.rds). Sections come from rakenne.csv: # - sung sections (laulu = nainen/mies): the figure gets eyes and a mouth that moves with the # centre-voice envelope, plus long hair and lashes (nainen) or short hair and brows (mies) # - kertosäe (chorus): disco moves in the 1970s dance-film style, lit floor and mirror ball # - soolo (keyboard solo): a dance solo under a spotlight # - other sections: the four moves of the original video # # Test frames only: FRAMES="100,600,1200" Rscript dance_kappale.R (writes testikuvat/) library(ggplot2) library(av) library(parallel) tempo <- readRDS("tempo_kappale.rds") bpm <- tempo$bpm; bps <- bpm / 60; offset <- tempo$offset voice <- readRDS("laulu_kayra.rds") rak <- read.csv("rakenne.csv", stringsAsFactors = FALSE, encoding = "UTF-8") fps <- 25 n_frames <- floor(tempo$duration * fps) beat_at <- function(t) (t - offset) * bps section_at <- function(t) { i <- max(which(rak$alku_s <= t + 1e-9), 1) rak[i, ] } # ---- moves ------------------------------------------------------------------------------- # A pose is a list of joint parameters. Arm angles are measured from straight down, # positive = away from the body. q = beats since the start of the move (0-8). pose0 <- list(la1 = 0.3, la2 = 0.3, ra1 = 0.3, ra2 = 0.3, lean = 0, shift = 0, spread = 0.3, jump = 0, dip = 0, x = 0, spin = 0, lkick = 0, rkick = 0, hipdrop = 0, footlift = 0) P <- function(...) modifyList(pose0, list(...)) smooth <- function(u) { u <- pmin(pmax(u, 0), 1); u * u * (3 - 2 * u) } dipf <- function(p) 0.5 * (1 + cos(2 * pi * p)) base_moves <- list( "bounce" = function(b, p, q) { d <- dipf(p); s <- cos(pi * (b + p)) P(la1 = 0.5 + 0.3 * d, la2 = 2.1 + 0.3 * d, ra1 = 0.5 + 0.3 * (1 - d), ra2 = 2.1 + 0.3 * (1 - d), lean = 0.08 * s, shift = 0.1 * s, dip = d) }, "raise the roof" = function(b, p, q) { d <- dipf(p); s <- cos(pi * (b + p)) P(la1 = 1.9 + 0.35 * (1 - d), la2 = 3.05, ra1 = 1.9 + 0.35 * (1 - d), ra2 = 3.05, lean = 0.05 * s, spread = 0.35, dip = d) }, "clap and sway" = function(b, p, q) { d <- dipf(p); s <- cos(pi * (b + p)); c <- smooth(1 - abs(2 * p - 1) * 2) P(la1 = 0.9 - 0.3 * c, la2 = 2.2 + 0.5 * c, ra1 = 0.9 - 0.3 * c, ra2 = 2.2 + 0.5 * c, lean = 0.12 * s, shift = 0.15 * s, dip = d) }, "jumping jacks" = function(b, p, q) { a <- sin(pi * p) P(la1 = 0.25 + 2.6 * a, la2 = 0.25 + 2.6 * a, ra1 = 0.25 + 2.6 * a, ra2 = 0.25 + 2.6 * a, spread = 0.12 + 0.4 * a, jump = 0.28 * a, dip = dipf(p)) } ) # 1970s disco: the point up and across, rolling arms, the hustle walk, point and spin point_arm <- function(b, p) { target <- function(k) if (k %% 2 == 0) c(2.45, 2.45) else c(-0.55, -0.8) w <- smooth(p / 0.3) target(b - 1) + (target(b) - target(b - 1)) * w } chorus_moves <- list( "disco point" = function(b, p, q) { a <- point_arm(b, p); up <- b %% 2 == 0; d <- dipf(p) P(la1 = 0.8, la2 = -0.9, ra1 = a[1], ra2 = a[2], lean = if (up) -0.1 else 0.08, shift = if (up) 0.14 else -0.1, spread = 0.32, dip = 0.6 * d) }, "rolling arms" = function(b, p, q) { th <- 2 * pi * (b + p); s <- sin(pi * (b + p) / 2) P(la1 = 0.45 + 0.25 * sin(th), la2 = 2.3 + 0.45 * cos(th), ra1 = 0.45 + 0.25 * sin(th + pi), ra2 = 2.3 + 0.45 * cos(th + pi), shift = 0.32 * s, lean = -0.08 * s, spread = 0.3, dip = dipf(p)) }, "hustle walk" = function(b, p, q) { s <- cos(pi * (b + p)); up <- smooth(1 - abs(2 * p - 1)) side <- if (b %% 2 == 0) 1 else -1 P(la1 = if (side < 0) 2.5 * up + 0.4 else 0.4, la2 = if (side < 0) 2.9 * up + 0.4 else 0.6, ra1 = if (side > 0) 2.5 * up + 0.4 else 0.4, ra2 = if (side > 0) 2.9 * up + 0.4 else 0.6, shift = 0.2 * s, lean = 0.14 * s, spread = 0.26, dip = dipf(p)) }, "point and spin" = function(b, p, q) { if (q < 4) return(chorus_moves[["disco point"]](b, p, q)) sp <- 2 * pi * smooth((q - 4) / 2) if (q < 6) return(P(la1 = 2.7, la2 = 3.0, ra1 = 2.7, ra2 = 3.0, spin = sp, spread = 0.18)) P(la1 = 0.8, la2 = -0.9, ra1 = 2.45, ra2 = 2.45, lean = -0.12, shift = 0.16, spin = 2 * pi, spread = 0.34) } ) # Dance solo for the keyboard solo (16 bars = 8 moves of 8 beats) robot_pos <- list(c(1.57, 3.14, 1.57, 0), c(1.57, 0, 1.57, 3.14), c(0.2, 1.57, 1.57, 1.57), c(1.57, 1.57, 0.2, 1.57)) solo_moves <- list( "robot" = function(b, p, q) { a <- robot_pos[[b %% 4 + 1]]; o <- robot_pos[[(b - 1) %% 4 + 1]]; w <- smooth(p / 0.15) v <- o + (a - o) * w P(la1 = v[1], la2 = v[2], ra1 = v[3], ra2 = v[4], lean = 0.06 * ((b %% 2) * 2 - 1), dip = 0.3 * dipf(p)) }, "high kicks" = function(b, p, q) { k <- sin(pi * p); left <- b %% 2 == 0 P(la1 = 1.5, la2 = 1.7, ra1 = 1.5, ra2 = 1.7, lkick = if (left) k else 0, rkick = if (left) 0 else k, lean = if (left) 0.08 * k else -0.08 * k, spread = 0.2) }, "glide" = function(b, p, q) { step <- (b %% 2 == 0); u <- sin(pi * p) P(x = -0.9 * sin(pi * q / 8), la1 = 0.5 + 0.3 * u, la2 = 1.2, ra1 = 0.5 + 0.3 * (1 - u), ra2 = 1.2, lean = -0.1, spread = 0.22, lkick = if (step) 0.25 * u else 0, rkick = if (step) 0 else 0.25 * u) }, "double spin" = function(b, p, q) { sp <- 2 * pi * (smooth(q / 4) + smooth((q - 4) / 4)) P(la1 = 2.8, la2 = 3.05, ra1 = 2.8, ra2 = 3.05, spin = sp, spread = 0.16, dip = 0.4 * dipf(p)) }, "windmill" = function(b, p, q) { th <- pi * (b + p) P(la1 = th, la2 = th, ra1 = -th, ra2 = -th, lean = 0.12 * sin(th), shift = 0.1 * sin(th), spread = 0.38, dip = dipf(p)) }, "split jumps" = function(b, p, q) { a <- sin(pi * ((b %% 2) + p) / 2) P(la1 = 1.9 + 0.3 * a, la2 = 2.2 + 0.3 * a, ra1 = 1.9 + 0.3 * a, ra2 = 2.2 + 0.3 * a, spread = 0.18 + 0.8 * a, jump = 0.2 * a, footlift = 0.35 * a) }, "knee drop" = function(b, p, q) { w <- sin(2 * pi * (b + p) / 2) P(hipdrop = 0.42 * smooth(q / 1), spread = 0.55, la1 = 2.5 + 0.35 * w, la2 = 2.8 + 0.3 * w, ra1 = 2.5 - 0.35 * w, ra2 = 2.8 - 0.3 * w, lean = 0.1 * w) }, "big finish" = function(b, p, q) { if (q < 4) { r <- smooth(q / 4) return(P(la1 = 0.3 + 2.4 * r, la2 = 0.3 + 2.6 * r, ra1 = 0.3 + 2.4 * r, ra2 = 0.3 + 2.6 * r, hipdrop = 0.25 * (1 - r), spread = 0.4)) } if (q < 5) { a <- sin(pi * (q - 4)) return(P(la1 = 2.7, la2 = 2.9, ra1 = 2.7, ra2 = 2.9, jump = 0.3 * a, spread = 0.2 + 0.3 * a)) } P(la1 = 0.8, la2 = -0.9, ra1 = 2.5, ra2 = 2.5, lean = -0.14, shift = 0.18, spread = 0.4) } ) moves_for <- function(osa) switch(osa, "kertosäe" = chorus_moves, "soolo" = solo_moves, base_moves) # Which move is active at continuous beat `beat`, and how far into it (q) move_at <- function(t, beat) { s <- section_at(t) start <- max(beat_at(s$alku_s), 0) into <- max(beat - round(start), 0) mv <- moves_for(s$osa); k <- (into %/% 8) %% length(mv) + 1 list(section = s, fun = mv[[k]], name = names(mv)[k], q = into %% 8, id = paste(s$osa, k)) } pose_at <- function(t) { beat <- max(beat_at(t), 0); b <- floor(beat); p <- beat - b m <- move_at(t, beat) pose <- m$fun(b, p, m$q) # blend from the previous move over the first half beat of a new one t_prev <- t - 0.6 / bps if (t_prev > 0) { mp <- move_at(t_prev, max(beat_at(t_prev), 0)) if (mp$id != m$id && m$q < 0.5) { old <- mp$fun(b, p, mp$q + 0.6) w <- smooth(m$q / 0.5) old$spin <- old$spin %% (2 * pi); if (old$spin > pi) old$spin <- old$spin - 2 * pi pose <- Map(function(a, o) w * a + (1 - w) * o, pose, old) } } list(pose = pose, move = m$name, section = m$section, beat = b, p = p) } # ---- figure ------------------------------------------------------------------------------ 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 mid + h * perp } skeleton <- function(po) { hip <- c(po$x + po$shift, 1.1 - 0.14 * po$dip - po$hipdrop + po$jump) dir <- c(sin(po$lean), cos(po$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)) list(elbow = elbow, hand = elbow + 0.48 * c(side * sin(a2), -cos(a2))) } la <- arm(-1, po$la1, po$la2); ra <- arm(1, po$ra1, po$ra2) fy <- po$jump + po$footlift lfoot <- c(po$x + po$shift * 0.5 - po$spread - 0.35 * po$lkick, fy + 0.75 * po$lkick) rfoot <- c(po$x + po$shift * 0.5 + po$spread + 0.35 * po$rkick, fy + 0.75 * po$rkick) 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)) bones <- setNames(as.data.frame(bones), c("x", "y", "xend", "yend")) # spin: squash x around the body's centre line cx <- hip[1]; k <- cos(po$spin) bones$x <- cx + (bones$x - cx) * k; bones$xend <- cx + (bones$xend - cx) * k head[1] <- cx + (head[1] - cx) * k list(bones = bones, head = head, facing = k, lean = po$lean, floor_x = po$x + po$shift * 0.5) } circle <- function(center, r, n = 48, sx = 1, sy = 1) { a <- seq(0, 2 * pi, length.out = n) data.frame(x = center[1] + sx * r * cos(a), y = center[2] + sy * r * sin(a)) } # Face and hair for sung sections. o = mouth opening 0-1. face_layers <- function(s, laulu, o, t, bg) { if (!laulu %in% c("nainen", "mies")) return(list()) h <- s$head; f <- s$facing; L <- list() up <- c(sin(s$lean), cos(s$lean)); rt <- c(up[2], -up[1]) # head's own up and right at <- function(dx, dy) h + f * dx * rt + dy * up hair_col <- if (laulu == "nainen") "#ffcf4a" else "#b8875a" if (laulu == "nainen") { # two long strands that swing with the body sw <- -0.25 * s$lean for (side in c(-1, 1)) { st <- t(sapply(seq(0, 1, length.out = 12), function(u) at(side * (0.21 + 0.05 * u) + sw * u^2, 0.12 - 0.52 * u))) L[[length(L) + 1]] <- geom_path(aes(x, y), data = data.frame(x = st[, 1], y = st[, 2]), colour = hair_col, linewidth = 5, lineend = "round") } cap <- t(sapply(seq(0.15, pi - 0.15, length.out = 20), function(a) at(0.235 * cos(a), 0.235 * sin(a)))) L[[length(L) + 1]] <- geom_path(aes(x, y), data = data.frame(x = cap[, 1], y = cap[, 2]), colour = hair_col, linewidth = 5, lineend = "round") } else { cap <- t(sapply(seq(0.35, pi - 0.35, length.out = 16), function(a) at(0.24 * cos(a), 0.24 * sin(a)))) L[[length(L) + 1]] <- geom_path(aes(x, y), data = data.frame(x = cap[, 1], y = cap[, 2]), colour = hair_col, linewidth = 6, lineend = "round") } if (f < 0.25) return(L) # facing away: hair only blink <- (t %% 3.7) < 0.12 for (side in c(-1, 1)) { e <- at(side * 0.08, 0.04) if (blink) { L[[length(L) + 1]] <- annotate("segment", x = e[1] - 0.03 * f, xend = e[1] + 0.03 * f, y = e[2], yend = e[2], colour = "white", linewidth = 1.6) } else { L[[length(L) + 1]] <- geom_polygon(aes(x, y), data = circle(e, 0.028, 20, sx = f), fill = "white") } if (laulu == "nainen") { for (k in -1:1) { a <- at(side * 0.08 + k * 0.022, 0.075); b <- at(side * 0.08 + k * 0.032, 0.1) L[[length(L) + 1]] <- annotate("segment", x = a[1], y = a[2], xend = b[1], yend = b[2], colour = "white", linewidth = 0.8) } } else { a <- at(side * 0.12, 0.1); b <- at(side * 0.04, 0.09) L[[length(L) + 1]] <- annotate("segment", x = a[1], y = a[2], xend = b[1], yend = b[2], colour = "white", linewidth = 1.8, lineend = "round") } } m <- at(0, -0.09) L[[length(L) + 1]] <- geom_polygon(aes(x, y), data = circle(m, 1, 30, sx = 0.055 * f, sy = 0.012 + 0.06 * o), fill = if (laulu == "nainen") "#ff5fa2" else "#3a1020", colour = if (laulu == "nainen") "#ff9cc8" else "white", linewidth = 1) L } XW <- 3.45 * 1280 / 840 / 2 # half width of the stage at the 1280 x 840 aspect palette_disco <- c("#ff5fa2", "#ffcf4a", "#4ad6ff", "#9b6bff", "#5cff9d", "#ff7a45") draw_frame <- function(i) { t <- (i - 1) / fps st <- pose_at(t); s <- skeleton(st$pose); sec <- st$section started <- t >= offset flash <- if (started) dipf(st$p)^4 else 0 laulu <- sec$laulu o <- if (laulu %in% c("nainen", "mies")) voice$env[min(i, length(voice$env))] else 0 o <- if (o < 0.35) 0 else (o - 0.35) / 0.65 bg <- switch(sec$osa, "kertosäe" = grDevices::rgb(0.16 + 0.12 * flash, 0.04 + 0.03 * flash, 0.20 + 0.12 * flash), "soolo" = grDevices::rgb(0.03 + 0.04 * flash, 0.03 + 0.03 * flash, 0.07 + 0.06 * flash), grDevices::rgb(0.07 + 0.10 * flash, 0.06 + 0.03 * flash, 0.16 + 0.14 * flash)) shadow_w <- 0.55 - 0.25 * min(st$pose$jump / 0.3, 1) g <- ggplot() if (sec$osa == "kertosäe") { set.seed(st$beat) tiles <- data.frame(x0 = seq(-XW, XW, by = 0.48)) tiles$fill <- sample(palette_disco, nrow(tiles), replace = TRUE) g <- g + geom_rect(aes(xmin = x0 + 0.02, xmax = x0 + 0.46, ymin = -0.24, ymax = -0.02, fill = I(fill)), data = tiles, alpha = 0.35 + 0.45 * flash) rays <- data.frame(a = runif(14, pi * 1.05, pi * 1.95), len = runif(14, 0.8, 2.6)) g <- g + geom_segment(aes(x = 1.45, y = 2.95, xend = 1.45 + len * cos(a), yend = 2.95 + len * sin(a)), data = rays, colour = "white", alpha = 0.10 + 0.25 * flash, linewidth = 0.6) + geom_polygon(aes(x, y), data = circle(c(1.45, 2.95), 0.13), fill = "grey80", colour = "white", linewidth = 0.8) } if (sec$osa == "soolo") { fx <- s$floor_x cone <- data.frame(x = c(fx - 0.15, fx + 0.15, fx + 0.9, fx - 0.9), y = c(3.2, 3.2, -0.02, -0.02)) g <- g + geom_polygon(aes(x, y), data = cone, fill = "#fff2b3", alpha = 0.10 + 0.08 * flash) + geom_polygon(aes(x, y), data = circle(c(fx, -0.01), 1, sx = 0.95, sy = 0.08), fill = "#fff2b3", alpha = 0.25) } g <- g + annotate("segment", x = -XW, xend = XW, y = 0, yend = 0, colour = "grey45", linewidth = 0.8) + geom_polygon(aes(x, y), data = circle(c(s$floor_x, 0), 1, sx = shadow_w, sy = 0.06), fill = "black", alpha = 0.35) + geom_segment(aes(x, y, xend = xend, yend = yend), data = s$bones, colour = "white", linewidth = 3.2, lineend = "round") # hair strands go behind the head outline, face in front fl <- face_layers(s, laulu, o, t, bg) hair <- Filter(function(l) inherits(l$geom, "GeomPath") && !inherits(l$geom, "GeomSegment"), fl) rest <- Filter(function(l) !(inherits(l$geom, "GeomPath") && !inherits(l$geom, "GeomSegment")), fl) for (l in hair) g <- g + l g <- g + geom_polygon(aes(x, y), data = circle(s$head, 0.22), fill = bg, colour = "white", linewidth = 3.2) for (l in rest) g <- g + l label_sec <- switch(sec$osa, "kertosäe" = "chorus", "säkeistö" = "verse", "soolo" = "keyboard solo: dance solo", "break" = "break", "intro" = "intro", "loppu" = "outro", sec$osa) if (laulu %in% c("nainen", "mies")) label_sec <- paste0(label_sec, " · ", if (laulu == "nainen") "female vocal" else "male vocal") g + annotate("text", x = -XW + 0.1, y = 3.05, hjust = 0, colour = "white", size = 6, label = sprintf("%.1f BPM", bpm), fontface = "bold") + annotate("text", x = -XW + 0.1, y = 2.83, hjust = 0, colour = "grey75", size = 4.3, label = label_sec) + annotate("text", x = -XW + 0.1, y = 2.65, hjust = 0, colour = "grey60", size = 4, label = st$move) + annotate("text", x = XW - 0.1, y = 3.05, hjust = 1, colour = "#ff5fa2", size = 6, label = if (started) paste(rep("●", st$beat %% 4 + 1), collapse = " ") else "") + coord_fixed(xlim = c(-XW, XW), ylim = c(-0.25, 3.2), expand = FALSE) + theme_void() + theme(plot.background = element_rect(fill = bg, colour = NA)) } render <- function(idx, files) { invisible(mclapply(seq_along(idx), function(j) { ragg::agg_png(files[j], width = 1280, height = 840, res = 144) print(draw_frame(idx[j])); dev.off() }, mc.cores = max(1, detectCores() - 1))) } test <- Sys.getenv("FRAMES") if (nzchar(test)) { idx <- as.integer(strsplit(test, ",")[[1]]) dir.create("testikuvat", showWarnings = FALSE) render(idx, file.path("testikuvat", sprintf("frame_%05d.png", idx))) cat("wrote", length(idx), "test frames to testikuvat/\n") } else { frame_dir <- Sys.getenv("FRAME_DIR", file.path(tempdir(), "dance_kappale_frames")) dir.create(frame_dir, showWarnings = FALSE, recursive = TRUE) files <- file.path(frame_dir, sprintf("frame_%05d.png", seq_len(n_frames))) render(seq_len(n_frames), files) av_encode_video(files, output = "dance_kappale.mp4", framerate = fps, audio = "kappale.mp3", vfilter = "format=yuv420p") cat("wrote dance_kappale.mp4\n") }