Ok, so the Max users will have seen, in the example folder, a document called nb_of_slices. It was a sort of attempt to specify the number of slices I want, and get Max to iteratively guestimate the threshold needed to make it work.
I was dumb, as I was inversing the error, and adding a bit of noise to avoid oscillation in my feedback. Cybernetic 101 (actually, probably 001). it kinda works, but just kinda. It made my teacher @weefuzzy smile.
For my piece for Wet Ink, I needed it to work all the time. So I decided to do something a bit more intelligent. It is still probably at the bottom of the list of what one can do, but it does a sort of linearization of the problem by taking the last 2 thresholds and how many slices they yield, and interpolate linearly to a guestimated value. And does that over until it reaches the right amount. And it works.
I’ve commented the code, so it should be self explanatory, but please feel free to ask questions and/or suggest improvements.