-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathCut_Wav_TextGrid.Praat
103 lines (90 loc) · 6.42 KB
/
Cut_Wav_TextGrid.Praat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# This Praat script will cut the initial and end silence to a given value of wavs and TextGrids in a directory
# 切分句子首、尾静音段
#
# This script is distributed under the GNU General Public License.
# 博客说明:https://blog.csdn.net/shaopengfei/article/details/112252779
# Copyright 2020.04.22 feelins[[email protected]]
# 2021.04.28 update to relative path
form Dialogue_Cut
comment Directory path of input WAV files:
text input_wav_path old_wavs\
comment Directory path of input TextGrid files:
text input_textgrid_path -
comment Do you have TextGrid files:
boolean hasTextGrid 0
comment Duration do you want to leave(second):
real splitValue 0.1
comment Directory path of ouput WAV files:
text output_wav_path new_wavs\
comment Directory path of output TextGrid files:
text output_textgrid_path -
endform
clearinfo
# 如果新目录不存在,自动创建
if hasTextGrid
createDirectory: output_textgrid_path$
endif
createDirectory: output_wav_path$
Create Strings as file list: "fileList", input_wav_path$ + "*.wav"
numOfFiles = Get number of strings
for ifile from 1 to numOfFiles
selectObject: "Strings fileList"
fileName$ = Get string: ifile
simpleName$ = fileName$ - ".wav"
writeInfoLine: simpleName$
wavFullPath$ = input_wav_path$ + simpleName$ + ".wav"
textGridFullPath$ = input_textgrid_path$ + simpleName$ + ".TextGrid"
Read from file: wavFullPath$
simpleNameNoBlank$ = selected$("Sound",1)
if hasTextGrid
Read from file: textGridFullPath$
else
selectObject: "Sound " + simpleNameNoBlank$
To TextGrid (silences): 100, 0, -25, 0.1, 0.1, "sil", "sounding"
endif
selectObject: "TextGrid " + simpleNameNoBlank$
totalIntervals = Get number of intervals: 1
checkstr$ = simpleName$ + tab$
# 检查第一个音素sil时长
first$ = Get label of interval: 1, 1
startTime = Get starting point: 1, 1
endTime = Get end point: 1, 1
durationB = endTime - startTime
newStart = startTime
if durationB > splitValue
newStart = endTime - splitValue
checkstr$ = checkstr$ + string$(durationB-splitValue) + tab$
else
checkstr$ = checkstr$ + string$(0) + tab$
endif
# 检查最后一个音素sil时长
last$ = Get label of interval: 1, totalIntervals
startTime = Get starting point: 1, totalIntervals
endTime = Get end point: 1, totalIntervals
durationE = endTime - startTime
newEnd = endTime
if durationE > splitValue
newEnd = startTime + splitValue
checkstr$ = checkstr$ + string$(durationE-splitValue) + tab$
else
checkstr$ = checkstr$ + string$(0) + tab$
endif
selectObject: "Sound " + simpleNameNoBlank$
Extract part: newStart, newEnd, "rectangular", 1, "no"
selectObject: "TextGrid " + simpleNameNoBlank$
Extract part: newStart, newEnd, "no"
selectObject: "Sound " + simpleNameNoBlank$ + "_part"
Save as WAV file: output_wav_path$ + simpleName$ + ".wav"
if hasTextGrid
selectObject: "TextGrid " + simpleNameNoBlank$ + "_part"
Save as text file: output_textgrid_path$ + simpleName$ + ".TextGrid"
endif
selectObject: "Sound " + simpleNameNoBlank$
plusObject: "TextGrid " + simpleNameNoBlank$
plusObject: "Sound " + simpleNameNoBlank$ + "_part"
plusObject: "TextGrid " + simpleNameNoBlank$ + "_part"
Remove
endfor
selectObject: "Strings fileList"
Remove
exit over!