-
Notifications
You must be signed in to change notification settings - Fork 7
/
Equalize_duration.txt
314 lines (249 loc) · 8.93 KB
/
Equalize_duration.txt
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
#######################################################
# Equalize signal duration
#
# by either stretching/shrinking the signal,
# or by padding with silence at the beginning or end
#
# Matthew Winn
# November 2022
# version 2
#######################################################
form Equalize duration
comment how do you want the sounds to be equalized
choice method: 1
button stretch/shrink_duration
button pad silence after the sound
button pad silence before the sound
comment
choice match_to_this_target: 2
button longest duration in the batch
button shortest duration in the batch
button average duration in the batch
button specific duration (in s) below
real optional_target_duration 0.740
comment
choice which_sounds: 1
button sounds in the object list
button sounds in a folder:
sentence folder C:/Users/Matt/Documents/PraatScripts/Duration_normalization/All_my_sounds
boolean save_files 0
comment if saving, add this to the filename on save:
sentence suffix _eq
endform
########################################
########################################
clearinfo
# if your output sounds weird, check these pitch settings
minpitch = 75
maxpitch = 400
remove_new_files = 1
# logic check
call check_choice_compatibility
# measure all the durations of the sounds
call get_all_durations 'which_sounds$'
max_duration = get_all_durations.max_duration
max_duration_rounded = get_all_durations.max_duration_rounded
# make sure longest duration will support user's choice
call check_longest_duration_compatibility
# set target duration
if match_to_this_target$ == "longest duration in the batch"
target_duration = get_all_durations.max_duration
sub_folder_name$ = "eq_dur_max"
elsif match_to_this_target$ == "shortest duration in the batch"
target_duration = get_all_durations.min_duration
sub_folder_name$ = "eq_dur_min"
elsif match_to_this_target$ == "average duration in the batch"
target_duration = get_all_durations.mean_duration
sub_folder_name$ = "eq_dur_mean"
elsif match_to_this_target$ == "specific duration (in s) below"
target_duration = optional_target_duration
target_duration_ms = round(target_duration*1000)
folder_label$ = "'target_duration_ms'"
if target_duration_ms < 1000
folder_label$ = "0'target_duration_ms'"
endif
if target_duration_ms < 100
folder_label$ = "00'target_duration_ms'"
endif
sub_folder_name$ = "eq_dur_target_'folder_label$'"
endif
if save_files == 1
print saving files'newline$'
# make the directory for the new sounds
system mkdir "'folder$'/'sub_folder_name$'"
endif
# cycle through the sounds, alter each one
if which_sounds$ == "sounds in the object list"
# sounds have already been numbered by previous procedure
for thisSound from 1 to numberOfSelectedSounds
select sound'thisSound'
name$ = selected$("Sound")
call alter_sound 'name$'
endfor
elsif which_sounds$ == "sounds in a folder:"
# list the sounds in the folder
Create Strings as file list: "fileList", "'folder$'\*.wav"
num_files = Get number of strings
for file_index from 1 to num_files
select Strings fileList
filename$ = Get string: file_index
Read from file: "'folder$'\'filename$'"
name$ = selected$("Sound")
call alter_sound 'name$'
# save the sound
if save_files == 1
select Sound 'name$'_eq
Save as WAV file... 'folder$'/'sub_folder_name$'/'name$''suffix$'.wav
endif
select Sound 'name$'
if remove_new_files == 1
plus Sound 'name$'_eq
endif
Remove
endfor
endif
#################################
procedure alter_sound .name$
# get some basic info
select Sound '.name$'
samplerate = Get sampling frequency
num_channels = Get number of channels
dur = Get total duration
if method$ == "pad silence after the sound"
if dur < target_duration
#print using 'method$' for duration 'target_duration''newline$'
buffer_duration = target_duration - dur
Create Sound from formula... buffer num_channels 0 buffer_duration samplerate 0
select Sound '.name$'
plus Sound buffer
Concatenate
select Sound buffer
Remove
select Sound chain
Rename... '.name$'_eq
else
Copy... '.name$'_eq
endif
elsif method$ == "pad silence before the sound"
if dur < target_duration
#print using 'method$' for duration 'target_duration''newline$'
buffer_duration = target_duration - dur
Create Sound from formula... buffer num_channels 0 buffer_duration samplerate 0
select Sound '.name$'
Copy... temp
select Sound buffer
plus Sound temp
Concatenate
select Sound buffer
plus Sound temp
Remove
select Sound chain
Rename... '.name$'_eq
else
Copy... '.name$'_eq
endif
elsif method$ == "stretch/shrink_duration"
# alter using Lengthen procedure
#print using 'method$' for duration 'target_duration''newline$'
duration_ratio = target_duration/dur
print 'name$''tab$' with duration 'tab$''dur:3''tab$' warped by'tab$''duration_ratio:3' to match target duration of'tab$''target_duration:3''newline$'
select Sound '.name$'
Lengthen (overlap-add): minpitch, maxpitch, target_duration/dur
Rename: "'.name$'_eq"
endif
endproc
print DONE!
##################################
procedure get_all_durations .which_sounds$
# initialize table
Create Table with column names: "durations", 0, "sound duration"
if .which_sounds$ == "sounds in the object list"
pause select all sounds to be used for this operation
numberOfSelectedSounds = numberOfSelected ("Sound")
# number the sounds
for sound_index to numberOfSelectedSounds
sound'sound_index' = selected("Sound",sound_index)
endfor
# get duration of all sounds
for sound_index from 1 to numberOfSelectedSounds
select sound'sound_index'
.name$ = selected$("Sound")
.dur = Get total duration
select Table durations
Append row
row_number = sound_index
Set string value: row_number, "sound", .name$
Set numeric value: row_number, "duration", .dur
endfor
elsif .which_sounds$ == "sounds in a folder:"
Create Strings as file list: "fileList", "'folder$'/*.wav"
num_files = Get number of strings
if num_files > 0
for file_index from 1 to num_files
select Strings fileList
filename$ = Get string: file_index
Read from file: "'folder$'\'filename$'"
.name$ = selected$("Sound")
.dur = Get total duration
select Table durations
Append row
row_number = file_index
Set string value: row_number, "sound", .name$
Set numeric value: row_number, "duration", .dur
select Sound '.name$'
Remove
endfor
else
exit no sounds in the folder (check the path?)
endif
endif
selectObject: "Table durations"
.min_duration = Get minimum: "duration"
.max_duration = Get maximum: "duration"
.mean_duration = Get mean: "duration"
.max_duration_rounded = ceiling(.max_duration*1000)/1000
endproc
procedure check_choice_compatibility
# if pad with silence, must match to longest
if left$(method$,3) == "pad"
if match_to_this_target$ == "shortest duration in the batch" || match_to_this_target$ == "average duration in the batch"
beginPause: "Must update your choice for target duration"
comment: "padding duration must match to either longest in the batch,"
comment: "or you can either use a specific target duration (below)"
choice: "match_to_this_target" , 1
option: "longest duration in the batch"
option: "specific duration (in s) below"
real: "optional_target_duration", 0.74
clicked = endPause: "quit - abandon the procedure", "Continue", 2
if clicked == 1
exit
endif
endif
endif
endproc
procedure check_longest_duration_compatibility
# If the longest sound is longer than fixed target duration,
# then you must extend the fixed target duration
# This is only needed when you're padding silence;
# if you're shrinking & stretching, then this doesn't matter
if left$(method$,3) == "pad"
if match_to_this_target$ == "specific duration (in s) below"
# make a version rounded to the nearest 3 digits, rounded up
max_duration_rounded = ceiling(max_duration*1000)/1000
while optional_target_duration < max_duration_rounded
beginPause: "Must update target duration"
comment: "your target duration is shorter than the longest sound in your batch"
comment: "you must either use this duration (below)"
comment: "or a longer duration"
real: "target_duration", max_duration_rounded
clicked = endPause: "quit - abandon the procedure", "Continue", 2
if clicked == 1
exit
endif
# update the in-use variable (optional_target_duration)
# with the user's new input
optional_target_duration = target_duration
endwhile
endif
endif
endproc