mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
调整相关逻辑
This commit is contained in:
parent
b1a7607dd5
commit
1846d3e935
4 changed files with 9 additions and 125 deletions
|
|
@ -7,12 +7,10 @@ import asyncio
|
|||
import traceback
|
||||
import websockets
|
||||
|
||||
from asyncio import Task
|
||||
from typing import Callable, Any
|
||||
from config.logger import setup_logging
|
||||
from core.utils.tts import MarkdownCleaner
|
||||
from core.utils.alibl_endpoint import build_ws_connect_options, resolve_ws_url
|
||||
from core.utils.alibl_event import extract_sentence_start_text
|
||||
from core.providers.tts.base import TTSProviderBase
|
||||
from core.providers.tts.dto.dto import SentenceType, ContentType, InterfaceType
|
||||
|
||||
|
|
@ -35,7 +33,6 @@ class TTSProvider(TTSProviderBase):
|
|||
self.api_key = config.get("api_key")
|
||||
if not self.api_key:
|
||||
raise ValueError("api_key is required for CosyVoice TTS")
|
||||
self.report_on_last = True
|
||||
|
||||
# WebSocket配置
|
||||
self.ws_url = resolve_ws_url(config.get("ws_url"))
|
||||
|
|
@ -44,7 +41,6 @@ class TTSProvider(TTSProviderBase):
|
|||
self._monitor_task = None
|
||||
self.activate_session = False
|
||||
self.last_active_time = None
|
||||
self._legacy_subtitle_sent = False
|
||||
|
||||
# 模型和音色配置
|
||||
self.model = config.get("model", "cosyvoice-v2")
|
||||
|
|
@ -134,9 +130,6 @@ class TTSProvider(TTSProviderBase):
|
|||
if message.sentence_type == SentenceType.FIRST:
|
||||
# 重置流式处理状态
|
||||
self.reset_stream_state()
|
||||
self.current_sentence_id = message.sentence_id
|
||||
self.tts_audio_first_sentence = True
|
||||
self._legacy_subtitle_sent = False
|
||||
# 初始化会话
|
||||
try:
|
||||
if not getattr(self.conn, "sentence_id", None):
|
||||
|
|
@ -366,29 +359,20 @@ class TTSProvider(TTSProviderBase):
|
|||
logger.bind(tag=TAG).debug("TTS任务启动成功~")
|
||||
elif event == "result-generated":
|
||||
output = data.get("payload", {}).get("output", {})
|
||||
output_type = output.get("type")
|
||||
tts_text = extract_sentence_start_text(data)
|
||||
if tts_text:
|
||||
tts_text = self._restore_original_text(tts_text)
|
||||
elif not output_type and not self._legacy_subtitle_sent:
|
||||
tts_text = self.get_tts_text(task_id)
|
||||
self._legacy_subtitle_sent = bool(tts_text)
|
||||
if tts_text:
|
||||
logger.bind(tag=TAG).info(
|
||||
f"句子语音生成成功: {tts_text}"
|
||||
)
|
||||
self.tts_audio_queue.put(
|
||||
(SentenceType.FIRST, [], tts_text, task_id)
|
||||
)
|
||||
if output.get("type") == "sentence-begin":
|
||||
original_text = output.get("original_text")
|
||||
self.tts_text = self._restore_original_text(original_text)
|
||||
logger.bind(tag=TAG).debug(f"句子语音生成开始: {self.tts_text}")
|
||||
self.tts_audio_queue.put((SentenceType.FIRST, [], self.tts_text))
|
||||
elif output.get("type") == "sentence-end":
|
||||
logger.bind(tag=TAG).info(f"句子语音生成成功: {self.tts_text}")
|
||||
elif event == "task-finished":
|
||||
logger.bind(tag=TAG).debug("TTS任务完成~")
|
||||
self.activate_session = False
|
||||
self.clear_tts_text(task_id)
|
||||
self._process_before_stop_play_files()
|
||||
elif event == "task-failed":
|
||||
error_code = header.get("error_code", "unknown")
|
||||
error_message = header.get("error_message", "未知错误")
|
||||
self.clear_tts_text(task_id)
|
||||
logger.bind(tag=TAG).error(
|
||||
f"TTS任务失败: {error_code} - {error_message}"
|
||||
)
|
||||
|
|
@ -417,7 +401,6 @@ class TTSProvider(TTSProviderBase):
|
|||
self.ws = None
|
||||
# 监听任务退出时清理引用
|
||||
finally:
|
||||
self.clear_tts_text(getattr(self, "current_sentence_id", None))
|
||||
self.activate_session = False
|
||||
self._monitor_task = None
|
||||
|
||||
|
|
|
|||
|
|
@ -439,9 +439,9 @@ class TTSProviderBase(ABC):
|
|||
if sentence_type is not SentenceType.MIDDLE:
|
||||
if self.report_on_last:
|
||||
# 累积模式:适用于全程只有一个语音流的TTS(如seed-tts-2.0)
|
||||
# FIRST时累积文本,音频持续累积,仅在LAST时统一上报
|
||||
# FIRST时只记录文本,音频持续累积,仅在LAST时统一上报
|
||||
if text:
|
||||
enqueue_text = f"{enqueue_text or ''}{text}"
|
||||
enqueue_text = text
|
||||
if sentence_type == SentenceType.LAST:
|
||||
enqueue_tts_report(self.conn, enqueue_text, enqueue_audio)
|
||||
enqueue_audio = []
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
def extract_sentence_start_text(data):
|
||||
output = data.get("payload", {}).get("output", {})
|
||||
if output.get("type") != "sentence-begin":
|
||||
return None
|
||||
text = output.get("original_text")
|
||||
return text if isinstance(text, str) and text.strip() else None
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from core.utils.alibl_endpoint import (
|
||||
DEFAULT_WS_URL,
|
||||
build_ws_connect_options,
|
||||
resolve_ws_url,
|
||||
)
|
||||
from core.utils.alibl_event import extract_sentence_start_text
|
||||
|
||||
|
||||
class AliBLStreamEventTests(unittest.TestCase):
|
||||
def test_sentence_begin_returns_original_text(self):
|
||||
event = {
|
||||
"payload": {
|
||||
"output": {
|
||||
"type": "sentence-begin",
|
||||
"original_text": "第一句。",
|
||||
}
|
||||
}
|
||||
}
|
||||
self.assertEqual(extract_sentence_start_text(event), "第一句。")
|
||||
|
||||
def test_non_begin_events_do_not_return_subtitles(self):
|
||||
for event_type in ("sentence-synthesis", "sentence-end"):
|
||||
event = {
|
||||
"payload": {
|
||||
"output": {
|
||||
"type": event_type,
|
||||
"original_text": "第一句。",
|
||||
}
|
||||
}
|
||||
}
|
||||
self.assertIsNone(extract_sentence_start_text(event))
|
||||
|
||||
def test_empty_sentence_text_is_ignored(self):
|
||||
event = {
|
||||
"payload": {
|
||||
"output": {"type": "sentence-begin", "original_text": " "}
|
||||
}
|
||||
}
|
||||
self.assertIsNone(extract_sentence_start_text(event))
|
||||
|
||||
|
||||
class AliBLWorkspaceEndpointTests(unittest.TestCase):
|
||||
def test_connection_uses_happy_eyeballs(self):
|
||||
self.assertEqual(
|
||||
build_ws_connect_options(15),
|
||||
{
|
||||
"open_timeout": 15,
|
||||
"happy_eyeballs_delay": 0.25,
|
||||
"interleave": 1,
|
||||
},
|
||||
)
|
||||
|
||||
def test_default_endpoint_remains_backward_compatible(self):
|
||||
self.assertEqual(resolve_ws_url(None), DEFAULT_WS_URL)
|
||||
|
||||
def test_beijing_workspace_endpoint_is_supported(self):
|
||||
endpoint = (
|
||||
"wss://ws-123.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference"
|
||||
)
|
||||
self.assertEqual(resolve_ws_url(endpoint), endpoint)
|
||||
|
||||
def test_singapore_workspace_endpoint_is_supported(self):
|
||||
endpoint = (
|
||||
"wss://ws-123.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/inference"
|
||||
)
|
||||
self.assertEqual(resolve_ws_url(endpoint), endpoint)
|
||||
|
||||
def test_non_alibaba_endpoint_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_ws_url("wss://example.com/api-ws/v1/inference")
|
||||
|
||||
def test_insecure_endpoint_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_ws_url(
|
||||
"ws://ws-123.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference"
|
||||
)
|
||||
|
||||
def test_unresolved_workspace_placeholder_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_ws_url(
|
||||
"wss://<WorkspaceId>.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Reference in a new issue