
form Calculate durations of labeled segments
	comment Where are your wav/TextGrid/Pitch files?
	text openpath 请在此输入声音标注文件存放的文件夹地址
	comment Which tier of the TextGrid object would you like to analyse?
	integer Tier 1
	comment How many pitch point numbers would you like to extract?
	integer pointnum 30
	comment Where do you want to save the results?
	text textfile pitchoutput.csv
endform

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

Create Strings as file list... fileList 'openpath$'*.TextGrid
numberOfFiles=Get number of strings
filedelete 'textfile$'
fileappend "'textfile$'" label,duration,pitch1-30,'newline$'
for ifile from 1 to numberOfFiles
select Strings fileList
fileName$=Get string... 'ifile'
simpleName$=fileName$-".TextGrid"
wavName$=simpleName$+".wav"
pitchName$=simpleName$+".Pitch"

Read from file... 'openpath$''pitchName$'
select Pitch 'simpleName$'
Down to PitchTier
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
		fileappend "'textfile$'" 'label$','duration',
		select PitchTier 'simpleName$'
		stepnum=pointnum-1
		tempstep=duration/stepnum
		for ii from 0 to stepnum
			tempTime=start+tempstep*ii
			tempValue=Get value at time... 'tempTime'
			tempValue='tempValue:0'
			fileappend "'textfile$'" 'tempValue',
		endfor
		fileappend "'textfile$'" 'newline$'
		# append the label and the duration to the end of the text file, separated with a tab:		
		#resultline$ = "'label$'	'duration''newline$'"
		#fileappend "'textfile$'" 'resultline$'
		
	endif
endfor
select Pitch 'simpleName$'
plus PitchTier 'simpleName$'
Remove
select Sound 'simpleName$'
plus TextGrid 'simpleName$'
Remove
endfor
exit over!