Using TouchOSC with Sonic Pi

I’ve recently found another ace way to use Sonic Pi with pupils (thanks to the creator himself for showing me this!) by using another device as input and programming the output. TouchOSC is a paid for app that can interface with Sonic Pi and send messages. You can program each of the controls on the various interfaces to play a note, samples and so on. I am definitely going to use this when I want to teach input output and conditionals (to play a certain sound when the toggle is on or off). Here is a link to a video demo of what I made this morning.

6 Likes

This is awesome. I just heard about this app. Can’t wait to try it. Could you post the code you used for this example just as a window into what was going on?

1 Like

Hi there, thank you! :slight_smile: I’ve just got it connected on my Windows PC at home so I’ve copied and pasted the code I wrote this evening, so differs slightly from the video. The samples I play are ones I bought from Samplephonics (80s synthwav). BTW, I’m using the Automat5 layout in TouchOSC.
Have fun!

live_loop :fA do
  use_real_time
  v = sync "/osc/1/faderA"
  use_synth :blade
  play v[0] * 100, release: 0.1
end


live_loop :fB do
  use_real_time
  v = sync "/osc/1/faderB"
  use_synth :pretty_bell
  play v[0] * 100, release: 0.1
end


live_loop :fC do
  use_real_time
  v = sync "/osc/1/faderC"
  use_synth :dsaw
  play v[0] * 100, release: 0.1
end


live_loop :fD do
  use_real_time
  v = sync "/osc/1/faderD"
  use_synth :pluck
  play v[0] * 100, release: 1
  
  
end

live_loop :fAbuttonA do
  use_real_time
  v = sync "/osc/toggleA_1"
  if v[0] == 1
    sample bass, "115", 0, amp: 2
  end
end

live_loop :fAbuttonB do
  use_real_time
  v = sync "/osc/toggleA_2"
  if v[0] == 1
    sample bass, "115", 1, amp: 2
  end
end

live_loop :fBbuttonA do
  use_real_time
  v = sync "/osc/toggleB_1"
  if v[0] == 1
    sample drum, "115", 0, amp: 2
  end
end

live_loop :fBbuttonB do
  use_real_time
  v = sync "/osc/toggleB_2"
  if v[0] == 1
    sample drum, "115", 1, amp: 2
  end
end

live_loop :fCbuttonA do
  use_real_time
  v = sync "/osc/toggleC_1"
  if v[0] == 1
    with_fx :lpf do
      sample synth1, "115", 1, amp: 2, release: 2
    end
  end
end

live_loop :fCbuttonB do
  use_real_time
  v = sync "/osc/toggleC_2"
  if v[0] == 1
    with_fx :pitch_shift do
      sample synth1, "115", 2, amp: 1, release: 2
    end
  end
end

live_loop :fDbuttonA do
  use_real_time
  v = sync "/osc/toggleD_1"
  with_fx :ixi_techno do
    if v[0] == 1
      sample arp, "115", amp: 1, rate: 1.1
    end
  end
end

live_loop :fDbuttonB do
  use_real_time
  v = sync "/osc/toggleD_2"
  with_fx :ixi_techno do
    if v[0] == 1
      sample synth1, "115", 3, amp: 1, rate: 1.1
    end
  end
end
4 Likes

Can you recommend a tutorial or provide some directions as to how to get Touch OSC and Sonic Pi talking to each other? The app doesn’t really seem to offer much direction. I set SPi to receive remote OSC messages and set the outgoing port on the app to 4559. But beyond that I’m not sure what else I need to do. I ran the first section of your code and adjusted the A fader in the app but no luck. I’m sure I’m missing some steps in here but not sure where. Any help would be appreciated. Thanks

live_loop :fA do
  use_real_time
  v = sync "/osc/1/faderA"
  use_synth :blade
  play v[0] * 100, release: 0.1
end

Here is an article I posted recently which gives details of a simple TouchOSC controlled project that might be of help. I have used the program very extensively in a variety of projects. It is great and easy to use.

