-
Notifications
You must be signed in to change notification settings - Fork 24
/
replace_part_of_textgrid.praat
321 lines (293 loc) · 9.84 KB
/
replace_part_of_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
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
# This script will replace part of a TextGrid object with segments and labels in
# another, shorter TextGrid.
# Two TextGrids have to be selected in the Object list.
# The longer of these two TextGrids will be taken as the "mother" TextGrid.
# If the two TextGrids are of equal duration, the first one in the object list
# will be considered as the "mother".
# The user is prompted to select the starting time point or a segment
# in the mothe TextGrid.
# Segments and labels from the time of the cursor or the beginning of segment
# will be replaced by the small grid.
# A new TextGrid will be created that contains the result.
#
# This script is distributed under the GNU General Public License.
# Copyright 11.7.2002 Mietta Lennes
#
form Replace TextGrid by another
choice Replace 1
button all tiers that have the same number
button all tiers that have the same name
button only one tier
integer in_tier_number 1
comment You will be prompted for the starting time of the part to be replaced...
endform
grid1$ = selected$ ("TextGrid", 1)
grid2$ = selected$ ("TextGrid", 2)
# Decide which of the two Grids will be replaced with which:
select TextGrid 'grid1$'
dur1 = Get duration
select TextGrid 'grid2$'
dur2 = Get duration
if dur1 > dur2
biggrid$ = grid1$
smallgrid$ = grid2$
select = 1
elsif dur2 > dur1
biggrid$ = grid2$
smallgrid$ = grid1$
select = 1
else
biggrid$ = grid1$
smallgrid$ = grid2$
select = 0
endif
select TextGrid 'biggrid$'
Edit
editor TextGrid 'biggrid$'
pause Please select the interval from the start of which the TextGrid 'biggrid$' should be replaced, and press Continue when ready!
start = Get starting point of interval
endeditor
select TextGrid 'smallgrid$'
smallNumberOfTiers = Get number of tiers
smallstart = Get starting time
smallduration = Get duration
select TextGrid 'biggrid$'
bigNumberOfTiers = Get number of tiers
bigstart = Get starting time
bigduration = Get duration
if bigstart <> 0 or smallstart <> 0
exit Both TextGrids must start from time 0. Cannot replace! Please make sure the time scale is okay and try again...
endif
# Make an empty copy of the big TextGrid, with just the correct tiers:
n$ = ""
for n from 1 to bigNumberOfTiers
select TextGrid 'biggrid$'
tiername$ = Get tier name... n
n$ = n$ + "'tiername$' "
endfor
n$ = left$ (n$, (length (n$) - 1))
Create TextGrid... 0 bigduration 'n$'
Rename... 'biggrid$'_replaced
if replace = 1
# all tiers with the same number should be replaced...
if smallNumberOfTiers > bigNumberOfTiers
newtier = 0
moretiers = smallNumberOfTiers - bigNumberOfTiers
printline 'moretiers' more tiers are needed
# new tiers will be added to the grid with corresponding names
for i to moretiers
newtier = bigNumberOfTiers + i
select TextGrid 'smallgrid$'
newlabel$ = Get tier name... newtier
select TextGrid 'biggrid$'_replaced
Insert interval tier... newtier 'newlabel$'
select TextGrid 'biggrid$'
Insert interval tier... newtier 'newlabel$'
endfor
endif
for y to smallNumberOfTiers
call ReplaceTier y y
endfor
if moretiers > 0
for n to moretiers
select TextGrid 'biggrid$'
bigNumberOfTiers = Get number of tiers
Remove tier... bigNumberOfTiers
endfor
endif
if bigNumberOfTiers > smallNumberOfTiers
beg = smallNumberOfTiers + 1
select TextGrid 'biggrid$'_replaced
for t from beg to bigNumberOfTiers
newNumberOfTiers = Get number of tiers
Remove tier... newNumberOfTiers
endfor
for t from beg to bigNumberOfTiers
call CopyIntervalTier 'biggrid$' t 'biggrid$'_replaced
endfor
endif
elsif replace = 2
# all tiers with the same name should be replaced...
for i to bigNumberOfTiers
replaced = 0
select TextGrid 'biggrid$'
bigtiername$ = Get tier name... i
for p to smallNumberOfTiers
select TextGrid 'smallgrid$'
smalltiername$ = Get tier name... p
if bigtiername$ = smalltiername$
call ReplaceTier p i
replaced = 1
endif
endfor
if replaced = 0
call CopyIntervalTier 'biggrid$' i 'biggrid$'_replaced
endif
endfor
if bigNumberOfTiers < smallNumberOfTiers
select TextGrid 'biggrid$'_replaced
newNumberOfTiers = Get number of tiers
for p to smallNumberOfTiers
exists = 0
select TextGrid 'smallgrid$'
smalltiername$ = Get tier name... p
for i to newNumberOfTiers
select TextGrid 'biggrid$'_replaced
bigtiername$ = Get tier name... p
if bigtiername$ = smalltiername$
exists = 1
endif
endfor
if exists = 0
call ReplaceTier p i
endif
endfor
endif
else
# only tier 'in_tier_number' should be replaced...
if smallNumberOfTiers = 1
call ReplaceTier in_tier_number 1
if bigNumberOfTiers > 1
for t from 2 to bigNumberOfTiers
call CopyIntervalTier 'biggrid$' t 'biggrid$'_replaced
endfor
endif
elsif smallNumberOfTiers >= in_tier_number
if bigNumberOfTiers < in_tier_number
exit There are only 'bigNumberOfTiers' in the TextGrid 'biggrid$'. Tier 'in_tier_number' in TextGrid 'biggrid$' was not replaced! Exit...
endif
if in_tier_number > 1
stop = in_tier_number - 1
for t to stop
call CopyIntervalTier 'biggrid$' t 'biggrid$'_replaced
endfor
endif
call ReplaceTier in_tier_number in_tier_number
beg = in_tier_number + 1
for t from beg to bigNumberOfTiers
call CopyIntervalTier 'biggrid$' t 'biggrid$'_replaced
endfor
else
exit There are only 'smallNumberOfTiers' in the TextGrid 'smallgrid$'. Tier 'in_tier_number' in TextGrid 'biggrid$' was not replaced! Exit...
endif
endif
echo Finished!
if replace = 1
printline All tiers from 1 to smallNumberOfTiers in 'biggrid$' were replaced by tiers in 'smallgrid$',
elsif replace = 2
printline All tiers that had the same name in 'biggrid$' as in 'smallgrid$' were replaced by the tiers in 'smallgrid$',
else
printline starting from time 'start:3' s, ending at time 'replace_end:3' s.
endif
printline
printline The original TextGrids were not modified.
printline The result is the new TextGrid 'biggrid$'_replaced.
#--------
procedure ReplaceTier replaceThisTier withThisTier
# Check if the first interval has an identical label:
select TextGrid 'biggrid$'
duration = Get duration
numberOfIntervals2 = Get number of intervals... replaceThisTier
int2 = Get interval at time... replaceThisTier start
start2 = Get starting point... replaceThisTier int2
end2 = Get end point... replaceThisTier int2
for interval from 1 to int2
select TextGrid 'biggrid$'
if interval = 1
label2$ = Get label of interval... replaceThisTier 1
select TextGrid 'biggrid$'_replaced
Set interval text... replaceThisTier 1 'label2$'
elsif interval < int2
boundary = Get starting point... replaceThisTier interval
label2$ = Get label of interval... replaceThisTier interval
select TextGrid 'biggrid$'_replaced
Insert boundary... replaceThisTier boundary
Set interval text... replaceThisTier interval 'label2$'
else
select TextGrid 'biggrid$'_replaced
Insert boundary... replaceThisTier start2
endif
endfor
select TextGrid 'biggrid$'
int2 = Get interval at time... replaceThisTier start
start2 = Get starting point... replaceThisTier int2
end2 = Get end point... replaceThisTier int2
label2$ = Get label of interval... replaceThisTier int2
select TextGrid 'biggrid$'_replaced
Insert boundary... replaceThisTier end2
newNumberOfIntervals = Get number of intervals... replaceThisTier
newint = Get interval at time... replaceThisTier start
select TextGrid 'smallgrid$'
numberOfIntervals1 = Get number of intervals... withThisTier
int1 = 1
start1 = 0
end1 = Get end point... withThisTier int1
label1$ = Get label of interval... withThisTier int1
select TextGrid 'biggrid$'_replaced
if start2 < start
if label1$ <> label2$ or (label1$ <> "" and label2$ <> "")
Insert boundary... replaceThisTier start
int2 = int2 + 1
endif
endif
# go through the small grid and replace part of tier by it:
for int1 from 1 to numberOfIntervals1
select TextGrid 'smallgrid$'
label1$ = Get label of interval... withThisTier int1
end1 = Get end point... withThisTier int1
if int1 > 1
start1 = Get starting point... withThisTier int1
boundary = start + start1
select TextGrid 'biggrid$'_replaced
Insert boundary... replaceThisTier boundary
int2 = int2 + 1
endif
select TextGrid 'biggrid$'_replaced
Set interval text... replaceThisTier int2 'label1$'
replace_end = start + end1
if replace_end >= duration
int1 = numberOfIntervals1
endif
endfor
newNumberOfIntervals = Get number of intervals... replaceThisTier
label1$ = Get label of interval... replaceThisTier newNumberOfIntervals
# copy the rest of the tier from the old big grid:
select TextGrid 'biggrid$'
int2 = Get interval at time... replaceThisTier replace_end
start2 = Get starting point... replaceThisTier int2
end2 = Get end point... replaceThisTier int2
label2$ = Get label of interval... replaceThisTier int2
if label1$ <> label2$ or (label1$ <> "" and label2$ <> "")
select TextGrid 'biggrid$'_replaced
Insert boundary... replaceThisTier replace_end
newNumberOfIntervals = Get number of intervals... replaceThisTier
Set interval text... replaceThisTier newNumberOfIntervals 'label2$'
endif
while end2 < duration or int2 < numberOfIntervals2
int2 = int2 + 1
select TextGrid 'biggrid$'
start2 = Get starting point... replaceThisTier int2
end2 = Get end point... replaceThisTier int2
label2$ = Get label of interval... replaceThisTier int2
select TextGrid 'biggrid$'_replaced
Insert boundary... replaceThisTier start2
newNumberOfIntervals = newNumberOfIntervals + 1
Set interval text... replaceThisTier newNumberOfIntervals 'label2$'
endwhile
endproc
# ------
procedure CopyIntervalTier gridname1$ tier1 gridname2$
select TextGrid 'gridname1$'
tiername$ = Get tier name... tier1
Extract tier... tier1
Rename... 'tiername$'
select TextGrid 'gridname2$'
plus IntervalTier 'tiername$'
Append
Rename... temp
select TextGrid 'gridname2$'
plus IntervalTier 'tiername$'
Remove
select TextGrid temp
Rename... 'gridname2$'
endproc