Want to Pass Some Arguments Through a Function

Once again, i was trying to find a more refined way to do things…
There’s some parameters that i use often to “mangle” my
loops; so i tried this function :

use_debug false
use_bpm 86

live_loop :metro do
  sleep 1

define :beats do |spl,rt,os,sl|
  os = os/16
  sample spl,rate: rt,onset: os
  sleep sl

lp = :loop_garzul

use_sample_defaults beat_stretch: 4,amp: 1,attack: 0.01, sustain: 0.0, release: 0.2,
  num_slices: 16,rpitch: 0

live_loop :groove,sync: :metro do
  beats lp,1,1,0.0
  sleep 1

Once again, it didn’t work…Why ?
Thank you in advance !

P.S: Pass “DENSITY” parameter through this function
also would be nice.

1 Like

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

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

Vary the parameters to get a feel for how you can change things.


Hi Robin :

Thank you for your response;
you were very kind in clarifying all these to me.
Yes,i make several mistakes regards Sonic Pi
language itself;many items seems quite similar to me.
In this stage of learning, i copy much of my code from
somebody else, and now, think it covered most of my
issues about samples and beats. Perhaps in the future,
could be a compilation from this comunity, putting the
best codes in a book or course. I’ll experiment with your
code, and see what i get. Once again :

Thank you very much !

1 Like