modify sync block

This commit is contained in:
jiegeaiai 2024-11-07 08:26:03 +08:00
parent ebbe8f2989
commit c8fc8097e7
8 changed files with 49 additions and 24 deletions

View File

@ -70,14 +70,17 @@ class AudioInferenceHandler(AudioHandler):
logger.info(f'use device:{device}') logger.info(f'use device:{device}')
while self._is_running: while self._is_running:
print('AudioInferenceHandler mel_batch:000')
if self._exit_event.is_set(): if self._exit_event.is_set():
start_time = time.perf_counter() start_time = time.perf_counter()
batch_size = self._context.batch_size batch_size = self._context.batch_size
try: try:
mel_batch = self._mal_queue.get() print('AudioInferenceHandler mel_batch:')
size = self._audio_queue.size() mel_batch = self._mal_queue.get(timeout=0.03)
print('AudioInferenceHandler mel_batch:111')
# print('AudioInferenceHandler mel_batch:', len(mel_batch), 'size:', size) # print('AudioInferenceHandler mel_batch:', len(mel_batch), 'size:', size)
except queue.Empty: except queue.Empty:
print('AudioInferenceHandler mel_batch:111')
continue continue
# print('origin mel_batch:', len(mel_batch)) # print('origin mel_batch:', len(mel_batch))
@ -91,12 +94,18 @@ class AudioInferenceHandler(AudioHandler):
is_all_silence = False is_all_silence = False
if not self._is_running: if not self._is_running:
return print('AudioInferenceHandler not running')
break
if is_all_silence: if is_all_silence:
for i in range(batch_size): for i in range(batch_size):
if not self._is_running:
print('AudioInferenceHandler not running1111')
break
print('AudioInferenceHandler is_all_silence 111')
self.on_next_handle((None, mirror_index(length, index), audio_frames[i * 2:i * 2 + 2]), self.on_next_handle((None, mirror_index(length, index), audio_frames[i * 2:i * 2 + 2]),
0) 0)
print('AudioInferenceHandler is_all_silence 222')
index = index + 1 index = index + 1
else: else:
logger.info('infer=======') logger.info('infer=======')
@ -135,12 +144,15 @@ class AudioInferenceHandler(AudioHandler):
count_time = 0 count_time = 0
for i, res_frame in enumerate(pred): for i, res_frame in enumerate(pred):
if not self._is_running:
break
self.on_next_handle( self.on_next_handle(
(res_frame, mirror_index(length, index), audio_frames[i * 2:i * 2 + 2]), (res_frame, mirror_index(length, index), audio_frames[i * 2:i * 2 + 2]),
0) 0)
index = index + 1 index = index + 1
logger.info(f'total batch time: {time.perf_counter() - start_time}') logger.info(f'total batch time: {time.perf_counter() - start_time}')
else: else:
print('AudioInferenceHandler mel_batch:333')
time.sleep(1) time.sleep(1)
break break
logger.info('AudioInferenceHandler inference processor stop') logger.info('AudioInferenceHandler inference processor stop')
@ -149,7 +161,10 @@ class AudioInferenceHandler(AudioHandler):
logger.info('AudioInferenceHandler stop') logger.info('AudioInferenceHandler stop')
self._is_running = False self._is_running = False
self._exit_event.clear() self._exit_event.clear()
# self._run_thread.join() if self._run_thread.is_alive():
print('AudioInferenceHandler stop join')
self._run_thread.join()
print('AudioInferenceHandler stop exit')
def pause_talk(self): def pause_talk(self):
print('AudioInferenceHandler pause_talk', self._audio_queue.size(), self._mal_queue.size()) print('AudioInferenceHandler pause_talk', self._audio_queue.size(), self._mal_queue.size())

View File

@ -92,11 +92,10 @@ class AudioMalHandler(AudioHandler):
self.frames = self.frames[-(self._context.stride_left_size + self._context.stride_right_size):] self.frames = self.frames[-(self._context.stride_left_size + self._context.stride_right_size):]
def get_audio_frame(self): def get_audio_frame(self):
try: if not self._queue.is_empty():
# print('AudioMalHandler get_audio_frame')
frame = self._queue.get() frame = self._queue.get()
type_ = 0 type_ = 0
except queue.Empty: else:
frame = np.zeros(self.chunk, dtype=np.float32) frame = np.zeros(self.chunk, dtype=np.float32)
type_ = 1 type_ = 1
# print('AudioMalHandler get_audio_frame type:', type_) # print('AudioMalHandler get_audio_frame type:', type_)

View File

@ -48,7 +48,6 @@ class HumanRender(AudioHandler):
logging.info('human render exit') logging.info('human render exit')
def _run_step(self): def _run_step(self):
try: try:
value = self._queue.get(timeout=.005) value = self._queue.get(timeout=.005)
if value is None: if value is None:
@ -85,6 +84,10 @@ class HumanRender(AudioHandler):
super().on_message(message) super().on_message(message)
def on_handle(self, stream, index): def on_handle(self, stream, index):
print('human render:', self._is_running)
if not self._is_running:
return
self._queue.put(stream) self._queue.put(stream)
# res_frame, idx, audio_frames = stream # res_frame, idx, audio_frames = stream
# self._voice_render.put(audio_frames, self._last_audio_ps) # self._voice_render.put(audio_frames, self._last_audio_ps)
@ -112,6 +115,7 @@ class HumanRender(AudioHandler):
if self._exit_event is None: if self._exit_event is None:
return return
self._queue.clear()
self._exit_event.clear() self._exit_event.clear()
if self._thread.is_alive(): if self._thread.is_alive():
self._thread.join() self._thread.join()

