From 9523e688f44fdfd16fdf4ccf04a04c5266ed546a Mon Sep 17 00:00:00 2001 From: "xiehao.0527" Date: Wed, 18 Jun 2025 20:20:20 +0800 Subject: [PATCH] upgrade asr to stream mode --- .../core/providers/asr/volcengine.py | 262 ++++++++++++++---- 1 file changed, 201 insertions(+), 61 deletions(-) diff --git a/main/xiaozhi-server/core/providers/asr/volcengine.py b/main/xiaozhi-server/core/providers/asr/volcengine.py index 4ff3e0051..5d0c877d3 100644 --- a/main/xiaozhi-server/core/providers/asr/volcengine.py +++ b/main/xiaozhi-server/core/providers/asr/volcengine.py @@ -14,12 +14,14 @@ from config.logger import setup_logging TAG = __name__ logger = setup_logging() +import asyncio +import opuslib_next # 假设需要 opus 解码 class ASRProvider(ASRProviderBase): def __init__(self, config: dict, delete_audio_file: bool): super().__init__() - self.interface_type = InterfaceType.NON_STREAM + self.interface_type = InterfaceType.STREAM # 修改为流式 self.api_key = config.get("api_key") self.model_name = config.get("model_name") self.output_dir = config.get("output_dir") @@ -36,19 +38,189 @@ class ASRProvider(ASRProviderBase): # 确保输出目录存在 os.makedirs(self.output_dir, exist_ok=True) - def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str: - """PCM数据保存为WAV文件""" - module_name = __name__.split(".")[-1] - file_name = f"asr_{module_name}_{session_id}_{uuid.uuid4()}.wav" - file_path = os.path.join(self.output_dir, file_name) + # 新增流式处理所需的状态变量 + self.asr_ws: Optional[websockets.WebSocketClientProtocol] = None + self.forward_task: Optional[asyncio.Task] = None + self.is_processing: bool = False + self.text: str = "" # 用于存储当前识别的文本 + self.decoder = opuslib_next.Decoder(16000, 1) # 如果输入是 opus + self.current_session_id: Optional[str] = None + self.audio_buffer = bytearray() # 用于缓存音频数据 - with wave.open(file_path, "wb") as wf: - wf.setnchannels(1) - wf.setsampwidth(2) # 2 bytes = 16-bit - wf.setframerate(16000) - wf.writeframes(b"".join(pcm_data)) + async def open_audio_channels(self, conn): + await super().open_audio_channels(conn) + self.current_session_id = conn.session_id # 保存当前会话 ID + + async def receive_audio(self, conn, audio: bytes, audio_have_voice: bool): + if not self.current_session_id: + self.current_session_id = conn.session_id + + if conn.client_listen_mode == "auto" or conn.client_listen_mode == "realtime": + have_voice = audio_have_voice + else: + have_voice = conn.client_have_voice + # 如果本次没有声音,本段也没声音,就把声音丢弃了 + conn.asr_audio.append(audio) + if have_voice == False and conn.client_have_voice == False: + conn.asr_audio = conn.asr_audio[-10:] + return + + if have_voice and self.asr_ws is None and not self.is_processing: + try: + self.is_processing = True + logger.bind(tag=TAG).info(f"Connecting to ASR service: {self.ws_url}") + auth_header = {"Authorization": f"Bearer {self.api_key}"} + self.asr_ws = await websockets.connect( + self.ws_url, + additional_headers=auth_header, + close_timeout=10 + ) + + # 启动任务来接收和处理 ASR 结果 + self.forward_task = asyncio.create_task(self._forward_asr_results(conn)) + + # 发送初始化/会话开始请求 + init_request = self._create_stream_start_msg() # 需要实现此方法 + await self.asr_ws.send(json.dumps(init_request)) + logger.bind(tag=TAG).info(f"Sent init request: {init_request}") + + # 发送缓存的音频数据 + if conn.asr_audio and len(conn.asr_audio) > 0: + pcm_frame = self.decode_opus(conn.asr_audio) + await self._send_audio_chunk(b''.join(pcm_frame), False if have_voice else True) + conn.asr_audio.clear() + + except Exception as e: + logger.bind(tag=TAG).error(f"Failed to establish ASR connection: {e}", exc_info=True) + if self.asr_ws: + await self.asr_ws.close() + self.asr_ws = None + self.is_processing = False + return + + # 发送当前音频数据 + if self.asr_ws and self.is_processing: + try: + logger.bind(tag=TAG).info(f"Append audio data") + pcm_frame = self.decode_opus(conn.asr_audio) + await self._send_audio_chunk(b''.join(pcm_frame),False if have_voice else True) + conn.asr_audio.clear() + except Exception as e: + logger.bind(tag=TAG).error(f"Error sending audio data: {e}", exc_info=True) + + async def _send_audio_chunk(self, pcm_data: bytes, completed = False): + if not self.asr_ws or not self.is_processing: + return + + # 火山引擎流式 ASR 可能需要将 PCM 数据进行 Base64 编码或直接发送二进制 + # 这里假设直接发送二进制,并构造成 JSON 消息 + # 请参考火山引擎流式 ASR 文档确定正确的格式 + base64_audio = base64.b64encode(pcm_data).decode("utf-8") + audio_event = { + "audio": base64_audio, + } + audio_event["type"] = "input_audio_buffer.commit" if completed else "input_audio_buffer.append" + await self.asr_ws.send(json.dumps(audio_event)) + logger.bind(tag=TAG).info(f"Sent audio chunk, size: {len(pcm_data)} , completed: {completed}") + + def _create_stream_start_msg(self) -> dict: + """创建流式识别开始的请求消息,具体格式需参考火山引擎文档""" + # 参考之前的 _create_session_msg,但可能需要不同的参数和类型 + config = { + "input_audio_format": "pcm", + "input_audio_codec": "raw", + "input_audio_sample_rate": 16000, + "input_audio_bits": 16, + "input_audio_channel": 1, + "input_audio_transcription": { + "model": self.model_name, + # "stream": True # 标识为流式 + }, + "session_id": self.current_session_id or str(uuid.uuid4()), # 确保有 session_id + } + event = { + "type": "transcription_session.update", # 假设的开始消息类型 + "session": config + } + return event + + async def _forward_asr_results(self, conn): + try: + while self.asr_ws and not conn.stop_event.is_set() and self.is_processing: + try: + message = await self.asr_ws.recv() + event = json.loads(message) + logger.bind(tag=TAG).info(f"Received ASR result: {event}") + + # 解析火山引擎流式 ASR 的响应格式 + # 以下为示例,具体字段需要参考文档 + message_type = event.get("type") + if message_type == 'conversation.item.input_audio_transcription.result': # 假设的中间结果类型 + transcript_segment = event.get("transcript", "") + is_final = event.get("is_final", False) + self.text += transcript_segment # 累加中间结果 + if is_final: + logger.bind(tag=TAG).info(f"Final ASR result: {self.text}") + conn.reset_vad_states() # 假设有 VAD 状态重置 + await self.handle_voice_stop(conn, None) # 触发语音结束处理 + # self.text = "" # 清空,等待下一次识别 + # 注意:这里可能需要更复杂的逻辑来处理会话结束和文本清空 + elif message_type == 'conversation.item.input_audio_transcription.completed': # 假设的最终完成类型 + final_transcript = event.get("transcript", self.text) + logger.bind(tag=TAG).info(f"ASR transcription completed: {final_transcript}") + self.text = final_transcript # 确保使用最终结果 + conn.reset_vad_states() + await self.handle_voice_stop(conn, None) + # self.text = "" # 清空 + # await self.stop_ws_connection() # 收到完成后可以考虑关闭连接 + break # 结束接收任务 + elif message_type == 'error': # 假设的错误类型 + error_msg = event.get("message", "Unknown ASR error") + logger.bind(tag=TAG).error(f"ASR service error: {error_msg}") + break + + except websockets.ConnectionClosed: + logger.bind(tag=TAG).info("ASR WebSocket connection closed.") + break + except json.JSONDecodeError: + logger.bind(tag=TAG).error(f"Failed to decode JSON from ASR: {message}") + except Exception as e: + logger.bind(tag=TAG).error(f"Error processing ASR result: {e}", exc_info=True) + break + finally: + logger.bind(tag=TAG).info("ASR forward task finished.") + # 确保在任务结束时,如果连接还存在,则关闭 + if self.asr_ws: + await self.asr_ws.close() + self.asr_ws = None + self.is_processing = False + # self.text = "" # 确保文本清空 + + async def stop_ws_connection(self): + logger.bind(tag=TAG).info("Stopping ASR WebSocket connection...") + self.is_processing = False # 停止处理标志 + if self.forward_task: + self.forward_task.cancel() + try: + await self.forward_task + except asyncio.CancelledError: + logger.bind(tag=TAG).info("ASR forward task cancelled.") + self.forward_task = None + + if self.asr_ws: + try: + # 发送结束流的信令,如果火山引擎需要 + end_event = {"type": "input_audio_buffer.commit"} + await self.asr_ws.send(json.dumps(end_event)) + await self.asr_ws.close() + logger.bind(tag=TAG).info("ASR WebSocket connection closed successfully.") + except Exception as e: + logger.bind(tag=TAG).error(f"Error closing ASR WebSocket: {e}") + finally: + self.asr_ws = None + self.audio_buffer.clear() + # self.text = "" # 确保文本清空 - return file_path async def _receive_messages(self, client) -> Optional[str]: resp_msg = "" @@ -64,23 +236,6 @@ class ASRProvider(ASRProviderBase): resp_msg += event.get("transcript") - def _create_session_msg(self): - config = { - "input_audio_format": "pcm", - "input_audio_codec": "raw", - "input_audio_sample_rate": 16000, - "input_audio_bits": 16, - "input_audio_channel": 1, - "input_audio_transcription": { - "model": self.model_name - }, - } - event = { - "type": "transcription_session.update", - "session": config - } - return json.dumps(event) - async def _send_request( self, audio_data: List[bytes], segment_size: int ) -> Optional[str]: @@ -129,6 +284,7 @@ class ASRProvider(ASRProviderBase): logger.bind(tag=TAG).error(f"ASR request failed: {e}", exc_info=True) return None + @staticmethod def slice_data(data: bytes, chunk_size: int) -> (list, bool): """ @@ -148,38 +304,22 @@ class ASRProvider(ASRProviderBase): async def speech_to_text( self, opus_data: List[bytes], session_id: str, audio_format="opus" ) -> Tuple[Optional[str], Optional[str]]: - """将语音数据转换为文本""" - + """在流式模式下,此方法通常返回当前已识别的文本,并在语音结束时由 _forward_asr_results 处理。""" + file_path = None - try: - # 合并所有opus数据包 - if audio_format == "pcm": - pcm_data = opus_data - else: - pcm_data = self.decode_opus(opus_data) - combined_pcm_data = b"".join(pcm_data) + if not self.delete_audio_file and self.current_session_id: + # 注意:这里的 pcm_data 来源需要明确,流式处理中可能需要累积所有 PCM 数据 + # 或者在语音结束后统一处理。当前实现中 pcm_data 未定义。 + # 暂时注释掉文件保存逻辑,因为 pcm_data 的获取方式在流式中不明确 + # pcm_data_to_save = b"".join(self.decode_opus(opus_data)) if audio_format != "pcm" else b"".join(opus_data) + # if pcm_data_to_save: + # file_path = self.save_audio_to_file([pcm_data_to_save], self.current_session_id) + pass # 暂时不处理文件保存 - # 判断是否保存为WAV文件 - if self.delete_audio_file: - pass - else: - file_path = self.save_audio_to_file(pcm_data, session_id) + current_text = self.text + return current_text, file_path - # 直接使用PCM数据 - # 计算分段大小 (单声道, 16bit, 16kHz采样率) - size_per_sec = 1 * 2 * 16000 # nchannels * sampwidth * framerate - segment_size = int(size_per_sec * self.seg_duration / 1000) - - # 语音识别 - start_time = time.time() - text = await self._send_request(combined_pcm_data, segment_size) - if text: - logger.bind(tag=TAG).debug( - f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}" - ) - return text, file_path - return "", file_path - - except Exception as e: - logger.bind(tag=TAG).error(f"语音识别失败: {self.ws_url} {self.api_key} {e}", exc_info=True) - return "", file_path \ No newline at end of file + async def close(self): + """资源清理方法""" + await self.stop_ws_connection() + logger.bind(tag=TAG).info("ASRProvider closed.") \ No newline at end of file