Which syntax for parameters in define

Hi there,

Questions about parameters, question in code.

# so let met introduce you raymond my new synth
# first parameter : the instrument

instruments = ['piano', 'tri', 'pluck']

define :raymond do | numInstrument=0, a=0, d=0.5, s=0.5, r=0.5, amp=1 |
  use_synth instruments[numInstrument]
  use_synth_defaults attack: a, decay: d, sustain: s, release: r, amp: amp
end

raymond(0) # instrument the first one
play :c4
sleep 1

raymond() # instrument the first one cause numInstrument is set to 0 in define
play :c4
sleep 1

raymond(1) # instrument the second one
raymond 1 # we can write as this too

raymond 0 , '' , '',  '', 0.125, 2
play :c4
sleep 1

raymond(0,0.125) # instrument 0, attack: 0.125, the others parameters to the default values
play :c4
sleep 1

raymond 0, 0.125, 0.25, 0.125, 1, 2 # instrument 0, attack: 0.125, decay: 0.25, sustain: 0.125, release: 1, amp:2
play :c4

# ok now let say i want to set only the amp parameter and for the other values the default values
# what's the good syntax if it's possible ?
raymond(amp=4) # nope :-(
play :c4

# what about setting the two first arguments, not the third, and then the latest ?

raymond 0, , , 0.125, 0.5, 1 # instrument 0, attack: 0.125, decay: 0.25, sustain: 0.125, release: 1, amp:2
play :c4

Thanks for your help.

You can use named parameters like this:

define :test do |av: 1, bv: 2,cv: 3,dv: 4| #4 named paramters. Note the position of the :
  puts av,bv,cv,dv #print out the parameter values received
end
test                                                   #=> 1,2,3,4
test av: 2                                          #=> 2,2,3,4
test cv: 44                                        #=> 1,2,44,4
test dv: 33,av: 12                             #=> 12,2,3,33
test dv: 4, cv: 3, bv: 2, av: 1            #=> 1,2,3,4

when calling test you can specify any of the paramters in any order as shown in the examples.
Missing parameters will have the default value specified.

Here is a musical example:

synthlist=[:tri,:tb303,:saw]

define :test do|synth: 0, note: 72, sustain: 0, release: 1, sleep: 1, amp: 1,duration: 1|
  use_synth synthlist[synth]
  play note,sustain: sustain, release: release,amp: amp
  sleep duration
end

use_random_seed 31012019
test note: rrand_i(72,84),synth: rrand_i(0,2)
test sustain: 2,release: 2,duration: 0.4
test note: rrand_i(48,96),sustain: 2,release: 2
test synth: 2

This plays:

{run: 67, time: 0.0}
 └─ synth :saw, {note: 81.0, sustain: 0.0, release: 1.0, amp: 1}
 
{run: 67, time: 1.0}
 └─ synth :tri, {note: 72.0, sustain: 2.0, release: 2.0, amp: 1}
 
{run: 67, time: 1.4}
 └─ synth :tri, {note: 77.0, sustain: 2.0, release: 2.0, amp: 1}
 
{run: 67, time: 2.4}
 └─ synth :saw, {note: 72.0, sustain: 0.0, release: 1.0, amp: 1}
 
{run: 67, time: 3.4}
 └
```─ Stopped internal thread

Thanks @robin.newman for the good syntax :wink:

# so let met introduce you raymond my new synth
# first parameter : the instrument

instruments = ['piano', 'tri', 'pluck']

define :raymond do | num: 1, a: 0, d: 0.5, s: 0.5, r: 0.5, amp: 1, c:80 |
  use_synth instruments[num-1]
  use_synth_defaults attack: a, decay: d, sustain: s, release: r, amp: amp, cutoff: c
end

raymond
play :c4
sleep 1

raymond num:1 # instrument the first of the list of instruments same result
play :c4
sleep 1

raymond  num:3, s:2
play :c4
sleep 1