Extensive OSC tutorial

Here as promised is one example in which three osc commands independently control three parameters of sounding notes in a live loop. You could alternatively have one very long (even an hour) note using a fixed synth and control the parameters of that note such as pitch, volume, pan value using osc inputs. Let me know if you would like to see such an example. Wasn’t sure what you meant by continous sound.

use_osc "localhost",4560

#three osc messages. Normally sent from your hardware device
osc "/amp",0.4
osc "/cutoff",120
osc "/synth","saw" #you could use numbers and a lookup list to get the name of the synth instead

#three live loops to read the osc messages and get their data
#data is stored using set commands. The values are retireve dusing get commands in the test liveloop
live_loop :rx1 do
  use_real_time
  val = sync "/osc:127.0.0.1:4560/cutoff"
  puts val[0]
  set :lp,val[0]
end

live_loop :rx2 do
  use_real_time
  val = sync "/osc:127.0.0.1:4560/amp"
  puts val[0]
  set :vol,val[0]
end

live_loop :rx3 do
  use_real_time
  val= sync  "/osc:127.0.0.1:4560/synth"
  set :synthname,val[0]
end

#test live loop that plays a stream of ntoes with synth, volume and synth set by the osc commands
live_loop :test do
  cutoff=get(:lp)
  vol=get(:vol)
  synthname=get(:synthname)
  synth synthname,note: rrand_i(50,100),release: 0.2,cutoff: cutoff,amp: vol
  sleep 0.2
end

The osc commands could be sent from different ip addresses, in which case the rx liveloops would contain the relevant ip address as part of the sync string. They will always have to be port 4560 to work as Sonic Pi cues. You can extend the number of osc inputs and use several ip addresses together. If you dont care or need to separate which address they come from you can use sync “/osc*/cutoff” etc.

In the example above, you can change the values sent by the osc commands and re-run whenever you want. Your hardware may use rotary knobs or sliders to change values sent, in which case you may need to scale them.

2 Likes