Connecting Touch OSC on Laptop to SonicPi on same laptop

I’m trying to use the TouchOSC app on my Mac to control SonicPi on the same Mac and cannot seem to get the connection to work. I’ve looked online but have not found anything that helps to explain a basic setup. The boxes are checked to allow incoming OSC.
Below is the Sonic Pi code and I have attached screenshots of TouchOSC.
Thanks!


use_osc “local_host”, 4560

live_loop :foo do
use_real_time
a = sync “/osc/fader1”
end

a typo ? localhost not local_host ?

Hi There
You are nearly there. One or two things to change.
First the osc message actually received when you adjust the fader is of the form
/osc:127.0.0.1:9000/fader1 [0.4881889820098877]
It contains several elements.
1 The osc header which shows it is an osc message rather than a cue or a midi message
2 after a colon the ip address from which it is received (in this case 127.0.0.1) or “localhost”
3 after a second colon the port from which it is sent (9000)
4 after a / the control name fader1
5 the value of the fader in the range 0…1.0 note it is sent in a list [0.4881889820098877] in my example

To extract this you need to use
a = sync "/osc*/fader1". #the * matches and ignores the ip and port data.
a will then contain [0.4881889820098877] (in my example)
to get its value you need to use puts a[0] ie to get the first, in this case the only, element of the list.

If you only want to receive you do not need a use_osc statement. That only affects ip and port on which Sonic Pi SENDS osc messages. The ip address and port used by TouchOSC is controlled at THAT end, as shown in your setup photo.
Here is a simple progam which receives and prints the data as the slider is moved (my your mouse)

live_loop :test do
  a = sync "/osc*/fader1"
  puts "slider is at",a[0]
end

It is a good idea to add a port on which TouchOSC can receive incoming OSC mesages here. I put 9000 in.the Receive port.

Here is a fuller program I set up to test things which sends stuff bpth ways.

use_osc "localhost", 9000 #sets address and port to send to

live_loop :setIt do
  use_real_time
  #now send data to set fader: look at line info in language help to see how it works
  osc "/fader1",line(0,1,steps: 50,inclusive: true).mirror.tick
  sleep 0.1
  
end

live_loop :rx,delay: 0.1  do
  use_real_time
  a = sync "/osc*/fader1"
  n = 48+(a[0]*48).to_i #set range of n as 48 to 96 in integer steps (the .to_i ensures this)
  play n,release: 0.5
end

Note set up fader as per your example FIRST and start the TouchOSC program running before running theSonic Pi program.

You will not get the full value of TouchOSC on a Mac desktop because you really need a touch sensitive screen for best results, but you can either drive the control remotely 9as in my dxample) or you can drag the slider with your mouse.

Hope this helps. TouchOSC is a great proram, and you will ahve a lot of fun using it.

Ah yes, typo! Thanks for pointing that out.

Thanks so much Robin! This really helped me understand what is going on between the two programs.

So SonicPi is now receiving the messages and they are showing up in the cues window, but no sound is coming out? When I comment out the osc part of the code and just run it as a normal live_loop it produces sound but when I uncomment the osc code it goes silent even though the cues are still showing numbers changing?

live_loop :foo do
use_real_time
a = sync “/osc*/fader1”
puts “slider is at”, a[0]
v = a[0] * 100
play 60, amp: v
sleep 1
end

You will only get a sound once each time the slider moves and triggers a sync event. Also you don’t need the sleep 1 as the sync event gives a delay in the loop re-triggering.
I don’t think you want to multiply a[0] by 100 as it should be in the range 0-> 1.0 and this will give you amp: values of 0 to 100. Sonic Pi will certainly not like that! It could knock out playing until you reboot Sonic Pi. Also 60 is quite a low note for the rather soft default synth. Try using synth :tri and a pitch of 72
I tried this and it worked

use_synth :tri
live_loop :foo do
  use_real_time
  a = sync "/osc*/fader1"
  puts "slider is at", a[0]
  v = a[0]
  play 72, amp: v, release: 0.2 #the release value gives a short note
end

However you will ONLY hear notes while the slider moves and generates OSC messages to trigger this loop. If you want a volume control for continuously playing notes you need something like this.

use_synth :tri #synth to use
set :v,0.5 #set initial volume 0.5

#live loop to alter volume WHEN SLIDER MOVES
live_loop :foo do
  use_real_time
  a = sync "/osc*/fader1"
  puts "slider is at", a[0]
  v = a[0]
  puts v
  set :v,v #store v
end

live_loop :pl do #live loop to play notes continously
  vol = get(:v) #retrieve latest value
  #play a random note with current volume
  play rrand_i(48,84),amp: vol,release: 0.1
  sleep 0.1
end

This will start playing notes and amp: 0.5 when the program is run. The slider can then alter the volume according to its setting. To make it even better you could send an osc message TO the slider to set its initial position at mid scale. To do this add this code at the start. (Make sure that in touchOSC you set the imput port to 9000)

use_osc "localhost",9000
osc "/fader1",0.5 #set fader1 to midscale

Finally I notice you are posting your code using intelligent quotes rather than “straight” ones. Sonic Pi wont like this. Best way is to copy and paste your code directly from Sonic Pi, and then to insert three back ticks on teh line before and the line after the code. That’s what I’ve done here.

1 Like

Thanks Robin! Yes, this worked - I’m going to start playing with some of the other Touch OSC controls now.

That’s great. You’ll find it a lot of fun. It is very versatile with Sonic Pi. I strongly recommend trying it on a tablet or touch sensitive device, as well if you have one. You can build some great projects using one of these. Here are a couple I did. The fist one was with version 1, but works in version 2.

The second was done on version 2.