forked from klamping/deck.narrator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.R
111 lines (101 loc) · 4.06 KB
/
stopwatch.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
.interactive <- function(){
pres_name <- "test1"
pres_name <- "wutzler20_egu_agg_inla"
pres_title <- pres_name
# copy your pdf of the presentation into <pres_name>/slideshow.pdf
# and copy your audio to <pres_name>/audio.m4a and <pres_name>/audio.ogg
burst_slides(pres_name)
durations <- readRDS(file = file.path(pres_name,"durations.rds"))
#durations <- time_durations(pres_name)
#saveRDS(durations, file = file.path(pres_name,"durations.rds"))
slidecast <- gen_slides(durations, pres_name, pres_title)
writeLines(slidecast, file.path(pres_name, paste0("slidecast.html")))
cat("\n") # line feed
message("slides written to ",file.path("pres_name","slidecast.html"))
write_selfcontained_html(slidecast, pres_name)
message("slides written to ",file.path("pres_name",paste0(pres_name,".html")))
}
#burst_slides <- function(pres_name, widht = 1920, height = 1200) {
burst_slides <- function(pres_name, width = 1280, height = 800) {
prevwd <- setwd(pres_name)
on.exit(setwd(prevwd))
unlink(paste0("slideshow-*.jpg"))
system(sprintf("slidecrunch burst slideshow.pdf --width %d --height %d", width, height))
}
time_durations <- function(pres_name){
nSlides <- length(dir(file.path(pres_name), "^slideshow-.*\\.jpg"))
#try(system(paste0("audacity ", file.path(pres_name,"audio.m4a"), " &")))
#try(system(paste0("FoxitReader ", file.path(pres_name,"slideshow.pdf"), " &")))
cat("Keep your presentation slides before you,",
" start the audio after the countdown, and a the R-Shell hit ",
"<ENTER> at times of slide transition.")
cat("First <ENTER> starts the countdown.")
pause()
countdown(4L)
durations <- stopwatch(nSlides)
}
gen_slides <- function(durations, pres_name, pres_title = pres_name){
nSlidesTimed <- length(durations)
output <- gen_slides_sections(durations)
slidecast_head <- readLines("template/slidecast_head.html")
slidecast_head <- sub("Presentation Title",pres_title, slidecast_head)
slidecast_tail <- readLines("template/slidecast_tail.html")
slidecast <- c(slidecast_head , output, slidecast_tail )
}
pause = function(prompt = "Press <Enter> to continue...") {
if (interactive()) {
invisible(readline(prompt = prompt))
} else {
cat(prompt)
invisible(readLines(file("stdin"), 1))
}
}
countdown <- function(n){
for (i in rev(seq_len(n))) {
cat(i)
Sys.sleep(1)
}
cat(0)
}
stopwatch <- function(maxSlides = 80L){
print("Press <Enter> to continue... or any other + <Enter> to quit.")
input <- ""
slide <- 1L
#maxSlides <- 80L
durations <- numeric(maxSlides)
start_time = Sys.time()
while (!nzchar(input) & slide <= maxSlides) {
input <- pause(paste0("slide ",slide,": "))
end_time = Sys.time()
durations[slide] <- duration <- as.numeric(end_time - start_time)
start_time = Sys.time()
cat(duration)
slide <- slide + 1L
}
durations[seq_len(slide - 1L)]
}
gen_slides_sections <- function(durations){
# data-narrator-duration ending with 0 causes errors, make sure ending at .001
do.call(c, lapply(seq_along(durations), function(i){
sprintf('<section class="slide" data-narrator-duration="%.3f" data-duration="%.0f">
<img src="slideshow-%d.jpg" />
</section>
',round(durations[i]-0.001,2)+0.001, durations[i]*1000, i-1) # starts at slide 0
}))
}
write_selfcontained_html <- function(slidecast, pres_name){
slidecast_audio <- embed_audio(slidecast, pres_name)
writeLines(slidecast_audio, file.path(pres_name, paste0("slidecast_audio.html")))
# https://github.com/BitLooter/htmlark
system(paste0("htmlark ", file.path(pres_name,"slidecast_audio.html")," -o ", pres_name, ".html"))
}
embed_audio <- function(slidecast, pres_name){
prevwd <- setwd(pres_name)
on.exit(setwd(prevwd))
# https://github.com/BitLooter/htmlark, does not embed audio, need manually
# need to convert audio to data tag
system("base64 -w0 audio.ogg > audio_ogg.txt")
audio_str <- suppressWarnings(readLines("audio_ogg.txt"))
#writeLines(audio_str,"audio_ogg2.txt", sep = "")
slidecast <- sub('src="audio.ogg">', paste0('src="data:audio/ogg;base64,',audio_str,'">'), slidecast)
}