Calling a function with argument from another function

Hi Carrié
welcome to Sonic Pi.
The problem is that the play command expects a numeric note or a note symbol.
When it gets a choice of either :foo or :ocean it tries to convert it to a note symbol and fails, producing the error message that you get, as the two functions are not notes. A solution I came up with is shown below.

define :foo do
  play 30
  sleep 1
  play 55
  sleep 0.5
end

define :ocean do |num =3,amp_mul=0.125|
  num.times do
    s = synth [:bnoise, :cnoise, :gnoise].choose, amp: rrand(0.4, 1.5) * amp_mul
    control s, pan: rrand(-1,1),cutoff: rrand(60,110)
    sleep rrand(0.5,4)
  end
end


loop do
  choice=rrand_i(1,4) #get a random integer 1-4
  puts choice
  case choice # use Ruby case statement to process
  when 3 #choice is 3
    foo ##do foo
  when 4 #choice is 4
    ocean # do ocean
  else # otherwise it will be a note
    play [60,65,72][choice-1] #use choice-1 as an index into note list (index starts from 0 not 1)
  end #end the case statement
  sleep 1
end

This chooses a random number in the range 1 to 4 then uses the Ruby case statement to decide its course of action.
It deals with the two functions first. If choice is 3 it calls the function :foo
If choice if 4 it calls the function :ocean
otherwise it uses the value in choice to index which note to play from the list

Two further points. I notice you are using Sonic Pi 2.11.1 which is quite an old version. Not sure what computer you are using, but there are probably later versions available for it.

Secondly you may find it easier to copy and post your code directly into in_thread putting a line of three back ticks before it and a line of three back ticks after it as I have done in this post.

1 Like