modify audio handler

This commit is contained in:
brige 2024-11-05 22:06:02 +08:00
parent 4998d0b422
commit 1e1967ef93
5 changed files with 18 additions and 10 deletions

View File

@ -65,7 +65,7 @@ class SherpaNcnnAsr(AsrBase):
self._notify_complete('介绍中国5000年历史文学')
logger.info(f'_recognize_loop111')
segment_id += 1
time.sleep(5)
time.sleep(50)
logger.info(f'_recognize_loop222')
logger.info(f'_recognize_loop exit')
'''

View File

@ -18,13 +18,6 @@ class AudioHandler(ABC):
if self._handler is not None:
self._handler.on_message(message)
# @abstractmethod
# def pause_handle(self):
# if self._handler is not None:
# self._handler.pause_handle()
# else:
# logging.info(f'_handler is None')
@abstractmethod
def stop(self):
pass

View File

@ -13,6 +13,7 @@ class NLPBase(AsrObserver):
self._context = context
self._split_handle = split
self._callback = callback
self._is_running = False
@property
def callback(self):
@ -23,7 +24,7 @@ class NLPBase(AsrObserver):
self._callback = value
def _on_callback(self, txt: str):
if self._callback is not None:
if self._callback is not None and self._is_running:
self._callback.on_message(txt)
async def _request(self, question):
@ -42,14 +43,18 @@ class NLPBase(AsrObserver):
def ask(self, question):
logger.info(f'ask:{question}')
self._is_running = True
self._ask_queue.add_task(self._request, question)
logger.info(f'ask:{question} completed')
def stop(self):
logger.info('NLPBase stop')
self._is_running = False
self._ask_queue.add_task(self._on_close)
self._ask_queue.stop()
def pause_talk(self):
logger.info('NLPBase pause_talk')
self._is_running = False
self._ask_queue.clear()

View File

@ -13,6 +13,7 @@ class TTSBase(NLPCallback):
def __init__(self, handle):
self._handle = handle
self._message_queue = AsyncTaskQueue(5)
self._is_running = False
@property
def handle(self):
@ -30,7 +31,10 @@ class TTSBase(NLPCallback):
print(f'-------stream is None')
return
print(f'-------tts time:{time.time() - t:.4f}s')
await self._on_handle(stream, index)
if self._handle is not None and self._is_running:
await self._on_handle(stream, index)
else:
logger.info(f'handle is None, running:{self._is_running}')
async def _on_request(self, text: str):
pass
@ -45,6 +49,7 @@ class TTSBase(NLPCallback):
self.message(txt)
def message(self, txt):
self._is_running = True
txt = txt.strip()
if len(txt) == 0:
logger.info(f'message is empty')
@ -61,4 +66,6 @@ class TTSBase(NLPCallback):
self._message_queue.stop()
def pause_talk(self):
logger.info(f'TTSBase pause_talk')
self._is_running = False
self._message_queue.clear()

3
ui/__init__.py Normal file
View File

@ -0,0 +1,3 @@
#encoding = utf8