0

When I try reading each frame of a wav file, after a few frames it gives me this error at line 5: UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 8-9: unexpected end of data

My code:

import wave
obj = wave.open('sound.wav','rb')
frames = obj.getnframes()
for i in range(1, frames):
    test = ord(obj.readframes(i).decode('utf-16')[-1])
    input(test)
input()

The wav file here is just static that I generated to use for this test. I couldn't find any solutions on stack overflow or on other forums, and I did try using utf-8 and utf-32 and they both didn't work.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • In your own words, what do you think `.decode` does, and why do you expect it to be of any use for a .wav file? – Karl Knechtel Dec 06 '20 at 04:08
  • I fixed the formatting of your code. Please review the [formatting help](https://stackoverflow.com/help/formatting) to understand how to do it properly. – Karl Knechtel Dec 06 '20 at 04:09
  • When I read the wav file, it gave me this: b'\x99\xb0', and correct me if I'm wrong, but I thought that could be decoded with `.decode`. – Samuel Joseph Dec 06 '20 at 04:13
  • 1
    I hope the linked duplicate answers your question. The short version is that there is not any encoding you can *meaningfully* specify for `.decode` here, because the bytes in question *do not represent text*. – Karl Knechtel Dec 06 '20 at 04:13

0 Answers0