Skip to content

Commit

Permalink
feat: freeze link clock for first 3 seconds before syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Jun 19, 2024
1 parent dc57a8e commit 78ff8ca
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions sardine_core/clock/link_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ def __init__(
tempo: NUMBER = 120,
bpb: int = 4,
loop_interval: float = 0.001,
startup_delay: float = 3.0,
):
super().__init__(loop_interval=loop_interval)

self.startup_delay = startup_delay

self._link: Optional[link.Link] = None
self._tick: int = 0
self._beat: int = 0
Expand All @@ -38,6 +41,8 @@ def __init__(
self._framerate: float = 1 / 20
self._paused_link_phase: float = 0.0
self._time_shift: float = 0.0
self._synced: bool = False
self._startup_time: float = 0.0

## VORTEX ################################################

Expand Down Expand Up @@ -114,10 +119,16 @@ def beats_per_bar(self) -> int:

@property
def internal_origin(self) -> float:
if not self._synced:
return 0.0

return self._internal_origin

@property
def internal_time(self) -> float:
if not self._synced:
return 0.0

return self._internal_time + self._time_shift

@property
Expand Down Expand Up @@ -178,18 +189,29 @@ def _capture_link_info(self, *, update_transport: bool):

def before_loop(self):
self._time_shift = 0.0
self._synced = False

self._link = link.Link(self._tempo)
self._link.enabled = True
self._link.startStopSyncEnabled = True

# Set the origin at the start
# Record the initial time so we know how long to wait before syncing
self._capture_link_info(update_transport=False)
self._internal_origin = self.internal_time
self._startup_time = self._internal_time

def loop(self):
self._capture_link_info(update_transport=True)

# Give Link some time to sync before we start reporting time.
# Make sure the origin starts at phase 0.
if (
not self._synced
and self._internal_time - self._startup_time >= self.startup_delay
):
phase_time = self._link_phase * self.beat_duration
self._internal_origin = self._internal_time - phase_time
self._synced = True

if (
DEBUG
and getattr(self, "_pause_check", False)
Expand Down

0 comments on commit 78ff8ca

Please sign in to comment.