human/Human.py

50 lines
1.2 KiB
Python
Raw Normal View History

2024-09-02 00:13:34 +00:00
#encoding = utf8
2024-09-04 16:51:14 +00:00
import logging
import queue
2024-09-02 00:13:34 +00:00
from queue import Queue
2024-09-04 16:51:14 +00:00
import numpy as np
from tts.Chunk2Mal import Chunk2Mal
logger = logging.getLogger(__name__)
2024-09-02 00:13:34 +00:00
class Human:
def __init__(self):
self._tts = None
2024-09-04 16:51:14 +00:00
self._fps = 25 # 20 ms per frame
self._sample_rate = 16000
self._chunk = self._sample_rate // self._fps # 320 samples per chunk (20ms * 16000 / 1000)
self._chunk_2_mal = Chunk2Mal()
def on_destroy(self):
if self._tts is not None:
self._tts.stop()
logger.info('human destroy')
2024-09-02 00:13:34 +00:00
def set_tts(self, tts):
2024-09-04 16:51:14 +00:00
if self._tts == tts:
return
2024-09-02 00:13:34 +00:00
self._tts = tts
2024-09-04 16:51:14 +00:00
self._tts.start()
def read(self, txt):
if self._tts is None:
logger.warning('tts is none')
return
self._tts.push_txt(txt)
2024-09-02 00:13:34 +00:00
def push_audio_chunk(self, chunk):
2024-09-04 16:51:14 +00:00
self._chunk_2_mal.push_chunk(chunk)
self._audio_chunk_queue.put(chunk)
def pull_audio_chunk(self):
try:
chunk = self._audio_chunk_queue.get(block=True, timeout=1.0)
type = 1
except queue.Empty:
chunk = np.zeros(self._chunk, dtype=np.float32)
type = 0
return chunk, type