#########################################################################
###
###    EXAMPLE 2 - Utilizing Procedure 
###    TAE-JIN YOON
###    SUNGSHIN WOMEN'S UNIVERSITY
###    Last modified: 2017.04.14
###    Date created: 2017.04.14
###
###########################################################################



#PROMT THE USER FOR INPUT
# http://www.fon.hum.uva.nl/praat/manual/Create_Strings_as_file_list___.html

form Measure formant values for segments in a textgrid
    sentence Sound_dir ./seoulcorpus
    sentence Output result
    sentence Sound .wav
    positive maximum_formant 5500
    positive number_of_formants 5
endform

#DELETE THE OLD FORMANT FILE IF IT EXISTS 
# http://www.fon.hum.uva.nl/praat/manual/Scripting_6_4__Files.html

if fileReadable ("'output$'.txt")
	deleteFile: "'output$'.txt"
endif

debug = 0

##
## HEADER
###################
appendFileLine: "'output$'.txt",
      ... "Filename", tab$, "Word", tab$, "Left_Phone", tab$, "Phone", tab$, "Right_Phone", tab$,
      ... "Phone_Duration", tab$, "Word_Duration", tab$,
      ... "F1_1", tab$, "F2_1", tab$,
      ... "F1_2", tab$, "F2_2", tab$,
      ... "F1_3", tab$, "F2_3", tab$,
      ... "F1Bark_1", tab$, "F2Bark_1", tab$,
      ... "F1Bark_2", tab$, "F2Bark_2", tab$,
      ... "F1Bark_3", tab$, "F2Bark_3", tab$,
      ... "F0_1", tab$, "F0_2", tab$, "F0_3", tab$,
      ... "F0st_1", tab$, "F0st_2", tab$, "F0st_3"

#SELECT THE SOUND AND FIND THE FORMANTS

strings = Create Strings as file list: "list", sound_dir$ +"/"+ "*.wav"
numberOfFiles = Get number of strings

clearinfo

for ifile to numberOfFiles
    selectObject: strings
    sound_file$ = Get string: ifile
    basename$ = sound_file$ - ".wav"
    Read from file: sound_dir$+"/"+sound_file$


    # COUNT THE NUMBER OF INTERVALS IN THE PHONES TIER OF THE TEXTGRID
    Read from file: sound_dir$+"/"+basename$+".TextGrid"
    selectObject: "TextGrid 'basename$'"
    num_intervals = Get number of intervals... 1
    appendInfoLine: num_intervals

    # LOOPING the interval tiers
;    for i from 2 to num_intervals-1
    for i from 2 to 100

         selectObject: "TextGrid 'basename$'"
         phone$ = Get label of interval: 1, i


         ## 
         ## PHONE AND WORD LABELS AND ACCOMPANYING INFORMATION
         ## Procecure named textgrid_info 
         # http://www.fon.hum.uva.nl/praat/manual/Scripting_5_5__Procedures.html
         #######################################################################

         @textgrid_info: phone$

         ##
         ## if left phone (left$) is a fricative and right phone (right$) is a vowel, 
         ## then do extract the acoustic features
         ##


         if left$ == "s0" or left$ == "ss"


                  # Check whether the right phone is a vowel  
                  # In seoulcorpus, vowel is marked as:
                  # ii [i], ee [e], aa [a], xx [으], vv [어], uu [u], oo [o]
                  if phone$ == "ii" or phone$ == "ee" or phone$ == "xx" or phone$ == "vv" or phone$ == "uu" or phone$ == "oo"

                      # http://www.fon.hum.uva.nl/praat/manual/Scripting_6_2__Writing_to_the_Info_window.html
                      appendInfo: phone$, tab$, left$,tab$, right$, tab$, word$
                      appendInfo: newline$

                      # WE WILL PUT MORE HERE FOR VOWEL AND FRICATIVE ACOUSTIC FEATURES
                      ####################################################################



                  endif


              endif

    endfor


    # REMOVE OBJECTS WHEN DONE
    selectObject: "Sound 'basename$'"
    plusObject: "TextGrid 'basename$'"
    ;plusObject: "Formant 'basename$'"
    Remove
endfor

select all
Remove


procedure textgrid_info: phone$

          #GET TIMES DURING THE PHONE
          start = Get starting point: 1, i
          end = Get end point: 1, i
          quarter = start + (end-start) / 4
          halfway = start + (end-start) / 2
          three_quarters = start + (end-start) * 3 / 4

          phone_duration = (end-start)*1000


          # IDENTIFY WORD 


          word_index = Get interval at time: 2, halfway
          word$ = Get label of interval: 2, word_index


          word_start = Get starting point: 2, word_index
          word_end = Get end point: 2, word_index

          word_duration = (word_end-word_start) * 1000

          #IDENTIFY THE PRECEDING PHONE
          if start = word_start
                 left$ = "#"
          else
                 left$ = Get label of interval: 1, i-1

                 start_left = Get starting point: 1, i-1
                 end_left = Get end point: 1, i-1

          endif

          #IDENTIFY THE FOLLOWING PHONE
          if end = word_end
                  right$ = "#"
          else
                  right$ = Get label of interval: 1, i+1

          endif

endproc