Communicating with Sonic Pi using UDP sockets

I found the Sonic PI music synthesiser recently, and had fun playing with it (and the language is Ruby which is quite nice). I wondered whether it would be possible to use Julia to send OSC (Open Sound Control) instructions to it.

From the Sonic Pi manual (“Receiving OSC”, it suggests that it might be quite easy:

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:

from pythonosc import osc_message_builder
from pythonosc import udp_client

sender = udp_client.SimpleUDPClient('127.0.0.1', 4560)
sender.send_message('/trigger/prophet', [70, 100, 8]) 

Or, if we’re sending OSC from Clojure we might do something like this from the REPL:

(use 'overtone.core)
(def c (osc-client "127.0.0.1" 4560))
(osc-send c "/trigger/prophet" 70 100 8) 

So I found an OSC library written in Julia - GitHub - fundamental/OpenSoundControl.jl: Open Sound Control For Julia - and then realised that I don’t know anything about UDP socket programming.

Given the simplicity of the Python and Clojure code samples, I’m guessing it should be something as straightforward as this in Julia:

using OpenSoundControl, Sockets
sock = UDPSocket()
bind(sock, ip"127.0.0.1", 4560)
msg = OscMsg("/trigger/prophet", "[hhh]", 70, 100, 8)
send(sock, ip"127.0.0.1", 4560, msg.data)

but since this doesn’t work, perhaps I’ve missed something obvious. If anyone’s had any experience with this, or wants an excuse to play with synthesizers for a while, I’d love to know how to get it working!

1 Like

You should not call bind, that asks the OS to listen for UDP messages on that (ip, port).

The send call looks correct assuming the OSC receiving software is running on the same host as you are running Julia. If this is not the case, you can try

addr = getaddrinfo("sonicpi")
send(sock, addr, 4560, msg.data)

where "sonicpi" is replaced by the hostname of the machine running Sonic Pi.

You can check that network communication is working by running

sudo tcpdump -i eth0 -X udp port 4560

On the receiving device, you should see a hexadecimal representation of msg.data whenever you send a message. If the receiving device and the sending device are the same, you should replace eth0 by lo in the above command.

If network communication is working as expected, then it is possible Sonic Pi and Julia’s OpenSoundControl disagree about some part of the OSC spec, comparing message data for the Julia and Python versions seems like a good next step.

1 Like

Thanks for the help! I think I’m making some progress now… :slight_smile: