receive osc

edited April 2016 in Python Mode

I am having troubles receiving messages with the oscP5 library. from the Java example, I got this setup in python

        from oscP5 import *
        from netP5 import * 



        print 
        def setup():
            size(400, 400)
            background(0)
            port1 = 12000
            oscP5 = OscP5(this, port1)
            loc = NetAddress('127.0.0.1', 12000)


        def draw():pass 



        def oscEvent(OscMessage):
           msg = OscMessage
           print "### received an osc message." 
           print  " addrpattern: %s " % msg.addrPattern()
           print  " typetag:%s " % msg.typetag()

While sending messages works fine, for some reason this doesn't. I think there is something wrong with the way I assign the value in the oscEvent function. Any idea? Thanks

Answers

  • FYI this is the Java sketch where I got the oscEvent function

    `/**
     * oscP5sendreceive by andreas schlegel
     * example shows how to send and receive osc messages.
     * oscP5 website at http://www.sojamo.de/oscP5
     */
    
    
    OscP5 oscP5;
    NetAddress myRemoteLocation;
    
    void setup() {
      size(400,400);
      frameRate(25);
      /* start oscP5, listening for incoming messages at port 12000 */
      oscP5 = new OscP5(this,12000);
    
      /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
       * an ip address and a port number. myRemoteLocation is used as parameter in
       * oscP5.send() when sending osc packets to another computer, device, 
       * application. usage see below. for testing purposes the listening port
       * and the port of the remote location address are the same, hence you will
       * send messages back to this sketch.
       */
      myRemoteLocation = new NetAddress("127.0.0.1",12000);
    }
    
    
    void draw() {
      background(0);  
    }
    
    void mousePressed() {
      /* in the following different ways of creating osc messages are shown by example */
      OscMessage myMessage = new OscMessage("/test");
    
      myMessage.add(123); /* add an int to the osc message */
    
      /* send the message */
      oscP5.send(myMessage, myRemoteLocation); 
    }
    
    
    /* incoming osc message are forwarded to the oscEvent method. */
    void oscEvent(OscMessage theOscMessage) {
      /* print the address pattern and the typetag of the received OscMessage */
      print("### received an osc message.");
      print(" addrpattern: "+theOscMessage.addrPattern());
      println(" typetag: "+theOscMessage.typetag());
    }`
    
  • edited April 2016 Answer ✓

    Go here: https://github.com/jdf/Processing.py-Bugs/issues/157
    And demand the same fix done for serialEvent() to oscEvent() callback too. >-)

  • Will do! Thanks.

  • edited April 2016 Answer ✓

    hi, I am not too familiar with the python mode but give the following a go. I have tested this with oscP5 version 0.9.9 and 2.0.4, sending and receiving worked fine.

    # tested with oscP5 0.9.9 and 2.0.4 
    add_library('oscP5') 
    
    addr = "?"
    
    class Listen(OscEventListener):
        def oscEvent(self,m):
            global col, addr
            col = m.arguments()[0]
            # col = int(m.arguments()[0])
            addr = m.addrPattern()
            print("Listen.oscEvent",m.addrPattern(), m.arguments())
    
    def setup():
        global osc, loc, col
        size(600, 600, P3D)
        osc = OscP5(this, 12000)
        loc = NetAddress('127.0.0.1', 12000) # send to self
        osc.addListener(Listen()) # assigning a listener to class Listen
        col = 100
    
    def draw():
        global col,addr
        background(col)
        print(addr)
    
    def keyPressed():
        global osc,loc
        msg = OscMessage("/test")
        msg.add(random(255))
        osc.send(msg,loc)
        print("sending message",msg.addrPattern())
    
    def stop():
        global osc
        osc.dispose()
    

    giving the following a try with oscP5 2.0.4 also worked, in the code below a listener is directly assigned to a function (oscEvent)

    # in this example a listener is assigned to function oscEvent
    # assigning a function as listener only works with 2.0.4 not 0.9.9
    
    add_library('oscP5') 
    
    def setup():
        global osc, loc, col
        size(600, 600, P3D)
        osc = OscP5(this, 12000) # send to self
        loc = NetAddress('127.0.0.1', 12000)
        osc.addListener(oscEvent) # assigning a listener to function oscEvent
        col = 100
    
    def draw():
        global col
        background(col)
    
    def oscEvent(m):
        global col
        col = m.arguments()[0]
        # col = int(m.arguments()[0])
        print("oscEvent",m.addrPattern(), m.arguments())
    
    def keyPressed():
        global osc,loc
        msg = OscMessage("/test")
        msg.add(random(255))
        osc.send(msg,loc)
        print("sending message",msg.addrPattern())
    
    def stop():
        global osc
        osc.dispose()
    

    Also make sure that the udp port must be closed when exiting the sketch, so that it is not blocked when re-running the sketch. This is taken care of calling osc.dispose() from inside the stop() function

    Note: an earlier version of the examples above used m.getArguments() which is only implemented for 2.0.4 but not 0.9.9 which caused the examples not to run under 0.9.9. this has been highlighted in the discussion below. m.getArguments() was then replaced by m.arguments().

  • Thank you so much! I was really struggling here. FYI: The first solution works great while the second does not let me assign oscEvent as an argument to addListener.

  • One last question: how do I get the oscEvent function in the Listen class to return the value received (first example)? I tried to return it with the addrPattern method but, while printing works, I can't get it to be assigned to a sketch variable...

  • edited April 2016

    I have made adjustments to my previous post, have a look. the code snippets now also show how to transfer and store an osc argument inside a global variable (here col).

  • edited April 2017

    @sojamo, strangely your 1st example doesn't trigger the oscEvent() for me. It only sends. :(
    Also I've tweaked it a little but no success.
    For me it seems I'm forced to await for the actual oscEvent() fix. ~:>

    # forum.Processing.org/two/discussion/15995/receive-osc#Item_8
    # 2016-Apr-14
    
    add_library('oscP5')
    
    PORT, gray = 12000, 0200
    
    def setup():
        size(600, 400)
        smooth(4)
        noLoop()
        frameRate(15)
    
        global osc, loc, msg
        osc = OscP5(this, PORT)
        loc = NetAddress('127.0.0.1', PORT)
        msg = OscMessage('/test')
    
        osc.addListener(MyOscListener())
        print osc.listeners(), PORT
    
    
    def draw():
        background(gray)
        frame.setTitle('Grey Color: ' + `gray`)
    
    
    def mousePressed(): keyPressed()
    
    def keyPressed():
        if 'msg' not in globals(): return
    
        msg.clearArguments()
        msg.add(int(random(0400)))
    
        print 'Sending gray color message:', msg.arguments()[0]
        osc.send(msg, loc)
    
    
    class MyOscListener(OscEventListener):
        @staticmethod
        def oscEvent(m):
            global gray
            gray = m.arguments()[0]
    
            redraw()
            print 'Received oscEvent:', m.addrPattern(), gray, m.typetag()
    
  • works for me on osx and processing 3.0.2, your example also works fine.

  • Win 7, 64-bit P5 v3.0.2 and Osc v0.9.9. :-<

  • edited April 2016

    If I close all the PDEs (or kill all "java.exe" processes, keeping "javaw.exe" though) and re-run it and paste the code, at the 1st run I've got this after keyPressed():

    processing.app.SketchException: AttributeError: 
      'oscP5.OscMessage' object has no attribute 'getArguments'
        at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:248)
        at jycessing.mode.run.SketchRunner.access$4(SketchRunner.java:227)
        at jycessing.mode.run.SketchRunner$3.run(SketchRunner.java:122)
        at java.lang.Thread.run(Thread.java:745)
    

    On subsequent re-runs the sketch executes. But no oscEvent() triggers at all! :-&

  • edited April 2016

    If I comment out #gray = m.getArguments()[0], the oscEvent() trigger is called back successfully! @-)

  • edited April 2016 Answer ✓
    • Found out the culprit: Indeed there's no method called getArguments() in class OscMessage! =P~
    • Since I was following your template like this 1: col = m.getArguments()[0], I've ended up incurring in error! No idea why you were immune to it though. 8-}
    • Funnily, I did the right thing in my own keyPressed() before typing in the class:
      print 'Sending gray color message:', msg.arguments()[0]
    • B/c I've looked up the class' reference before using the method: ;))
      http://www.Sojamo.de/libraries/oscp5/reference/oscP5/OscMessage.html#arguments()

    P.S.: Once any error occurs in the OscEventListener, I've gotta close all PDEs or kill all "java.exe". Otherwise Python mode doesn't see any further editing made to the class! :(|)

  • I was running into the same problem of GoToLoop, now fixed. Thank you all for taking care of this issue!!

  • hi, sorry, my bad. when editing and updating my post from above, I did check the example only against oscP5 2.0.4 and later not 0.9.9 which is the version currently available from the library manager. getArguments() is only implemented with 2.0.4 and not 0.9.9. arguments() is available with both versions. I have made updates to my previous post to make the examples work for both versions, I have left a remark that mentions the change from getArguments() to arguments().

  • edited April 2016

    great

Sign In or Register to comment.