-
Notifications
You must be signed in to change notification settings - Fork 1
/
makeTextGridFromSilences.praat
367 lines (298 loc) · 10.9 KB
/
makeTextGridFromSilences.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Generate batch of textgrids V.1.0.0
# ===================================
# Written for Praat 6.0.31
# script by Antoin Eoin Rodgers
# rodgeran@tcd.ie
# Phonetics and speech Laboratory, Trinity College Dublin
# August 25, 2020
# This script reduces some of the donkey work involved in preparing speech
# data for analysis and processing:
# It does the following:
# 1. Cleans up the recording by remove low-freqency noise.
# 2. Automatically chunks the recording into separate phrases / repetitions
# based on regions of silence (using "To Textgrid ( silences)" function).
# 3. Displays the automatic textgrid annotation to allow for manual
# correction of errors.
# 4. Automatically labels each area of non-silence with a prefix +
# number(e.g. YNQ_1, BEAG_1)
# 5. Prompts the user to correct any errors
# 6. Backs up previous version of files.
# 7. Saves Chopped up large sounds into "SmallFiles" directory.
# check version compatibility
version$ = praatVersion$
if left$(version$, 1) != "6"
echo You are running Praat 'praatVersion$'.
... 'newline$'This script is designed to run on Praat version 6.0.31 or later.
... 'newline$'To run this script, update to the latest
... version at praat.org
exit
endif
# UI INPUT
beginPause: "Generate batch of textrids and sound files"
comment: "SOUND FILES"
sentence: "Directory", "enter directory here"
word: "Sound file suffix", ".wav"
comment: ""
comment: "SILENCES DETECTION"
comment: "Parameters for the intensity analysis"
natural: "Minimum pitch (Hz)", 100
real: "Time step (s)", "0.0 (= auto)"
comment: "Silent intervals detection"
real: "Silence threshold (dB)", -25.0
positive: "Minimum silent interval duration (s)", 0.2
positive: "Minimum sounding interval duration", 0.2
real: "sounding edge buffer (s)", "0.075"
sentence: "Sounding interval label or prefix", "sound"
optionMenu: "Annotate sound intervals by", 1
option: "number (1, 2, 3, ...)"
option: "prefix plus number (sound_1, sound_2, ...)"
comment: ""
comment: "EXTRA OPTIONS"
boolean: "Automatically update annotation text", 1
comment: ""
boolean: "Chop large file into smaller files and save", 1
real: "buffer size at edges",0.1
comment: ""
boolean: "Run high pass filter", 0
natural: "Lowest pass frequency", 40
natural: "Smoothing", 15
comment: ""
comment: "NB: Original files will be backed up to a sub-directory " +
... "called 'backup'."
myChoice = endPause: "Exit", "Continue", 2, 1
if myChoice = 1
exit
endif
# create manageable variable names
soundSuffix$ = sound_file_suffix$
soundDir$ = directory$
if (right$(soundDir$, 1) != "/" or right$(soundDir$, 1) != "\") and
... right$(soundDir$, 1) != ""
soundDir$ += "/"
endif
if soundDir$ = ""
soundRoot$ = "root directory"
else
soundRoot$ = left$(soundDir$, length(soundDir$) - 1)
endif
soundSuffix$ = sound_file_suffix$
if left$(soundSuffix$, 1) != "."
soundSuffix$ = "." + soundSuffix$
endif
minF0 = minimum_pitch
tStep = time_step
maxSil_dB = silence_threshold
minSil_T = minimum_silent_interval_duration
minSound_T = minimum_sounding_interval_duration
buffer = sounding_edge_buffer
soundIntLab$ = sounding_interval_label_or_prefix$
silIntLab$ = ""
annotateBy = annotate_sound_intervals_by
if annotateBy = 2
prefix$ = soundIntLab$ + "_"
else
prefix$ = ""
endif
autoUpdate = automatically_update_annotation_text
chopUpSound = chop_large_file_into_smaller_files_and_save
saveBuffer = buffer_size_at_edges
saveDir$ = soundDir$ + "SmallFiles"
runFilter = run_high_pass_filter
lowestPassHz = lowest_pass_frequency
# get list of wave files
soundList = Create Strings as file list: "fileList", soundDir$
... + "*'soundSuffix$'"
Rename: "soundList"
soundList = selected ()
numberOfFiles = Get number of strings
if numberOfFiles = 0
appendInfoLine: "No ""'soundSuffix$'"" files found."
appendInfoLine: "Exiting script."
exit
endif
#create directories
backup$ = soundDir$ + "backup"
createDirectory: backup$
backup$ += "/"
if chopUpSound
createDirectory: saveDir$
saveDir$ += "/"
endif
if runFilter
@filterSound: soundDir$, lowestPassHz, smoothing, soundList, backup$
endif
@createSoundTier: soundDir$, soundSuffix$, soundList,
... minF0, tStep,
... maxSil_dB, minSil_T, minSound_T, buffer,
... soundIntLab$, silIntLab$,
... prefix$, backup$
@checkManually: soundDir$, soundSuffix$, autoUpdate, soundList, prefix$,
... chopUpSound, saveBuffer, saveDir$
procedure filterSound: .soundDir$, .lowestPassHz, .smoothing,
... .soundList, .backup$
selectObject: .soundList
.numberOfFiles = Get number of strings
for .curFile to .numberOfFiles
selectObject: .soundList
.fileName$ = Get string: .curFile
.unFiltered = Read from file: .soundDir$ + .fileName$
Save as WAV file: .backup$ + .fileName$
.filtered = Filter (stop Hann band): 0, .lowestPassHz, .smoothing
Save as WAV file: .soundDir$ + .fileName$
plusObject: .unFiltered
Remove
endfor
endproc
procedure createSoundTier: .soundDir$, .soundSuffix$, .soundList,
... .minF0, .tStep,
... .maxSil_dB, .minSil_T, .minSound_T, .buffer,
... .soundIntLab$, .silIntLab$,
... .prefix$, .backup$
# adjust time step for "auto"
if .tStep = 0
.tStep = 0.8 / .minF0
endif
selectObject: .soundList
.numberOfFiles = Get number of strings
for .curFile to .numberOfFiles
selectObject: .soundList
.fileName$ = Get string: .curFile
.gridName$ = replace$(.fileName$, .soundSuffix$, ".TextGrid", 1)
.curSound = Read from file: .soundDir$ + .fileName$
.grid = To TextGrid (silences): .minF0, .tStep,
... .maxSil_dB, .minSil_T, .minSound_T,
... "", "x"
@makeSoundingTextGrid: .grid, .curSound, .buffer, .prefix$,
... 0, 0, ""
# back up old grid
if fileReadable(.soundDir$ + .gridName$)
Read from file: .soundDir$ + .gridName$
Save as text file: .backup$ + .gridName$
Remove
endif
# save new grid
selectObject: .grid
Save as text file: .soundDir$ + .gridName$
Remove
removeObject: .curSound
endfor
endproc
procedure checkManually: .soundDir$, .soundSuffix$, .autoUpdate, .soundList,
... .prefix$, .chopUpSound, .saveBuffer, .saveDir$
selectObject: .soundList
.numSounds = Get number of strings
for .i to .numSounds
selectObject: .soundList
.curSound$ = Get string: .i
.curGrid$ = replace$(.curSound$, .soundSuffix$, ".TextGrid", 1)
.curSound = Read from file: .soundDir$ + .curSound$
.curGrid = Read from file: .soundDir$ + .curGrid$
.startTime = Get start time
.endTime = Get end time
selectObject: .curSound
plusObject: .curGrid
Edit
editor: .curGrid
Zoom: .startTime, .endTime
endeditor
pauseText$ = "Checking " + replace$(.curSound$, .soundSuffix$, "", 1)
beginPause: pauseText$
comment: "Check and correct errors in "
... + replace$(.curSound$, .soundSuffix$, "", 1) + "."
edit_choice = endPause: "Exit", "Next", 2, 1
if edit_choice = 1
exit
endif
if .autoUpdate
@makeSoundingTextGrid: .curGrid, .curSound, 0, .prefix$,
... .chopUpSound, .saveBuffer, .saveDir$
endif
selectObject: .curGrid
Save as text file: .soundDir$ + .curGrid$
Remove
removeObject: .curSound
endfor
endproc
procedure makeSoundingTextGrid: .grid, .sound, .buffer, .prefix$,
... .chopUpSound, .saveBuffer, .saveDir$
selectObject: .grid
.startTime = Get start time
.endTime = Get end time
.table = Down to Table: "no", 3, "no", "no"
.numInts = Get number of rows
# remove silences tier
selectObject: .grid
Insert interval tier: 2, "sounds"
Remove tier: 1
selectObject: .table
# Calculate adjusted initial boundary values
.curMin = Get value: 1, "tmin"
.curMin -= .buffer * (.curMin >= (.startTime + .buffer))
.curMax = Get value: 1, "tmax"
.nextMin = Get value: 2, "tmin"
.curMax += .buffer * (.nextMin > (.curMin + .buffer))
Set string value: 1, "tmin", fixed$(.curMin, 3)
Set string value: 1, "tmax", fixed$(.curMax, 3)
# Calculate all but final adjusted boundary values
for .i from 2 to .numInts - 1
.prevMin = .curMin
.prevMax = .curMax
.curMin = .nextMin
.curMin -= .buffer * (.curMin >= (.prevMax + .buffer))
.curMax = Get value: .i, "tmax"
.nextMin = Get value: .i + 1, "tmin"
.curMax += .buffer * (.nextMin > (.curMin + .buffer))
Set string value: .i, "tmin", fixed$(.curMin, 3)
Set string value: .i, "tmax", fixed$(.curMax, 3)
endfor
# Calculate adjusted initial boundary values
.curMin = Get value: .numInts, "tmin"
.curMin -= .buffer * (.curMin >= (.startTime + .buffer))
.curMax = Get value: .numInts, "tmax"
.nextMin = .endTime
.curMax += .buffer * (.nextMin > (.curMin + .buffer))
Set string value: .numInts, "tmin", fixed$(.curMin, 3)
Set string value: .numInts, "tmax", fixed$(.curMax, 3)
for .i to .numInts
selectObject: .table
Set string value: .i, "text", .prefix$ + string$(.i)
.intStart = Get value: .i, "tmin"
.intEnd = Get value: .i, "tmax"
selectObject: .grid
Insert boundary: 1, .intStart
Insert boundary: 1, .intEnd
Set interval text: 1, .i * 2, .prefix$ + string$(.i)
endfor
if .chopUpSound
@chopUpSound: .table, .grid, .sound, .saveDir$, .saveBuffer
endif
removeObject: .table
endproc
procedure chopUpSound: .table, .grid, .sound, .saveDir$, .saveBuffer
selectObject: .sound
.name$ = selected$("Sound")
.startTime = Get start time
.endTime = Get end time
selectObject: .table
.numRows = Get number of rows
for .i to .numRows
.tmin[.i] = Get value: .i, "tmin"
.tmin[.i] -= ((.tmin[.i] - .saveBuffer) >= .startTime) * .saveBuffer
.text$[.i] = Get value: .i, "text"
.tmax[.i] = Get value: .i, "tmax"
.tmax[.i] += ((.tmax[.i] + .saveBuffer) >= .endTime) * .saveBuffer
endfor
for .i to .numRows
selectObject: .grid
Insert interval tier: 2, "temp"
Insert boundary: 2, .tmin[.i]
Insert boundary: 2, .tmax[.i]
Set interval text: 2, 2, .text$[.i]
plusObject: .sound
.temp = Extract non-empty intervals: 2, "no"
Save as WAV file: .saveDir$ + .name$ + "_" +selected$("Sound") + ".wav"
Remove
selectObject: .grid
Remove tier: 2
endfor
endproc