Global variable alternative

#1

Hi
I am trying to send a filename to SP using OSC and then using the file in the program. The idea is something like the draft below. This does not work. Also any suggestions on how this can be done better alternatively without using global variable? Thanks

$filePath=""

live_loop :a do
   use_real_time
  oscValues = sync "/osc/trigger/setname"
  samp = oscValues.join(";")
  $filePath = "C:/AUDIO/" + samp
end

live_loop :c do
  sample $filePath
  sleep 1
end
#2

Hi @keys,

perhaps you might be interested in reading about Sonic Pi’s global time state and the functions get and set: http://sonic-pi.net/tutorial.html#section-10

live_loop :a do
  use_real_time
  osc_values = sync "/osc/trigger/setname"
  samp = osc_values.join(";")
  set :file_path, "C:/AUDIO/" + samp
end

live_loop :c do
  sample get(:file_path)
  sleep 1
end

In fact, incoming OSC messages are inserted directly into the Time State system, so there’s no need for an explicit set:

live_loop :c do
  sample "C:/AUDIO/" + get("/osc/trigger/setname").join(";")
  sleep 1
end

However, I’m unsure what you’re doing with the values of that OSC message - specifically why your’e joining them with ";". Could you explain what you intend this code to do?

2 Likes
#3

Thanks for the advise. I will read up on get/set :slight_smile:

Ok what I am trying to do is simply to send a filename to Sonic Pi using OSC and then playback the file. The join(";") part was something I found on the Internet when trying to convert the array in the osc message to a string. But I think I need to read up on join as well :wink:

#4

I have still not made it working. I also tried to use the osc directly but I get an error message on the join function

live_loop :d do
  sample "C:/AUDIO/" + get("/osc/trigger/setname").join
  sleep 1
end

Error message
Runtime Error: [buffer 0, line 54] - NoMethodError
Thread death ±-> :live_loop_d
undefined method join' for nil:NilClass Did you mean? JSON workspace_zero:54:inblock (3 levels) in __spider_eval’

#5

Ok I did some more testing and i got it working sending the message into Sonic Pi from another client. Hence the issue seems to be external to Sonic Pi. I will investigate this further.

Thanks for the advise regarding get/set and for the heads up concerning join :slight_smile:

2 Likes
#6

With respect to joining, if your OSC data is coming in as a list of separate characters, then you can join without specifying an argument:

a = ['h', 'e', 'l', 'l', 'o']
puts a.join #=> "hello"

If you specify an argument, then it will place that argument between each individual letter:

a = ['h', 'e', 'l', 'l', 'o']
puts a.join(";") #=> "h;e;l;l;o"

If you’re controlling the code that’s creating and sending the original OSC message, it might make more sense to send the argument as a string of multiple characters which would mean you wouldn’t need to join anything at all.

It’s also worth noting that .join is a Ruby feature and is currently unsupported in any way (as are most Ruby features). This doesn’t mean you shouldn’t use them, but it does mean that they may stop working without notice in a future release. Only the functionality presented in the documentation is officially supported :slight_smile:

1 Like
#7

Thanks for explaining the .join feature to me :slight_smile:

Also thanks for warning me about the fact that this is a Ruby feature. At the same time I hope these features will not dissappear, because I think it is one of Sonic Pi’s strength that one can find general Ruby code on the Internet and implement it in the Sonic Pi code.