add:双向流tts结构改造,火山双向tts语音流式输入输出

This commit is contained in:
lizhongxiang
2025-03-31 15:15:59 +08:00
parent 264487574b
commit bb9df3f54c
8 changed files with 46 additions and 63 deletions
+3 -15
View File
@@ -50,21 +50,9 @@ class TTSProviderBase(ABC):
self.executor = ThreadPoolExecutor(max_workers=self.max_workers)
async def open_audio_channels(self):
pass
async def reset(self):
try:
logger.bind(tag=TAG).info("说明开始了新的对话,重建tts监听")
await self.stop_listen_resource()
self.tts_text_queue = queue.Queue()
self.tts_audio_queue = queue.Queue()
# 启动tts_text_queue监听线程
self.stop_event.clear()
tts_priority = threading.Thread(target=self._tts_text_priority_thread, daemon=True)
tts_priority.start()
except Exception as e:
logger.bind(tag=TAG).error(f"Failed to process TTS text: {e}")
traceback.print_exc()
# 启动tts_text_queue监听线程
tts_priority = threading.Thread(target=self._tts_text_priority_thread, daemon=True)
tts_priority.start()
async def stop_listen_resource(self):
"""资源清理方法"""
@@ -180,8 +180,8 @@ class TTSProvider(TTSProviderBase):
if len(chunk_total) % 2 == 0 and chunk_total[-2:] == b'\x00\x00':
audio = self._get_audio_from_tts(chunk_total)
audio_raw = audio_raw + audio.raw_data
# 长度凑够2贞开始发送,60ms*4=240ms
if len(audio_raw) >= 7680:
# 长度凑够2贞开始发送,60ms*2=120ms
if len(audio_raw) >= 3840:
opus_datas = self.wav_to_opus_data_audio_raw(audio_raw)
if index == 0:
yield TTSMessageDTO(u_id=u_id, msg_type=MsgType.TTS_TEXT_RESPONSE,
@@ -152,7 +152,7 @@ class TTSProvider(TTSProviderBase):
self.tts_text = ""
async def open_audio_channels(self):
self.loop_tts = asyncio.new_event_loop()
await super().open_audio_channels()
ws_header = {
"X-Api-App-Key": self.appId,
"X-Api-Access-Key": self.access_token,
@@ -316,6 +316,7 @@ class TTSProvider(TTSProviderBase):
async def close(self):
"""资源清理方法"""
await self.finish_connection()
await self.ws.close()
async def text_to_speak(self, u_id, text, is_last_text=False, is_first_text=False):