Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/agents/voice/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def _transform_audio_buffer(
# np.int16 needs 2-byte alignment; pad odd-length chunks safely.
combined_buffer += b"\x00"

np_array = np.frombuffer(combined_buffer, dtype=np.int16)
# TTS providers return PCM16 bytes in little-endian order. Decode that wire format
# explicitly, then normalize the result to the native int16 dtype returned to callers.
np_array = np.frombuffer(combined_buffer, dtype="<i2").astype(np.int16, copy=False)

if output_dtype == np.int16:
return np_array
Expand Down
21 changes: 21 additions & 0 deletions tests/voice/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ def test_streamed_audio_result_odd_length_buffer_int16() -> None:
assert transformed.tolist() == [1]


def test_streamed_audio_result_decodes_pcm16_as_little_endian(monkeypatch) -> None:
result = StreamedAudioResult(
ZeroPcmTTSModel(),
TTSModelSettings(dtype=np.int16),
VoicePipelineConfig(),
)
native_frombuffer = np.frombuffer

def simulate_big_endian_host(buffer, *, dtype):
if dtype is np.int16:
dtype = np.dtype(">i2")
return native_frombuffer(buffer, dtype=dtype)

monkeypatch.setattr(np, "frombuffer", simulate_big_endian_host)

transformed = result._transform_audio_buffer([b"\x01\x02\x03\x04"], np.int16)

assert transformed.dtype == np.int16
assert transformed.tolist() == [0x0201, 0x0403]


@pytest.mark.asyncio
async def test_streamed_audio_result_raises_falsy_task_exception() -> None:
class FalsyError(RuntimeError):
Expand Down