# Communicating with Sonic Pi using UDP sockets

**URL:** <https://discourse.julialang.org/t/communicating-with-sonic-pi-using-udp-sockets/60820>\
**Category:** New to Julia\
**Created:** [May 9, 2021, 12:33pm UTC](https://discourse.julialang.org/t/communicating-with-sonic-pi-using-udp-sockets/60820 "2021-05-09T12:33:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 9, 2021, 12:33pm UTC](https://discourse.julialang.org/t/communicating-with-sonic-pi-using-udp-sockets/60820/1 "2021-05-09T12:33:55Z")

</div>

I found the [Sonic PI music synthesiser](https://sonic-pi.net) 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:

```python
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:

```julia
(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](https://github.com/fundamental/OpenSoundControl.jl) - 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:

```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!

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 10, 2021, 5:07pm UTC](https://discourse.julialang.org/t/communicating-with-sonic-pi-using-udp-sockets/60820/2 "2021-05-10T17:07:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 11, 2021, 9:04am UTC](https://discourse.julialang.org/t/communicating-with-sonic-pi-using-udp-sockets/60820/3 "2021-05-11T09:04:48Z")

</div>

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