praat-合并多个wav文件

时间:2018-06-22 23:13:13

标签: concatenation wav praat

我有136个以数字命名的wav文件(1.wav至136.wav),以及一个包含2秒钟静默的wav文件。我想将它们合并如下,生成其他几个文件:

1 + silence + 2 + silence (copy) + 3 
1 + silence + 2 + silence (copy) + 4
2 + silence + 1 + silence (copy) + 3 
2 + silence + 1 + silence (copy) + 4

5 + silence + 6 + silence + (copy) + 7
5 + silence + 6 + silence + (copy) + 8
6 + silence + 5 + silence + (copy) + 7
6 + silence + 5 + silence + (copy) + 8

我有下面的脚本,该脚本将文件合并如下:

1 + silence + 2 + silence + (copy) + 1 (copy)
1 + silence + 2 + silence + (copy) + 2 (copy)
2 + silence + 1 + silence + (copy) + 1 (copy)
2 + silence + 1 + silence + (copy) + 1 (copy)


3 + silence + 4 + silence + (copy) + 2 (copy)
3 + silence + 4 + silence + (copy) + 3 (copy)
4 + silence + 3 + silence + (copy) + 3 (copy)
4 + silence + 3 + silence + (copy) + 4 (copy)



str = Create Strings as file list... soundlist 'directory$'/*.wav
num_file = Get number of strings

#writeInfoLine: num_file

#Copy... silence2
for i to num_file
    selectObject: str
    fileName$ = Get string: i
    #writeInfoLine: fileName$
    n_fn$ = fileName$
    num_c = number(n_fn$ - ".wav")
    n_sound_file$ = string$(num_c+1) + ".wav"


    if num_c mod 2 == 1
        cp_f1$ = fileName$ - ".wav" + "cp"
        cp_f2$ = n_sound_file$ - ".wav" + "cp"
        cp_sil1$ = "sil1"
        f_file$ = "Sound " + fileName$ - ".wav"
        s_file$ = "Sound " + n_sound_file$ - ".wav"
        f_file2$ = "Sound " + cp_f1$
        s_file2$ = "Sound " + cp_f2$
        sil_file$ = "Sound " + "silence"
        sil_file2$ = "Sound " + "sil1"
        writeInfoLine: cp_f1$
        new_f1$ = "Sound " + f_file$ + "_sil_" + s_file$ + "_sil_" + f_file$+".wav"
        new_f2$ = "Sound " + f_file$ + "_sil_" + s_file$ + "_sil_" + s_file$ + ".wav"
        new_f3$ = "Sound " + s_file$ + "_sil_" + f_file$ + "_sil_" + f_file$ + ".wav"
        new_f4$ = "Sound " + s_file$ + "_sil_" + f_file$ + "_sil_" + s_file$ + ".wav"

#########
        Read from file: directory$+ "/" + fileName$
        Read from file: directory$+ "/" + "silence.wav" 
        Read from file: directory$+ "/" + n_sound_file$
        selectObject: sil_file$
        Copy... sil1
        selectObject: f_file$
        Copy... 'cp_f1$'

        selectObject: f_file$
        plusObject: sil_file$
        plusObject: s_file$
        plusObject: sil_file2$
        plusObject: f_file2$

        Concatenate
        Save as WAV file... 'directory$'/'new_f1$'
        removeObject: f_file$, sil_file$,sil_file2$,f_file2$
#########

        Read from file: directory$+ "/" + fileName$
        Read from file: directory$+ "/" + "silence.wav" 
        Read from file: directory$+ "/" + n_sound_file$
        selectObject: sil_file$
        Copy... sil1
        selectObject: s_file$
        Copy... 'cp_f2$'




        selectObject: f_file$
        plusObject: sil_file$
        plusObject: s_file$
        plusObject: sil_file2$
        plusObject: s_file2$

        Concatenate
        Save as WAV file... 'directory$'/'new_f2$'
        removeObject: f_file$, sil_file$,sil_file2$,s_file2$

#####
        Read from file: directory$+ "/" + n_sound_file$
        Read from file: directory$+ "/" + "silence.wav" 
        Read from file: directory$+ "/" + fileName$
        selectObject: sil_file$
        Copy... sil1
        selectObject: s_file$
        Copy... 'cp_f2$'




        selectObject: s_file$
        plusObject: sil_file$
        plusObject: f_file$
        plusObject: sil_file2$
        plusObject: s_file2$

        Concatenate
        Save as WAV file... 'directory$'/'new_f3$'
        removeObject: s_file$, sil_file$,sil_file2$,s_file2$
####

        Read from file: directory$+ "/" + n_sound_file$
        Read from file: directory$+ "/" + "silence.wav" 
        Read from file: directory$+ "/" + fileName$
        selectObject: sil_file$
        Copy... sil1
        selectObject: f_file$
        Copy... 'cp_f1$'




        selectObject: s_file$
        plusObject: sil_file$
        plusObject: f_file$
        plusObject: sil_file2$
        plusObject: f_file2$

        Concatenate
        Save as WAV file... 'directory$'/'new_f4$'





    endif   

endfor

select all
    Remove

到目前为止,我使用的脚本将文件分为名为奇数和偶数的文件。就我当前的目的而言,该算法将更加复杂。如果能得到帮助,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

[免责声明:我是提到的Parselmouth库的作者]

我不是Praat脚本编写方面的专家,但是如果您愿意使用Python(以及Parselmouth library从Python访问Praat功能),那么以下Python代码似乎对我有用:

import glob
import parselmouth

directory = "/the/directory/of/your/choice"
output_directory = "/the/directory/of/your/choice/output"

all_files = glob.glob(directory + "/*.wav")
n_files = len(all_files) - 1 # Number of files minus 'silence.wav'

def concatenate_and_save(first, second, third):
    silence = parselmouth.Sound(directory + "/silence.wav")
    sound1 = parselmouth.Sound(directory + "/" + str(first) + ".wav")
    sound2 = parselmouth.Sound(directory + "/" + str(second) + ".wav")
    sound3 = parselmouth.Sound(directory + "/" + str(third) + ".wav")
    concatenated = parselmouth.Sound.concatenate([sound1, silence, sound2, silence, sound3])
    concatenated.save(output_directory + "/" + str(first) + "_sil_" + str(second) + "_sil_" + str(third) + ".wav", "WAV")

for i in range(1, n_files+1, 4):
    concatenate_and_save(i, i+1, i+2)
    concatenate_and_save(i, i+1, i+3)
    concatenate_and_save(i+1, i, i+2)
    concatenate_and_save(i+1, i, i+3)

很抱歉,如果您需要将代码作为Praat脚本使用,但我确实希望可以复制该算法的思想(即,以4为步长遍历文件数,然后使用正确的组合在Praat脚本中ii+1i+2i+3,并且也许使用一个函数来不重复自己?