Can I create OSC message handlers containing wildc

2019-06-04 02:16发布

问题:

I am trying to create an OSC msg handler, using pyosc, which can listen to incoming multitoggle messages from TouchOSC.

The multitoggle is a grid of toggleswitches. The incoming messages are in the form "/1/multitoggle1/5/8" or "/1/multitoggle1/x/y" where x and y are integers corresponding to the grid position.

server.addMsgHandler( "/1/multitoggle1/5/8", toggle_callback ) works fine but I need the 5 and the 8 to be arguments read in the handler so I can get at them without having to add a separate handler for each individual toggle.

s.addMsgHandler( "/1/multitoggle1/", toggle_callback ) does not seem to work.

It is a similar problem to this one but I can't implement the implied solution.

回答1:

I had the same problem and this was my solution:

for x in range(1,9):
    for y in range(1,6):
        s.addMsgHandler("/Channels/toggleChannels/"+`y`+"/"+`x`, toggleChannels)

def toggleChannels(addr,tags,data,source):
    split = addr.split("/")
    x = split.pop()
    y = split.pop()

I registered all handlers but used only one callback, worked great



回答2:

Or better yet, extracting things and preventing hardcoding:

# this is put into a config file for easy mod'ing
OSCPATH = {
    # Incoming OSC from the tracking subsys
    'ping': "/ping",
    'start': "/pf/started",
    'entry': "/pf/entry",
    'exit': "/pf/exit",
    'update': "/pf/update",
    'frame': "/pf/frame",
    'stop': "/pf/stopped",
    'set': "/pf/set/",
    'minx': "/pf/set/minx",
    'maxx': "/pf/set/maxx",
    'miny': "/pf/set/miny",
    'maxy': "/pf/set/maxy",
    'npeople': "/pf/set/npeople",
    # Outgoing OSC updates from the conductor
    'attribute': "/conducter/attribute",
    'rollcall': "/conducter/rollcall",
    'event': "/conducter/event",
    'conx': "/conducter/conx",
}


class OSCHandler(object):

    """Set up OSC server and other handlers."""

    def __init__(self, field):
        self.m_server = OSCServer( (OSCHOST, OSCPORT) )

        self.EVENTFUNC = {
            'ping': self.event_tracking_ping,
            'start': self.event_tracking_start,
            'stop': self.event_tracking_stop,
            'entry': self.event_tracking_entry,
            'exit': self.event_tracking_exit,
            'update': self.event_tracking_update,
            'frame': self.event_tracking_frame,
            'minx': self.event_tracking_set,
            'miny': self.event_tracking_set,
            'maxx': self.event_tracking_set,
            'maxy': self.event_tracking_set,
            'npeople': self.event_tracking_set,
        }

        for i in self.EVENTFUNC:
            self.m_server.addMsgHandler(OSCPATH[i], self.EVENTFUNC[i])

You'll see that several paths, including minx, miny, etc, map to the same function. These use the path param to take specific actions to handle this data.



回答3:

OSC does support wildcards in the Address Pattern of Methods (OSC speak for what you call Handlers).

They work similar to windows or unix command-line file name wildcards, not like regular expressions. For details, check out OSC Message Dispatching and Pattern Matching in the OSC 1.0 specifications.

In your example, you could define an address pattern "/1/multitoggle1/*/*", which would allow you to receive "/1/multitoggle1/5/8" and similar messages.



标签: python osc