OSC streaming with Python-OSC

hobbyhack
Posts: 2
Joined: Mon Jan 11, 2021 1:22 pm

Re: OSC streaming with Python-OSC

Post by hobbyhack »

James wrote: Mon Jan 11, 2021 2:07 pm If you set it to listen on "127.0.0.1" it should listen on all available network interfaces.
127.0.0.1 causes it to only listen locally. However, setting it to an empty string would work for all IP addresses.

Code: Select all

IP = ""
mfack1317
Posts: 4
Joined: Mon Feb 15, 2021 4:55 am

Re: OSC streaming with Python-OSC

Post by mfack1317 »

Hi!

I am new to the forum, and I read this thread carefully, I think I have a similar problem.

I am helping a friend writing a python code to
  1. get a message from MaxMSP over a defined IP/port
  2. translate the message with python
  3. send the message to SuperCollider
My friend uses MaxMSP and SuperCollider, I am supposed to be the "python-expert" :D

After some googling and talking to the python-osc library developer over github, I set up my code as follows:

Code: Select all

import argparse

from pythonosc import dispatcher
from pythonosc import osc_server
from pythonosc import udp_client

def main(path: str, *osc_arguments):
    msg = osc_arguments[-1]
    print("input message: {}".format(msg))
    msgOUT = msg+'out'
    # output
    print("output message: {}".format(msgOUT))
    ipOUT = osc_arguments[0][0]
    portOUT = osc_arguments[0][1]
    pathOUT= osc_arguments[0][2]
    talk2SC(ipOUT,portOUT,pathOUT,msgOUT)

def listen2Max(addrIN,addrOUT):
    '''
    set up server
    '''
    # input address
    ipIN   = addrIN[0]
    portIN = addrIN[1]
    pathIN = addrIN[2]
    # output address
    portOUT = addrOUT[0]
    pathOUT = addrOUT[1]
    # dispatcher to receive message
    disp = dispatcher.Dispatcher()
    disp.map(pathIN, main, ipIN, portOUT, pathOUT)
    # server to listen
    server = osc_server.ThreadingOSCUDPServer((ipIN,portIN), disp)
    print("Serving on {}".format(server.server_address))
    server.serve_forever()

def talk2SC(ip,port,path,mymove):
    '''
    set up client
    '''
    client = udp_client.SimpleUDPClient(ip,port)
    client.send_message(path, mymove)

if __name__ == "__main__":
    # generate parser
    parser = argparse.ArgumentParser(prog='scacchiOSC', formatter_class=argparse.RawDescriptionHelpFormatter, description='Interprete di messaggi OSC da Max\n')
    parser.add_argument("-II","--ipIN", type=str, default="127.0.0.1", help="The ip to listen on")
    parser.add_argument("-PI", "--portIN", type=int, default=5005, help="The port to listen on")
    parser.add_argument("-UI", "--uripathIN", type=str, default="/filter", help="MAX's URI path")
    parser.add_argument("-PO", "--portOUT", type=int, default=5006, help="The port to send messages to")
    parser.add_argument("-UO", "--uripathOUT", type=str, default="/filter", help="output URI path")
    args = parser.parse_args()
    # wrap up inputs
    outputAddress = [args.portOUT, args.uripathOUT]
    inputAddress = [args.ipIN, args.portIN, args.uripathIN]
    # listen to max
    listen2Max(inputAddress, outputAddress)
The part

Code: Select all

msgOUT = msg+'out'
is far more complicated, but it gets the job done.

My first problem is that I can't get messages from MaxMSP. I thought the problem was the path, but then I used the

Code: Select all

set_default_handler
method to see the default path by MaxMSP, but still I am not receiving any message.

On the other hand, I made a stupid test and set-up a python client and another python server with python-osc to do the following.
  1. fire up the "main" server listening on 127.0.0.1 port 5005 and sending on the same IP on port 5006
  2. fire up a second server listening on 127.0.0.1 port 5006
  3. use a client to send a message on the "main" server
And this works amazingly: the "main" server gets the message, it elaborates the output message and sends it over to the second server.

Any thoughts about why I am not able to make MaxMSP communicate with python? Is there any way to check if MaxMSP is really sending messages on the selected IP/port?

Any help is appreciated!

Best
mkay1999
Posts: 4
Joined: Fri Mar 12, 2021 3:06 am

Re: OSC streaming with Python-OSC

Post by mkay1999 »

hello I have been trying to stream muse 2 data into python but I keep getting this error: OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions.

Any idea how I can fix it?
mfack1317
Posts: 4
Joined: Mon Feb 15, 2021 4:55 am

Re: OSC streaming with Python-OSC

Post by mfack1317 »

Well, the error seems to be self-explanatory. Can you copy paste your code here? Maybe just the part where you access the socket.
mkay1999
Posts: 4
Joined: Fri Mar 12, 2021 3:06 am

Re: OSC streaming with Python-OSC

Post by mkay1999 »

rom pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import BlockingOSCUDPServer

IP = "192.168.0.45"
PORT = 5000

