Communication between python3 and sonic pi using osc

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.

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

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.

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

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 :slight_smile:

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.

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.

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.

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

/osc:10.0.1.100:12345/play_this_p

Previously this would have looked like:

/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:

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:

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:

sync "/osc*/play_this_p"

This will match any of the following incoming OSC events:

/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:

sync "/osc:10.0.1.100:*/play_this_p"

Hope that this helps :slight_smile:

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

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 :slight_smile:

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

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.

good news :slight_smile:
see you soon on this forum