Find Index of next smaller Item in a buffer

Hey there,

I have some time information (onsets of events) stored in a buffer, for example:

a = (5.0!12).rand.sort.trunc(0.001) // quantization here should be arbitrary
b = Buffer.loadCollection(s, a)

Using a Phasor/Sweep as a timing reference in a SynthDef, I would like to find the index of the “current” timestamp, or said more generally: I would like to find the index of next smaller item in a buffer. As far as I know, DetectIndex only finds items that are exactly equal.
Does anybody know if this is possible?

Help would be greatly appreciated! :>

All best,
moritz

Could you sort the buffer? Or use a new buffer with the indices in what ever order is needed?

So you’d have…

snd_file = {i -> sample}
onsets = {event_count -> i}
sorted_onsets = {order -> event_count}
1 Like

Yes, this would definitely be possible, I could also just use a Buffer that has the indexes as values at Sample Rate. I was just wondering if there might be a more efficient way, e.g. using something like DetectIndex.
Thank you for your response!

I don’t think you can get more efficient in terms of big O. This would be a single lookup, so constant. While a search might be quicker for small buffers, once you get over circa 100 (hand wavy guesstimate) it will become much slower. I mention this just because I’m working on a project that does something similar with over 106,000 sound grains and using buffers as lookups works well - baring the fact you reach the floating point limit…

I think detectindex is the only ugen to search though a buffer. Flucoma does a lot of this type of operation, so there might be something there that works for you? Kdtree maybe?

2 Likes

Thank you for the insights! :slight_smile: