Skip to content

gh-117716: Fix wave RIFF padding for data chunks#145237

Merged
encukou merged 1 commit intopython:mainfrom
mbeijen:gh-117716-riff-padding-wave-lib
Apr 15, 2026
Merged

gh-117716: Fix wave RIFF padding for data chunks#145237
encukou merged 1 commit intopython:mainfrom
mbeijen:gh-117716-riff-padding-wave-lib

Conversation

@mbeijen
Copy link
Copy Markdown
Contributor

@mbeijen mbeijen commented Feb 25, 2026

wave.Wave_write now writes the required RIFF pad byte when the data chunk size is odd.

Update RIFF chunk size calculations in both header writing and header patching so they include the alignment pad byte when present.

Add a regression test in test_wave.py that verifies odd-sized writes are padded, RIFF size is correct, and roundtrip reads preserve frame data.

@encukou
Copy link
Copy Markdown
Member

encukou commented Mar 17, 2026

Looking at the document referenced in the issue (emphasis mine):

Binary data of fixed or variable size. The start of ckData is word-aligned with respect to the start of the RIFF file. If the chunk size is an odd number of bytes, a pad byte with value zero is written after ckData. Word aligning improves access speed (for chunks resident in memory) and maintains compatibility with EA IFF. The ckSize value does not include the pad byte.

In this PR, the size includes the padding byte. Which is right?

@encukou
Copy link
Copy Markdown
Member

encukou commented Apr 14, 2026

@mbeijen, do you have an answer?

wave.Wave_write now writes the required RIFF pad byte when the data chunk
size is odd.

Update RIFF chunk size calculations in both header writing and header
patching so they include the alignment pad byte when present.

Add a regression test in test_wave.py that verifies
odd-sized writes are padded, RIFF size is correct, and roundtrip reads
preserve frame data.
@mbeijen mbeijen force-pushed the gh-117716-riff-padding-wave-lib branch from f8ff230 to ff3f783 Compare April 15, 2026 06:10
@mbeijen
Copy link
Copy Markdown
Contributor Author

mbeijen commented Apr 15, 2026

@mbeijen, do you have an answer?

Sure, and thanks for following up!! Plus, I've rebased the commit so it now applies cleanly after my other PRs affecting wave.py plus the tests.

The spec quote "ckSize does not include the pad byte" applies to each sub-chunk's own ckSize. The RIFF form's ckSize represents total file content minus the 8-byte RIFF header, which physically includes the pad byte. I double-checked this with libsndfile (what audacity uses) and it does exactly the same thing as this fix.

I verified this against libsndfile 1.2.2: for 1 byte of audio it writes data ckSize = 1 and RIFF ckSize = 38 (= 36 + 1 + 1), matching this PR exactly.

@encukou
Copy link
Copy Markdown
Member

encukou commented Apr 15, 2026

I see, that makes sense. Thanks for double-checking!

@encukou encukou merged commit ca064d9 into python:main Apr 15, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants