Skip to content

fix voice packet timing drift causing periodic audio hiccups#457

Open
coderobe wants to merge 1 commit into
shardlab:mainfrom
coderobe:patch-voice-packet-jitter-1
Open

fix voice packet timing drift causing periodic audio hiccups#457
coderobe wants to merge 1 commit into
shardlab:mainfrom
coderobe:patch-voice-packet-jitter-1

Conversation

@coderobe

Copy link
Copy Markdown

Summary

the send loop was rebasing its timer on the actual clock time after send_audio completed, so encryption and UDP latency accumulated into the inter-packet interval. RTP timestamps still incremented by exactly 20ms per packet, meaning Discord's jitter buffer would gradually drain and produce a periodic audio glitch when playing longer audio files

this switches to a fixed schedule (next_packet_at += IDEAL_LENGTH) so any per-packet send latency is absorbed by the next frame's sleep rather than compounding. Also rebase the schedule on resume after a pause to avoid bursting buffered frames.

While here: fixed options: null (NameError) -> options: '' in ffmpeg_command, removed a dead clock_gettime call, and marked the now-noop adjust_interval/adjust_offset/adjust_average/length_override attrs as deprecated


Changed

Discordrb::Voice voice_bot.rb, encoder.rb

Deprecated

Discordrb::Voice#adjust_interval
Discordrb::Voice#adjust_offset
Discordrb::Voice#adjust_average
Discordrb::Voice#length_override

Fixed

Discordrb::Voice: voice_bot audio playback stutter

The send loop was rebasing its timer on the actual clock time after
send_audio completed, so encryption and UDP latency accumulated into
the inter-packet interval. RTP timestamps still incremented by exactly
20ms per packet, meaning Discord's jitter buffer would gradually drain
and produce a periodic glitch.

Switch to a fixed schedule (next_packet_at += IDEAL_LENGTH) so any
per-packet send latency is absorbed by the next frame's sleep rather
than compounding. Also rebase the schedule on resume after a pause to
avoid bursting buffered frames.

While here: fix `options: null` (NameError) → `options: ''` in
ffmpeg_command, remove a dead clock_gettime call, and mark the now-noop
adjust_interval/adjust_offset/adjust_average/length_override attrs as
deprecated
@coderobe

coderobe commented May 18, 2026

Copy link
Copy Markdown
Author

without this change, any audio longer than a brief sound effect seems to repeatedly hiccup in semi-regular intervals. tested cherry-picked on top of #453

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant