Skip to content

Commit

Permalink
Merge pull request #248 from ssidorenko/osc-nudge-argschem
Browse files Browse the repository at this point in the history
Add nudge to OSC sender and allow selecting argscheme
  • Loading branch information
Bubobubobubobubo authored Sep 16, 2023
2 parents 99d6d03 + e3cdcad commit cfbb800
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
4 changes: 0 additions & 4 deletions sardine_core/handlers/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ def nudge(self):
def nudge(self, nudge):
self._nudge = nudge

def call_timed_with_nudge(self, deadline, method, **kwargs):
"""Applying nudge to call_timed method"""
return self.call_timed(deadline + self.nudge, method, **kwargs)

def __repr__(self) -> str:
return f"<{type(self).__name__} port={self._port_name!r} nudge={self._nudge}>"

Expand Down
40 changes: 32 additions & 8 deletions sardine_core/handlers/osc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
port: int = 23456,
name: str = "OSCSender",
ahead_amount: float = 0.0,
nudge: float = 0.0,
):
super().__init__()
self.loop = loop
Expand All @@ -31,6 +32,7 @@ def __init__(
self.client = osc_udp_client(address=self._ip, port=self._port, name=self._name)
self._events = {"send": self._send}
self._defaults: dict = {}
self.nudge = nudge

loop.add_child(self, setup=True)

Expand All @@ -51,20 +53,42 @@ def defaults(self):
return self._defaults

def _send(self, address: str, message: list) -> None:
msg = oscbuildparse.OSCMessage(address, None, message)
bun = oscbuildparse.OSCBundle(
oscbuildparse.unixtime2timetag(time.time() + self._ahead_amount),
[msg],
)
bun = self._make_bundle([[address, message]])
osc_send(bun, self._name)

def _send_bundle(self, messages: list) -> None:
bun = self._make_bundle(messages)
osc_send(bun, self._name)

def send_raw(self, address: str, message: list) -> None:
def send_raw(self, address: str, message: list, nudge=False) -> None:
"""
Public alias for the _send function. It can sometimes be useful to have it
when we do want to write some raw OSC message without formatting it in the
expected SuperDirt format.
"""
self._send(address, message)
if nudge:
self.call_timed_with_nudge(
self.env.clock.shifted_time, self._send, address, message
)
else:
self._send(address, message)

def send_raw_bundle(self, messages: list, nudge=False) -> None:
if nudge:
self.call_timed_with_nudge(
self.env.clock.shifted_time, self._send_bundle, messages
)
else:
self._send_bundle(messages)

def _make_bundle(self, messages: list) -> oscbuildparse.OSCBundle:
return oscbuildparse.OSCBundle(
oscbuildparse.unixtime2timetag(time.time() + self._ahead_amount),
[
oscbuildparse.OSCMessage(message[0], None, message[1])
for message in messages
],
)

@alias_param(name="iterator", alias="i")
@alias_param(name="divisor", alias="d")
Expand Down Expand Up @@ -97,4 +121,4 @@ def send(
serialized = list(chain(*sorted(message.items())))
else:
serialized = list(chain(*message.items()))
self.call_timed(deadline, self._send, f"/{address}", serialized)
self.call_timed_with_nudge(deadline, self._send, f"/{address}", serialized)
10 changes: 8 additions & 2 deletions sardine_core/handlers/osc_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def watch(self, address: str):
print(f"[yellow]Watching address [red]{address}[/red].[/yellow]")
self._generic_store(address)

def attach(self, address: str, function: Callable, watch: bool = False):
def attach(
self, address: str, function: Callable, watch: bool = False, argscheme=None
):
"""
Attach a callback to a given address. You can also toggle the watch
boolean value to tell if the value should be tracked by the receiver.
Expand All @@ -81,7 +83,11 @@ def attach(self, address: str, function: Callable, watch: bool = False):
print(
f"[yellow]Attaching function [red]{function.__name__}[/red] to address [red]{address}[/red][/yellow]"
)
osc_method(address, function)
osc_method(
address,
function,
argscheme=OSCARG_DATAUNPACK if argscheme is None else argscheme,
)
if watch:
self.watch(address)

Expand Down
4 changes: 4 additions & 0 deletions sardine_core/handlers/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ async def scheduled_func():
self._timed_tasks.add(task)
task.add_done_callback(self._timed_tasks.discard)

def call_timed_with_nudge(self, deadline, method, *args, **kwargs):
"""Applying nudge to call_timed method"""
return self.call_timed(deadline + self.nudge, method, *args, **kwargs)

@staticmethod
def pattern_element(
val: RecursiveElement,
Expand Down

0 comments on commit cfbb800

Please sign in to comment.