Find the last onset within a sample



a question about onset: In my naive mind I thought it should be possible to find the last detectable onset of a given sample with:

sample :loop_amen, rate: -1, onset: 0

This does not seem to be true. The sample is of course played backwards but index 0 still gives the first onset of the sample played from the beginning in non-reversed manner. This is logical but not what I expected.

Is there an easy way to find the last onset?



couple of functions I defined in this post should help
IistOnsets and numOnsets


Thanks Robin, that is helpful!



As with most programming languages, Sonic Pi’s arrays (which are really just Ruby’s arrays) are 0 indexed. Therefore asking for something at index 0 is asking for the first item. To ask for the last item, you should ask for item -1 (this is thanks to the fact that Sonic Pi’s onset opt acts like a ring, so going back one index (from 0 to -1) is the same as jumping to the last item.

Hope that this helps!

sample :loop_amen, rate: -1, onset: -1


Cool That’s much easier!


Without wanting to pretend to be as smart as this solution I somehow suspected that there must be an easy way like this :wink: Thanks!

Just to give some background: At Saturday I had the oportunity to present Sonic Pi at an IT fair for kids. I did mess around with Sonic Pi almost the whole day (which was fun and a very good exercise); during one of these sessions I recorded the ‘chord inversion’ examples, because one question was about recording your Sonic Pi tune. Then I loaded this sample to further mess around with it.

The next day I was thinking about an easy way to trim the sample for further manipulation in a quick way and without any external application; this brought me to onset