Press Run to start the engine: its live metrics show here.
12 OSC
In addition to MIDI, another way to get information in and out of Sonic
Pi is via the network using a simple protocol called OSC - Open Sound
Control. This will let you send messages to and from external programs
(both running on your computer and on external computers) which opens up
the potential for control way beyond MIDI which has limitations due to
its 1980s design.
For example, you could write a program in another programming language
which sends and receives OSC (there are OSC libraries for pretty much
every common language) and work directly with Sonic Pi. What you can use
this for is only limited by your imagination.
12.1 Receiving OSC
By default when Sonic Pi is launched it listens to port 4560 for
incoming OSC messages from programs on the same computer. This means
that without any configuration, you can send Sonic Pi an OSC message and
it will be displayed in the cue log just like incoming MIDI
messages. This also means that any incoming OSC message is also
automatically added to the Time State which means you can also use get
and sync to work with the incoming data - just like with MIDI and
synchronising live_loops - see sections 5.7 and 10.2 to recap how this
works.
A Basic OSC Listener
Let’s build a basic OSC listener:
A Basic OSC Listener
live_loop :foodo
use_real_time
a, b, c = sync "/osc*/trigger/prophet"
synth :prophet, note: a, cutoff: b, sustain: c
end
In this example we described an OSC path "/osc*/trigger/prophet" which
we’re syncing on. This can be any valid OSC path (all letters and
numbers are supported and the / is used like in a URL to break up the
path to multiple words). The /osc prefix is added by Sonic Pi to all
incoming OSC messages, so we need to send an OSC message with the path
/trigger/prophet for our sync to stop blocking and the prophet synth
to be triggered.
Sending OSC to Sonic Pi
We can send OSC to Sonic Pi from any programming language that has an
OSC library. For example, if we’re sending OSC from Python we might do
something like this:
Or, if we’re sending OSC from Clojure we might do something like this from the REPL:
Sending OSC to Sonic Pi · 2
(use 'overtone.core)
(def c (osc-client "127.0.0.1" 4560))
(osc-send c "/trigger/prophet" 70 100 8)
Receiving from External Machines
For security reasons, by default Sonic Pi does not let remote machines
send it OSC messages. However, you can enable support for remote
machines in Preferences->IO->Networked OSC->Allow OSC from other computers. Once
you’ve enabled this, you can receive OSC messages from any computer on
your network. Typically the sending machine will need to know your IP
address (a unique identifier for your computer on your network - kind of
like a phone number or an email address). You can discover the IP
address of your computer by looking at the IO section of the preferences
pane. (If your machine happens to have more than one IP address,
hovering the mouse over the listed address will pop up with a list of
all known addresses).
Note, some programs such as TouchOSC for iPhone and Android support
sending OSC as a standard feature. So, once you’re listening to remote
machines and know your IP address you can instantly start sending
messages from apps like TouchOSC which enable you to build your own
custom touch controls with sliders, buttons, dials etc. This can provide
you with an enormous range of input options.
12.2 Sending OSC
In addition to receiving OSC and working with it using Time State, we
can also send out OSC messages in time with our music (just like we can
send out MIDI messages in time with our music). We just need to know
which IP address and port we’re sending to. Let’s give it a try:
Sending OSC
use_osc "localhost", 4560
osc "/hello/world"
If you run the code above, you’ll notice that Sonic Pi is sending itself
an OSC message! This is because we set the IP address to the current
machine and the port to the default OSC in port. This is essentially the
same as posting a letter to yourself - the OSC packet is created, leaves
Sonic Pi, gets to the network stack of the operating system which then
routes the packet back to Sonic Pi and then it’s received as a standard
OSC message and is visible in the cue logger as the incoming message
/osc:127.0.0.1:4560/hello/world. (Notice how Sonic Pi automatically prefixes all
incoming OSC messages with /osc and then the hostname and port of the sender.)
Sending OSC to other programs
Of course, sending OSC messages to ourselves may be fun but it’s not
that useful. The real benefit starts when we send messages to other
programs:
Sending OSC to other programs
use_osc "localhost", 123456
osc "/hello/world"
In this case we’re assuming there’s another program on the same machine
listening to port 123456. If there is, then it will receive a
"/hello/world OSC message with which it can do what it wants.
If our program is running on another machine, we need to know its IP
address which we use instead of "localhost":
Sending OSC to other programs · 2
use_osc "192.168.10.23", 123456
osc "/hello/world"
Now we can send OSC messages to any device reachable to us via our local
networks and even the internet!
Sonic Pi
Lowering the many barriers of entry for creative experiences with code.