pythonwavwavesoundfile

Reading .wav as bytes


I'm reading .wav file in Python with 2 tools. First - with module soundfile:

wav = sf.read(speech_file)
b = io.BytesIO(wav[0])

In result I get such bytes data. It's correct:

....\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ .....

Then I read .wav with module wave:

wf = wave.open(speech_file, "rb")
data = wf.readframes(wf.getparams().nframes)

The result bytes are totally incorrect:

....a\x00\xda\x00\xcb\x00\xba\x00\xb0\x00\xa3\x00\x8f\x00|\x00g\x00S\x00=\x00&\x00\x0b\x00\xf3\xff\xd4\xff\xb0\xff\x8d\xffe\xff\xff\x18\xff\xef\xfe\xc6\xfe\x99\xfed\xfe-\xfe\xf5\xfd\xc0\xfd\x92\xfdj....

Why did not two different tools give the same result? How to make a second byte object from the first one? In fact, I can't use wave's bytes as it is totally different from original soundtrack.


Solution

  • The built-in module wave reads .wav files in DSD format, soundtrack is represented as sequence of 0 and 1. However, soundfile reads .wav in PCM format, which is array of signal's amplitude values.