Skip to content

Commit

Permalink
Make play_once and play_many be just one function
Browse files Browse the repository at this point in the history
Before when many playbacks were needed, play_many was executed in
its own thread and created a thread for play_once. But now just a
"play_once" thread is created and it calls a playback how many
times are needed
  • Loading branch information
WesleyMPG committed May 3, 2021
1 parent 0d16ad6 commit e6945e6
Showing 1 changed file with 20 additions and 31 deletions.
51 changes: 20 additions & 31 deletions atbswp/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,30 +354,23 @@ class PlayControl:
def __init__(self, count=1, infinite=False):
self.count = count
self.infinite = infinite
self._stop_locks = [True, True] # [once, many]
self._stop_locks = [True]
self._current_count = 0
self._play_once_thread = None
self._play_many_thread = None
self._play_thread = None

def _play_once(self, capture, append_function=None):
def _play(self, capture, append_function=None):
for line in capture:
if self._stop_locks[0]: break
if self.is_stoped(): break
exec(line)
self._stop_locks[0] = True
if append_function:
append_function()

def _play_many(self, capture, append_function=None):
self.current_count = 0
while (self.current_count < self.count or self.infinite)\
and not self._stop_locks[1]:
self._stop_locks[0] = False
self._play_once_thread = self._start_thread(self._play_once, capture)
self._play_once_thread.join()
self.current_count += 1
self._stop_locks = [True, True]
if append_function:
append_function()
self._current_count += 1
print('starting again')
if self._current_count < self.count or self.infinite \
and not self.is_stoped():
self._play(capture, append_function)
else:
self.stop()
if append_function:
append_function()

def _start_thread(self, target, *args, daemon=False):
thread = Thread(target=target, args=(*args,))
Expand All @@ -392,10 +385,10 @@ def set_config(self, count, infinite):

def get_current_count(self):
"""Returns the current playback time"""
return current_count
return self._current_count

def is_stoped(self):
return self._stop_locks[0] and self._stop_locks[1]
return self._stop_locks[0]

def play(self, capture, append_function=None):
"""Starts playback according with configs setted
Expand All @@ -404,17 +397,13 @@ def play(self, capture, append_function=None):
:param append_function: a function to be executed after
a playback
"""
if self.infinite or self.count > 1:
self._stop_locks = [False, False]
self._start_thread(self._play_many, capture,
append_function)
else:
self._stop_locks[0] = False
self._start_thread(self._play_once, capture,
append_function)
self._stop_locks[0] = False
self._current_count = 0
self._play_thread = self._start_thread(self._play, capture,
append_function)

def stop(self):
self._stop_locks = [True, True]
self._stop_locks[0] = True


class PlayInterface:
Expand Down

0 comments on commit e6945e6

Please sign in to comment.