Optimize the additive DAC code, fixing performance-related hangs #21662
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.
Description
In current code, the interrupt that fires when the DAC needs more samples could take so long to process that another interrupt is already waiting, as does another once that one is done, and so on. As a result, processing DAC interrupts may be the only thing the CPU can do. This prevents any code from working which would be able to request a note to be stopped:
Here are the changes in decreasing order of importance:
fmodf
out for awhile
loop that puts the offsets back within the chosen waveform, which is expected to run only once in most cases (saves hundreds of instructions per active tone per sample);dac_value_generate
(saves 3 instructions per active tone per sample).Even if the CPU is not hanging due to being busy all the time on a user's board, these optimizations still lower CPU utilization, so the input latency goes down, and/or the user can increase the maximum number of concurrent notes or the sample rate.
fmodf
being gone also saves 416 bytes of firmware size on STM32F303. So this is still a win.However, more stress testing is needed to make sure there aren't more causes for audio-related hangs.
Types of Changes
Issues Fixed or Closed by This PR
Checklist
I have also tested that the changes improve things.