Skip to content

Instantly share code, notes, and snippets.

@dfeng
Last active December 11, 2015 20:58
Show Gist options
  • Save dfeng/4658618 to your computer and use it in GitHub Desktop.
Save dfeng/4658618 to your computer and use it in GitHub Desktop.
# randomize 1:nSongs across each iteration (?)
# be aware of the songs in the basedir, so that we don't resing songs
beMarkov <- TRUE # flag: don't do previous songs
Sys.setlocale("LC_ALL", "chs")
basedir <- "D:/Karaoke/To Sing/" #"C:/Users/Susan/Dropbox"
outdir <- "D:/Karaoke/To Sing/"
addLeadZero <- function(x, n=2) {
x <- as.character(x)
while (nchar(x) < n) x <- paste(0, x, sep="") # lol
return(x)
}
if (file.exists("sunglist.csv")) {
sunglist <- read.csv("sunglist.csv")[,2]
} else {
beMarkov <- FALSE
}
ppl <- c("Bowen", "Leo", "Derek", "Susan")# "Vivien")
nSongs <- 4
totalSongs <- 0
songlist <- vector('list', length(ppl))
for (i in 1:length(ppl)) {
songlist[[i]] <- dir(paste(basedir, "/",
ppl[i],"-Karaoke",sep=""), pattern=".mp(e)?g")
if (beMarkov) {
songlist[[i]] <- songlist[[i]][!(songlist[[i]] %in% sunglist)]
}
totalSongs <- totalSongs + length(songlist[[i]])
}
newsunglist <- c()
k <- 1
pb <- txtProgressBar(style=3)
while (k <= nSongs*length(ppl)) {
for (i in 1:length(ppl)) {
if (length(songlist[[i]]) == 0) { k <- k + 1; next }
song <- sample(1:length(songlist[[i]]), 1)
extension <- regexpr(".mp(e)?g",songlist[[i]][song])
extension <- substr(songlist[[i]][song], extension,
extension+attr(extension,"match.length")-1)
print(songlist[[i]][song])
newsunglist <- c(newsunglist, songlist[[i]][song])
file.copy(paste(basedir, "/", ppl[i],"-Karaoke/", songlist[[i]][song],
sep=""), paste(outdir, addLeadZero(k), extension,sep=""), overwrite=T)
songlist[[i]] <- songlist[[i]][-song]
k <- k + 1
setTxtProgressBar(pb, k/(nSongs*length(ppl)))
}
}
write.csv(newsunglist, paste(basedir, "sunglist.csv", sep=""))
cat("\nFinished adding ", k-1, " songs\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment