0

I'm trying to connect Python with Supercollider through OSC, but it's not working.

I'm using Python3 and the library osc4py3.

The original idea was to send a text word by word, but upon trying I realized the connection was not working.

Here's the SC code:

(
OSCdef.new(\texto,{
    |msg, time, addr, port|
    [msg, time, addr,port].postIn;
},
'/texto/supercollider',
n
)
)

OSCFunc.trace(true);

o = OSCFunc(\texto);

And here's the Python code:

osc_startup()

osc_udp_client("127.0.0.1", 57120, "supercollider")

## here goes a function called leerpalabras to separate words in rows.

with open("partitura.txt", "r") as f:
   for palabra in leerpalabras(f):
        msg = oscbuildparse.OSCMessage("/texto/supercollider", ",s", palabra)
        osc_send(msg, "supercollider")
        sleep(2)

osc_terminate()

I've also tried with this, to see if maybe there was something wrong with my for loop (with the startup, and terminate of course):

msg = oscbuildparse.OSCMessage("/texto/supercollider", ",s", "holis")
osc_send(msg, "supercollider")

I run the trace method on SC, nothing appears on the post window when I run the Python script on terminal, but no error appears on neither one of them, so I'm a bit lost on what I can test to make sure is getting somewhere.

It doesn't print on the SC post window, it just says OSCdef(texto, /texto/supercollider, nil, nil, nil).

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sophiet
  • 13
  • 7

2 Answers2

0

When I run the SuperCollider piece of your example, and then run:

n = NetAddr("127.0.0.1", 57120);
n.sendMsg('/texto/supercollider', 1, 2, 3);

... I see the message printed immediately (note that you used postIn instead of postln, if you don't fix that you'll get an error instead of a printed message).

Like you, I don't see anything when I send via the Python library - my suspicion is that there's something wrong on the Python side? There's a hint in this response that you have to call osc_process() after sends, but that still doesn't work for me

You can try three things:

  1. Run OSCFunc.trace in SuperCollider and watch for messages (this will print ALL incoming OSC messages), to see if your OSCdef is somehow not receiving messages.

  2. Try a network analyzer like Packet Peeper (http://packetpeeper.org/) to watch network traffic on your local loopback network lo0. When I do this, I can clearly see messages sent by SuperCollider, but I don't see any of the messages I send from Python, even when I send in a loop and call osc_process().

  3. If you can't find any sign of Python sending OSC packets, try a different Python library - there are many others available.

scztt
  • 1,053
  • 8
  • 10
0

(I'm osc4py3 author) osc4py3 store messages to send within internal lists and returns immediately. These lists are processed during osc_process() calls or directly by background threads (upon selected theading model).

So, if you have selected as_eventloop threading model, you need to call osc_process() some times, like:

…
with open("partitura.txt", "r") as f:
   for palabra in leerpalabras(f):
        msg = oscbuildparse.OSCMessage("/texto/supercollider", ",s", palabra)
        osc_send(msg, "supercollider")
        for missme in range(4):
           osc_process()
           sleep(0.5)
…

See doc: https://osc4py3.readthedocs.io/en/latest/userdoc.html#threading-model