Skip to content

Instantly share code, notes, and snippets.

@rasmusab
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasmusab/1979b606f111bb5fe395 to your computer and use it in GitHub Desktop.
Save rasmusab/1979b606f111bb5fe395 to your computer and use it in GitHub Desktop.
Defines a function that plays the Mario theme while an experssion is executing (Linux only)
# The code below will probably only work on Linux and requires VLC on the path.
library(beepr)
library(stringr)
# Plays a file with vlc and returns the vlc instance's PID
play_vlc_pid <- function(fname) {
system(paste("vlc -Idummy --no-loop --no-repeat --playlist-autostart --no-media-library --play-and-exit", fname),
ignore.stdout = TRUE, ignore.stderr=TRUE,wait = FALSE)
ps_out <- system("ps -eo pid,comm,etime| grep vlc", intern=TRUE)
# Find the pid
pid_df <- read.table(textConnection(paste(ps_out, collapse="\n")), header=FALSE,
stringsAsFactors=FALSE, col.names= c("pid", "command", "time"))
vlc_pid <- pid_df$pid[ # Find the pid that is connected to a vlc
str_detect(pid_df$time, "00:0\\d") & # that is less that 10 seconds old and
which.min(as.numeric(str_extract(pid_df$time, "[0-9]+$"))) # youngest old.
]
vlc_pid
}
# Plays the mario theme while expr is executing, plays the flag pole jingle when the execution have finished.
# The file "main-theme-overworld.mp3" has to be on the path.
# It can be downloaded from here: https://dl.dropboxusercontent.com/u/5304231/main-theme-overworld.mp3
mario_beep <- function(expr) {
pid <- play_vlc_pid("main-theme-overworld.mp3")
expr
system(paste("kill", pid))
#system("pkill vlc")
beep(8)
}
# Use it like this:
# mario_beep(Sys.sleep(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment