Skip to content

python: Zero-pad short float PCM input in Encoder.encode() - #92

Open
0z5a wants to merge 1 commit into
google:mainfrom
0z5a:fix/python-encode-float-padding
Open

0z5a wants to merge 1 commit into
google:mainfrom
0z5a:fix/python-encode-float-padding

Conversation

@0z5a

@0z5a 0z5a commented Nov 14, 2026

Copy link
Copy Markdown

Encoder.encode() documents that PCM input shorter than a frame is padded with zeros. The 16/24-bit path does this. The float path computes the padding as pcm_len - frame_samples instead of from the input length, so:

  • mono float input shorter than a frame raises ValueError: Buffer size too small ... from ctypes;
  • multi-channel float input is padded by a fixed (num_channels - 1) * frame_samples, so input shorter than one channel's frame fails the same way.

This change pads up to pcm_len based on the actual buffer length, the same way the integer path does. Full-length input is unchanged. Multi-channel full-length input no longer gets an unused zero tail appended.

Test: test_encode_pads_short_input in python/tests/basic_test.py checks that encoding a short input gives the same bytes as encoding the same input explicitly zero-padded on a fresh encoder, for 1 and 2 channels and float/16/24-bit input. Before this change the float cases fail with ValueError; the 16/24-bit cases pass both before and after.

pytest python/tests

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

The float path (bit_depth=None) computed the padding from the frame
size instead of the input length, so a mono input shorter than a frame
was never padded and encode() failed with "Buffer size too small" from
ctypes. The docstring says input shorter than a frame is zero padded,
as the 16/24-bit paths already do.

This branch has not been deployed

No deployments
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.

1 participant