liumaolin
commited on
Commit
·
f5226c0
1
Parent(s):
4895dc2
Standardize service lifecycle management by replacing `stop` with `exit` and introducing `is_exited` check
Browse files- Replace `stopped` with `is_exited` across all services for consistency and clarity.
- Add `_exit_event` in `core/base.py` for improved control over service termination.
- Update related methods and properties across audio, text, and speech services.
- Refactor system routes to align with new service lifecycle methods.
- src/voice_dialogue/api/core/service_manager.py +1 -1
- src/voice_dialogue/api/routes/system_routes.py +1 -1
- src/voice_dialogue/core/base.py +18 -5
- src/voice_dialogue/services/audio/capture.py +1 -1
- src/voice_dialogue/services/audio/generator.py +1 -1
- src/voice_dialogue/services/audio/player.py +5 -3
- src/voice_dialogue/services/speech/monitor.py +1 -1
- src/voice_dialogue/services/speech/recognizer.py +1 -1
- src/voice_dialogue/services/text/generator.py +1 -1
src/voice_dialogue/api/core/service_manager.py
CHANGED
|
@@ -115,7 +115,7 @@ class ServiceManager:
|
|
| 115 |
service = self.services[service_name]
|
| 116 |
logger.info(f"正在停止服务: {service_name}")
|
| 117 |
|
| 118 |
-
service.
|
| 119 |
|
| 120 |
# 等待服务停止(最多等待5秒)
|
| 121 |
timeout = 5
|
|
|
|
| 115 |
service = self.services[service_name]
|
| 116 |
logger.info(f"正在停止服务: {service_name}")
|
| 117 |
|
| 118 |
+
service.exit()
|
| 119 |
|
| 120 |
# 等待服务停止(最多等待5秒)
|
| 121 |
timeout = 5
|
src/voice_dialogue/api/routes/system_routes.py
CHANGED
|
@@ -120,7 +120,7 @@ async def stop_system(request: Request):
|
|
| 120 |
audio_capture_service = service_manager.get_service("audio_capture")
|
| 121 |
if audio_capture_service:
|
| 122 |
try:
|
| 123 |
-
audio_capture_service.
|
| 124 |
logger.info("音频捕获服务已停止")
|
| 125 |
|
| 126 |
# 等待服务停止
|
|
|
|
| 120 |
audio_capture_service = service_manager.get_service("audio_capture")
|
| 121 |
if audio_capture_service:
|
| 122 |
try:
|
| 123 |
+
audio_capture_service.exit()
|
| 124 |
logger.info("音频捕获服务已停止")
|
| 125 |
|
| 126 |
# 等待服务停止
|
src/voice_dialogue/core/base.py
CHANGED
|
@@ -5,14 +5,17 @@ class BaseThread(threading.Thread):
|
|
| 5 |
|
| 6 |
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None):
|
| 7 |
super().__init__(group, target, name, args, kwargs, daemon=daemon)
|
| 8 |
-
|
|
|
|
| 9 |
self._is_ready_event = threading.Event()
|
|
|
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
self.
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
@property
|
| 18 |
def is_ready(self):
|
|
@@ -24,3 +27,13 @@ class BaseThread(threading.Thread):
|
|
| 24 |
self._is_ready_event.set()
|
| 25 |
else:
|
| 26 |
self._is_ready_event.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None):
|
| 7 |
super().__init__(group, target, name, args, kwargs, daemon=daemon)
|
| 8 |
+
|
| 9 |
+
self._exit_event = threading.Event()
|
| 10 |
self._is_ready_event = threading.Event()
|
| 11 |
+
self._stop_event = threading.Event()
|
| 12 |
|
| 13 |
+
def exit(self):
|
| 14 |
+
self._exit_event.set()
|
| 15 |
|
| 16 |
+
@property
|
| 17 |
+
def is_exited(self):
|
| 18 |
+
return self._exit_event.is_set()
|
| 19 |
|
| 20 |
@property
|
| 21 |
def is_ready(self):
|
|
|
|
| 27 |
self._is_ready_event.set()
|
| 28 |
else:
|
| 29 |
self._is_ready_event.clear()
|
| 30 |
+
|
| 31 |
+
def stop(self):
|
| 32 |
+
self._stop_event.set()
|
| 33 |
+
|
| 34 |
+
@property
|
| 35 |
+
def is_stopped(self):
|
| 36 |
+
return self._stop_event.is_set()
|
| 37 |
+
|
| 38 |
+
def resume(self):
|
| 39 |
+
self._stop_event.clear()
|
src/voice_dialogue/services/audio/capture.py
CHANGED
|
@@ -47,7 +47,7 @@ class EchoCancellingAudioCapture(BaseThread):
|
|
| 47 |
self.is_ready = True
|
| 48 |
|
| 49 |
try:
|
| 50 |
-
while not self.
|
| 51 |
size = ctypes.c_int(0)
|
| 52 |
is_voice_active = ctypes.c_bool(False)
|
| 53 |
# 获取音频数据
|
|
|
|
| 47 |
self.is_ready = True
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
while not self.is_exited:
|
| 51 |
size = ctypes.c_int(0)
|
| 52 |
is_voice_active = ctypes.c_bool(False)
|
| 53 |
# 获取音频数据
|
src/voice_dialogue/services/audio/generator.py
CHANGED
|
@@ -57,7 +57,7 @@ class TTSAudioGenerator(BaseThread):
|
|
| 57 |
|
| 58 |
self.is_ready = True
|
| 59 |
|
| 60 |
-
while not self.
|
| 61 |
try:
|
| 62 |
voice_task: VoiceTask = self.text_input_queue.get(block=False, timeout=0.1)
|
| 63 |
except Empty:
|
|
|
|
| 57 |
|
| 58 |
self.is_ready = True
|
| 59 |
|
| 60 |
+
while not self.is_exited:
|
| 61 |
try:
|
| 62 |
voice_task: VoiceTask = self.text_input_queue.get(block=False, timeout=0.1)
|
| 63 |
except Empty:
|
src/voice_dialogue/services/audio/player.py
CHANGED
|
@@ -29,7 +29,7 @@ class AudioStreamPlayer(BaseThread):
|
|
| 29 |
def run(self):
|
| 30 |
self.is_ready = True
|
| 31 |
|
| 32 |
-
while not self.
|
| 33 |
|
| 34 |
try:
|
| 35 |
voice_task: VoiceTask = self.audio_playing_queue.get(block=False, timeout=0.1)
|
|
@@ -82,8 +82,10 @@ class AudioStreamPlayer(BaseThread):
|
|
| 82 |
|
| 83 |
voice_state_manager.set_audio_playing(task_id)
|
| 84 |
voice_state_manager.reset_task_id()
|
| 85 |
-
|
| 86 |
-
self.
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if self.audio_playing_queue.empty():
|
| 89 |
print(f'回答播放完了')
|
|
|
|
| 29 |
def run(self):
|
| 30 |
self.is_ready = True
|
| 31 |
|
| 32 |
+
while not self.is_exited:
|
| 33 |
|
| 34 |
try:
|
| 35 |
voice_task: VoiceTask = self.audio_playing_queue.get(block=False, timeout=0.1)
|
|
|
|
| 82 |
|
| 83 |
voice_state_manager.set_audio_playing(task_id)
|
| 84 |
voice_state_manager.reset_task_id()
|
| 85 |
+
|
| 86 |
+
if not self.is_stopped:
|
| 87 |
+
audio_data, sample_rate = voice_task.tts_generated_sentence_audio
|
| 88 |
+
self.playing_audio(audio_data, sample_rate)
|
| 89 |
|
| 90 |
if self.audio_playing_queue.empty():
|
| 91 |
print(f'回答播放完了')
|
src/voice_dialogue/services/speech/monitor.py
CHANGED
|
@@ -217,7 +217,7 @@ class SpeechStateMonitor(BaseThread):
|
|
| 217 |
is_audio_sent_for_processing = False
|
| 218 |
is_audio_frames_empty = True
|
| 219 |
|
| 220 |
-
while not self.
|
| 221 |
try:
|
| 222 |
# 1. 管理任务生命周期
|
| 223 |
self.task_id = voice_state_manager.task_id
|
|
|
|
| 217 |
is_audio_sent_for_processing = False
|
| 218 |
is_audio_frames_empty = True
|
| 219 |
|
| 220 |
+
while not self.is_exited:
|
| 221 |
try:
|
| 222 |
# 1. 管理任务生命周期
|
| 223 |
self.task_id = voice_state_manager.task_id
|
src/voice_dialogue/services/speech/recognizer.py
CHANGED
|
@@ -31,7 +31,7 @@ class ASRWorker(BaseThread):
|
|
| 31 |
|
| 32 |
self.is_ready = True
|
| 33 |
|
| 34 |
-
while not self.
|
| 35 |
try:
|
| 36 |
voice_task: VoiceTask = self.user_voice_queue.get(block=False, timeout=0.1)
|
| 37 |
except Empty:
|
|
|
|
| 31 |
|
| 32 |
self.is_ready = True
|
| 33 |
|
| 34 |
+
while not self.is_exited:
|
| 35 |
try:
|
| 36 |
voice_task: VoiceTask = self.user_voice_queue.get(block=False, timeout=0.1)
|
| 37 |
except Empty:
|
src/voice_dialogue/services/text/generator.py
CHANGED
|
@@ -202,7 +202,7 @@ class LLMResponseGenerator(BaseThread):
|
|
| 202 |
self.is_ready = True
|
| 203 |
|
| 204 |
"""主运行循环"""
|
| 205 |
-
while not self.
|
| 206 |
try:
|
| 207 |
voice_task: VoiceTask = self.user_question_queue.get(block=False, timeout=0.1)
|
| 208 |
self._process_voice_task(voice_task)
|
|
|
|
| 202 |
self.is_ready = True
|
| 203 |
|
| 204 |
"""主运行循环"""
|
| 205 |
+
while not self.is_exited:
|
| 206 |
try:
|
| 207 |
voice_task: VoiceTask = self.user_question_queue.get(block=False, timeout=0.1)
|
| 208 |
self._process_voice_task(voice_task)
|