Hi Glauco
There are quite a few problems with your code.
First I think you may be confusing :slice and :onset. I suspect that you are trying to use the former in this code, as you use num_slices: as one of your parameters. num_slices: divides up your sample into a number of segments (by default 16 of them) all of equal length. You can then use the slice: option to select the segment you wish to hear. To do this the parameter must be an integer in the range 0 to num_slices - 1. onset: on the other hand divides up your sample into a number of segments each triggered by an onset algorithm which detects sharp changes in amplitude as for example given by a cymbal clash or drum beat. Onsets may well be of differing lengths and duration, and the number of detected onsets will vary with the sample concerned.
Another problem you have is dividing os by 16. Since os is an integer, you will probably always get 0 for this, or 1 if s is in the range 16-31, so the input will not have much effect. You require an integer for either slice: or onset: to work.
Another problems is that you set sustain: to 0. This means that you only get the decay section once your sample has finished playing. Since many percussive samples die to more or less zero before the sample duration finishes, this is unlikely to give you much sound.
beat_stretch: rate: and pitch: will all interact with each other with difficult to predict results because in essence they all affect the speed at which the sample is played and hence its pitch and duration.
I would suggest starting by manually playing your loop sample with different parameters, and to experiment to see how a particular parameter alters what you hear. One you get the feel for that it will be easier to put them in a function.
A good starting point would be based on one of the examples for sample
Here is a function I played with this afternoon which allows control of slice, number of slices, rate and density. It is a good idea initially to have use`-debug set to true so that you can see the actual opts used for each sound you hear.
use_debug true
use_sample_bpm :loop_garzul #bpm set so 1 beat = sample duration
define :beat_slicer do |s=1,n=16,r=1,d=1| #s s-slice,n=num_slices,r=rate,d = density
s = s%n #makes sure s is in range 0 to n-1 (remainder when s divided by n)
density d do
sample :loop_garzul,slice: s, num_slices: n,rate: r
sleep 1.0/(n*r.abs) #r.abs allows negative r to work
end
end
live_loop :x do
n=16 #set number of slices
#select one or both examples below
#this example uses each slice in turn using tick and a rate of -1 which plays backwards, density 1,2 or 3
beat_slicer tick,n/4,-1,[1,2,3].choose
#this example chooses the slice at random, rate 1.75,density 1 or 2.
beat_slicer rand_i(n/2),n/2,1.75,[1,2].choose
end
Vary the parameters to get a feel for how you can change things.