BytesIO unexpected behavior: adds bytes at the beginning of .wav file

67 Views Asked by At

Here is the scenario:

  • I read a .wav file using the Python wave package
  • I write the data in a file-like BytesIO object using this same wave package
  • I check if the original data is the same as the data from this file-like object

Here is the code:

filepath = "my_file_path.wav"

with wave.open(filepath) as f:
    params = f.getparams()
    data = f.readframes(f.getnframes())
    
bytes_io = io.BytesIO()
wf =  wave.open(bytes_io, "wb")
wf.setnchannels(params.nchannels)
wf.setsampwidth(params.sampwidth)
wf.setframerate(params.framerate)
wf.writeframes(data)

bytes_io.seek(0)
new_data = bytes_io.read()

print(len(new_data))
print(len(data))
print(new_data[:-len(data)])
assert new_data[-len(data):] == data

This prints:

700740
700696
b'RIFF<\xb1\n\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80>\x00\x00\x00}\x00\x00\x02\x00\x10\x00data\x18\xb1\n\x00'

Why is there this difference in terms of bytes?

0

There are 0 best solutions below