Add a pcm_to_wav helper and use it across core - #5326
Merged
Merged
Conversation
Every example that persists or uploads audio from AudioBufferProcessor hand-rolls the same wave.open block, including the pipecat init template. Give it a home next to the other PCM helpers.
Buffers reach the helper as bytearray (segmented STT, AudioBufferProcessor turn audio), and a caller that truncates PCM can leave a trailing partial frame, which wave would write into the data chunk without counting in the header.
The websocket transports, segmented STT, audio context messages, and the eval recorder each hand-rolled the same wave boilerplate.
markbackman
force-pushed
the
mb/pcm-to-wav
branch
from
August 14, 2026 13:23
1a89a9a to
d579bff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pcm_to_wav()topipecat.audio.utils, which wraps raw 16-bit PCM in a WAV container and returns the file as bytes.bytes,bytearray, ormemoryview, because audio reaches it as abytearrayin practice —SegmentedSTTServiceaccumulates one, andAudioBufferProcessorhands one to its turn-audio events.wavewrites those into the data chunk without counting them in the header, which leaves readers disagreeing about the length and swaps channels on a stereo stream truncated mid-frame.waveboilerplate in the three websocket transports,SegmentedSTTService,LLMContext.create_audio_message(), and the two eval helpers. That removes ~55 lines and drops theio/waveimports from five modules.Two WAV-writing spots stay as they are:
local_smart_turn_v3.py(debug-only path, writes from a numpy array) andaudio/dtmf/utils.py(a WAV read, not a write).The conversions have no user-visible effect — the helper emits the same bytes as the code it replaces. The helper commit is cherry-picked from #5285.
Testing
uv run pytest tests/test_audio_utils.pycovers mono, stereo, empty,bytearrayinput, and a partial trailing frame.test_krisp_viva_*failures are order-dependent and fail at the branch point too.