This commit is contained in:
seven 2025-03-28 22:09:43 +08:00
parent 89baacf295
commit 40a29f0fcc
2 changed files with 25 additions and 22 deletions

View file

@ -4,6 +4,7 @@ import asyncio
import time
from core.utils.util import remove_punctuation_and_length, get_string_no_punctuation_or_emoji
import types
import inspect
TAG = __name__
logger = setup_logging()
@ -35,9 +36,10 @@ async def sendAudioStream(conn, audios):
start_time = time.perf_counter()
play_position = 0
if isinstance(audios,types.GeneratorType):
# if isinstance(audios,types.GeneratorType):
if inspect.isasyncgen(audios):
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
@ -71,9 +73,10 @@ async def sendAudio(conn, audios):
start_time = time.perf_counter()
play_position = 0
if isinstance(audios,types.GeneratorType):
# if isinstance(audios,types.GeneratorType):
if inspect.isasyncgen(audios):
count = 0
for opus_packet in audios:
async for opus_packet in audios:
if len(opus_packet) < 1:
continue
if count < 3:

View file

@ -47,26 +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
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 = {