这是完成(至少部分)工作的代码。
请记住,在这种情况下,Max 将字符串发送到 python。我收到的消息是这样的:O-O e8g8 8 1
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+'whatever'
# 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)
此代码从客户端获取消息,如下所示:
from pythonosc import dispatcher
from pythonosc import osc_server
from pythonosc import udp_client
from typing import List, Any
def talk2SC(ip,port,path):
'''
set up client
'''
# maxmsg = 'Nxe6 f4e6'
maxmsg = 'e4 e2e4'
client = udp_client.SimpleUDPClient(ip,port)
client.send_message(path, maxmsg)
if __name__ == '__main__':
talk2SC("127.0.0.1",5005,"/filter")
并将消息发送到这样的服务器:
from pythonosc import dispatcher
from pythonosc import osc_server
from pythonosc import udp_client
from typing import List, Any
def main(address: str, *osc_arguments):
'''
prende il messaggio di MAX e lo traduce aggiornando la scacchiera
'''
# read message
msg = osc_arguments[0]
# initialize board
print(osc_arguments)
# outputc
def listen2Max(ip,port,path):
'''
set up server
'''
# dispatcher to receive message
disp = dispatcher.Dispatcher()
handler = disp.map(path, main)
# print(handler)
# server to listen
server = osc_server.ThreadingOSCUDPServer((ip,port), disp)
print("Serving on {}".format(server.server_address))
server.serve_forever()
if __name__ == '__main__':
listen2Max("127.0.0.1",5006,"/chmove")
棘手的部分是让它与 Max 通信。幸运的是,我在这里找到了答案:
- 将每个 Max 消息格式化为
/path "O-O e8g8 8 1"
- 让python监听指定的
/path
如果希望 python 监听每条路径,可以更改解析器项中的默认路径并"*"
用作默认路径。然后
- Max 的消息可以有任何路径
- 在解析器中将有
parser.add_argument("-UI", "--uripathIN", type=str, default="*", help="MAX's URI path")
现在,我只需要修复 SuperCollider 部分。