2 Likes

Thanks. I appreciate the help. But I am afraid that is way over my head at this point. I am really looking for a super simple example of what I need to do in Sonic Pi and TouchOSC to jsut get them communicating: entering port numbers, IP addresses, OSC messages to send, what code to put in SPi to receive messages etc. I am a total newbie when it comes to this and am having trouble making sense of any code people are putting up as examples. I would love to just be able to control one slider or button in TouchOSC to make something, anything happen in SPi at this point!

No problem. I’ll try and write you a simple abc on connecting TouchOSC and Sonic Pi together later today.

1 Like

@Perpglo perhaps you could have a go explaining this? :slight_smile:

2 Likes

They say the best way to demonstrate knowledge is by teaching what you know to somebody else…

2 Likes

You’ve done the right thing by ticking ‘Receive Remote OSC Messages’ (an error I first made in that I hadn’t ticked it).

Next, have you configured TouchOSC to talk to Sonic Pi by going to settings in the app>Connections>Host and change it so that it is the IP address of the shared router (I’m assuming your computer and device with the TouchOSC app on are connected to the same network?). Then run the program with that code you posted earlier and fiddle with the A slider and you should hear sound? If not, fiddle with the slider and see if anything shows up under Cues (see pic). If nothing shows up under Cues, then the app and computer are still not connected… :confused:

Capture

2 Likes

In case you don’t use a VPN like me, the host IP address should be of the machine running Sonic Pi. This IP address can be found by looking in the IO section of the prefs. :blush:

1 Like

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.

2 Likes

Thank you sooooo much! This was super helpful. I am now successfully communicating through TouchOSC on my iPhone to Sonic Pi. So excited about all the possibilities.

2 Likes

That’s great. It is a super useful program (TouchOSC) and I’m sure you will have a lot of fun with it.

Awesome! Post a video of what you program it to do. :smile:

Hi Robin, hi Perpglo,

thanks also from my side! This will get me starting more easily. I bought the app but haven’t tried it out yet (I did mess around a bit with Control, which seems to have the same range of functionality - plus: it is Open Source).

But I guess touchOSC will be a brilliant tool for custom Sonic-Pi-midi interfaces.

Martin

1 Like

Amazing stuff! Much easier to set up than I thought. Cellphone- and Wifi-powered control of Sonic Pi! Just spent about an hour and had a custom layout made with the touchOSC editor as well as osc messages within Sonic Pi. First project will be my ‘4-track’ recorder [1, 2] with a touchOSC layout. Using Arturia minilab was a good exercise but is very clumsy. I will post as soon as I have a working example. The good thing is, that you can distribute the interface as well…

For all who want to use touchOSC with Linux: You have to have installed some kind of client/bridge to receive the osc messages. I am successfully using touchosc2midi. I did not know that when I started… (That is wrong. See @samaaron and my comment below.)

Martin

Just for those who like the basic idea and care to improve it:

A few months ago I did a session with a Digeridoo-player. That was fun!

What I want to achieve now in a first step:

  • Get live sound from another player into Sonic Pi (need to buy an external interface so that my laptop can work with it)
  • Use the looper to record bits of it and play it back (using the touchOSC-interface to when and what I want to record)
  • Have the option to modify the samples coming in (along with what I live-produce in Sonci Pi)
  • Having a lot of fun with Sonic Pi as a live instrument

Martin

1 Like

That surprises me! Are you sending OSC or MIDI from TouchOSC? You’re much better off sending OSC and then it’s just a matter of Linux having a network stack which can handle UDP - which clearly all distros do! I’m very curious to know what issues you were having and why you needed a bridge…

1 Like

If you use midi with TouchOSC then touchosc2midi performs the same function as the TouchOSC Bridge supplied with TouchOSC for Mac and Windows. This allows TouchOSC to send midi signals effectively over a LAN. Personally I use OSC almost exclusively now with TouchOSC communicating with Sonic Pi so don’t use this. It works fine just sending OSC messages to linux (eg RPi).

2 Likes