让我们一点一点地浏览你的脚本:
i = 2
for i to n
此处第一行将不起作用,因为默认情况下for
循环将其控制变量初始化为1
。你应该改写for i from 2 to n
。
select topSound
plus soundObject'i'
topSound = Concatenate
这就是为什么您的声音以错误的顺序连接的原因。在 Praat 中,按照它们在对象列表中出现的顺序Concatenate
加入声音。不幸的是,没有简单的方法可以在对象列表中移动对象。但是你可以通过复制对象来解决这个问题,因为新创建的对象总是出现在列表的底部。
selectObject: soundObject[i] ; Select the sound
tmp = Copy: selected$("Sound") ; Copy (= move to bottom)
removeObject: soundObject[i] ; Remove the original
soundObject[i] = tmp ; Update the object ID
selectObject: topSound, soundObject[i] ; Select the new objects
topSound = Concatenate ; Concatenate in the right order
有了这两个更改,您的脚本就差不多完成了。剩下的问题是,因为您在文件超过最大持续时间时保存文件,所以最后一部分(其余部分可能会更短)永远不会被保存。您需要记住在循环结束后单独保存该部分。
我做了一些其他的小改动,比如添加一个表单、将变量更改为更合适的数组以及更新一般的语法(selectObject
而不是select
),但我试图在不清楚时对其进行注释。把所有这些放在一起,你会得到这样的东西
form Reticulate splines...
sentence Sound_path Desktop/englishTest.wav
sentence Output_path Desktop/praat_output/
endform
sound = Read from file: sound_path$
sound$ = selected$("Sound")
silences = To TextGrid (silences):
... 100, 0, -25, 0.3, 0.1, "silent", "sounding"
selectObject: sound, silences
Extract intervals where:
... 1, "no", "is equal to", "sounding"
n = numberOfSelected("Sound")
for i to n
soundObject[i] = selected("Sound", i)
endfor
topSound = soundObject[1]
selectObject: topSound
durTop = Get total duration
# new is a counter for the new objects we'll be making
new = 0
# Start for loop from second position
for i from 2 to n
selectObject: soundObject[i]
dur = Get total duration
if durTop + dur <= 15
# Rearrange objects in object list
tmp = soundObject[i]
selectObject: soundObject[i]
soundObject[i] = Copy: selected$("Sound")
removeObject: tmp
previous = topSound
selectObject: topSound, soundObject[i]
topSound = Concatenate
durTop = Get total duration
# Remember to remove unwanted objects!
removeObject: previous, soundObject[i]
else
# Save an array of new indices
new += 1
final[new] = topSound
topSound = soundObject[i]
durTop = dur
endif
endfor
# Remember to add the last sound
new += 1
final[new] = topSound
# Clean up unwanted objects
removeObject: silences
# Loop through the array to rename them
nocheck selectObject: undefined
for i to new
selectObject: final[i]
Rename: sound$ + "_" + string$(i)
## You can save the objects automatically here
## but this is not the best design in my opinion
# Save as WAV file: output_path$ + selected$("Sound")
endfor
# Select the newly extracted parts
nocheck selectObject: undefined
for i to new
plusObject: final[i]
endfor
这可以通过例如零填充文件名中的数字来进一步改进,但这超出了范围。:)
更新: 这是一种改进的可能性,算法略有不同,将较长的块分解成不大于指定最大值的片段。