I have made a series of three videos posted on youtube which show in detail how to create a simple layout in TouchOSC and set it up to control Sonic Pi 3. You can find the videos
here video1
here video2
here video3
or as a playlist here
The code used in the final Sonic Pi program (which is explained in detail in the third video) is below
#Test program to illustrate use of TouchOSC with Sonic Pi 3
#by Robin Newman, October 2017
set :trans,0 #set initial slider recorded position
#optional reset slider on TouchOSC
osc_send "192.168.1.240",9000,"/test/fader1",0
#optional reset onoff toggle to off
osc_send "192.168.1.240",9000,"/test/toggle1",0
sleep 0.2 #allow it to happen
#live loop to handle the push button
live_loop :b1 do
use_real_time
b = sync "/osc/test/push1" #match the push button address
puts "button 1 is ",b[0]
if b[0]>0 # only proceed when button is pushed, not released
use_synth :tri
10.times do #use 10 times loop to play some notes
play scale(:e3,:minor_pentatonic).choose,release: 0.1
sleep 0.1
end
end
end
#live loop to read slider position and record it with set :trans
live_loop :slider do
use_real_time
b = sync "/osc/test/fader1"
puts "slider is ",b[0]
set :trans,b[0]*12 #scale to +- octave
end
#live loop to respond to presses on the toggle switch toggle1
#this is used to switch on and off a long (1000 sec) note
#The pitch of the note is controlled by the slider value
live_loop :toggle do
use_real_time
b = sync "/osc/test/toggle1"
puts "toggle1 is ",b[0]
if b[0]>0 #if it is active (pressed and on)
use_synth :tri
x=play note(:c4)+get(:trans),sustain: 1000 #start a very long note and record referece in x
set :x,x #save x in :x for retrieval in following loop
in_thread do #a thread containing a continuously running loop
loop do
#get the control value and alter the pitch
#using the stored :trans value. Alter over a 0.1 slide time
control get(:x),note: note(:c4)+get(:trans),note_slide: 0.1
sleep 0.1 # wait for slide to finish and rerun loop
end
end
else #toggle has just been switched to off
control get(:x),amp: 0,amp_slide: 0.1 #fade out note
sleep 0.1
kill get(:x) #kill the long note (sustain: 1000)
end
end
As explained in the second video, the TouchOSC template is a zip or compressed file named tester.touchosc which contains a file index.xml shown below
<?xml version="1.0" encoding="UTF-8"?><layout version="15" mode="1" orientation="horizontal"><tabpage name="dGVzdA==" scalef="0.0" scalet="1.0" ><control name="bGFiZWwx" x="269" y="102" w="231" h="20" color="yellow" type="labelh" text="VG91Y2hPU0MgYW5kIFNvbmljIFBpIDM=" size="18" background="true" outline="false" ></control><control name="cHVzaDE=" x="181" y="193" w="45" h="45" color="red" scalef="0.0" scalet="1.0" type="push" local_off="false" ></control><control name="ZmFkZXIx" x="361" y="193" w="50" h="349" color="red" scalef="-1.0" scalet="1.0" type="faderv" response="absolute" inverted="true" centered="true" ></control><control name="dG9nZ2xlMQ==" x="547" y="193" w="45" h="45" color="red" scalef="0.0" scalet="1.0" type="toggle" local_off="false" ></control></tabpage></layout>
You can copy and save this as a text file index.html
Then compress this (right click and choose compress on the Mac, and right click and choose send to compressed folder on Windows) and rename the compressed file as tester.touchosc
This can then be put in the folder of TouchOSC files that are used with the TouchOSC editor, from whene you can load it into the Editor and then sync it to your IOS or Android device, running TouchOSC.
I hope you find the videos helpful. Please ask if anything is not clear.