Praat 脚本 | 批量生成各类声学参数文件

作者:熊子瑜
脚本ID:Praat.XZY20230915.023
上传时间:2023年9月15日

简介:可根据用户所指定的声音文件路径或声音文件名列表文件,批量生成全部声音文件的声学参数数据对象(PitchTier、IntensityTier、FormantGrid)。如果相应的声学参数文件已存在,脚本程序会自动跳过,不再重新生成,以免覆盖修改后的声学参数文件。需注意:(1)如果输入的是声音文件所在的路径,请以“\”符号结尾;(2)如果输入的是声音文件名列表文件,该列表文件请使用UTF8编码格式,声音文件名需使用全路径。

#By XIONG Ziyu
#last modify: 2021/9/4
#Praat version: 6.1.05
#功能:根据指定的声音文件路径或列表,自动生成声学参数数据对象(PitchTier、IntensityTier、FormantGrid),此脚本适合任意声音文件进行操作。
#注意:(1)如果输入的是声音文件所在的路径,请以“\”符号结尾
#      (2)如果输入的是声音文件名列表文件,该列表文件请使用UTF8编码格式,声音文件名请使用全路径
#      (3)如果相应的声学参数文件已经存在,则不再重新生成,以免覆盖修改后的声学参数文件


#对话框,接受用户输入参数
#Sound_FilePath_or_FileList:声音文件所在的路径或者声音文件名列表文件
#Is_Stereo_Sound: 设定声音文件是否为双声道立体声
#Channel_Index_for_Data_Analyse:如果是双通道立体声,则需要设定某个声道的数据来分析声学参数
#Pitch_floor,Pitch_ceiling:请根据发音人的音域范围设定合适的音高上下限,会影响分析结果。女声:100Hz~480Hz(一般);男声:60Hz~300Hz(一般)
#Maximum_Frequency_Of_Formants:第5共振峰的上限频率值。女声:5500Hz;男声:5000Hz
#Data_Type:分析生成哪类声学参数文件:音高数据PitchTier、音强数据IntensityTier、共振峰数据FormantGrid

form 批量生成各类声学参数文件
	sentence Sound_FilePath_or_FileList C:\SOUNDS\
	boolean Is_Stereo_Sound 0
	positive Channel_Index_for_Data_Analyse 1
	positive Pitch_floor_(Hz) 75.0
	positive Pitch_ceiling_(Hz) 600.0
	positive Maximum_Frequency_Of_Formants_(Hz) 5500
	optionmenu Data_Type: 1
		option PitchTier
		option IntensityTier
		option FormantGrid
endform

#清空主窗口对象列表中的数据对象
Create TextGrid: 0, 1, "Mary John bell", "bell"
select all
Remove

#将Praat读取和存储文本文件的编码格式设定为UTF8,避免汉字乱码
Text reading preferences: "UTF-8"
Text writing preferences: "UTF-8"

maxFre=maximum_Frequency_Of_Formants
channelIndex=channel_Index_for_Data_Analyse

#判断是文件路径还是文件名列表
sound_FilePath_or_FileList$=replace$(sound_FilePath_or_FileList$,"/","\",0)
fPathOrList$=sound_FilePath_or_FileList$
sPath$=left$(fPathOrList$,rindex(fPathOrList$,"\"))
if right$(fPathOrList$,1)!="\"
	if fileReadable(fPathOrList$)
		Read Strings from raw text file: "'fPathOrList$'"
		Rename: "List"
	else
		exitScript: "声音文件名列表文件不存在,请核查!"+newline$+newline$
	endif
else
	Create Strings as file list: "List", "'fPathOrList$'*.wav"
endif

#批量提取声学参数文件并保存
selectObject: "Strings List"
fileNum=Get number of strings
if fileNum=0
	exitScript: "没有发现要操作的声音文件,请检查!"+newline$+newline$
endif
for f from 1 to fileNum
	selectObject: "Strings List"
	filename$=Get string: 'f'
	if index(filename$,":")=0
		filename$= fPathOrList$+filename$
	endif
	pos=rindex(filename$,".")
	extname$=""
	if pos>0
		extname$=right$(filename$,length(filename$)-pos+1)
	endif
	if data_Type=1
		pfilename$=filename$-extname$+".PitchTier"
	endif
	if data_Type=2
		pfilename$=filename$-extname$+".IntensityTier"
	endif
	if data_Type=3
		pfilename$=filename$-extname$+".FormantGrid"
	endif
	sfilename$=filename$-extname$+".wav"
	echo 'f'/'fileNum'  'sfilename$'

	#如果相应的声学参数文件已经存在,则不再重新生成,以免覆盖修改后的声学参数文件
	if fileReadable(pfilename$)=0 and fileReadable(sfilename$)
		if is_Stereo_Sound=1 and channelIndex>0
			Read two Sounds from stereo file: "'sfilename$'"
			cname$=selected$("Sound")
			cname$=left$(cname$,length(cname$)-4)
			selectObject: "Sound 'cname$'_ch'channelIndex'"
		else
			Read from file: "'sfilename$'"
		endif
		if data_Type=1
			Filter (stop Hann band): 0, 100, 10
			noprogress To Pitch: 0.01, 'pitch_floor', 'pitch_ceiling'
			Down to PitchTier
			nowarn Save as text file: "'pfilename$'"
		endif
		if data_Type=2
			noprogress To Intensity: 100, 0, "yes"
			Down to IntensityTier
			nowarn Save as text file: "'pfilename$'"
		endif
		if data_Type=3
			noprogress To Formant (burg): 0, 5, 'maxFre', 0.025, 50
			Down to FormantGrid
			nowarn Save as text file: "'pfilename$'"
		endif
		select all
		minus Strings List
		Remove
	endif
endfor
select all
Remove
exitScript: "操作过程已结束,请检查数据结果!"+newline$+newline$