def muse_handler(address, *args):
"""handle all muse data"""
print(f"{address}: {args}")

def eeg_handler(address, *args):
"""handle raw eeg data"""
print(f"{address}: {args}")

def battery_handler(address, *args):
"""handle battery data"""
level, batt_voltage, adc_voltage, temp = args
print(f"Battery level: {level}")

def blink_handler(address, *args):
"""handle blink data"""

# Note: when the jaw stays clenched blink message is spammed

# it seems to only send a 1 when a blink is detected
# so no reason to do much to process the data
print("Blink detected")

def jaw_handler(address, *args):
"""handle jaw clench data"""

# it seems to only send a 1 when a clench is detected
# so no reason to do much to process the data
print("Jaw Clench detected")

def marker_handler(address, *args):
"""handle marker data"""

# this didn't do anything for me. Might be because I have
# the S model
print(f"{address}: {args}")

def default_handler(address, *args):
print(f"DEFAULT {address}: {args}")

def get_dispatcher():
dispatcher = Dispatcher()
# dispatcher.map("/muse/batt", battery_handler)
dispatcher.map("/muse/elements/blink", blink_handler)
dispatcher.map("/muse/elements/jaw_clench", jaw_handler)
# dispatcher.map("/Marker/*", marker_handler)

# this will handle any unidentified messages if you would like
# dispatcher.set_default_handler(default_handler)

# use this dispatched to handle anythin muse
# dispatcher.map("/muse/*", muse_handler)

return dispatcher

# TODO
# dispatcher.map("/muse/*", muse_handler)
# dispatcher.map("/muse/eeg", eeg_handler)
# dispatcher.map("/muse/elements/delta_absolute", delta_absolute_handler)
# dispatcher.map("/muse/elements/theta_absolute", theta_absolute_handler)
# dispatcher.map("/muse/elements/alpha_absolute", alpha_absolute_handler)
# dispatcher.map("/muse/elements/beta_absolute", beta_absolute_handler)
# dispatcher.map("/muse/elements/gamma_absolute", gamma_absolute_handler)
# dispatcher.map("/muse/elements/horseshoe", horseshoe_handler)
# dispatcher.map("/muse/elements/touching_forehead", touching_forehead_handler)
# dispatcher.map("/muse/gyro", gyro_handler)
# dispatcher.map("/muse/acc", accel_handler)
# dispatcher.set_default_handler(default_handler)


def start_blocking_server(ip, port):
server = BlockingOSCUDPServer((ip, port), dispatcher)
server.serve_forever() # Blocks forever

if __name__ == '__main__':
dispatcher = get_dispatcher()
start_blocking_server(IP, PORT)
mfack1317
Posts: 4
Joined: Mon Feb 15, 2021 4:55 am

Re: OSC streaming with Python-OSC

Post by mfack1317 »

Are you sure about the IP address?
mkay1999
Posts: 4
Joined: Fri Mar 12, 2021 3:06 am

Re: OSC streaming with Python-OSC

Post by mkay1999 »

Yes 100% sure
mkay1999
Posts: 4
Joined: Fri Mar 12, 2021 3:06 am

Re: OSC streaming with Python-OSC

Post by mkay1999 »

I thought that it was a firewall issue but I allowed python access so I don't think that is an issue as well
mfack1317
Posts: 4
Joined: Mon Feb 15, 2021 4:55 am

Re: OSC streaming with Python-OSC

Post by mfack1317 »

idk, I found many possible solutions just by googling your error, it might be related to admin rights...
However, to solve my problem I ended up using socket, another python library, which allows for a much more basic use of server/client...give it a try, maybe
ALarini
Posts: 5
Joined: Thu Dec 09, 2021 1:59 pm

Re: OSC streaming with Python-OSC

Post by ALarini »

Hi everybody, sorry to pick this topic up again but I'm having a really strange problem, and I did a lot of research in internet but this topic seems the most accurate place to ask for this.

Problem: my osc server in Python looks like working properly, but when I try to receive from the Muse it seems like not receiving any message.

I'm on Windows, trying to get the Muse2018 data (through Muse Direct) to Python.
I've tried all the code I could find using python-osc, and of course all the examples in this topic, blocking or threading server, but always with the same result.
With each code, if I add myself the client to the code to test the server, messages are received and dispatched correctly.
Same code, no client but listening to muse, and no message seems to arrive. [The code works correctly, any step is followed the same and no error message appears]

I stream on 127.0.0.1. I've tried ~ any 5000/7000 port. [Every time the same: ok with built in client, no messages from muse]
Using same 127.0.0.1 and port, Muse Lab receives everything correctly.
I've tried also "bridging" through Muse Lab, which can also output the data. Same result: data received by Lab, silence from my OSC server.
Oh, the only difference from the codes I've found around is the address to map, which for me starts with Person0/.
But I've tried all the "/Person0/*" that appear also in Muse Lab, and adding default dispatcher as well. Same result.

Sorry but I really can't find anybody reporting the same issue. Maybe I'm missing something really silly :) Would you have any idea? Thank you very much.
Post Reply