使用Praat脚本,如何通过静音拆分wav文件,然后将语音剪辑连接成更小的wav文件?

时间:2016-02-24 17:29:12

标签: audio concatenation wav praat

我正在制作Praat脚本:

1-打开一个wav文件

2-根据沉默拆分wav文件

3-根据持续时间连接间隔,以便新的wav段文件每次<= 15秒

4-将新的wav段写入单独的wav文件

我在使这个脚本工作方面取得了一些进展,但我遇到了两个主要问题:

1-在我连接片段后创建第一个15秒剪辑我的输出停止,因此我在输出中缺少部分wav文件

2-剪辑以相反的顺序连接

到目前为止,这是我的脚本。请帮忙!我是Praat脚本的新手,我完全难过了..

Read from file... Desktop/englishTest.wav
name$ = selected$("Sound", 1)
outputDir$ = "Desktop/praat_output/"
To TextGrid (silences)... 100 0 -25 0.3 0.1 silent sounding
plus Sound 'name$'
Extract intervals where... 1 no "is equal to" sounding

n = numberOfSelected("Sound")

for i to n
    soundObject'i'=selected("Sound", i)
endfor

topSound = soundObject1
select topSound
durTop = Get total duration

i = 2
for i to n
    select soundObject'i'
    dur = Get total duration
    if durTop + dur <= 15
        select topSound
        plus soundObject'i'
        topSound = Concatenate
        select topSound
        durTop = Get total duration
    else
        select topSound
        Save as WAV file... 'outputDir$''name$'_'i'.wav
        topSound = soundObject'i'
        durTop = dur
    endif
endfor

1 个答案:

答案 0 :(得分:1)

让我们一点一点地浏览你的脚本:

i = 2
for i to n

这里第一行没有效果,因为for循环默认将其控制变量初始化为1。你应该写for i from 2 to n

select topSound
plus soundObject'i'
topSound = Concatenate

这就是为什么你的声音以错误的顺序连接。在Praat中,Concatenate按照它们出现在对象列表中的顺序加入声音。不幸的是,没有简单的方法来在Object列表中移动对象。但您可以通过复制对象来解决此问题,因为新创建的对象始终显示在列表的底部。

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

这可以通过例如对文件名中的数字进行零填充来进一步改进,但这超出了范围。 :)

更新: Here's一种改进的可能性,算法略有不同,并将更长的块拆分成不超过指定最大值的碎片。