Skip to content

Allocate output buffer for typical use, not worst case#49

Merged
kahrendt merged 2 commits into
mainfrom
opus-minimum-output-buffer
May 11, 2026
Merged

Allocate output buffer for typical use, not worst case#49
kahrendt merged 2 commits into
mainfrom
opus-minimum-output-buffer

Conversation

@kahrendt
Copy link
Copy Markdown
Contributor

Don't allocate the worst case scenario size for Opus packets. Only increases the buffer size to the max if actually necessary. aiosendspin seems to only send Opus packets that are 20 ms, so that default should cover the normal use case.

This is beneficial for conserving internal memory, especially on ESP32 where it is very beneficial to put the output buffer in internal memory.

…creases the buffer size to the max if actually necessary.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces baseline decoded-audio buffer allocations by sizing the Opus decode buffer for the typical 20ms frame rather than the 120ms worst-case, and only growing the buffer when a larger packet is actually encountered. This helps conserve memory (notably internal RAM on ESP32) while still handling uncommon larger Opus packets.

Changes:

  • Replace worst-case decode buffer sizing with get_decode_buffer_size() (typical for Opus, fixed upper bound for FLAC/PCM).
  • Add a single retry path in SyncTask::decode_chunk() that reallocates the decode buffer if the decoder’s size estimate increases mid-stream.
  • Update decoder documentation/comments to describe the new “buffer size estimate can grow” behavior for Opus.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/sync_task.cpp Uses the decoder’s current buffer-size estimate and retries decode once after growing the buffer on demand.
src/decoder.h Renames/clarifies the decoded-output sizing API and documents Opus mid-stream growth behavior.
src/decoder.cpp Implements typical-size initial Opus estimate and raises the estimate when OPUS_BUFFER_TOO_SMALL occurs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sync_task.cpp Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@kahrendt kahrendt enabled auto-merge (squash) May 11, 2026 16:47
@kahrendt kahrendt merged commit 52d541f into main May 11, 2026
4 checks passed
@kahrendt kahrendt deleted the opus-minimum-output-buffer branch May 11, 2026 16:52
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.

2 participants