xiaozhi-esp32-server/main/xiaozhi-server/core/providers/asr/vosk.py
huozaimengli 15650e1a6c refactor(asr): 统一音频预处理逻辑并引入AudioArtifacts
重构所有ASR提供商的speech_to_text方法,将重复的音频解码、合并和文件保存逻辑提取到基类的speech_to_text_wrapper中。引入AudioArtifacts数据类封装PCM帧、字节数据、文件路径和临时路径,简化各提供商实现。移除各提供商中的冗余文件清理代码,由基类统一处理。

新增requires_file()和prefers_temp_file()方法允许提供商声明文件需求,优化内存和磁盘使用。保持接口兼容性的同时提高代码复用性和可维护性。
2026-01-25 11:27:34 +08:00

92 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import json
import time
from typing import Optional, Tuple, List
from .base import ASRProviderBase
from config.logger import setup_logging
from core.providers.asr.dto.dto import InterfaceType
import vosk
TAG = __name__
logger = setup_logging()
class ASRProvider(ASRProviderBase):
def __init__(self, config: dict, delete_audio_file: bool = True):
super().__init__()
self.interface_type = InterfaceType.LOCAL
self.model_path = config.get("model_path")
self.output_dir = config.get("output_dir", "tmp/")
self.delete_audio_file = delete_audio_file
# 初始化VOSK模型
self.model = None
self.recognizer = None
self._load_model()
# 确保输出目录存在
os.makedirs(self.output_dir, exist_ok=True)
def _load_model(self):
"""加载VOSK模型"""
try:
if not os.path.exists(self.model_path):
raise FileNotFoundError(f"VOSK模型路径不存在: {self.model_path}")
logger.bind(tag=TAG).info(f"正在加载VOSK模型: {self.model_path}")
self.model = vosk.Model(self.model_path)
# 初始化VOSK识别器(采样率必须为16kHz)
self.recognizer = vosk.KaldiRecognizer(self.model, 16000)
logger.bind(tag=TAG).info("VOSK模型加载成功")
except Exception as e:
logger.bind(tag=TAG).error(f"加载VOSK模型失败: {e}")
raise
async def speech_to_text(
self, opus_data: List[bytes], session_id: str, audio_format="opus"
) -> Tuple[Optional[str], Optional[str]]:
"""将语音数据转换为文本"""
try:
# 检查模型是否加载成功
if not self.model:
logger.bind(tag=TAG).error("VOSK模型未加载,无法进行识别")
return "", None
artifacts = self.get_current_artifacts()
if artifacts is None:
return "", None
if not artifacts.pcm_bytes:
logger.bind(tag=TAG).warning("合并后的PCM数据为空")
return "", None
start_time = time.time()
# 进行识别(VOSK推荐每次送入2000字节的数据)
chunk_size = 2000
text_result = ""
for i in range(0, len(artifacts.pcm_bytes), chunk_size):
chunk = artifacts.pcm_bytes[i:i+chunk_size]
if self.recognizer.AcceptWaveform(chunk):
result = json.loads(self.recognizer.Result())
text = result.get('text', '')
if text:
text_result += text + " "
# 获取最终结果
final_result = json.loads(self.recognizer.FinalResult())
final_text = final_result.get('text', '')
if final_text:
text_result += final_text
logger.bind(tag=TAG).debug(
f"VOSK语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text_result.strip()}"
)
return text_result.strip(), artifacts.file_path
except Exception as e:
logger.bind(tag=TAG).error(f"VOSK语音识别失败: {e}")
return "", None