Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix unsync bug #233

Merged
merged 4 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sardine_core/clock/internal_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def internal_origin(self, origin: float):

@tempo.setter
def tempo(self, new_tempo: NUMBER):
self.env.dispatch('tempo_change', self.tempo, new_tempo)
new_tempo = float(new_tempo)

if not 1 <= new_tempo <= 999:
Expand Down
1 change: 1 addition & 0 deletions sardine_core/clock/link_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def internal_origin(self, origin: float):

@tempo.setter
def tempo(self, new_tempo: float) -> None:
self.env.dispatch('tempo_change', self.tempo, new_tempo)
if self._link is not None:
session = self._link.captureSessionState()
session.setTempo(new_tempo, self.beats_per_bar)
Expand Down
7 changes: 6 additions & 1 deletion sardine_core/scheduler/async_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,11 @@ def _correct_interval(self, period: Union[float, int]):
self._can_correct_interval = False

def _correct_interval_background_job(self, period: Union[float, int]):
"""Alternative version for fixed-rate background jobs"""
"""
Alternative version for fixed-rate background jobs. The interval or
period is not indexed on the clock like with the _correct_interval
method above.
"""
interval = period
if self._can_correct_interval and interval != self._last_interval:
time = self._expected_time
Expand Down Expand Up @@ -501,6 +505,7 @@ async def _runner(self):

try:
while self._is_ready_for_iteration():
#self._last_interval = self._get_period(self._last_state) * self.clock.beat_duration
try:
await self._run_once()
except Exception as exc:
Expand Down
14 changes: 14 additions & 0 deletions sardine_core/scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def __init__(
self._runners: dict[str, AsyncRunner] = {}
self.deferred = deferred_scheduling

def _react_to_tempo_change(self, old_tempo: int|float, new_tempo: int|float):
"""
In reaction to a tempo change, the scheduler should
trigger an event for each runner prompting to update
their current interval_shift. This prevents runners
from desynchronization everytime tempo change happens.
"""
scale = old_tempo / new_tempo
for runner in self._runners.values():
runner.interval_shift *= scale

def __repr__(self) -> str:
n_runners = len(self._runners)
return "<{} ({} {}) deferred={}>".format(
Expand Down Expand Up @@ -118,7 +129,10 @@ def _reload_runners(self, *, interval_correction: bool):

def setup(self):
self.register("stop")
self.register("tempo_change")

def hook(self, event: str, *args):
if event == "stop":
self.reset()
if event == "tempo_change":
self._react_to_tempo_change(*args)
Loading