library(tuneR)
library(ggridges)
library(dplyr)
library(plotly)
a<-dir(pattern="mp3") ## Find all the mp3 files in the current  directory
x<-a[2]  ## Choose one of them for illusrtation
stereoMP3File <- readMP3(x)  ## Read it in and converto to a wav file
## Nost files are steroe with a right and left band. This is potentially confusing so either one band has to be chosen or the two combined.
wavFile <- extractWave(stereoMP3File, interact = FALSE)
if (nchannel(wavFile) > 1) {
  wavFile <- mono(wavFile, "both")
}
plot(wavFile,main=x)

a<-dir(pattern="mp3")

f<-function(x)
  {stereoMP3File <- readMP3(x)
wavFile <- extractWave(stereoMP3File, interact = FALSE)
if (nchannel(wavFile) > 1) {
  wavFile <- mono(wavFile, "both")
}
perioWav <- periodogram(wavFile, width = 4096)
freqWav <- FF(perioWav)
data.frame(song=x,freq=freqWav)
}

d<-lapply(a,f)
d<-do.call("rbind",d)

write.csv(d,"amvu.csv")
d<-read.csv("amvu.csv")

d<-na.omit(d)
d$song<-gsub("-arctic_monkeys-","am-",d$song)
d$song<-gsub("[[:digit:]]", "", d$song)
d$song<-gsub(".mp3","",d$song)

d<-subset(d,d$freq<150)
d<-subset(d,d$freq>20)
d<-arrange(d,song)

ggplot(d,aes(x = freq,y=song)) +
  geom_density_ridges() +geom_hline(yintercept = 10,col="red")