Skip to content

Commit

Permalink
fix blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
mscheltienne committed Oct 1, 2024
1 parent 064d76b commit 391043b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stimuli/audio/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _set_signal(self, signal: NDArray) -> None:

@copy_doc(BaseBackend.play)
def play(self, when: float | None = None, *, blocking: bool = False) -> None:
self._backend.play(when=when)
self._backend.play(when=when, blocking=blocking)

def plot(self) -> tuple[Figure, Axes]:
"""Plot the audio signal waveform.
Expand Down
9 changes: 8 additions & 1 deletion stimuli/audio/backend/sounddevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ def play(self, when: float | None = None, *, blocking: bool = False) -> None:
else self._clock.get_time_ns() + int(when * 1e9)
)
if blocking:
sleep(when + self._duration)
wait = self._duration
if when is not None:
wait += when
sleep(0.95 * wait) # 5% margin
while self._target_time is not None:
# hog CPU for the last blocking part, based on _target_time and not on
# an estimated duration as sleep(...) does.
pass

@copy_doc(BaseBackend.stop)
def stop(self) -> None:
Expand Down

0 comments on commit 391043b

Please sign in to comment.