human/tts/TTSBase.py

54 lines
1.1 KiB
Python
Raw Normal View History

2024-09-02 00:13:34 +00:00
#encoding = utf8
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):
while not self._exit_event.is_set():
try:
txt = self._queue.get(block=True, timeout=1)
except queue.Empty:
continue
self._request(txt)
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()
def stop(self):
if self._exit_event is None:
return
self._exit_event.set()
self._thread.join()
def clear(self):
self._queue.queue.clear()
def push_txt(self, txt):
self._queue.put(txt)