﻿
form Track formant of labeled segments
	comment Where are your wav/TextGridfiles?
	text openpath C:\Users\ran\Desktop\原桌面文件\
	comment Which tier of the TextGrid object would you like to analyse?
	integer Tier 1
	comment Enter speaker gender (m or f only)
        sentence Gender m
        comment How many formant numbers would you like to extract?
	integer pointnum 10
	comment Name of result do you want to save with:
	text textfile Formantoutput.xlsx

endform


if left$(openpath$,1)<>"\"
	openpath$=openpath$+"\"
endif

Create Strings as file list... fileList 'openpath$'*.TextGrid
numberOfFiles=Get number of strings
filedelete 'Formantoutput$'
fileappend "'openpath$'Formantoutput.csv" speaker'tab$'vowel/frame'tab$'context'tab$'F1'tab$'F2'tab$'F3'tab$'gl F1'tab$'gl F2'tab$'gl F3'newline$'
for ifile from 1 to numberOfFiles
select Strings fileList
fileName$=Get string... 'ifile'
simpleName$=fileName$-".TextGrid"
wavName$=simpleName$+".wav"

Read from file... 'openpath$''wavName$'

     if gender$ = "f"
          To Formant (burg)... 0.0025 5 5500 0.025 50
          else
          To Formant (burg)... 0.0025 5 5000 0.025 50
     endif

Read from file... 'openpath$''wavName$'
Read from file... 'openpath$''fileName$'


numberOfIntervals = Get number of intervals... tier

for interval from 1 to numberOfIntervals
	select TextGrid 'simpleName$'
	label$ = Get label of interval... tier interval
	# if the interval has some text as a label, then calculate the duration.
	if label$ <> ""
		start = Get starting point... tier interval
		end = Get end point... tier interval
		duration = end - start
		select Formant 'simpleName$'
		stepnum=pointnum-1
		tempstep=duration/stepnum
		for ii from 0 to stepnum
			tempTime=start+tempstep*ii
			select Formant 'simpleName$'
                        f_one = Get value at time... 1 'tempTime' Hertz Linear
                        f_two = Get value at time... 2 'tempTime' Hertz Linear
                        f_three = Get value at time... 3 'tempTime' Hertz Linear
                        fileappend "'openpath$'Formantoutput.csv" 'simpleName$''tab$''label$''tab$'""'tab$''f_one:0''tab$''f_two:0''tab$''f_three:0''newline$'
                endfor
	endif
endfor
select all
minus Strings fileList
Remove
endfor
select all
Remove
exit OK!

