修改设备端对接
This commit is contained in:
@@ -25,9 +25,24 @@ class MiniMaxTTS(TTS, BaseService):
|
||||
self.voice_id = selected_role["minimax_voice_id"]
|
||||
self.model = "speech-02-turbo"
|
||||
self.selected_role = selected_role
|
||||
self._client_session: Optional[aiohttp.ClientSession] = None
|
||||
|
||||
async def connect(self):
|
||||
"""连接到MiniMax服务"""
|
||||
if self._client_session is None or self._client_session.closed:
|
||||
connector = aiohttp.TCPConnector(
|
||||
limit=20,
|
||||
limit_per_host=5,
|
||||
force_close=True,
|
||||
enable_cleanup_closed=True
|
||||
)
|
||||
timeout = aiohttp.ClientTimeout(total=120, connect=30)
|
||||
self._client_session = aiohttp.ClientSession(
|
||||
connector=connector,
|
||||
timeout=timeout,
|
||||
max_field_size=1024*1024,
|
||||
max_line_size=10*1024*1024
|
||||
)
|
||||
self.connected = True
|
||||
session_logger.info(
|
||||
self.device_id,
|
||||
@@ -38,6 +53,8 @@ class MiniMaxTTS(TTS, BaseService):
|
||||
async def close(self):
|
||||
"""关闭MiniMax服务连接"""
|
||||
self.connected = False
|
||||
if self._client_session and not self._client_session.closed:
|
||||
await self._client_session.close()
|
||||
|
||||
async def tts(
|
||||
self,
|
||||
@@ -138,48 +155,66 @@ class MiniMaxTTS(TTS, BaseService):
|
||||
|
||||
audio_buffer = bytearray()
|
||||
|
||||
async with aiohttp.ClientSession() as client_session:
|
||||
async with client_session.post(url, json=payload, headers=headers) as response:
|
||||
if response.status != 200:
|
||||
error_text = await response.text()
|
||||
session_logger.error(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
f"MiniMax TTS请求失败: {response.status}, {error_text}"
|
||||
)
|
||||
return None
|
||||
|
||||
session_logger.info(
|
||||
if self._client_session is None or self._client_session.closed:
|
||||
await self.connect()
|
||||
|
||||
async with self._client_session.post(url, json=payload, headers=headers) as response:
|
||||
if response.status != 200:
|
||||
error_text = await response.text()
|
||||
session_logger.error(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
"开始接收MiniMax TTS流式响应"
|
||||
f"MiniMax TTS请求失败: {response.status}, {error_text}"
|
||||
)
|
||||
return None
|
||||
|
||||
session_logger.info(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
"开始接收MiniMax TTS流式响应"
|
||||
)
|
||||
|
||||
buffer = b""
|
||||
chunk_size = 8192
|
||||
async for chunk in response.content.iter_chunked(chunk_size):
|
||||
buffer += chunk
|
||||
|
||||
async for line in response.content:
|
||||
if line.startswith(b'data:'):
|
||||
try:
|
||||
data_json = json.loads(line[5:])
|
||||
if "data" in data_json and "audio" in data_json["data"]:
|
||||
status = data_json["data"].get("status", 1)
|
||||
|
||||
if status == 1: # 只处理status=1(合成中)的音频数据,忽略status=2(合成结束)的汇总数据
|
||||
audio_hex = data_json["data"]["audio"]
|
||||
audio_binary = binascii.unhexlify(audio_hex)
|
||||
audio_buffer.extend(audio_binary)
|
||||
|
||||
if status == 2: # 合成结束
|
||||
session_logger.info(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
"MiniMax TTS流式合成完成"
|
||||
)
|
||||
except Exception as e:
|
||||
session_logger.error(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
f"处理MiniMax TTS流式响应出错: {str(e)}",
|
||||
exc_info=True
|
||||
)
|
||||
while b'\n' in buffer:
|
||||
line, buffer = buffer.split(b'\n', 1)
|
||||
line = line.strip()
|
||||
|
||||
if not line or not line.startswith(b'data:'):
|
||||
continue
|
||||
|
||||
try:
|
||||
data_json = json.loads(line[5:])
|
||||
if "data" in data_json and "audio" in data_json["data"]:
|
||||
status = data_json["data"].get("status", 1)
|
||||
|
||||
if status == 1:
|
||||
audio_hex = data_json["data"]["audio"]
|
||||
audio_binary = binascii.unhexlify(audio_hex)
|
||||
audio_buffer.extend(audio_binary)
|
||||
|
||||
if status == 2:
|
||||
session_logger.info(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
"MiniMax TTS流式合成完成"
|
||||
)
|
||||
except json.JSONDecodeError as e:
|
||||
session_logger.warning(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
f"JSON解析失败: {str(e)}"
|
||||
)
|
||||
except Exception as e:
|
||||
session_logger.error(
|
||||
self.device_id,
|
||||
self.session_id,
|
||||
f"处理MiniMax TTS流式响应出错: {str(e)}",
|
||||
exc_info=True
|
||||
)
|
||||
|
||||
async with aiofiles.open(output_file, 'wb') as f:
|
||||
await f.write(audio_buffer)
|
||||
|
||||
Reference in New Issue
Block a user