audio - 如何将操纵的WAV文件保存在对象列表中?

标签 audio scripting praat

我有以下问题:我想对240个WAV文件进行低通过滤。该脚本仅在创建低通滤波后的声音并将其显示在对象列表(“..._ band”)中之前运行。但是,Praat不会将它们导出为WAV文件。选择输出文件夹后,我收到警告消息“命令'获取字符串数'对于当前选择不可用”。

简而言之,我的问题是如何将WAV声音及其新文件名分别保存在对象列表中?另请参见Screenshot

脚本见下文。

非常感谢您的帮助!

问候,

#determine praat version
ver1$ = left$(praatVersion$, (rindex(praatVersion$, ".")-1));
ver1 = 'ver1$'
if ver1 < 5.2
    exit Please download a more recent version of Praat
endif

if ver1 == 5.2
    ver2$ = right$(praatVersion$, length(praatVersion$) - (rindex(praatVersion$, ".")));
    ver2 = 'ver2$'
    if ver2 < 4
        exit Please download a more recent version of Praat (minor)
    endif
endif

beginPause ("Low-Pass Filter Instructions")
    comment ("1. Select a folder containing the wave files to be low-pass filtered")
    comment ("2. Wave files will be low-pass filtered (0 - 400 Hz)")
    comment ("3. Select an output folder for the low-pass filtered wave files to be saved to")
    comment ("Click 'Next' to begin")
clicked = endPause("Next", 1);

#wavefile folder path
sourceDir$ = chooseDirectory$ ("Select folder containing wave files")
if sourceDir$ == ""
    exit Script exited. You did not select a folder.
else
    sourceDir$ = sourceDir$ + "/";
endif

Create Strings as file list... list 'sourceDir$'/*.wav

numberOfFiles = Get number of strings
levels$ = ""
for ifile to numberOfFiles
    select Strings list
    currentList = selected ("Strings")
    filename$ = Get string... ifile
    Read from file... 'sourceDir$'/'filename$'
    currentSound = selected ("Sound")
    filterFreqRange = Filter (pass Hann band)... 0 400 20

    select currentSound
    Remove

endfor

select currentList
Remove

#output folder path  - where the wave files get saved
outputDir$ = chooseDirectory$ ("Select folder to save wave files")
if outputDir$ == ""
    exit Script exited. You did not select a folder.
else
    outputDir$ = outputDir$ + "/";
endif

numberOfFiles = Get number of strings
for ifile to numberOfFiles
    select Strings list
    currentList = selected ("Strings")
    filename$ = Get string... ifile
    currentSound = selected ("Sound")
    endif
    Save as WAV file... 'outputDir$'/'filename$'
    select currentSound
    Remove
endfor

#clean up
select currentList
Remove

#clear the info window
clearinfo
#print success message
printline Successfully low-pass filtered 'numberOfFiles' wave files.

最佳答案

乍一看,该命令不可用,因为当您到达脚本中的该点时,选择为空。它是空的,因为在第一个循环结束时,您选择了Strings对象,然后对其进行Remove

通常,您的脚本不能正确处理对象选择,这在Praat中是一个大问题,因为可用命令会根据 Activity 选择而变化。

因此,即使删除列表的Remove行,第二次运行Get number of strings也会遇到问题,因为到那时为止,您已经更改了选择。即使删除了该行(实际上并不需要),选择selected("Sound")对象后运行Strings仍然会遇到问题,因为届时您将没有任何选定的Sound对象(无论如何都没有它们)。

脚本的更惯用的版本也可以在单个循环中运行,在该循环中,声音被逐个读取,过滤和删除(这也提高了内存的使用效率),如下所示:

form Low-Pass Filter...
    real From_frequency_(Hz) 0
    real To_frequency_(Hz) 400
    real Smoothing_(Hz) 20
    comment Leave paths empty for GUI selectors
    sentence Source_dir 
    sentence Output_dir 
endform

if praatVersion < 5204
  exitScript: "Please download a more recent version of Praat"
endif

if source_dir$ == ""
    source_dir$ = chooseDirectory$("Select folder containing wave files")
    if source_dir$ == ""
        exit
    endif
endif

if output_dir$ == ""
    output_dir$ = chooseDirectory$("Select folder to save filtered files")
    if output_dir$ == ""
        exit
    endif
endif

list = Create Strings as file list: "list", source_dir$ + "/*.wav"
numberOfFiles = Get number of strings
levels$ = ""

for i to numberOfFiles
    selectObject: list
    filename$ = Get string: i
    sound = Read from file: source_dir$ + "/" + filename$
    filtered = Filter (pass Hann band): from_frequency, to_frequency, smoothing

    Save as WAV file: output_dir$ + "/" + selected$("Sound")
    removeObject: sound, filtered
endfor

尽管您可能想知道,但如果不需要自动读取和保存文件(即,如果可以将脚本用于列表中的对象,则可以),则可以将脚本简化为
Filter (pass Hann band): 0, 400, 20

适用于多个选定的Sound对象。

如果您绝对需要循环(即,如果您要处理非常大的文件集),那么您可能也有兴趣使用 vieweach plugin,其目的是尽量减少这种样板(完整的免责声明:我写了插入)。我也为此写了更长的blog post

关于audio - 如何将操纵的WAV文件保存在对象列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39534742/

相关文章:

Python将音频流PCM字符串高效转换为数组,重复使用

python - 捕获屏幕上所有内容的脚本

linux - 无法在 Linux 命令行上执行 Praat 命令

audio - 为什么我的音调对象的声音与PRAAT中的原始音频文件不同?

algorithm - RTP:推荐策略以实现流畅的音频流

audio - 将USB网络摄像头和USB扬声器组合到Windows中的单个虚拟音频设备中

arrays - 阵列访问的奇怪天花板

需要 Linux 脚本帮助

praat - Praat 中的音调修改

c# - FLV车身长度