# Communication between python3 and sonic pi using osc

**URL:** <https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357>\
**Category:** Support, Help & Resources\
**Created:** [February 27, 2020, 8:01pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357 "2020-02-27T20:01:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul7777nash](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@paul7777nash](https://in-thread.sonic-pi.net/u/paul7777nash)\
**Post date:** [February 27, 2020, 8:01pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/1 "2020-02-27T20:01:32Z")

</div>

PYTHONSCRIPT

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

from gpiozero import DistanceSensor

from time import sleep

from pythonosc import osc\_message\_builder

from pythonosc import udp\_client

sensor = DistanceSensor(echo=10, trigger=9)

sensor1 = DistanceSensor(echo=8, trigger=7)

sender = udp\_client.SimpleUDPClient(‘127.0.0.1’, 4559)

while True:

pitch = round(sensor.distance \* 100 + 30)

sender.send\_message(’/play\_this\_p’, [pitch])

volume=round(sensor1.distance \*100+30)

sender.send\_message(’/play\_this\_v’, [volume])

sleep(0.1)

print(sensor.distance,0,sensor1.distance,1 )

sleep(1)

SONIC PI SCRIPT

live\_loop :listen do

message = sync “/play\_this\_p”

note = message [:args][0]

message1 = sync “/play\_this\_v”

vol = message1[:args][0]

play note, amp: vol

end

The script is a python script to set up pitch and volume for a theremin machine. The output Was sent to Sonic PI by udp\_client.SimpleUDPCClient and the sender.send\_message call in sonic pi script The Sonic Pi scrip receives it and is supposed to give pitch and volume to audio output  
In sonic pi I can see the cues box shows that data has been received by Sonic PI but there is no indication that this data is being sent to the “play” function in Sonic Pi. The oscilloscope sows no response and there is no sound output either. The Sonic Pi osc receive code is below this text. Can you give me some help here. I have tried the internet and l they do I refer me to your tutorial. Which I cannot find the reason for lack of communication in. Also what is the :arg for in this case.

Thanks for your help it has been a long time since I did coding so my brain is not working very well with this new program set.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [February 27, 2020, 8:51pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/2 "2020-02-27T20:51:01Z")

</div>

welcome @paul7777nash

first please edit your post to colorize your code with `````  
then for me `message = sync “/play_this_p” ` sounds strange  
you should type sync and then choose in the list something like `osc:blablbalbaa/ `  
hope it helps

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [February 27, 2020, 9:05pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/3 "2020-02-27T21:05:16Z")

</div>

> [@Link sonic pi to Python](http://in-thread.sonic-pi.net/t/link-sonic-pi-to-python/2938/5):
>
> @Gydhia Here is a simple example with a Python client sending OSC messages to Sonic Pi. First, the Python client: import random import time from pythonosc import udp\_client client = udp\_client.SimpleUDPClient('127.0.0.1', 4559) while True: client.send\_message("/bpm", random.randint(40, 200)) time.sleep(5) The Python code establishes a client connection to the Sonic Pi OSC server which runs on port 4559 on my localhost (127.0.0.1). OSC is a network protocol, so it doesn’t matter w…

---

<div class="post-metadata">

**Author:** ![paul7777nash](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@paul7777nash](https://in-thread.sonic-pi.net/u/paul7777nash)\
**Post date:** [February 28, 2020, 4:23am UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/4 "2020-02-28T04:23:51Z")

</div>

got this code from magpi magazine . i added osc:play\_this\_p as the argument. still dies not work. i added a play 60 amp:0.5 after the end of the loop and it made sound. it is af if the loop is not working at all.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [February 28, 2020, 9:20am UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/5 "2020-02-28T09:20:04Z")

</div>

try this, to see if liveloop is working.  
you should hear a `C4`

> [@paul7777nash](#):
>
> ```ruby
> live_loop :listen do
> 
> play :c4, amp: 3
> 
> message = sync “/play_this_p”
> 
> note = message [:args][0]
> 
> message1 = sync “/play_this_v”
> 
> vol = message1[:args][0]
> 
> play note, amp: vol
> 
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [February 28, 2020, 9:59am UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/6 "2020-02-28T09:59:56Z")

</div>

Hi @paul7777nash.  
I suspect that your code for syncing to the incoming messages and then for extracting the values out of them is not quite right. (I don’t think the `[:args]` is necessary in this case?, and the address you need to `sync` on needs to be a little different also). A couple of questions:

- What version of Sonic Pi are you using to try this?
- Have you tried something like the examples in the built-in tutorial for sending/receiving OSC messages? (See section 12.1). It describes the kind of syntax you need.

Let me know - I’m happy to answer any specific questions you may still have, in case no one else beats me to it 🙂

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [February 28, 2020, 1:54pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/7 "2020-02-28T13:54:56Z")

</div>

Hi Paul

As Ethan says, it is important to know the version of Sonic Pi you are using (presumably on a raspberry pi as you are using GPIO pins!).  
The syntax of commands has changed from Sonic Pi 3.0.1 (the only released version for Raspberry Pi which will work with OSC commands) to more recent versions Sonic PI 3.1 and Sonic Pi 3.2. Also, neither of these two later versions has an installation package available for Rasberry Pi which works with OSC or midi commands. The current version on Raspbian Buster has a curt down version of Sonic Pi 3.1 which does not support OSC or midi. For these later two versions I am afraid that at present you have to build Sonic Pi yourself.  
However, I suspect you may have the earlier versions 3.0.1 on Raspbian Scratch as you say you can see cues coming into the cues log.

---

<div class="post-metadata">

**Author:** ![paul7777nash](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@paul7777nash](https://in-thread.sonic-pi.net/u/paul7777nash)\
**Post date:** [February 28, 2020, 4:37pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/8 "2020-02-28T16:37:00Z")

</div>

i have version 3.0.1 of sonic pi.  
yes i have tried the examples and they did not help, or explain why this still fails .  
also i changed the python script to round the volume to two places as the amp function seems to want 0.2 type numbers, the cues box still shows unrounded number to 8 decimal places  
tried the **get** function in sonic, still nothing.  
tried the suggestion yo make a simple live\_loop. that did not work either. i have already re-installed sonic pi from the rasberry pi site.

---

<div class="post-metadata">

**Author:** ![pgloor](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/pgloor/32/1168_2.png) [@pgloor](https://in-thread.sonic-pi.net/u/pgloor)\
**Post date:** [February 28, 2020, 6:39pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/9 "2020-02-28T18:39:56Z")

</div>

I’m refering to the example of @nlb.

I’m already using Sonic Pi 3.2. In my case the trigger works with a “/osc\*” prefix. The code in Pi Sonic should look like this:

message = sync “/osc\*/play\_this\_p”

The related code in Python looks ok.

As far as I remember, the prefix changed from 3.1 to 3.2.  
3.1: /osc  
3.2: /osc\*

Also note: the default port has changed from 4559 to 4560 in Sonic Pi v3.2.

I hope that helps.

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [February 28, 2020, 8:00pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/10 "2020-02-28T20:00:50Z")

</div>

> [@pgloor](#):
>
> As far as I remember, the prefix changed from 3.1 to 3.2.  
> 3.1: /osc  
> 3.2: /osc\*

This is because in v3.2 the incoming OSC messages contain the IP address and port of the sender. For example:

```ruby
/osc:10.0.1.100:12345/play_this_p

```

Previously this would have looked like:

```ruby
/osc/play_this_p

```

However without the hostname and port, it is impossible to distinguish two similar OSC messages from two different senders. This is now possible in v3.2.

The difference in receiving them is minimal.

Whereas you used to do something like:

```ruby
sync "/osc/play_this_p"

```

As the address and port are now part of the incoming OSC message, this no longer works. You can instead do:

```ruby
sync "/osc:10.0.1.100:12345/play_this_p"

```

which will only match a `/play_this_p` OSC message sent from a machine running on 10.0.1.100 from port 12345.

However, if you want the old behaviour of matching a `/play_this_p` message from _any_ machine on _any_ port, then we can use the special `*` matcher to match any host and port:

```ruby
sync "/osc*/play_this_p"

```

This will match any of the following incoming OSC events:

```ruby
/osc:10.0.1.110:12345/play_this_p
/osc:10.0.1.100:8888/play_this_p
/osc:10.0.1.111:12345/play_this_p

```

Finally, if you wanted to match a `/play_this_p` message from address `10.0.1.100` on any port you would do:

```ruby
sync "/osc:10.0.1.100:*/play_this_p"

```

Hope that this helps 🙂

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [February 28, 2020, 8:58pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/11 "2020-02-28T20:58:09Z")

</div>

if you don’t give us some informations we can’t guess them 🙂

Version of Sonic PI : 3.0.1  
Raspberry pi OS : ???  
A link to the article you cite ?

We are all enthusiasts to help you but please help us to help you 🙂

---

<div class="post-metadata">

**Author:** ![paul7777nash](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@paul7777nash](https://in-thread.sonic-pi.net/u/paul7777nash)\
**Post date:** [February 28, 2020, 10:29pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/12 "2020-02-28T22:29:51Z")

</div>

sorry i have sonic pi 3.1 osc worked once and then never again. is there another alternative, for a new user?

---

<div class="post-metadata">

**Author:** ![paul7777nash](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@paul7777nash](https://in-thread.sonic-pi.net/u/paul7777nash)\
**Post date:** [February 29, 2020, 5:12am UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/13 "2020-02-29T05:12:38Z")

</div>

i finally got all of the settings and the addressing correct. now my theremin WORKS. thanks for all of the suggestions, they sent me on the right path.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [February 29, 2020, 7:25am UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/14 "2020-02-29T07:25:33Z")

</div>

good news 🙂  
see you soon on this forum

---

<div class="post-metadata">

**Author:** ![Ziani](https://avatars.discourse-cdn.com/v4/letter/z/c68b51/32.png) [@Ziani](https://in-thread.sonic-pi.net/u/Ziani)\
**Post date:** [November 25, 2021, 6:20pm UTC](https://in-thread.sonic-pi.net/t/communication-between-python3-and-sonic-pi-using-osc/3357/15 "2021-11-25T18:20:28Z")

</div>

Something very similar happened to me.

As is, the script you sent me worked for me on version 3.1, I updated it to version 3.3.1 and it stopped working. I did Sam’s thing to put \* for ports and it worked for me, I almost went crazy.

Sorry for google traductor, im spansih. xd
