###################################################################
## 此脚本按设定的分贝值批量自动检测声音文件的起点和终点进行标注。##
## Written by Qibin Ran (ranqibin@126.com)                       ##
## 2019-04-20                                                    ##
###################################################################

form 批量自动标注起点和终点脚本
	comment 声音文件所在的文件夹地址
	text inputDirectory C:\Users\dell\Desktop\2020年5月19日——端点检测与声音提取、自动标注起终点\2起终点标注
	comment 设定起点的分贝值
	positive dBlevel1 50
	comment 设定终点的分贝值
	positive dBlevel2 50
endform

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

Create Strings as file list... fileList 'inputDirectory$'*.wav
	numberOfFiles = Get number of strings

		for ifile from 1 to numberOfFiles
			select Strings fileList
			fileName$ = Get string... 'ifile'
			simpName$ = fileName$ - ".wav"
			Read from file... 'inputDirectory$''fileName$'

				To Intensity: 100, 0
				n = Get number of frames
				for i to n
					intensity = Get value in frame: i
					if intensity > dBlevel1
						time1 = Get time from frame: i

						select Sound 'simpName$'
						To TextGrid... syllable
						plus Sound 'simpName$'
						Edit

						editor TextGrid 'simpName$'
	
						Select... 0 0
						Show analyses... "yes" "yes" "yes" "no" "no" 10
						Intensity settings... -5.0 100.0 "mean energy" "yes"

							Move cursor to... time1
							Add interval on tier 1
						endeditor
						i = n + 1
					endif
				endfor

				for ii to n
					x = n + 1 -ii
					select Intensity 'simpName$'
					intensity = Get value in frame: x
					if intensity > dBlevel2
						time2 = Get time from frame: x

						select Sound 'simpName$'
						plus TextGrid 'simpName$'
						Edit

						editor TextGrid 'simpName$'
	
						Select... 0 0
						Show analyses... "yes" "yes" "yes" "no" "no" 10
						Intensity settings... -5.0 100.0 "mean energy" "yes"

							Move cursor to... time2
							Add interval on tier 1
						endeditor
						ii = n + 1
					endif
				endfor

				select TextGrid 'simpName$'
				Write to text file... 'inputDirectory$''simpName$'.TextGrid
			select all
			minus Strings fileList
			Remove
		endfor
select all
Remove
exit OK!所有声音文件已经自动标注起点和终点！