Bug report
Bug description:
import io import wave test_bytes = b"RIFF&\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80\xbb\x00\x00\x80\xbb\x00\x00\x01\x00\x08\x00data\x01\x00\x00\x00\x80\x00" r = io.BytesIO(test_bytes) with wave.open(r, mode="rb") as rw: nframes = rw.getnframes() sampwidth = rw.getsampwidth() nchannels = rw.getnchannels() framerate = rw.getframerate() frames = rw.readframes(-1) assert nframes == 1 assert sampwidth == 1 assert nchannels == 1 assert frames == b"\x80" w = io.BytesIO() with wave.open(w, mode="wb") as ww: ww.setsampwidth(sampwidth) ww.setnchannels(nchannels) ww.setframerate(framerate) ww.writeframes(frames) value = w.getvalue() # b'RIFF%\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80\xbb\x00\x00\x80\xbb\x00\x00\x01\x00\x08\x00data\x01\x00\x00\x00\x80' print(value) # AssertionError assert test_bytes == value assert test_bytes[-1] == value[-1] == 0 assert ( int.from_bytes(test_bytes[4:8], byteorder="little") == int.from_bytes(value[4:8], byteorder="little") == 38 )
The data written is missing the last pad byte\x00.
Along with this, the size of the RIFF chunk is also incorrect.
ref: https://www.tactilemedia.com/info/MCI_Control_Info.html
If the chunk size is an odd number of bytes, a pad byte with value zero is written after ckData.
CPython versions tested on:
3.12
Operating systems tested on:
Windows