Python | pydub:如何从np.array而不是wav文件加载wav示例到pydub?

时间:2021-04-18 19:40:25

How would I load an audio np.array file into PyDub library? Currently, I use AudioSegment.from_wav(file_path), but it is not convenient, if I already have the wav file loaded as a numpy array:

如何将音频np.array文件加载到PyDub库中?目前,我使用AudioSegment.from_wav(file_path),但是如果我已经将wav文件作为numpy数组加载,那就不方便了:

sample_rate, wav_sample = scipy.io.wavfile.read(file_path)

UPDATE: my wav files are all 16 bit, single channel.

更新:我的wav文件都是16位,单通道。

1 个解决方案

#1


2  

Ok, take this answer with a grain of salt as I don't know pydub enough to see if it's working properly, but you should be able to do it from the class initializer providing all the parameters it needs:

好的,我不知道pydub是否能正常工作,但你应该能够从类初始值设定器中提供所需的所有参数:

sample_rate, wav_sample = scipy.io.wavfile.read(file_path) 
segment = AudioSegment(data=wav_sample.tobytes(),
                       sample_width=2,
                       frame_rate=sample_rate, channels=1)

It seems to work as it should, assuming a 16bit single channel sample.

假设一个16位单通道样本,它似乎应该工作。

Different sample width should be easy to infer from the array size (something like wav_sample.nbytes() / len(wav_sample) should do).

应该很容易从数组大小推断出不同的样本宽度(类似于wav_sample.nbytes()/ len(wav_sample)应该这样做)。

Please do some test yourself and let us know!

请自己做一些测试并告诉我们!

EDIT: multiple channels is a little trickier, pydub as far as I can tell wants interwoven channels, while scipy returns them as multiple columns. But it should be easy enough with numpy to reshape the data in the format pydub wants, something like the following (not tested)?

编辑:多个频道有点棘手,据我所知pydub想要交织的频道,而scipy将它们作为多列返回。但它应该很容易用numpy以pydub想要的格式重塑数据,如下所示(未测试)?

np.vstack((wav_sample[:,0],wav_sample[:,1])).reshape((-1,), order='F') 

#1


2  

Ok, take this answer with a grain of salt as I don't know pydub enough to see if it's working properly, but you should be able to do it from the class initializer providing all the parameters it needs:

好的,我不知道pydub是否能正常工作,但你应该能够从类初始值设定器中提供所需的所有参数:

sample_rate, wav_sample = scipy.io.wavfile.read(file_path) 
segment = AudioSegment(data=wav_sample.tobytes(),
                       sample_width=2,
                       frame_rate=sample_rate, channels=1)

It seems to work as it should, assuming a 16bit single channel sample.

假设一个16位单通道样本,它似乎应该工作。

Different sample width should be easy to infer from the array size (something like wav_sample.nbytes() / len(wav_sample) should do).

应该很容易从数组大小推断出不同的样本宽度(类似于wav_sample.nbytes()/ len(wav_sample)应该这样做)。

Please do some test yourself and let us know!

请自己做一些测试并告诉我们!

EDIT: multiple channels is a little trickier, pydub as far as I can tell wants interwoven channels, while scipy returns them as multiple columns. But it should be easy enough with numpy to reshape the data in the format pydub wants, something like the following (not tested)?

编辑:多个频道有点棘手,据我所知pydub想要交织的频道,而scipy将它们作为多列返回。但它应该很容易用numpy以pydub想要的格式重塑数据,如下所示(未测试)?

np.vstack((wav_sample[:,0],wav_sample[:,1])).reshape((-1,), order='F')