Skip to content

Commit

Permalink
fix audio getting stuck (2/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dae committed Mar 14, 2020
1 parent f30853f commit 0ecc189
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
38 changes: 16 additions & 22 deletions qt/aqt/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ def retryWait(proc: subprocess.Popen) -> int:
##########################################################################


class PlayerInterrupted(Exception):
pass


class SimpleProcessPlayer(Player): # pylint: disable=abstract-method
"A player that invokes a new process for each tag to play."

Expand All @@ -231,7 +227,6 @@ def __init__(self, taskman: TaskManager) -> None:
self._taskman = taskman
self._terminate_flag = False
self._process: Optional[subprocess.Popen] = None
self._lock = threading.Lock()

def play(self, tag: AVTag, on_done: OnDoneCallback) -> None:
self._terminate_flag = False
Expand Down Expand Up @@ -260,23 +255,22 @@ def _wait_for_termination(self, tag: AVTag) -> None:
)

while True:
with self._lock:
# should we abort playing?
if self._terminate_flag:
self._process.terminate()
self._process = None
return

# wait for completion
try:
self._process.wait(0.1)
if self._process.returncode != 0:
print(f"player got return code: {self._process.returncode}")
self._process = None
return
except subprocess.TimeoutExpired:
# process still running, repeat loop
pass
# should we abort playing?
if self._terminate_flag:
self._process.terminate()
self._process = None
return

# wait for completion
try:
self._process.wait(0.1)
if self._process.returncode != 0:
print(f"player got return code: {self._process.returncode}")
self._process = None
return
except subprocess.TimeoutExpired:
# process still running, repeat loop
pass

def _on_done(self, ret: Future, cb: OnDoneCallback) -> None:
try:
Expand Down
8 changes: 2 additions & 6 deletions qt/aqt/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from anki.sound import AVTag, TTSTag
from anki.utils import checksum, isWin, tmpdir
from aqt import gui_hooks
from aqt.sound import OnDoneCallback, PlayerInterrupted, SimpleProcessPlayer
from aqt.sound import OnDoneCallback, SimpleProcessPlayer


@dataclass
Expand Down Expand Up @@ -234,11 +234,7 @@ def _play(self, tag: AVTag) -> None:
self._wait_for_termination(tag)

def _on_done(self, ret: Future, cb: OnDoneCallback) -> None:
try:
ret.result()
except PlayerInterrupted:
# don't fire done callback when interrupted
return
ret.result()

# inject file into the top of the audio queue
from aqt.sound import av_player
Expand Down

0 comments on commit 0ecc189

Please sign in to comment.