8000 AbletonOSCClient: Pass OSC address to handlers · steeltrack/AbletonOSC@ba0822f · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
AbletonOSCClient: Pass OSC address to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Jan 2, 2024
1 parent d24a3d9 commit ba0822f
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions client/client.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, hostname="127.0.0.1", port=REMOTE_PORT, client_port=LOCAL_POR
def handle_osc(self, address, *params):
# print("Received OSC: %s %s" % (address, params))
if address in self.address_handlers:
self.address_handlers[address](params)
self.address_handlers[address](address, params)
if self.verbose:
print(address, params)

Expand All @@ -48,15 +48,38 @@ def stop(self):
def send_message(self,
address: str,
params: Iterable = ()):
"""
Send a message to the given OSC address on the server.
Args:
address (str): The OSC address to send to (e.g. /live/song/set/tempo)
params (Iterable): Optional list of arguments to pass to the OSC message.
"""
self.client.send_message(address, params)

def add_handler(self,
def set_handler(self,
address: str,
fn: Callable = None):
"""
Set the handler for the specified OSC message.
Args:
address (str): The OSC address to listen for (e.g. /live/song/get/tempo)
fn (Callable): The function to trigger when a message received.
Must accept a two arguments:
- str: the OSC address
- tuple: the OSC parameters
"""
self.address_handlers[address] = fn

def remove_handler(self,
address: str):
"""
Remove the handler for the specified OSC message.
Args:
address (str): The OSC address whose handler to remove.
"""
del self.address_handlers[address]

def await_message(self,
Expand All @@ -79,14 +102,14 @@ def await_message(self,
rv = None
_event = threading.Event()

def received_response(params):
def received_response(address, params):
print("Received response: %s %s" % (address, str(params)))
nonlocal rv
nonlocal _event
rv = params
_event.set()

self.add_handler(address, received_response)
self.set_handler(address, received_response)
_event.wait(timeout)
self.remove_handler(address)
if not _event.is_set():
Expand All @@ -100,13 +123,13 @@ def query(self,
rv = None
_event = threading.Event()

def received_response(params):
def received_response(address, params):
nonlocal rv
nonlocal _event
rv = params
_event.set()

self.add_handler(address, received_response)
self.set_handler(address, received_response)
self.send_message(address, params)
_event.wait(timeout)
self.remove_handler(address)
Expand Down

0 comments on commit ba0822f

Please sign in to comment.
0