python 如何在没有音频库的情况下编辑原始 PCM 音频数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/841049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to edit raw PCM audio data without an audio library?
提问by Soviut
I'm interested in precisely extracting portions of a PCM WAV file, down to the sample level. Most audio modules seem to rely on platform-specific audio libraries. I want to make this cross platform and speed is not an issue, are there any native python audio modules that can do this?
我对精确提取 PCM WAV 文件的部分感兴趣,直到样本级别。大多数音频模块似乎依赖于特定于平台的音频库。我想让这个跨平台并且速度不是问题,有没有可以做到这一点的原生 python 音频模块?
If not, I'll have to interpret the PCM binary. While I'm sure I can dig up the PCM specs fairly easily, and raw formats are easy enough to walk, I've never actually dealt with binary data in Python before. Are there any good resources that explain how to do this? Specifically relating to audio would just be icing.
如果没有,我将不得不解释 PCM 二进制文件。虽然我确信我可以很容易地挖掘 PCM 规范,并且原始格式很容易走,但我以前从未真正在 Python 中处理过二进制数据。有没有很好的资源来解释如何做到这一点?具体与音频相关的将是锦上添花。
采纳答案by tzot
I read the question and the answers and I feel that I must be missing something completely obvious, because nobody mentioned the following two modules:
我阅读了问题和答案,我觉得我一定遗漏了一些非常明显的东西,因为没有人提到以下两个模块:
Perhaps I come from a parallel universe and Guido's time machine is actually a space-time machine :)
或许我来自平行宇宙,Guido 的时间机器实际上是一个时空机器 :)
Should you need example code, feel free to ask.
如果您需要示例代码,请随时询问。
PS Assuming 48kHz sampling rate, a video frame at 24/1.001==23.976023976… fps is 2002 audio samples long, and at 25fps it's 1920 audio samples long.
PS 假设 48kHz 采样率,视频帧在 24/1.001==23.976023976…fps 是 2002 个音频样本长,而在 25fps 是 1920 个音频样本长。
回答by JaCraig
I've only written a PCM reader in C++ and Java, but the format itself is fairly simple. A decent description can be found here: http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
我只用 C++ 和 Java 编写了一个 PCM 阅读器,但格式本身相当简单。一个体面的描述可以在这里找到:http: //ccrma.stanford.edu/courses/422/projects/WaveFormat/
Past that you should be able to just read it in (binary file reading, http://www.johnny-lin.com/cdat_tips/tips_fileio/bin_array.html) and just deal with the resulting array. You may need to use some bit shifting to get the alignments correct (https://docs.python.org/reference/expressions.html#shifting-operations) but depending on how you read it in, you might not need to.
过去,您应该能够读取它(二进制文件读取,http://www.johnny-lin.com/cdat_tips/tips_fileio/bin_array.html)并处理结果数组。您可能需要使用一些位移来使对齐正确(https://docs.python.org/reference/expressions.html#shifting-operations),但根据您读取它的方式,您可能不需要。
All of that said, I'd still lean towards David's approach.
尽管如此,我仍然倾向于大卫的方法。
回答by David Z
Is it really important that your solution be pure Python, or would you accept something that can work with native audio libraries on various platforms (so it's effectively cross-platform)? There are several examples of the latter at http://wiki.python.org/moin/PythonInMusic
你的解决方案是纯 Python 真的很重要,还是你会接受可以在各种平台上使用本机音频库的东西(所以它实际上是跨平台的)?在http://wiki.python.org/moin/PythonInMusic 中有几个后者的例子
回答by basszero
Seems like a combination of open(..., "rb"), struct module, and some details about the wav/riff file format(probably better reference out there) will do the job.
似乎 open(..., "rb"), struct module和有关wav/riff 文件格式的一些细节(可能更好的参考)的组合可以完成这项工作。
Just curious, what do you intend on doing with the raw sample data?
只是好奇,你打算用原始样本数据做什么?
回答by Ian Conway
I was looking this up and I found this: http://www.swharden.com/blog/2009-06-19-reading-pcm-audio-with-python/It requires Numpy (and matplotlib if you want to graph it)
我正在查找这个,我发现了这个:http: //www.swharden.com/blog/2009-06-19-reading-pcm-audio-with-python/ 它需要 Numpy(如果你想绘制它,还有 matplotlib )
import numpy
data = numpy.memmap("test.pcm", dtype='h', mode='r')
print "VALUES:",data
Check out the original author's site for more details.
查看原作者的网站以获取更多详细信息。