usb_audio: allocate the mic scratch buffer from the port heap - #4
Merged
relic-se merged 1 commit intoAug 13, 2026
Merged
Conversation
Scott suggested port_malloc() for this in review. It replaces the static int16_t[USB_AUDIO_MAX_SAMPLE_RATE / 1000 * USB_AUDIO_N_CHANNELS] with an allocation sized by enable() for 1 ms at the negotiated rate, so a board that never calls enable() pays nothing for it. On Metro RP2040 .bss drops 188 bytes, the 192 freed less 4 for the pointer, against 32 bytes of added text. enable() can be called more than once, so it releases any chunk sized for a previous rate before taking a new one, and disable() frees as well. Tested 4 s at 48 kHz stereo, 1 kHz left and 3 kHz right, on Metro RP2040, Metro RP2350 and Feather nRF52840. All three write 768044 bytes with full channel separation, matching the static buffer exactly. The nRF52840 keeps both directions on endpoint 8. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Author
|
That de-allocation bug is the case this covers. |
Owner
Thanks for validating this as well. Niche use-case, but I could see it happening. |
Owner
|
Looking great. Thank you! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scott asked for
port_malloc()on the mic scratch buffer in review. I had allthree platforms on the farm today, so I incorporated the suggestion and tested it
rather than leave it hanging.
Same numbers as the static buffer, and the nRF52840 keeps both directions on
endpoint 8. On RP2040
.bssdrops 188 bytes, the 192 freed less 4 for thepointer, against 32 bytes of added text.
Two details beyond the straight swap.
enable()can be called more than once, soit frees any chunk sized for a previous rate first, and
disable()frees too. Itested a boot.py calling
enable(16000)thenenable(48000)and the second onewins.
Yours to take or leave.
🤖 Generated with Claude Code