library(emuR)
library(dplyr)
library(ggplot2)

path2ae = file.path(mypath,"ae_emuDB")
dir.exists(path2ae)
ae.db = load_emuDB(path2ae)
summary(ae.db)
serve(ae.db)

alle.s = query(ae.db,"[Phonetic=~ .+]")
dim(alle.s)
head(alle.s)
alle.s$labels

dip.s = query(ae.db,"Phonetic=ai|ei") #Segmentliste von ai und ei 
# durch Abfrage nach ai ODER ei
table(dip.s$labels)
?get_trackdata
dip.wav = get_trackdata(ae.db,dip.s,"MEDIAFILE_SAMPLES",resultType = "tibble")
class(dip.wav)
library(ggplot2)
temp=dip.wav$sl_rowIdx==1
ggplot(dip.wav[temp,]) +
  aes(y=T1, x=times_rel) +
  geom_line()


ggplot(dip.wav[temp,]) +
  aes(y=T1, x=times_orig) +
  geom_line()

ggplot(dip.wav) +
  aes(y=T1, x=times_rel) +
  geom_line() +
  facet_wrap(~sl_rowIdx)
summary(ae.db)
library(wrassp)
wav.paths = list_files(ae.db,"wav")$absolute_file_path
?wrassp
#Um die Grundfrequenz für die D_Bank zu berechnen:
ksvF0(listOfFiles = wav.paths)
# um festzustellen, wie die Extension ist:
wrasspOutputInfos[["ksvF0"]]
#Liste der Grundfrequenzdateien:
list_files(ae.db,"f0")$absolute_file_path

#man muss ae.db sagen, dass es Grundfrequenzdateien gibt:
?add_ssffTrackDefinition()
add_ssffTrackDefinition(ae.db,"grund","F0","f0")
summary(ae.db)

dip.f0 = get_trackdata(ae.db,dip.s,"grund",resultType = "tibble")
table(dip.f0$sl_rowIdx)


temp=dip.f0$sl_rowIdx==1
ggplot(dip.f0[temp,]) +
  aes(y=T1, x=times_rel) +
  geom_line()

ggplot(dip.f0) +
  aes(y = T1, x = times_rel) +
  geom_line() +
  facet_wrap(~sl_rowIdx)

#ensemble plot der Grundfrequenz von [ai,ei]
ggplot(dip.f0) +
  aes(y = T1, x= times_rel,col=labels,group=sl_rowIdx) +
  geom_line()

?wrassp #use forest()

forest(wav.paths)

wrasspOutputInfos[["forest"]]
#man muss ae.db sagen, dass es Formanten gibt:
add_ssffTrackDefinition(ae.db,"formant","fm","fms")

#Formantdaten für ae.db und dip.s
dip.fm = get_trackdata(ae.db,dip.s,"formant",resultType = "tibble")
names(dip.fm)  

ggplot(dip.fm) +
  aes(y=T1,x=times_rel,col=labels,group=sl_rowIdx) +
  geom_line()
