如何在python3中将24位wav文件转换为16位或32位文件 [英] How to convert a 24-bit wav file to 16 or 32 bit files in python3
问题描述
我正在尝试制作一堆 .wav 文件的频谱图,以便我可以进一步分析它们(在 python 3.6 中),但是,我不断收到这个令人讨厌的错误
ValueError: Unsupported bit depth: the wav file has 24-bit data.
我查看了其他堆栈溢出帖子,例如如何编写 24 位 WAV 文件在 Python 中?但论文并没有解决问题!
我找到了一个名为 Pysoundfile 的音频库
http://pysoundfile.readthedocs.io/en/0.9.0/
我用
安装了它pip3 安装 pysoundfile
我查看了文档,但我仍然不清楚如何将 24 位 .wav 文件转换为 32 位 wav 文件或 16 位 wav 文件,以便我可以从中创建频谱图.
任何帮助将不胜感激!
我建议使用 SoX 用于此任务.更改位深度非常简单:
sox old.wav -b 16 new.wav
如果您必须使用 Python,那么您可以使用您发现的 PySoundFile.这是一个小代码片段:
导入声音文件数据,采样率 = soundfile.read('old.wav')soundfile.write('new.wav', 数据, 采样率, subtype='PCM_16')
您还应该使用 soundfile.available_subtypes
以查看您可以将文件转换为哪些子类型.以下是其使用示例,取自他们的文档:
I am trying to make spectrogram's of a bunch of .wav files so I can further analyze them(in python 3.6), however, I keep getting this nasty error
ValueError: Unsupported bit depth: the wav file has 24-bit data.
I have looked into other stack overflow posts such as How do I write a 24-bit WAV file in Python? but theses didn't solve the issue!
I found a audio library called Pysoundfile
http://pysoundfile.readthedocs.io/en/0.9.0/
I installed it with
pip3 install pysoundfile
I have looked over the documentation and it is still not clear to me how to convert a 24-bit .wav file to a 32-bit wav file or a 16-bit wav file so that I can create a spectrogram from it.
Any help would be appreciated!
I would suggest using SoX for this task. Changing the bit depth is very simple:
sox old.wav -b 16 new.wav
If you must use Python, then you could use PySoundFile as you found. Here's a little code snippet:
import soundfile
data, samplerate = soundfile.read('old.wav')
soundfile.write('new.wav', data, samplerate, subtype='PCM_16')
You should also use soundfile.available_subtypes
to see which subtypes you can convert a file to. Here's its sample usage, taken from their documentation:
>>> import soundfile as sf
>>> sf.available_subtypes('FLAC')
{'PCM_24': 'Signed 24 bit PCM',
'PCM_16': 'Signed 16 bit PCM',
'PCM_S8': 'Signed 8 bit PCM'}
这篇关于如何在python3中将24位wav文件转换为16位或32位文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!