How to access data from onset function (using lambda)?

After responding to this post, I started tinkering around more with onset and became interested in accessing certain information about specific onsets, for example , the finish: time.

When playing a samples using onset, this info appears in the console:
49

How can I access the number 0.1209 to store in a variable, use for a sleep value etc?

Looking at the documentation, I came across and example which used a lambda (which I know nothing about) to print out some information. I did a little research and was able to print out the finish number for each onset in the index, but I don’t know how to access those numbers to use them. Here is the code I used

8.times do |x|
  l = lambda {|c| puts c[x][:finish] ; c[x]}
  sample :loop_amen, onset: l
  sleep 0.25 
end

Any help would be appreciated.

How about using the time state to store and retrieve that value? (set() and get()):

8.times do |x|
  l = lambda {|c| set(:finish, c[x][:finish]); c[x]}
  sample :loop_amen, onset: l
  puts get(:finish)
  sleep 0.25
end

That seems to do the trick! Thanks!

1 Like

All this suggests to me that there should be a way nicer approach to getting this information.

How about something like this:

sample_onsets :loop_amen #=> returns a ring of start/finish pairs
sample_onsets :loop_amen, 3 #=> returns the 4th onset start/finish pair
3 Likes

This is a function I have used in programs to do this:

define :listOnsets do |s|
  set :onsetsMaps,[] #initialise a blank list
  l = lambda {|c| set :onsetsMaps,c.to_a; c[0]} #set the info obtained in :listonsets
  sample s, onset: l,amp: 0,finish: 0 #trigger the lambda function played sample at 0 volume and finish=0
  return get(:onsetsMaps)
end

m = listOnsets(:loop_amen_full)
m.length.times do |i|
  puts "onset ",i,"start",m[i][:start],"finish",m[i][:finish]  
end

I used it for example in this piece Onset Percussion

1 Like

Something like that would be great! Ideally, it would be good to be able to get the index length, the start and finish separately as well as the length of each onset in relation to the whole sample (just in relation to the 0 to 1 length, not the actual duration, although that might be interesting too)

sample_onsets_index :loop_amen #=> returns index length
sample_onsets_start :loop_amen, 3 #=> returns 4th onset start time
sample_onsets_finish :loop_amen, 3 #=> returns 4th onset finish time
sample_onsets_length :loop_amen, 3 #=> returns 4th onset length

Not sure if there would be a less verbose way to write that, but that is the idea.

This all came about because I was trying to make a visual example of how onset works. Here’s what I came up with:

I wish I knew more about Ruby to do stuff like this!