views:

1385

answers:

4

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?

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.

+1  A: 

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

David Zaslavsky
+1  A: 

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?

basszero
I need to slice up a long wav into pieces that correspond to a series of in and out points. It has to be accurate so that it can be precisely matched to video at any frame rate. Most libraries are only accurate to 1/10th of a second, meanwhile even our lowest framerates start at 12 fps. I would much rather have 44,000th of a second accuracy.
Soviut
+3  A: 

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 (http://www.python.org/doc/2.5.2/ref/shifting.html) 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.

JaCraig
+1  A: 

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.

ΤΖΩΤΖΙΟΥ
ISTM that RTFMing is a long lost art.
ΤΖΩΤΖΙΟΥ
Wow, not sure how I missed that, but I guess it just got buried under all the audio library wrappers out there. Hopefully this is cross platform and accurate enough, but it looks like it is.
Soviut