Andrew Schwartz
Andrew Schwartz

Reputation: 4657

Determine maximum allowed sample value in wave file in Python

scipy.io.wavfile.read returns the data as integers. To do audio processing, I'd like to convert these to floats. I want to allow any arbitrary bit depth in the input files, so I need to know the correct number by which to normalize the integer data. For example, for 16-bit wav files, I'd divide the integer array by ( 2**15 - 1 ). But for 8-bit audio, I'd have to divide by ( 2**7 - 1 )

Is there an easy way to get this bit-depth so I can convert all of these files into comparable float data? I feel like this is pretty basic, yet I can't seem to find it.

For a few reasons, I am limited, at least at the moment, to not installing more featureful packages such as scikits.

Upvotes: 0

Views: 1362

Answers (2)

Matthias
Matthias

Reputation: 4914

I've written a tutorial on this and a helper function called pcm2float() that does what you want.

Given a signal in a NumPy array sig and a target data type dtype, it simply does this:

sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)

Upvotes: 0

Warren Weckesser
Warren Weckesser

Reputation: 114921

You can use numpy.iinfo to get information about an integer data type. For example, here's a 16 bit signed integer array:

In [338]: data = np.array([10, 20, -30], dtype=np.int16)

In [339]: ii = np.iinfo(data.dtype)

In [340]: ii.max
Out[340]: 32767

In [341]: ii.min
Out[341]: -32768

In [342]: ii.bits
Out[342]: 16

And here's an 8 bit unsigned integer array:

In [350]: data = np.array([10, 20, 200, 5], dtype=np.uint8)

In [351]: ii = np.iinfo(data.dtype)

In [352]: ii.max
Out[352]: 255

In [353]: ii.min
Out[353]: 0

In [354]: ii.bits
Out[354]: 8

Upvotes: 1

Related Questions