form Measure formant values for segments in a textgrid
sentence Sound_dir ./seoulcorpus
sentence Output result2
sentence Sound .wav
positive maximum_formant 5500
positive number_of_formants 5
endform
if fileReadable ("'output$'.txt")
deleteFile: "'output$'.txt"
endif
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"
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$
selectObject: "Sound 'basename$'"
To Formant (burg): 0, 'number_of_formants', 'maximum_formant', 0.025, 50
;selectObject: "Sound 'basename$'"
;To Pitch: 0, 75, 500
Read from file: sound_dir$+"/"+basename$+".TextGrid"
selectObject: "TextGrid 'basename$'"
num_intervals = Get number of intervals... 1
appendInfoLine: num_intervals
for i from 2 to num_intervals-1
; for i from 2 to 100
selectObject: "TextGrid 'basename$'"
phone$ = Get label of interval: 1, i
if phone$ != "" and phone$ != "sp"
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
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
if start = word_start
left$ = "#"
else
left$ = Get label of interval: 1, i-1
endif
if end = word_end
right$ = "#"
else
right$ = Get label of interval: 1, i+1
endif
appendInfo: phone$, tab$, left$,tab$, right$, tab$
appendInfo: newline$
selectObject: "Formant 'basename$'"
f1_1 = Get value at time: 1, quarter, "Hertz", "Linear"
f2_1 = Get value at time: 2, quarter, "Hertz", "Linear"
f1_2 = Get value at time: 1, halfway, "Hertz", "Linear"
f2_2 = Get value at time: 2, halfway, "Hertz", "Linear"
f1_3 = Get value at time: 1, three_quarters,"Hertz", "Linear"
f2_3 = Get value at time: 2, three_quarters, "Hertz", "Linear"
;f1_1 = Get value at time: 1, quarter, "Bark", "Linear"
f1_1_Bark = hertzToBark(f1_1)
f2_1_Bark = hertzToBark(f2_1)
f1_2_Bark = hertzToBark(f1_2)
f2_2_Bark = hertzToBark(f2_2)
f1_3_Bark = hertzToBark(f1_3)
f2_3_Bark = hertzToBark(f2_3)
selectObject: "Sound 'basename$'"
Extract part: word_start-0.05, word_end+0.05, "rectangular", 1.0, 1
selectObject: "Sound 'basename$'_part"
To Pitch: 0, 75, 300
selectObject: "Pitch 'basename$'_part"
f0_1 = Get mean: start, quarter, "Hertz"
f0_2 = Get mean: quarter, three_quarters, "Hertz"
f0_3 = Get mean: three_quarters, end, "Hertz"
f0_1_st = hertzToSemitones(f0_1)
f0_2_st = hertzToSemitones(f0_2)
f0_3_st = hertzToSemitones(f0_3)
selectObject: "Sound 'basename$'_part"
plusObject: "Pitch 'basename$'_part"
Remove
appendFileLine: "'output$'.txt",
... basename$, tab$, word$, tab$, left$, tab$, phone$, tab$, right$, tab$,
... fixed$ (phone_duration, 3), tab$, fixed$ (word_duration, 3), tab$,
... fixed$ (f1_1, 3), tab$, fixed$ (f2_1, 3), tab$,
... fixed$ (f1_2, 3), tab$, fixed$ (f2_2, 3), tab$,
... fixed$ (f1_3, 3), tab$, fixed$ (f2_3, 3), tab$,
... fixed$ (f1_1_Bark, 3), tab$, fixed$ (f2_1_Bark, 3), tab$,
... fixed$ (f1_2_Bark, 3), tab$, fixed$ (f2_2_Bark, 3), tab$,
... fixed$ (f1_3_Bark, 3), tab$, fixed$ (f2_3_Bark, 3), tab$,
... fixed$ (f0_1, 3), tab$, fixed$ (f0_2, 3), tab$, fixed$ (f0_3, 3), tab$,
... fixed$ (f0_1_st, 3), tab$, fixed$ (f0_2_st, 3), tab$, fixed$ (f0_3_st, 3)
endif
endfor
selectObject: "Sound 'basename$'"
plusObject: "TextGrid 'basename$'"
plusObject: "Formant 'basename$'"
Remove
endfor
select all
Remove