Skip to main content
 首页 » 编程设计

python之从计算机读取多个 .wav 文件并将它们合并到 numpy 数组

2024年12月31日11mayingbao

我需要将多个 wav 文件读取到单独的 numpy 数组中,然后将这些 numpy 数组合并为一个并将其保存为 wav 文件。

我该怎么做?

请您参考如下方法:

解决方法:

from scipy.io.wavfile import read, write 
import numpy as np 
 
fs, x = read('test1.wav') 
f2, y = read('test2.wav') 
 
#z = x + y                    # this is to "mix" the 2 sounds, probably not what you want 
z = np.concatenate((x, y))    # this will add the sounds one after another 
 
write('out.wav', fs, z) 

当执行 x + y 时,如果 2 个数组的长度不同,您需要对最短的数组进行零填充,以便它们在求和之前最终具有相同的长度。