简体   繁体   English

如何将 OSC 消息从 Pure Data 发送到 Python?

[英]How to send OSC message from Pure Data to Python?

I created a patch on Pure Data to send the word "Hello" via the OSC protocol.我在 Pure Data 上创建了一个补丁,以通过 OSC 协议发送“Hello”一词。 I want this word to appear in a Python script.我希望这个词出现在 Python 脚本中。 Is it possible?可能吗?

you need an OSC-library for python.您需要一个用于 python 的 OSC 库。

among the many possible candidates, there are:在众多可能的候选人中,有:

iirc, the latter two are unmaintained and/or Python2-only (which you should not use anymore), but check yourself. iirc,后两者未维护和/或仅限 Python2(您不应使用),但请自行检查。

I solved it using python-osc library for this code:我使用此代码的python-osc库解决了它:

"""
Code created by Rômulo Vieira, March 2021
inspired and adapted from the standard code of the python-osc library, available at: https://pypi.org/project/python-osc/

The code causes a Pure data patch to send a message in OSC format to the multicast address of the local network.

The final object of this code is to verify when the patch connects to the network and when it disconnects

Free for use, alteration and distribution 
"""

# Module that creates user-friendly and uncomplicated command line applications 
import argparse

# Pythonosc library module that does not require the sender and receiver of the message to be directly connected 
from pythonosc import dispatcher
# Pythonosc library module that imports a server capable of receiving messages in OSC format 
from pythonosc import osc_server

# Creating the basic functionalities of the server, with IP and standard port 

# Checking the scope of execution. If you run the module directly, execute the function, if you import the module, don’t run it
if __name__ == "__main__":
  # Checks the validity of the past argument. That is, if the input data is accepted by the script
  parser = argparse.ArgumentParser()
  # setting the server IP
  parser.add_argument("--ip",
  # The IP must be from the multicast address of the local network 
      default="224.0.0.1", help="The ip to listen on") 
  # setting the server port     
  parser.add_argument("--port",
  # arbitrarily chosen number. It can be any free port on the computer.
      type=int, default=60000, help="The port to listen on")   
  # Analyzes the data entered and prints it on the screen, if they are valid    
  args = parser.parse_args()  
  # The dispatcher maps OSC addresses to functions and calls the functions with the messages’ arguments.
  dispatcher = dispatcher.Dispatcher() 
  #The dispatcher associates addresses with functions by storing them in a mapping
  # An address can contains wildcards as defined in the OSC specifications
  # Call the Dispatcher.map method with an address pattern and a handler callback function
  dispatcher.map("/hello", print, "Hello") # print Pure Data messages on terminal 
  dispatcher.map("/publish/thing", print) 
  dispatcher.map("/publish/input", print)
  dispatcher.map("/publish/output", print)
  dispatcher.map("/goodbye", print)

  # Establishing connection with OSC server
  server = osc_server.ThreadingOSCUDPServer(
  # Reading IP, port and received message   
      (args.ip, args.port), dispatcher)
  # Prints a message on the screen indicating acceptance of the connection. IP and port used are also displayed     
  print("Serving on {}".format(server.server_address))
  # Indicates which server will remain open until it is manually closed by the user
  server.serve_forever()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM