Test OSC Control program for Sonic Pi

Here is a simpler system which receives inputs from two separate sources via OSC
The received data is stored using set commands and is then available for a separate live loop to do whatever you want with it. To test, I am generating the OSC messages locally in Sonic Pi, but they can come externally from your ESP8266, in which case you miss out (or comment out) the local section.

use_osc "localhost",4559 # used only for local testing where SP creates the data.
use_debug false
use_osc_logging false

live_loop :var1 do #receive and store var1 data
  v1 = sync "/osc/test/var1"
  set :v1,v1[0] # store the received data
end

live_loop :var2 do
  v2 = sync "/osc/test/var2" #receive and store var2 datga
  set :v2,v2[0] #store the second variable value
end

#as many as you need like the above

live_loop :latestData do #independent live loop processes latest available data
  puts "latest values var1 #{get(:v1)} var2 #{get(:v2)}"
  synth :tri,note: get(:v1),release: 0.4 #first variable adjust pitch
  synth :saw,note: get(:v2),release: 0.4 #second variable adjusts pitch
  sleep 0.4 #latest values read every 0.4 beats
end

#local generation of test osc messages
live_loop :createData1 do
  osc "/test/var1",rrand_i(60,72)
  sleep 0.2
end
live_loop :createData2 do
  osc "/test/var2",rrand_i(72,84)
  sleep 0.6
end

The two test data streams run at different rates, so you can hear the two variables updating at different times not synced to each other. Depending on how rapid a response you need, you may require to add use_real_time.

2 Likes