Skip to content

Commit

Permalink
debug: add debugging for phase deviation in link clock
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Jun 19, 2024
1 parent 39f034a commit 8f880e4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sardine_core/clock/link_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

__all__ = ("LinkClock",)

DEBUG = False


class LinkClock(BaseThreadedLoopMixin, BaseClock):
def __init__(
Expand Down Expand Up @@ -190,6 +192,20 @@ def before_loop(self):
def loop(self):
self._capture_link_info(update_transport=True)

if (
DEBUG
and getattr(self, "_pause_check", False)
and self.time >= self._pause_state[0]
):
self._pause_check = False
pause_time, pause_phase = self._pause_state
deviation = self._link_phase - pause_phase
print(
f"({pause_time:.3f}, {pause_phase:.3f}) -> "
f"({self.time:.3f}, {self._link_phase:.3f}), "
f"{deviation = :.3f}"
)

def after_loop(self):
self._link = None

Expand All @@ -202,13 +218,22 @@ def hook(self, event: str, *args):
# Remember the current phase so the next time the clock is resumed,
# we can rewind time so the phase appears continuous.
self._paused_link_phase = self._link_phase

if DEBUG:
self._pause_check = False
self._pause_state = (self.time, self._link_phase)

elif event == "resume":
# Alternative formula: (-bpb + lp - plp) % -bpb
delta = (self._link_phase - self._paused_link_phase) % self.beats_per_bar
if delta > 0:
# Don't allow time to jump forward, rewind instead.
delta -= self.beats_per_bar

if DEBUG:
self._pause_check = True
print(f"Time shifting by: {delta * self.beat_duration:.3f}")

self._time_shift += delta * self.beat_duration

if self._last_capture is not None:
Expand Down

0 comments on commit 8f880e4

Please sign in to comment.