View File

@ -10,18 +10,18 @@ logger = logging.getLogger(__name__)
class NLPBase(AsrObserver): class NLPBase(AsrObserver):
def __init__(self, context, split, callback=None): def __init__(self, context, split, callback=None):
self._ask_queue = AsyncTaskQueue() self._ask_queue = AsyncTaskQueue('NLPBaseQueue')
self._context = context self._context = context
self._split_handle = split self._split_handle = split
self._callback = callback self._callback = callback
self._is_running = False self._is_running = False
EventBus().register('stop', self.onStop) EventBus().register('stop', self.on_stop)
def __del__(self): def __del__(self):
EventBus().unregister('stop', self.onStop) EventBus().unregister('stop', self.on_stop)
def onStop(self, *args, **kwargs): def on_stop(self, *args, **kwargs):
self.stop() self.stop()
@property @property

View File

@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
class TTSBase(NLPCallback): class TTSBase(NLPCallback):
def __init__(self, handle): def __init__(self, handle):
self._handle = handle self._handle = handle
self._message_queue = AsyncTaskQueue(5) self._message_queue = AsyncTaskQueue('TTSBaseQueue', 5)
self._is_running = False self._is_running = False
@property @property

View File

@ -60,7 +60,7 @@ class PyGameUI:
def stop(self): def stop(self):
logger.info('stop') logger.info('stop')
if self._human_context is not None: if self._human_context is not None:
self._human_context.pause_talk() # self._human_context.pause_talk()
self._human_context.stop() self._human_context.stop()
def on_render(self, image): def on_render(self, image):

View File

@ -5,45 +5,49 @@ import threading
class AsyncTaskQueue: class AsyncTaskQueue:
def __init__(self, work_num=1): def __init__(self, name, work_num=1):
self._queue = asyncio.Queue() self._queue = asyncio.Queue()
self._worker_num = work_num self._worker_num = work_num
self._current_worker_num = work_num self._current_worker_num = work_num
self._thread = threading.Thread(target=self._run_loop) self._name = name
self._thread = threading.Thread(target=self._run_loop, name=name)
self._thread.start() self._thread.start()
self.__loop = None self.__loop = None
def _run_loop(self): def _run_loop(self):
print('_run_loop') print(self._name, '_run_loop')
self.__loop = asyncio.new_event_loop() self.__loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.__loop) asyncio.set_event_loop(self.__loop)
self._tasks = [self.__loop.create_task(self._worker()) for _ in range(self._worker_num)] self._tasks = [self.__loop.create_task(self._worker()) for _ in range(self._worker_num)]
self.__loop.run_forever() self.__loop.run_forever()
print("exit run") print(self._name, "exit run")
if not self.__loop.is_closed(): if not self.__loop.is_closed():
self.__loop.close() self.__loop.close()
async def _worker(self): async def _worker(self):
print('_worker') print(self._name, '_worker')
while True: while True:
print(f'{self._name} get queue')
task = await self._queue.get() task = await self._queue.get()
print(f"Get task size: {self._queue.qsize()}") print(f'{self._name} get queue11')
print(f"{self._name} Get task size: {self._queue.qsize()}")
if task is None: # None as a stop signal if task is None: # None as a stop signal
break break
func, *args = task # Unpack task func, *args = task # Unpack task
print(f"Executing task with args: {args}") print(f"{self._name}, Executing task with args: {args}")
await func(*args) # Execute async function await func(*args) # Execute async function
self._queue.task_done() self._queue.task_done()
print('_worker finish') print(self._name, '_worker finish')
self._current_worker_num -= 1 self._current_worker_num -= 1
if self._current_worker_num == 0: if self._current_worker_num == 0:
print('loop stop') print(self._name, 'loop stop')
self.__loop.stop() self.__loop.stop()
def add_task(self, func, *args): def add_task(self, func, *args):
return self.__loop.call_soon_threadsafe(self._queue.put_nowait, (func, *args)) # return self.__loop.call_soon_threadsafe(self._queue.put_nowait, (func, *args))
self._queue.put_nowait((func, *args))
def stop_workers(self): def stop_workers(self):
for _ in range(self._worker_num): for _ in range(self._worker_num):

View File

@ -10,6 +10,9 @@ class SyncQueue:
self._queue = Queue(maxsize) self._queue = Queue(maxsize)
self._condition = threading.Condition() self._condition = threading.Condition()
def is_empty(self):
return self._queue.empty()
def put(self, item): def put(self, item):
with self._condition: with self._condition:
while self._queue.full(): while self._queue.full():