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


kiel.db=load_emuDB(file.path(mypath,"kielread_emuDB"))
summary(kiel.db)

wav_paths = list_files(kiel.db, "wav")$absolute_file_path
# calculate the formants with library(wrassp)
forest(wav_paths)

# check what the extension is
wrasspOutputInfos[["forest"]]

# verify the files exist
list_files(kiel.db, "fms")$absolute_file_path

# tell Emu that there are such files and incorporate them into the database
add_ssffTrackDefinition(kiel.db, "FORMANTS", "fm", "fms") 
#as we will see below, it is important that the name is "FORMANTS"
#only then we will be able to see and to correct the formants in the webApp

summary(kiel.db) 
#or
list_ssffTrackDefinitions(kiel.db)

kai.s=query(kiel.db,"Phonetic== a:|i:")
kai.fm=get_trackdata(kiel.db,kai.s,ssffTrackName = "FORMANTS",resultType = "tibble")
ggplot(kai.fm) +
  aes(y=T1,x=times_rel,col = labels, group=sl_rowIdx) + 
  geom_line()


# so much for homework; now:
# -calculate gender-specific formants

#K67 = male, K68 = female

male_wav_paths = list_files(kiel.db, "wav",bundlePattern = "K67.*")$absolute_file_path
female_wav_paths = list_files(kiel.db, "wav",bundlePattern = "K68.*")$absolute_file_path
# calculate the formants with library(wrassp)
forest(male_wav_paths,gender="m")
forest(female_wav_paths,gender="f")

# -introduce gender to kai.fm
kai.fm=get_trackdata(kiel.db,kai.s,ssffTrackName = "FORMANTS",resultType = "tibble")
serve(kiel.db) #or (depending on version of emuR and/or the EmuWebApp)
serve(kiel.db,autoOpenURL = "https://ips-lmu.github.io/EMU-webApp/?autoConnect=true")

kai.fm$gender="male" #3365 X "male"
#use substr (sub-string) to create a new string with the first three symbols;
#check whether they are identical to "K68" (= the female speaker)
temp = substr(kai.fm$bundle,1,3)=="K68"
kai.fm$gender[temp]="female"

#or: use grepl(pattern,x)

kai.fm$gender="male"
temp = grepl("K68",kai.fm$bundle)
kai.fm$gender[temp]="female"

table(kai.fm$gender)
#female   male 
#1978     1387 




ggplot(kai.fm) +
  aes(y=T1,x=times_rel,col = labels, group=sl_rowIdx) + 
  geom_line() +
  facet_wrap(~gender)

# find outliers

#there are several ways you could go, e.g. serve() database with the seglist parameter

serve(kiel.db,autoOpenURL = "https://ips-lmu.github.io/EMU-webApp/?autoConnect=true",seglist = kai.s)
#however, that means that you need to look at each segment by clicking through every segment 

#it may be easier to find the errors more directly:
temp = kai.fm$T1==0
kai.fm[temp,]
#you can use this emuRtrackdata as a pseudo-seglist and enter it into "seglist =" 

serve(kiel.db,autoOpenURL = "https://ips-lmu.github.io/EMU-webApp/?autoConnect=true",seglist=kai.fm[temp,])

# with temp = kai.fm$T1==0, some segments are more than one time in the seglist
# a more correct approach would be:
kai.errors = kai.fm %>%
  group_by(sl_rowIdx)%>%
  summarise(T1errors=any(T1==0))#= is there any 0 in T1 of the token?

nrow(kai.errors)==nrow(kai.s) # = one row per segment (should be 139 in both cases)

kai.s[kai.errors$T1errors,] # show the segments containing any T1==0
serve(kiel.db,autoOpenURL = "https://ips-lmu.github.io/EMU-webApp/?autoConnect=true",seglist=kai.s[kai.errors$T1errors,])

#then correct the formants (this can only be done when their name is "FORMANTS")
#it might be necessary to correct segment boundaries as well
#################################################################################

#repeat the plot:
#in case that you shifted boundaries, you have to do the query() again:

kai.s=query(kiel.db,"Phonetic== a:|i:")
#certainly, you have to re-read the formants:
kai.fm=get_trackdata(kiel.db,kai.s,ssffTrack="FORMANTS",resultType = "tibble")
#and, unfortunately, add once again the gender information:
kai.fm$gender="male"
temp = substr(kai.fm$bundle,1,3)=="K68"
kai.fm$gender[temp]="female"
#then
ggplot(kai.fm) +
  aes(y=T1,x=times_rel,col = labels, group=sl_rowIdx) + 
  geom_line() +
  facet_wrap(~gender)
# or plot it with normalized times (column "times_norm"):
ggplot(kai.fm) +
  aes(y=T1,x=times_norm,col = labels, group=sl_rowIdx) + 
  geom_line() +
  facet_wrap(~gender)

#In order to plot an averaged plot (called an ensemble average),
# we have to consider that the segments have differing lengths

# count the observations (frames) per segment:
table(kai.fm$sl_rowIdx)

#use normalize_length to normalize the length of each segment to 21 frames:

kai.fm.norm=normalize_length(kai.fm)
table(kai.fm.norm$sl_rowIdx) #now 21 everywhere


ggplot(kai.fm.norm) +
  aes(y=T1,x=times_norm,col = labels, group=sl_rowIdx) + 
  geom_line() +
  facet_wrap(~gender)

#we can now calculate an average per speaker(gender), per vowel, and per sample (because all
# vowels now have 21 interpolated samples):

kai.fm.norm.average = kai.fm.norm %>% 
  group_by(gender,labels,times_norm) %>%
  summarise(T1 = mean(T1),T2 = mean(T2))

kai.fm.norm.average

ggplot(kai.fm.norm.average) +
  aes(y=T1,x=times_norm,col = labels) + 
  geom_line() +
  facet_wrap(~gender)

####################################################################

####################################################################