调整流控参数优化

This commit is contained in:
Chingfeng Li
2025-08-06 10:09:28 +08:00
parent 095fe72db0
commit ce6a711c8e
3 changed files with 30 additions and 34 deletions
@@ -447,21 +447,15 @@ class TTSProvider(TTSProviderBase):
elif event_name == "SentenceBegin": elif event_name == "SentenceBegin":
opus_datas_cache = [] opus_datas_cache = []
elif event_name == "SentenceEnd": elif event_name == "SentenceEnd":
if ( # 发送缓存的数据
not is_first_sentence if self.conn.tts_MessageText:
or first_sentence_segment_count > 10 logger.bind(tag=TAG).info(
): f"句子语音生成成功: {self.conn.tts_MessageText}"
# 发送缓存的数据 )
if self.conn.tts_MessageText: self.tts_audio_queue.put(
logger.bind(tag=TAG).info( (SentenceType.FIRST, [], self.conn.tts_MessageText)
f"句子语音生成成功: {self.conn.tts_MessageText}" )
) self.conn.tts_MessageText = None
self.tts_audio_queue.put(
(SentenceType.MIDDLE, [], self.conn.tts_MessageText)
)
self.conn.tts_MessageText = None
# 第一句话结束后,将标志设置为False
is_first_sentence = False
elif event_name == "SynthesisCompleted": elif event_name == "SynthesisCompleted":
logger.bind(tag=TAG).debug(f"会话结束~~") logger.bind(tag=TAG).debug(f"会话结束~~")
self._process_before_stop_play_files() self._process_before_stop_play_files()
+19 -17
View File
@@ -253,7 +253,7 @@ class TTSProviderBase(ABC):
text = None text = None
try: try:
try: try:
sentence_type, audio_datas, text = self.tts_audio_queue.get(timeout=1) sentence_type, audio_datas, text = self.tts_audio_queue.get(timeout=0.1)
except queue.Empty: except queue.Empty:
if self.conn.stop_event.is_set(): if self.conn.stop_event.is_set():
break break
@@ -288,15 +288,14 @@ class TTSProviderBase(ABC):
# 短暂等待后重试 # 短暂等待后重试
time.sleep(retry_interval) time.sleep(retry_interval)
# 更新设备消费估计(这里假设设备以恒定速率消费) # status = self.flow_controller.get_status()
# 实际应用中可能需要从设备端获取真实的消费情况 # logger.bind(tag=TAG).debug(
estimated_consumption = int(retry_interval * FlowControlConfig.DEFAULT_REFILL_RATE) # f"流控状态: 缓冲区使用率={status['buffer_usage_percent']:.1f}%, "
self.flow_controller.update_device_consumption(estimated_consumption) # f"可用令牌={status['available_tokens']}..."
status = self.flow_controller.get_status() # f"发送帧数={status['sent_frames']}..."
logger.bind(tag=TAG).debug( # f"消费帧数={status['consumed_frames']}..."
f"流控状态: 缓冲区使用率={status['buffer_usage_percent']:.1f}%, " # f"代播放帧数={status['sent_frames'] - status['consumed_frames']}..."
f"可用令牌={status['available_tokens']}..." # )
)
else: else:
# 可以发送,记录发送的帧数 # 可以发送,记录发送的帧数
self.flow_controller.record_sent_frames(frame_count) self.flow_controller.record_sent_frames(frame_count)
@@ -314,16 +313,19 @@ class TTSProviderBase(ABC):
enqueue_tts_report(self.conn, text, audio_datas) enqueue_tts_report(self.conn, text, audio_datas)
# 输出流控状态(调试用) # 输出流控状态(调试用)
if frame_count > 0: # 只在较大的音频块时输出状态 # if frame_count > 0: # 只在较大的音频块时输出状态
status = self.flow_controller.get_status() # status = self.flow_controller.get_status()
logger.bind(tag=TAG).debug( # logger.bind(tag=TAG).debug(
f"流控状态: 缓冲区使用率={status['buffer_usage_percent']:.1f}%, " # f"流控状态: 缓冲区使用率={status['buffer_usage_percent']:.1f}%, "
f"可用令牌={status['available_tokens']}..." # f"可用令牌={status['available_tokens']}..."
) # f"发送帧数={status['sent_frames']}..."
# f"消费帧数={status['consumed_frames']}..."
# f"代播放帧数={status['sent_frames'] - status['consumed_frames']}..."
# )
else: else:
# 没有音频数据,直接发送 # 没有音频数据,直接发送
future = asyncio.run_coroutine_threadsafe( future = asyncio.run_coroutine_threadsafe(
sendAudioMessage(self.conn, sentence_type, audio_datas, text), self._send_audio_with_flow_control(sentence_type, audio_datas, text),
self.conn.loop, self.conn.loop,
) )
future.result() future.result()
@@ -174,9 +174,9 @@ class FlowControlConfig:
# 默认流控参数 # 默认流控参数
DEFAULT_MAX_DEVICE_BUFFER = 40 # 设备端最大缓冲帧数 DEFAULT_MAX_DEVICE_BUFFER = 40 # 设备端最大缓冲帧数
DEFAULT_REFILL_RATE = 5 # 默认令牌补充速率(帧/秒) DEFAULT_REFILL_RATE = OPUS_FRAMES_PER_SECOND # 默认令牌补充速率(帧/秒)
DEFAULT_MAX_WAIT_TIME = 5.0 # 流控最大等待时间(秒) DEFAULT_MAX_WAIT_TIME = 5.0 # 流控最大等待时间(秒)
DEFAULT_RETRY_INTERVAL = 0.1 # 流控重试间隔(秒) DEFAULT_RETRY_INTERVAL = 0.06 # 流控重试间隔(秒)
# 预缓冲参数 # 预缓冲参数
PRE_BUFFER_FRAMES = 3 # 预缓冲帧数 PRE_BUFFER_FRAMES = 3 # 预缓冲帧数