form output filename
    sentence output result.txt
endform

sound$ = Read from file: "sound/dhl_part.wav"
textgrid$ = Read from file: "sound/dhl_part.TextGrid"

########################
### MAKE PITCH Object  #
########################
selectObject: "Sound dhl_part"
To Pitch: 0, 75, 600

# Select TextGrid file

selectObject: "TextGrid dhl_part"

# Find out how many intervals there are in tier 2
num_intervals = Get number of intervals... 2

# Add name for F0
writeInfoLine: "phone",tab$,"word",tab$,"F0",tab$,"F0st"

# to the output file
writeFileLine: output$, "phone",tab$,"word",tab$,"F0",tab$,"F0st"

# Loop the interval tiers
for i from 1 to num_intervals

    phone$ = Get label of interval: 2, i

    if phone$ == "ae" or phone$ == "ei"
       ... or phone$ == "eh" or phone$ == "iy"

        startTime = Get start point: 2, i
        endTime = Get end point: 2, i
        midTime = startTime + (endTime - startTime)/2
        phone_duration = (endTime - startTime)*1000

        # IDENTIFY WORD 
        word_index = Get interval at time: 1, midTime
        word$ = Get label of interval: 1, word_index
        word_start = Get starting point: 1, word_index
        word_end = Get end point: 1, word_index
        word_duration = (word_end - word_start)*1000

        appendInfo: phone$,tab$,word$,tab$
        # output
        appendFile: output$, phone$,tab$,word$,tab$

         ####################
         # Measure F0
         ####################
         selectObject: "Pitch dhl_part"
         f0 = Get value at time: midTime, "Hertz", "Linear"

         # CHANGE TO SEMITONE
         #######################
         # In R
         #    f0 = seq(50, 600, 1)
         #    semitone = 12*log(f0/100)*(log(2))
         #    plot(f0, semitone, type="b")
         #    grid()
         ############################################

         f0st = hertzToSemitones(f0)

         appendInfoLine: fixed$(f0,2),tab$,fixed(f0st,2)

         #output
         appendFileLine: output$, fixed$(f0,2),tab$,fixed(f0st,2)
         selectObject: "TextGrid dhl_part"
    endif
endfor