update:优化

This commit is contained in:
hrz
2025-05-24 23:43:16 +08:00
parent 574d34bc2c
commit 7a598d5839
6 changed files with 32 additions and 158 deletions
+7 -10
View File
@@ -11,7 +11,6 @@ from core.handle.reportHandle import enqueue_tts_report
from abc import ABC, abstractmethod
from core.utils.tts import MarkdownCleaner
from core.utils.util import audio_to_data
from core.utils import opus_encoder_utils
TAG = __name__
logger = setup_logging()
@@ -33,9 +32,6 @@ class TTSProviderBase(ABC):
self.output_file = config.get("output_dir")
self.tts_queue = queue.Queue()
self.audio_play_queue = queue.Queue()
self.opus_encoder = opus_encoder_utils.OpusEncoderUtils(
sample_rate=16000, channels=1, frame_size_ms=60
)
# 添加实现类型属性,默认为非流式
self.interface_type = TTSImplementationType.NON_STREAMING
@@ -43,9 +39,14 @@ class TTSProviderBase(ABC):
def generate_filename(self):
pass
def to_tts(self, text):
def to_tts(self, text, index):
"""如果是流式实现,一般没有文件生成,我们返回枚举值"""
if self.interface_type != TTSImplementationType.SINGLE_STREAMING:
if self.interface_type != TTSImplementationType.NON_STREAMING:
if index == 1:
future = asyncio.run_coroutine_threadsafe(
self.start_session(self.conn.session_id), loop=self.conn.loop
)
future.result()
asyncio.run(self.text_to_speak(text, None))
return self.interface_type.value
@@ -221,10 +222,6 @@ class TTSProviderBase(ABC):
f"audio_play_priority priority_thread: {text} {e}"
)
def wav_to_opus_data_audio_raw(self, raw_data_var, is_end=False):
opus_datas = self.opus_encoder.encode_pcm_to_opus(raw_data_var, is_end)
return opus_datas
async def start_session(self, session_id):
pass
@@ -4,12 +4,11 @@ import threading
import traceback
import uuid
import json
from datetime import datetime
import websockets
from datetime import datetime
from config.logger import setup_logging
from core.providers.tts.base import TTSProviderBase, TTSImplementationType
from core.utils.util import pcm_to_data
TAG = __name__
logger = setup_logging()
@@ -167,7 +166,6 @@ class TTSProvider(TTSProviderBase):
target=self._start_monitor_tts_response_thread(), daemon=True
)
tts_priority.start()
await self.start_session(conn.session_id)
def generate_filename(self, extension=".wav"):
return os.path.join(
@@ -381,7 +379,7 @@ class TTSProvider(TTSProviderBase):
and res.header.message_type == AUDIO_ONLY_RESPONSE
):
logger.bind(tag=TAG).info(f"推送数据到队列里面~~")
opus_datas = self.wav_to_opus_data_audio_raw(res.payload)
opus_datas = pcm_to_data(res.payload)
logger.bind(tag=TAG).info(
f"推送数据到队列里面帧数~~{len(opus_datas)}"
)
@@ -390,15 +388,15 @@ class TTSProvider(TTSProviderBase):
json_data = json.loads(res.payload.decode("utf-8"))
self.tts_text = json_data.get("text", "")
logger.bind(tag=TAG).info(f"句子开始~~{self.tts_text}")
self.audio_play_queue.put((None, self.tts_text, 0))
self.audio_play_queue.put(([], self.tts_text, 0))
elif res.optional.event == EVENT_TTSSentenceEnd:
logger.bind(tag=TAG).info(f"句子结束~~{self.tts_text}")
self.audio_play_queue.put((None, self.tts_text, 0))
self.audio_play_queue.put(([], self.tts_text, 0))
elif res.optional.event == EVENT_SessionFinished:
logger.bind(tag=TAG).info(f"会话结束~~,最后一句补零")
# opus_datas = self.wav_to_opus_data_audio_raw(b"", is_end=True)
self.audio_play_queue.put((None, self.tts_text, 0))
opus_datas = pcm_to_data(b"")
self.audio_play_queue.put((opus_datas, self.tts_text, 0))
else:
continue
except websockets.ConnectionClosed: