0

我有 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

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

4

1 回答 1

0

[免责声明:我是提到的 Parselmouth 图书馆的作者]

我不是 Praat 脚本专家,但如果您愿意使用 Python(以及Parselmouth 库从 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 步进行,然后使用正确的组合i, i+1, i+2,和i+3, 并且可能使用一个函数来不重复自己)在 Praat 脚本中?

于 2018-08-07T18:05:16.093 回答