# Use animaker to coordinate series of frames # for use with something like the 'animation' package # Idea based on # http://tlevine.tumblr.com/post/2607261072/how-many-gifts-did-my-true-love-give-to-me-on-all gifts.nonum <- rev(c( 'Drummers Drumming', 'Pipers Piping', 'Lords-a-Leaping', 'Ladies Dancing', 'Maids-a-Milking', 'Swans-a-Swimming', 'Geese-a-Laying', 'Golden Rings', 'Colly Birds', 'French Hens', 'Turtle Doves', '' )) gifts <- paste(1:12, gifts.nonum) gifts[1] <- 'A Partridge in a Pear Tree' # Timing info available on Wikipedia # (though audio used appears to vary from that - see below) # http://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_%28song%29#Lyrics # Audio from # http://freemusicarchive.org/music/RTists_for_Christmas/An_Album_for_Christ/RTists_for_Christmas_-_An_Album_for_Christ_-_01_12_Days_of_Christmas # CC-BY-NC-SA # Couldn't use this one because of ND clause! # http://freemusicarchive.org/music/Roger_McGuinn/Folk_Den_Project/20090517154150816 # CC-BY-NC-ND library(animaker) # Timings based on inspection of audio using audacity lengths <- c(rep(3, 7), 8, # golden rings rep(3, 2), 4, # turtle doves 17) # partridge # Intro introduction <- atomic(label="intro", durn=10) # Plot axes background <- atomic(label="bg", durn=NA) # Night of christmas label gifttext <- atomic(label="gift", durn=NA) # Cumulative rectangles totalgifts <- atomic(label="cumrect", durn=NA) # Highlighted rectangles numgifts <- atomic(label="hirect", durn=.5) tracks <- lapply(lengths, function(l) { trac(gifttext, totalgifts, numgifts, durn=l) }) timeline <- vec(introduction, trac(background, do.call("vec", tracks))) plot(timeline) frameTiming(timeline) frameApply(timeline, fps=2, pause=FALSE) intro <- function(info) { plot.new() text(.5, .5, "On the twelfth day of christmas\nMy true love gave to me") } bg <- function(info) { par(mar=c(4, 5, 2, 5)) plot.new() plot.window(c(0, sum(12:1)), c(-.5, 1.5)) axis(1) } gift <- function(info) { mtext(gifts[(12:1)[info$vecNum[2]]], side=3) } cumrect <- function(info) { rect(0, 0, cumsum(12:1)[info$vecNum[2]], 1, col="grey") } hirect <- function(info) { if (info$vecNum[2] == 1) { rect(0, 0, 12, 1, col="red", border=NA) } else { rect(cumsum(12:1)[info$vecNum[2] - 1], 0, cumsum(12:1)[info$vecNum[2]], 1, col="red", border=NA) } } drawFrame <- function(info) { lapply(info, function(x) { get(x$label)(x) }) } # frameApply(timeline, drawFrame, fps=2) library(animation) saveVideo(frameApply(timeline, drawFrame, fps=10, pause=FALSE), interval=1/10 * .65, ani.height=200, outdir="ImageFiles") # Save some frames system(paste("cp ", paste(paste(tempdir(), "/Rplot", c(1, 100, 150, 200, 250, 300), ".png", sep=""), collapse=" "), " ImageFiles/", sep="")) # Convert to .ogg for format compatible with Chrome # AND merge audio with video (codec settings appropriate # for my system and for google-chrome on my system) system("avconv -i ImageFiles/animation.mp4 -i audioClip.mp3 -c:a libvorbis animation.ogg")