-1

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Why do you think the first result is correct? A WAVE file starts out with a known header.. The first few bytes are the characters "RIFF". I don't see that. The `wave` module skips over the header and returns the data, and that data looks far more like 16-bit PCM data. In the real world, you'll never get a long sequence of 0s. There's always background noise. – Tim Roberts Sep 22 '21 at 21:07
  • I converted bytes into numpy array and then visualised it. The first tool give real sound signal http://timg.in/BFlNn – dkagramanyan Sep 22 '21 at 21:11
  • You're aware that the samples are 16-bit values, not bytes? – Tim Roberts Sep 22 '21 at 21:35
  • Really, there's not much we can do without seeing the code nor access to the file. The `wave` module works very well; I've used it many times. – Tim Roberts Sep 22 '21 at 21:37
  • Loaded .wav and bytes data to the [drive](https://drive.google.com/drive/folders/1YwHXeU4LSv0sn-dia5dKXbiRq83tZR6O?usp=sharing). Hope it will help solve the issue. Original .wav soundtrack is russian speech. All code I have loaded above – dkagramanyan Sep 22 '21 at 21:50
  • Yes, original data is 16-bit values. In my pipeline i need to use bytes – dkagramanyan Sep 22 '21 at 21:52

1 Answers1

0

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '21 at 12:18