-
Couldn't load subscription status.
- Fork 2.8k
fix: handle odd-length audio chunks in voice streaming (fixes #1824) #1928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,7 +88,16 @@ async def _add_error(self, error: Exception): | |||||||||||||||||||||||||||
| def _transform_audio_buffer( | ||||||||||||||||||||||||||||
| self, buffer: list[bytes], output_dtype: npt.DTypeLike | ||||||||||||||||||||||||||||
| ) -> npt.NDArray[np.int16 | np.float32]: | ||||||||||||||||||||||||||||
| np_array = np.frombuffer(b"".join(buffer), dtype=np.int16) | ||||||||||||||||||||||||||||
| # Combine all chunks | ||||||||||||||||||||||||||||
| combined_buffer = b"".join(buffer) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Pad with a zero byte if the buffer length is odd | ||||||||||||||||||||||||||||
| # This is needed because np.frombuffer with dtype=np.int16 requires | ||||||||||||||||||||||||||||
| # the buffer size to be a multiple of 2 bytes | ||||||||||||||||||||||||||||
| if len(combined_buffer) % 2 != 0: | ||||||||||||||||||||||||||||
| combined_buffer += b"\x00" | ||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Padding an odd-length audio buffer with Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+91
to
+99
|
||||||||||||||||||||||||||||
| # Combine all chunks | |
| combined_buffer = b"".join(buffer) | |
| # Pad with a zero byte if the buffer length is odd | |
| # This is needed because np.frombuffer with dtype=np.int16 requires | |
| # the buffer size to be a multiple of 2 bytes | |
| if len(combined_buffer) % 2 != 0: | |
| combined_buffer += b"\x00" | |
| # Combine all chunks, padding with a zero byte if the total length is odd | |
| total_len = sum(len(c) for c in buffer) | |
| if total_len % 2 != 0: | |
| buffer = [*buffer, b"\x00"] | |
| combined_buffer = b"".join(buffer) |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comparing output_dtype directly to np.int16 may fail for equivalent values like 'int16' or np.dtype('int16'). Normalize the dtype for robust comparison:
if np.dtype(output_dtype) == np.dtype(np.int16):
return np_array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Padding the trailing half-sample with 0 introduces synthesized audio data and can cause subtle artifacts in streaming scenarios. A more accurate approach is to carry over the trailing single byte to the next call (e.g., keep self._pending_byte, prepend it on the next invocation, and if the combined length is odd, stash the last byte instead of padding), preserving sample integrity without injecting data.