mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 12:23:55 +08:00
update:兼容豆包流式ASR
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
import os
|
||||
import time
|
||||
import copy
|
||||
import uuid
|
||||
import wave
|
||||
import opuslib_next
|
||||
from abc import ABC, abstractmethod
|
||||
from config.logger import setup_logging
|
||||
from typing import Optional, Tuple, List
|
||||
from core.utils.util import remove_punctuation_and_length
|
||||
from core.handle.reportHandle import enqueue_asr_report
|
||||
from core.handle.receiveAudioHandle import startToChat
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
@@ -13,6 +18,54 @@ logger = setup_logging()
|
||||
class ASRProviderBase(ABC):
|
||||
def __init__(self):
|
||||
self.audio_format = "opus"
|
||||
self.conn = None
|
||||
|
||||
# 打开音频通道
|
||||
# 这里默认是非流式的处理方式
|
||||
# 流式处理方式请在子类中重写
|
||||
async def open_audio_channels(self, conn):
|
||||
self.conn = conn
|
||||
|
||||
# 接收音频
|
||||
# 这里默认是非流式的处理方式
|
||||
# 流式处理方式请在子类中重写
|
||||
async def receive_audio(self, audio, audio_have_voice):
|
||||
if (
|
||||
self.conn.client_listen_mode == "auto"
|
||||
or self.conn.client_listen_mode == "realtime"
|
||||
):
|
||||
have_voice = audio_have_voice
|
||||
else:
|
||||
have_voice = self.conn.client_have_voice
|
||||
# 如果本次没有声音,本段也没声音,就把声音丢弃了
|
||||
if have_voice == False and self.conn.client_have_voice == False:
|
||||
self.conn.asr_audio.append(audio)
|
||||
self.conn.asr_audio = self.conn.asr_audio[-10:]
|
||||
return
|
||||
|
||||
# 如果本段有声音,且已经停止了
|
||||
if self.conn.client_voice_stop:
|
||||
self.conn.client_abort = False
|
||||
# 音频太短了,无法识别
|
||||
if len(self.conn.asr_audio) < 15:
|
||||
self.conn.asr_audio.clear()
|
||||
self.conn.reset_vad_states()
|
||||
else:
|
||||
await self.handle_voice_stop()
|
||||
|
||||
# 处理语音停止
|
||||
async def handle_voice_stop(self):
|
||||
raw_text, _ = await self.speech_to_text(
|
||||
self.conn.asr_audio, self.conn.session_id
|
||||
) # 确保ASR模块返回原始文本
|
||||
self.conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
|
||||
text_len, _ = remove_punctuation_and_length(raw_text)
|
||||
if text_len > 0:
|
||||
# 使用自定义模块进行上报
|
||||
await startToChat(self.conn, raw_text)
|
||||
enqueue_asr_report(self.conn, raw_text, copy.deepcopy(self.conn.asr_audio))
|
||||
self.conn.asr_audio.clear()
|
||||
self.conn.reset_vad_states()
|
||||
|
||||
def save_audio_to_file(self, pcm_data: List[bytes], session_id: str) -> str:
|
||||
"""PCM数据保存为WAV文件"""
|
||||
|
||||
Reference in New Issue
Block a user