Skip to content

soundd: fix audio glitch in get_sound_data when frames < sound length#37766

Open
Lidang-Jiang wants to merge 2 commits intocommaai:masterfrom
Lidang-Jiang:fix/soundd-36285
Open

soundd: fix audio glitch in get_sound_data when frames < sound length#37766
Lidang-Jiang wants to merge 2 commits intocommaai:masterfrom
Lidang-Jiang:fix/soundd-36285

Conversation

@Lidang-Jiang
Copy link
Copy Markdown

Summary

Fix boundary condition bug in Soundd.get_sound_data() where current_sound_frame and loops were not recomputed after each chunk write inside the while loop. When the audio callback requested fewer frames than the sound's total length, subsequent callbacks duplicated the tail of the sound instead of wrapping around to the beginning.

  • Add per-iteration recomputation of current_sound_frame and loops inside the while loop (2 lines)
  • Add 8 regression tests covering wrap-around, single-play stop, multi-callback continuity, and volume scaling

Fixes #36285

Before (bug reproduction)
============================================================
Test: frames=50, sound_data length=70, infinite looping
============================================================

Callback 1 (frames=50):
  current_sound_frame after: 50
  result: [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]...[45. 46. 47. 48. 49.]
  Correct (should be [0..49]): True

Callback 2 (frames=50):
  current_sound_frame after: 100
  result: [50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.
 68. 69. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65.
 66. 67. 68. 69. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59.]
  Expected: [50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.
 68. 69.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.
 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.]
  Correct (should be [50..69, 0..29]): False

  >>> BUG CONFIRMED: second callback does NOT wrap correctly!
  >>> Got duplicated tail data instead of wrapping to start

============================================================
Test: frames=50, sound_data length=70, single play (num_loops=1)
============================================================

Callback 2 (frames=50):
  current_sound_frame after: 100
  result: [50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.
 68. 69. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65.
 66. 67. 68. 69. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59.]
  Expected: [50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.
 68. 69.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
  Correct (should be [50..69, zeros]): False

  >>> BUG CONFIRMED: single-play boundary also broken!

============================================================
SOME TESTS FAILED - BUG CONFIRMED
============================================================
After (bug fixed)
============================================================
Test: frames=50, sound_data length=70, infinite looping
============================================================

Callback 2 (frames=50):
  current_sound_frame after: 100
  result: [50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.
 68. 69.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.
 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.]
  Correct (should be [50..69, 0..29]): True

============================================================
Test: frames=50, sound_data length=70, single play (num_loops=1)
============================================================

Callback 2 (frames=50):
  current_sound_frame after: 70
  result: [50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.
 68. 69.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
  Correct (should be [50..69, zeros]): True

============================================================
ALL TESTS PASSED
============================================================

test_soundd_standalone.py 8 passed in 0.21s

Test plan

  • Standalone bug reproduction script (before/after comparison)
  • 8 unit tests in test_soundd_standalone.py: boundary wrap, single-play stop, multi-callback stream, silence, volume scaling
  • Tests added to selfdrive/ui/tests/test_soundd.py for CI integration

When the audio callback requests fewer frames than the sound's total
length, subsequent callbacks did not recompute the local
current_sound_frame and loops variables after each chunk write. This
caused the tail of the sound to be duplicated instead of wrapping
around to the beginning.

Add per-iteration recomputation of current_sound_frame and loops
inside the while loop, plus regression tests covering boundary
conditions (wrap-around, single-play stop, multi-callback continuity).

Fixes commaai#36285

Signed-off-by: Lidang-Jiang <[email protected]>
@github-actions github-actions bot added the ui label Apr 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

Process replay diff report

Replays driving segments through this PR and compares the behavior to master.
Please review any changes carefully to ensure they are expected.

✅ 0 changed, 66 passed, 0 errors

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

UI Preview

mici: ⚠️ Videos differ! View Diff Report
big: ⚠️ Videos differ! View Diff Report

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_sound_data bug in soundd.py

1 participant