Reputation: 609
I am running a sound analysis function on around 25k short audio files. My code works but will take a very long time to run. What would be a good approach to parallelize it?
Thanks a lot,
files = list.files(getwd(), pattern=".mp3", all.files=FALSE, full.names=FALSE)
out=NULL
for (i in files) {
res <- try(soundgen::analyze(i,pitchMethods = 'dom', plot = FALSE, summary = TRUE), silent = TRUE)
res["1", "duration"] <- i[[1]]
out=rbind(out,res)
print(i)
}
Upvotes: 1
Views: 127
Reputation: 2945
You can use the parallel package to achieve this easily:
library(parallel)
library(soundgen)
files <- list.files(getwd(), pattern=".mp3", all.files=FALSE, full.names=FALSE)
out <- NULL
soundAnalysis <- function(file){
res <- try(soundgen::analyze(file,pitchMethods = 'dom', plot = FALSE, summary = TRUE), silent = TRUE)
res["1", "duration"] <- file[[1]]
out <- rbind(out,res)
print(i)
}
output <- mclapply(X = files, FUN = soundAnalysis, mc.cores = detectCores())
Upvotes: 1