library(tuneR)
library(ggridges)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:ggridges':
## 
##     scale_discrete_manual
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
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)

perioWav <- periodogram(wavFile, width = 4096)
plot(perioWav)

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,"up.csv")
d<-na.omit(d)
d$song<-gsub("Joy Division","",d$song)
d$song<-gsub("-","",d$song)
d$song<-gsub("[[:digit:]]", "", d$song)
d$song<-gsub("mp","",d$song)
d$song<-gsub("()","",d$song)
d$song<-gsub("\\(","",d$song)
d$song<-gsub("\\)","",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() 
## Picking joint bandwidth of 2.39

d$song<-gsub("\\.","",d$song)