fix(voice): keep emitted PCM16 audio arrays writable - #4733
Closed
subhashpolisetti wants to merge 1 commit into
Closed
fix(voice): keep emitted PCM16 audio arrays writable#4733subhashpolisetti wants to merge 1 commit into
subhashpolisetti wants to merge 1 commit into
Conversation
Member
|
Thanks for the proposal. I don't think the SDK should make every emitted PCM16 buffer writable by copying every chunk. transform_data already supports the use case by returning a transformed array, and a consumer that specifically needs in-place mutation can copy that array explicitly. The patch would add an allocation on the default hot path for every user without an existing writeability contract, so I am closing this. |
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
StreamedAudioResult._transform_audio_bufferbuilds every emitted chunk withnp.frombuffer, which aliases the immutablebytesit was handed, and returns that view unchanged on theint16branch:The
float32branch computes a new array and is writable.TTSModelSettings.dtypedefaults tonp.int16, so the read-only array is what callers get unless they opt out.Anything that edits the array in place then fails. A
transform_datacallback applying gain withdata *= 2raisesValueError: output array is read-only, and so does a consumer writing intoVoiceStreamEventAudio.data. The identical callback succeeds whendtypeisnp.float32, so whether in-place work is allowed depends on a setting that says nothing about mutability, and neither the field docs nor the voice docs mention the difference.Copying on the
int16branch is the narrowest fix.b"".join(...)currently returns the single buffered chunk unchanged, so today that branch allocates nothing; the copy adds one allocation there and leavesfloat32untouched, sinceastypealready allocates. Joining into abytearrayto get a writable view was rejected because it forces an allocation on both dtypes and makesfloat32strictly more expensive for no benefit. No public signature changes.Test plan
tests/voice/test_pipeline.py::test_voicepipeline_transform_data_can_edit_audio_in_place, parametrized over both supported dtypes. It drives the publicVoicePipelineand compares the emitted samples against a baseline run of the same pipeline without the transform, so the expected values come from an independent run rather than from the implementation's own arithmetic. It asserts the baseline is non-empty first, so the comparison cannot pass vacuously.Verified it fails without the source change by restoring
src/agents/voice/result.pytomainand rerunning: theint16case fails withValueError: output array is read-onlyand thefloat32case passes. That split is the inconsistency the change removes..agents/skills/code-change-verification/scripts/run.shpasses end to end: format, lint, typecheck and the full suite.uv run mypy --platform win32 srcis clean, anduv run pytest tests/voice -qis 210 passed.Issue number
None. Found while auditing the PCM framing path in the voice pipeline.
Checks
.agents/skills/code-change-verification/scripts/run.sh/reviewbefore submitting this PR