gpt4 book ai didi

audio - 使用Praat脚本,我如何通过静默方式分割wav文件,然后将语音剪辑连接成较小的wav文件?

转载 作者:行者123 更新时间:2023-12-02 22:32:42 25 4
gpt4 key购买 nike

我正在编写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

最佳答案

让我们逐一检查脚本:

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

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

更新: Here's一种改进的可能性,其算法略有不同,并且将较长的块分成不大于指定最大值的片段。

关于audio - 使用Praat脚本,我如何通过静默方式分割wav文件,然后将语音剪辑连接成较小的wav文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com