Xinne/osc4py3

Applying a function to the received OSC message AND send that function's output via OSC

Closed this issue · 3 comments

Hi,

I'm creating a python script to do the following with both udp server and client (two different port, on localhost):

  1. It waits for an OSC message (i.e. text).
  2. When it does receive a OSC message, a function is applied on that message and generates an output.
  3. The output is sent via OSC.

With osc4py3, I got the following but unfortunately, it doesn't generates an output in this way:

from osc4py3.as_allthreads import *
from osc4py3 import oscbuildparse
from osc4py3 import oscmethod as osm

osc_startup()

osc_udp_server(127.0.0.1, 12000, "udplisten")
osc_udp_client(127.0.0.1, 12001, "udpclient")

def handlerfunction(address, *args):
	msgcount += 1
	generated = generator(args[0][0]) # I checked args[0][0] is the right portion of OSC message
	msg = oscbuildparse.OSCMessage("/output", None, [generated])
	osc_send(msg, "udpclient")
osc_method("/message", handlerfunction, argscheme=osm.OSCARG_ADDRESS + osm.OSCARG_DATA)

finished = False
while not finished:
	osc_process()

osc_terminate()

The OSC client is sending a message like /message text. I expect the generator function to process and its result goes back out to osc_send.

FYI, I've checked that my function generator() does work ;) It also worked when I incorporated into rundemo.py via input() or in any other contexts. Now I'm wondering whether this is not how osc_method or handlerfunction should be used.

Happy to hear if I am doing something wrong! Thanks in advance.

The code I sent by email (note use of logging, this allows to get traces from background threads when exceptions occurs), for testing, need definition of generator (here removed from within handler function to test):

import logging

from osc4py3.as_allthreads import *
from osc4py3 import oscbuildparse
from osc4py3 import oscmethod as osm

# A logger to monitor activity... and debug.
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger("osc")
logger.setLevel(logging.DEBUG)

IP = "127.0.0.1"
PORT = 12000
msgcount = 0

osc_startup(logger=logger)
#osc_startup()

osc_udp_server(IP, PORT, "udplisten")
osc_udp_client(IP, int(PORT+1), "udpclient")

def handlerfunction(address, *args):
    global msgcount
    msgcount += 1
    generated = args[0][0] # I checked args[0][0] is the right portion of OSC message I'd like to use
    logger.debug("Genrated is: %s", generated)
    msg = oscbuildparse.OSCMessage("/gentext/conditional", None, [generated])
    # Relay message to program YYY
    osc_send(msg, "udpclient")

# Receive /message from program XXX
osc_method("/message", handlerfunction, argscheme=osm.OSCARG_ADDRESS + osm.OSCARG_DATA)

finished = False
while not finished:
    #print("\n" + time.asctime())
    #print("Listening...")
    osc_process() # Process OSC in the loop.

osc_terminate() 

Thanks for your comments. I found that the problem was about threading. And loading osc4py3.as_eventloop fixed it.