Faster way to convert from 24 bit wav pcm format to float?

Posted by LMO on Stack Overflow See other posts from Stack Overflow or by LMO
Published on 2012-03-19T23:25:38Z Indexed on 2012/03/19 23:30 UTC
Read the original article Hit count: 258

Filed under:
|

I need to read data in from a wav file in 24 bit pcm format, and convert to float. I'm using Python 2.7.2.

The wave package reads the data in as a string, so what I've tried is:

# read in entire wav file
wdata = f.readframes(nFrames) 

# unpack into signed integers and convert to float      
data = array.array('f')
for i in range(0,nFrames*3,3):
    data.append(float(struct.unpack('<i', '\x00'+ wdata[i:i+3])[0]))

# normalize sample values
data = data / 0x800000

This is quite a bit faster than my earlier approaches, but still quite slow. Can anyone suggest a more efficient method?

© Stack Overflow or respective owner

Related posts about python

Related posts about pcm