Python Read WAV Data Format, PCM or ALAW – Python Tutorial

By | August 30, 2021

As to a wav audio file, it can be encoded by pcm, alaw or other encoding fomort. How to get this encoding format? In this tutorial, we will use python to get it.

In python, we can use scipy.io.wavfile.read() to read a wav file. There is a part of wav file format list.

wav file format list

How to know WAV Data Format?

We also can use scipy.io.wavfile.read() to get this encoding format. Here is an example:

from scipy.io import wavfile
import numpy as np

sample_rate, sig = wavfile.read("0011586.wav")
print("sample rate: %d" % sample_rate)
print(sig)

if sig.dtype == np.int16:
    print("PCM16 integer")
if sig.dtype == np.float32:
    print("PCM32 float")

Run this code, you may get this result:

sample rate: 8000
[  8   8   8 ... -40 -56 -56]
PCM16 integer

However, scipy only support PCM and IEEE_FLOAT, if wav file is encoded by other format, you will get an error.

For example:

sample_rate, sig = wavfile.read("0011586.V3")
print("sample rate: %d" % sample_rate)
print(sig)

if sig.dtype == np.int16:
    print("PCM16 integer")
if sig.dtype == np.float32:
    print("PCM32 float")

Run this code, you will get this error:

ValueError: Unknown wave file format: ALAW. Supported formats: PCM, IEEE_FLOAT

From this error, we can find 0011586.V3 is encoded by ALAW format. This format is not supported by scipy.

Leave a Reply