diff --git a/audio_processes.py b/audio_processes.py index fc4fda0..0d9fffa 100644 --- a/audio_processes.py +++ b/audio_processes.py @@ -1437,11 +1437,8 @@ class OutputProcess: print(f" - time_since_last_chunk: {time_since_last_chunk:.3f}秒") - # 检查TTS是否正在生成 - 修复逻辑 - # 如果TTS生成未完成但队列和缓冲区都为空,可能是状态标记错误,自动修正 - if not self.tts_generation_complete and tts_queue_size == 0 and len(self.tts_buffer) == 0: - print(f"🔧 检测到TTS生成状态异常:tts_generation_complete=False但无数据,自动修正为True") - self.tts_generation_complete = True + # 检查TTS是否正在生成 - 移除自动修正逻辑 + # 不再自动修正TTS生成状态,等待真正的TTS_COMPLETE信号 # TTS正在生成的条件:队列中有任务 或 还有待处理的缓冲区内容 或 TTS生成未完成 tts_is_generating = (tts_queue_size > 0 or len(self.tts_buffer) > 0 or not self.tts_generation_complete) @@ -1455,28 +1452,17 @@ class OutputProcess: # 等待播放完成检测中的超时机制来处理这种情况 # 特殊处理2:如果all_audio_received为False但其他条件都满足,强制设置为True - print(f"🔍 检查特殊处理条件:") - print(f" - not all_audio_received: {not self.all_audio_received}") + # 移除自动修正all_audio_received的逻辑,等待真正的音频数据 + print(f"🔍 播放状态检查(移除自动修正机制):") print(f" - llm_generation_complete: {self.llm_generation_complete}") print(f" - tts_generation_complete: {self.tts_generation_complete}") + print(f" - all_audio_received: {self.all_audio_received}") print(f" - pre_buffer_empty: {self.pre_buffer_empty}") print(f" - playback_buffer_empty: {self.playback_buffer_empty}") print(f" - no_active_playback: {self.no_active_playback}") print(f" - tts_queue_size == 0: {tts_queue_size == 0}") print(f" - not tts_is_generating: {not tts_is_generating}") - if (not self.all_audio_received and - self.llm_generation_complete and - self.tts_generation_complete and - self.pre_buffer_empty and - self.playback_buffer_empty and - self.no_active_playback and - tts_queue_size == 0 and - not tts_is_generating): - # 这种情况可能是音频播放开始后没有正确设置all_audio_received - print(f"🔧 检测到all_audio_received为False但其他播放条件已满足,强制设置为True") - self.all_audio_received = True - # 检查是否所有条件都满足 - 使用更新的状态变量,添加TTS生成状态检查 all_conditions_met = ( self.llm_generation_complete and @@ -1835,6 +1821,14 @@ class OutputProcess: self.tts_generation_complete = True print(f"🎵 OutputProcess TTS生成已完成") + # 发送TTS完成信号到主队列 + try: + tts_complete_command = "TTS_COMPLETE:" + self.audio_queue.put(tts_complete_command) + print(f"🎵 已发送TTS完成信号到主队列") + except Exception as e: + print(f"❌ 发送TTS完成信号失败: {e}") + # 简化:直接使用统一播放完成检测机制 self.logger.info("TTS生成完成,等待统一播放完成检测机制处理...") @@ -1846,6 +1840,15 @@ class OutputProcess: except Exception as e: self.logger.error(f"TTS音频生成失败: {e}") + # 即使失败也要设置TTS完成状态,避免系统卡住 + self.tts_generation_complete = True + # 发送TTS完成信号到主队列 + try: + tts_complete_command = "TTS_COMPLETE:" + self.audio_queue.put(tts_complete_command) + print(f"🎵 TTS生成失败,但已发送TTS完成信号到主队列") + except Exception as send_error: + print(f"❌ 发送TTS完成信号失败: {send_error}") return False # ========== 智能句子缓冲系统 - 从 recorder.py 借鉴 ========== diff --git a/control_system.py b/control_system.py index b1a0959..25bc74e 100644 --- a/control_system.py +++ b/control_system.py @@ -28,6 +28,19 @@ from audio_processes import ( RecordingState, ControlCommand, ProcessEvent ) +def input_process_target(command_queue, event_queue, config): + """输入进程的目标函数 - 在子进程中创建InputProcess实例""" + try: + print("🎙️ 输入进程目标函数开始执行...") + input_process = InputProcess(command_queue, event_queue, config) + print("🎙️ InputProcess实例创建成功,开始运行...") + input_process.run() + print("🎙️ 输入进程运行完成") + except Exception as e: + print(f"❌ 输入进程出错: {e}") + import traceback + traceback.print_exc() + def output_process_target(audio_queue, config, event_queue): """输出进程的目标函数 - 在子进程中创建OutputProcess实例""" try: @@ -210,11 +223,8 @@ class ControlSystem: } self.input_process = mp.Process( - target=InputProcess( - self.input_command_queue, - self.input_event_queue, - input_config - ).run + target=input_process_target, + args=(self.input_command_queue, self.input_event_queue, input_config) ) # 创建并启动输出进程 @@ -264,17 +274,23 @@ class ControlSystem: # 等待校准完成 if self.wait_for_calibration_complete(timeout=30): print("✅ 校准完成") + # 校准完成后播放打招呼 + print("🎭 播放角色打招呼...") + greeting_success = self.play_greeting() + if not greeting_success: + print("⚠️ 打招呼播放失败,继续运行...") else: print("⚠️ 校准超时,继续运行...") - if auto_monitoring: - # 自动启动监听 - print("🎯 自动启动音频监听...") - success = self.start_monitoring() - if success: - print("✅ 监听已启动") - else: - print("⚠️ 监听启动失败") + # 注释掉自动启动监听功能,让打招呼播放完成后自动开启监听 + # if auto_monitoring: + # # 自动启动监听 + # print("🎯 自动启动音频监听...") + # success = self.start_monitoring() + # if success: + # print("✅ 监听已启动") + # else: + # print("⚠️ 监听启动失败") print("=" * 60) print("🎙️ 系统就绪,开始检测语音...") @@ -1257,6 +1273,40 @@ class ControlSystem: return filtered_text + def play_greeting(self): + """播放角色打招呼语音""" + try: + # 获取角色配置 + character_config = self._load_character_config(self.config['processing']['character']) + if not character_config or "greeting" not in character_config: + print("⚠️ 角色配置中没有找到greeting字段,跳过打招呼") + return True + + greeting_text = character_config["greeting"] + print(f"🎭 播放角色打招呼: {greeting_text}") + + # 设置状态为播放状态 + self.state = RecordingState.PLAYING + + # 发送打招呼文本到TTS + success = self._send_text_to_output_process(greeting_text) + if not success: + print("❌ 打招呼TTS生成失败") + return False + + # 手动设置LLM完成状态(因为打招呼没有LLM生成过程) + self._notify_llm_complete() + + # 发送结束信号(TTS_COMPLETE信号由TTS进程在真正完成时发送) + self.output_audio_queue.put(None) + + print("✅ 打招呼语音已发送到输出队列") + return True + + except Exception as e: + print(f"❌ 播放打招呼失败: {e}") + return False + def _text_to_speech_streaming(self, text: str) -> bool: """文本转语音(流式)""" if not self.config['processing']['enable_tts']: diff --git a/logs/InputProcess_20250921_200851.log b/logs/InputProcess_20250921_200851.log new file mode 100644 index 0000000..a5c9d39 --- /dev/null +++ b/logs/InputProcess_20250921_200851.log @@ -0,0 +1,3 @@ +2025-09-21 20:08:51 - InputProcess_logger - INFO - 日志系统初始化完成 - 进程: InputProcess +2025-09-21 20:08:51 - InputProcess_logger - INFO - 日志文件: logs/InputProcess_20250921_200851.log +2025-09-21 20:08:51 - InputProcess_logger - INFO - [InputProcess] TTS工作线程已启动 diff --git a/logs/InputProcess_20250921_200916.log b/logs/InputProcess_20250921_200916.log new file mode 100644 index 0000000..a6f13d9 --- /dev/null +++ b/logs/InputProcess_20250921_200916.log @@ -0,0 +1,4 @@ +2025-09-21 20:09:16 - InputProcess_logger - INFO - 日志系统初始化完成 - 进程: InputProcess +2025-09-21 20:09:16 - InputProcess_logger - INFO - 日志文件: logs/InputProcess_20250921_200916.log +2025-09-21 20:09:16 - InputProcess_logger - INFO - [InputProcess] TTS工作线程已启动 +2025-09-21 20:09:16 - InputProcess_logger - INFO - [InputProcess] 输入进程启动 diff --git a/logs/OutputProcess_20250921_200916.log b/logs/OutputProcess_20250921_200916.log new file mode 100644 index 0000000..7fb44c7 --- /dev/null +++ b/logs/OutputProcess_20250921_200916.log @@ -0,0 +1,9 @@ +2025-09-21 20:09:16 - OutputProcess_logger - INFO - 日志系统初始化完成 - 进程: OutputProcess +2025-09-21 20:09:16 - OutputProcess_logger - INFO - 日志文件: logs/OutputProcess_20250921_200916.log +2025-09-21 20:09:16 - OutputProcess_logger - INFO - [OutputProcess] 播放工作线程已启动 +2025-09-21 20:09:16 - OutputProcess_logger - INFO - [OutputProcess] TTS工作线程已启动 +2025-09-21 20:09:16 - OutputProcess_logger - INFO - [OutputProcess] 输出进程启动 +2025-09-21 20:09:17 - OutputProcess_logger - INFO - [OutputProcess] 音频设备初始化成功 +2025-09-21 20:09:54 - OutputProcess_logger - ERROR - [OutputProcess] 处理音频队列时出错: +2025-09-21 20:09:54 - OutputProcess_logger - ERROR - [OutputProcess] 输出进程错误: +2025-09-21 20:09:57 - OutputProcess_logger - INFO - [OutputProcess] 输出进程退出