How to edit raw PCM audio data without an audio library?

三世轮回 提交于 2019-11-28 23:55:36
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:

  • audioop: manipulate raw audio data
  • wave: read and write WAV files

Perhaps I come from a parallel universe and Guido's time machine is actually a space-time machine :)

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.

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/

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.

All of that said, I'd still lean towards David's approach.

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

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.

Just curious, what do you intend on doing with the raw sample data?

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)

import numpy
data = numpy.memmap("test.pcm", dtype='h', mode='r')
print "VALUES:",data

Check out the original author's site for more details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!