This commit is contained in:
Sakura-RanChen
2025-08-20 09:30:43 +08:00
parent d138e1dcef
commit 74826c1c59
3 changed files with 26 additions and 7 deletions
@@ -1,4 +1,5 @@
import json
import asyncio
from core.providers.tts.dto.dto import SentenceType
from core.utils import textUtils
@@ -52,8 +53,12 @@ async def send_tts_message(conn, state, text=None):
stop_tts_notify_voice = conn.config.get(
"stop_tts_notify_voice", "config/assets/tts_notify.mp3"
)
audios, _ = conn.tts.audio_to_opus_data(stop_tts_notify_voice)
await sendAudio(conn, audios)
conn.tts.audio_to_opus_data_stream(
stop_tts_notify_voice,
callback=lambda audio_data: asyncio.run_coroutine_threadsafe(
sendAudio(conn, audio_data), conn.loop
),
)
# 清除服务端讲话状态
conn.clearSpeakStatus()
@@ -11,7 +11,7 @@ from datetime import datetime
from core.utils import textUtils
from abc import ABC, abstractmethod
from config.logger import setup_logging
from core.utils.audio_flow_control import FlowControlConfig
from core.utils.audio_flow_control import FlowControlConfig, simulate_device_consumption
from core.utils.util import audio_bytes_to_data_stream, audio_to_data_stream
from core.utils.tts import MarkdownCleaner
from core.utils.output_counter import add_device_output
@@ -356,9 +356,8 @@ class TTSProviderBase(ABC):
# 模拟设备消费(实际应用中应该从设备获取反馈)防止音字不同步
if isinstance(audio_datas, bytes):
# 模拟设备播放延迟(60ms per frame), 实际情况可以低一点(50ms),增加使用体验
await asyncio.sleep(0.06)
self.flow_controller.update_device_consumption(1)
frame_count = 1
asyncio.create_task(simulate_device_consumption(self.flow_controller, frame_count))
# 在类中添加流控制器重置方法
def reset_flow_controller(self):
@@ -151,6 +151,21 @@ class AudioFlowController:
)
async def simulate_device_consumption(
flow_controller: AudioFlowController, frame_count: int
):
"""
模拟设备消费音频帧的过程
实际应用中应该根据设备反馈来更新消费情况
Args:
flow_controller: 流控制器实例
frame_count: 消费的帧数
"""
# 模拟设备播放延迟(60ms per frame
await asyncio.sleep(frame_count * 0.06)
flow_controller.update_device_consumption(frame_count)
# 流控配置常量
class FlowControlConfig:
"""流控配置常量"""
@@ -183,4 +198,4 @@ class FlowControlConfig:
return AudioFlowController(
max_device_buffer=max_buffer or cls.DEFAULT_MAX_DEVICE_BUFFER,
refill_rate=refill_rate or cls.DEFAULT_REFILL_RATE
)
)