Skip to content

Conversation

@ruskaruma
Copy link
Contributor

Summary

In #2010 we noticed realtime PCM16 audio was being reported in microseconds. The root cause was our fallback branch in calculate_audio_length_ms, which divided by 24 * 2 and then by 1000, effectively treating every byte as a millisecond. I normalized the format first, split g711 from PCM handling, and introduced explicit constants for the sample rate and sample width so PCM16 now returns the true millisecond duration. This also covers empty buffers and any future lowercase/uppercase format variants.

To keep coverage honest, I updated the realtime playback tracker tests to assert against the corrected math using pytest.approx, and refreshed the manual tracker test to reflect the new millisecond totals.

Test plan

  • uv run pytest tests/realtime/test_playback_tracker_manual_unit.py tests/realtime/test_playback_tracker.py tests/realtime/test_openai_realtime.py
  • uv run pytest

Fixes #2010.

@ruskaruma
Copy link
Contributor Author

Greetings @seratch, I’ve opened this PR referencing issue #2010. Please review the patch and let me know if it correctly addresses the problem or if any adjustments are needed.

@seratch seratch changed the title Fix realtime PCM duration calculation (#2010) fix: #2010 Improve realtime PCM duration calculation Nov 10, 2025
@ruskaruma ruskaruma force-pushed the main branch 2 times, most recently from f5bfae1 to 198921a Compare November 10, 2025 19:12
@ruskaruma ruskaruma changed the title fix: #2010 Improve realtime PCM duration calculation fix: #2010 Improve PCM duration calculation and handle VAD truncation (#2009) Nov 10, 2025
@ruskaruma
Copy link
Contributor Author

Greetings, @seratch .
I’ve made these changes and fixed the issues:

Updated calculate_audio_length_ms to return correct PCM16 durations (fixes #2010)

Made the VAD speech-start event emit conversation.item.truncate so the server properly records interruptions (fixes #2009)

Here’s the test plan:

uv run pytest tests/realtime/test_playback_tracker_manual_unit.py tests/realtime/test_playback_tracker.py tests/realtime/test_openai_realtime.py

uv run pytest

Let me know if you’d like me to tweak or add anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typo in realtime code in calculate_audio_length_ms function

2 participants