#encoding = utf8 import logging import queue from io import BytesIO from queue import Queue from threading import Thread, Event class TTSBase: def __init__(self, human): self._human = human self._thread = None self._queue = Queue() self._exit_event = None self._io_stream = BytesIO() self._fps = 50 self._sample_rate = 16000 self._chunk = self._sample_rate // self._fps def _on_run(self): logging.info('tts run') while not self._exit_event.is_set(): try: txt = self._queue.get(block=True, timeout=1) except queue.Empty: continue self._request(txt) logging.info('tts exit') def _request(self, txt): pass def start(self): if self._exit_event is not None: return self._exit_event = Event() self._thread = Thread(target=self._on_run) self._thread.start() logging.info('tts start') def stop(self): if self._exit_event is None: return self._exit_event.set() self._thread.join() logging.info('tts stop') def clear(self): self._queue.queue.clear() def push_txt(self, txt): self._queue.put(txt)