From 4c8369f2169686d3df61b6ff99454a66f3cc18a7 Mon Sep 17 00:00:00 2001 From: seven Date: Fri, 28 Mar 2025 21:52:05 +0800 Subject: [PATCH] add --- .../core/handle/sendAudioHandle.py | 4 +-- .../core/providers/tts/linkerai.py | 30 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 43b7aea8e..2d85c4318 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -37,7 +37,7 @@ async def sendAudioStream(conn, audios): if isinstance(audios,types.GeneratorType): count = 0 - for opus_packet in audios: + async for opus_packet in audios: if opus_packet.startswith(b'text:'): await send_tts_message(conn, "sentence_start", opus_packet.replace(b'text:','').decode('utf-8')) continue @@ -73,7 +73,7 @@ async def sendAudio(conn, audios): if isinstance(audios,types.GeneratorType): count = 0 - for opus_packet in audios: + async for opus_packet in audios: if len(opus_packet) < 1: continue if count < 3: diff --git a/main/xiaozhi-server/core/providers/tts/linkerai.py b/main/xiaozhi-server/core/providers/tts/linkerai.py index 6bb1a09fc..0040b554b 100644 --- a/main/xiaozhi-server/core/providers/tts/linkerai.py +++ b/main/xiaozhi-server/core/providers/tts/linkerai.py @@ -8,6 +8,7 @@ import os from pydub import AudioSegment import opuslib_next import numpy as np +import aiohttp class TTSProvider(TTSProviderBase): def __init__(self, config, delete_audio_file): @@ -46,15 +47,26 @@ class TTSProvider(TTSProviderBase): f.write('%s\n'%(json.dumps(params,ensure_ascii=False))) f.write('%s'%(json.dumps(headers,ensure_ascii=False))) - def yield_data(self,url,params,headers): - response = requests.get(url=url, headers=headers, params=params, stream=True) - if response.status_code == 200: - for chunk in response.iter_content(chunk_size=None): - if chunk: - yield chunk - else: - print(f"请求失败,状态码: {response.status_code}") - print(f"错误信息: {response.text}") + # def yield_data(self,url,params,headers): + # response = requests.get(url=url, headers=headers, params=params, stream=True) + # if response.status_code == 200: + # for chunk in response.iter_content(chunk_size=None): + # if chunk: + # yield chunk + # else: + # print(f"请求失败,状态码: {response.status_code}") + # print(f"错误信息: {response.text}") + + + async def yield_data(self,base_url:str='',params:dict={},headers:dict={}): + async with aiohttp.ClientSession() as session: + try: + async with session.get(base_url, params=params, headers=headers,timeout=10) as response: + if response.status == 200: + async for r in response.content.iter_chunks(): + yield r[0] + except aiohttp.ClientError as e: + pass def double_stream(self,question:str='',device_id:str=''): params = {