diff --git a/tts/tts_audio_handle.py b/tts/tts_audio_handle.py index 25d130a..b64ca95 100644 --- a/tts/tts_audio_handle.py +++ b/tts/tts_audio_handle.py @@ -3,6 +3,7 @@ import heapq import logging import os import shutil +from threading import Lock from eventbus import EventBus from utils import save_wav @@ -28,7 +29,7 @@ class TTSAudioHandle(AudioHandler): self.stop() def on_clear_cache(self, *args, **kwargs): - pass + self._index = -1 @property def sample_rate(self): @@ -58,6 +59,7 @@ class TTSAudioSplitHandle(TTSAudioHandle): self.sample_rate = self._context.sample_rate self._chunk = self.sample_rate // self._context.fps self._priority_queue = [] + self._lock = Lock() self._current = 0 self._is_running = True logger.info("TTSAudioSplitHandle init") @@ -81,26 +83,35 @@ class TTSAudioSplitHandle(TTSAudioHandle): if not self._is_running: return heapq.heappush(self._priority_queue, (index, chunks)) - current = self._priority_queue[0][0] - print('TTSAudioSplitHandle::on_handle', index, current, self._current, len(self._priority_queue)) - if current == self._current: - self._current = self._current + 1 - chunks = heapq.heappop(self._priority_queue)[1] - if chunks is None: - pass - else: - for chunk in chunks: - self.on_next_handle(chunk, 0) + + with self._lock: + current = self._priority_queue[0][0] + if current == 0: + self._current = 0 + print('TTSAudioSplitHandle::on_handle', index, current, self._current, len(self._priority_queue)) + if current == self._current: + self._current = self._current + 1 + chunks = heapq.heappop(self._priority_queue)[1] + if chunks is None: + pass + else: + for chunk in chunks: + self.on_next_handle(chunk, 0) def stop(self): self._is_running = False def on_clear_cache(self, *args, **kwargs): + super().on_clear_cache() if self._priority_queue is None or len(self._priority_queue) == 0: return - current = self._priority_queue[0][0] + + with self._lock: + print('TTSAudioSplitHandle::on_clear_cache', self._current) + self._current = 0 + print('TTSAudioSplitHandle::on_clear_cache', self._current) + self._priority_queue.clear() - self._current = current class TTSAudioSaveHandle(TTSAudioHandle):