update:豆包tts流式ASR空字符串问题

This commit is contained in:
hrz
2025-06-04 18:33:29 +08:00
parent 03bcf5d2c7
commit 5b8a567f26
4 changed files with 20 additions and 16 deletions
@@ -30,8 +30,8 @@ async def handleAudioMessage(conn, audio):
async def resume_vad_detection(conn): async def resume_vad_detection(conn):
# 等待1秒后恢复VAD检测 # 等待2秒后恢复VAD检测
await asyncio.sleep(1) await asyncio.sleep(2)
conn.just_woken_up = False conn.just_woken_up = False
@@ -82,9 +82,9 @@ class ASRProviderBase(ABC):
) # 确保ASR模块返回原始文本 ) # 确保ASR模块返回原始文本
conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}") conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
text_len, _ = remove_punctuation_and_length(raw_text) text_len, _ = remove_punctuation_and_length(raw_text)
self.stop_ws_connection()
if text_len > 0: if text_len > 0:
# 使用自定义模块进行上报 # 使用自定义模块进行上报
self.stop_ws_connection()
await startToChat(conn, raw_text) await startToChat(conn, raw_text)
enqueue_asr_report(conn, raw_text, asr_audio_task) enqueue_asr_report(conn, raw_text, asr_audio_task)
@@ -54,12 +54,8 @@ class ASRProvider(ASRProviderBase):
await super().open_audio_channels(conn) await super().open_audio_channels(conn)
async def receive_audio(self, conn, audio, audio_have_voice): async def receive_audio(self, conn, audio, audio_have_voice):
if not isinstance(audio, bytes):
return
conn.asr_audio.append(audio) conn.asr_audio.append(audio)
if audio_have_voice == False and conn.client_have_voice == False: conn.asr_audio = conn.asr_audio[-10:]
conn.asr_audio = conn.asr_audio[-10:]
# 如果本次有声音,且之前没有建立连接 # 如果本次有声音,且之前没有建立连接
if audio_have_voice and self.asr_ws is None and not self.is_processing: if audio_have_voice and self.asr_ws is None and not self.is_processing:
@@ -114,9 +110,6 @@ class ASRProvider(ASRProviderBase):
# 发送缓存的音频数据 # 发送缓存的音频数据
if conn.asr_audio and len(conn.asr_audio) > 0: if conn.asr_audio and len(conn.asr_audio) > 0:
logger.bind(tag=TAG).info(
f"发送缓存音频数据: {len(conn.asr_audio)}"
)
for cached_audio in conn.asr_audio[-10:]: for cached_audio in conn.asr_audio[-10:]:
try: try:
pcm_frame = self.decoder.decode(cached_audio, 960) pcm_frame = self.decoder.decode(cached_audio, 960)
@@ -166,6 +159,18 @@ class ASRProvider(ASRProviderBase):
payload = result["payload_msg"] payload = result["payload_msg"]
if "result" in payload: if "result" in payload:
utterances = payload["result"].get("utterances", []) utterances = payload["result"].get("utterances", [])
# 检查duration和空文本的情况
if (
payload.get("audio_info", {}).get("duration", 0) > 2000
and not utterances
and not payload["result"].get("text")
):
logger.bind(tag=TAG).error(f"识别文本:空")
self.text = ""
conn.reset_vad_states()
await self.handle_voice_stop(conn, None)
break
for utterance in utterances: for utterance in utterances:
if utterance.get("definite", False): if utterance.get("definite", False):
self.text = utterance["text"] self.text = utterance["text"]
@@ -235,7 +240,7 @@ class ASRProvider(ASRProviderBase):
"sample_rate": self.rate, "sample_rate": self.rate,
}, },
} }
logger.bind(tag=TAG).info( logger.bind(tag=TAG).debug(
f"构造请求参数: {json.dumps(req, ensure_ascii=False)}" f"构造请求参数: {json.dumps(req, ensure_ascii=False)}"
) )
return req return req
@@ -195,7 +195,7 @@ class TTSProvider(TTSProviderBase):
try: try:
logger.bind(tag=TAG).debug("等待TTS文本队列消息...") logger.bind(tag=TAG).debug("等待TTS文本队列消息...")
message = self.tts_text_queue.get(timeout=1) message = self.tts_text_queue.get(timeout=1)
logger.bind(tag=TAG).info( logger.bind(tag=TAG).debug(
f"收到TTS任务|{message.sentence_type.name} {message.content_type.name} | 会话ID: {self.conn.sentence_id}" f"收到TTS任务|{message.sentence_type.name} {message.content_type.name} | 会话ID: {self.conn.sentence_id}"
) )
if self.conn.client_abort: if self.conn.client_abort:
@@ -221,7 +221,7 @@ class TTSProvider(TTSProviderBase):
elif ContentType.TEXT == message.content_type: elif ContentType.TEXT == message.content_type:
if message.content_detail: if message.content_detail:
try: try:
logger.bind(tag=TAG).info( logger.bind(tag=TAG).debug(
f"开始发送TTS文本: {message.content_detail}" f"开始发送TTS文本: {message.content_detail}"
) )
future = asyncio.run_coroutine_threadsafe( future = asyncio.run_coroutine_threadsafe(
@@ -229,7 +229,7 @@ class TTSProvider(TTSProviderBase):
loop=self.conn.loop, loop=self.conn.loop,
) )
future.result() future.result()
logger.bind(tag=TAG).info("TTS文本发送成功") logger.bind(tag=TAG).debug("TTS文本发送成功")
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}") logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}")
continue continue
@@ -250,7 +250,6 @@ class TTSProvider(TTSProviderBase):
loop=self.conn.loop, loop=self.conn.loop,
) )
future.result() future.result()
logger.bind(tag=TAG).info("TTS会话结束成功")
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"结束TTS会话失败: {str(e)}") logger.bind(tag=TAG).error(f"结束TTS会话失败: {str(e)}")
continue continue