Added safety iteration bound to boost fold() - #30
Conversation
The fold() function in boost.h relies on an unbounded for (;;) loop. While it mathematically converges by halving the overshoot, floating-point precision limits can theoretically cause the convergence to stall just outside the threshold. If this happens, it hangs the audio core indefinitely. Added a hard limit of 20 iterations to guarantee bounded execution time for realtime safety. If it hits the limit, it returns the closest approximated value.
There was a problem hiding this comment.
Maybe check if the change is really small instead of abruptly stopping, using something like this:
#include <math.h>
float direct_wavefold(float in, float level) {
if (level <= 0.0f) return 0.0f;
float period = 4.0f * level;
float x = (in / period) + 0.25f;
float f = x - floorf(x);
// Generate triangle between -level and +level
float folded = 2.0f * level * fabsf(2.0f * f - 1.0f) - level;
return folded;
}|
@Pacsfury Good catch, but I just ran the math on this and I don't think they actually output the same thing! The original code has that The analytical version is a pure periodic triangle wave. It doesn't dampen on extreme clipping, so it would probably sound quite a bit harsher than the original algorithm. I'm happy to swap it to your analytical version if we want to change the character of the distortion to a pure triangle wave! But if we want to keep the original damped sound, we probably have to stick with the iterative loop (with my safety bound so it doesn't hang the core). Let me know what you think is best. |
|
Yes, you are right. I just coded that fast to put an example, probably wrong, way to fix the main issue in your code: some precition may be lost. Thanks Ps: sorry for the short texts |
|
No worries at all, I completely understand what you were going for! Thanks for reviewing the PR, let me know if there's anything else you need me to tweak before merging. |
|
Not at all, if you make a change for precition as I asked, being a coauthor of the commit would apreciated, but if not, everything ic correct! Thanks |
Pacsfury
left a comment
There was a problem hiding this comment.
Its ok, only that some precition may be lost
|
Thanks! I think I'll stick with the original iterative loop to preserve that damped-fold sound character. Since you mentioned everything is correct with this approach, could you switch your review status from "Request Changes" to "Approve" so we can get it merged? Thanks again for the review! |
|
Didnt i click approve? Ig i misclicked, doing now |
|
@torvalds This fixes a potential infinite loop stall in |
0d04135 to
52eac9e
Compare
The fold() function in boost.h relies on an unbounded for (;;) loop. While it mathematically converges by halving the overshoot, floating-point precision limits can theoretically cause the convergence to stall just outside the threshold. If this happens, it hangs the audio core indefinitely.
Added a hard limit of 20 iterations to guarantee bounded execution time for realtime safety. If it hits the limit, it returns the closest approximated value.