Last active
August 29, 2015 14:04
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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