11import json
22import time
33import asyncio
4+ from typing import TYPE_CHECKING
5+
6+ if TYPE_CHECKING :
7+ from core .connection import ConnectionHandler
48from core .utils import textUtils
59from core .utils .util import audio_to_data
610from core .providers .tts .dto .dto import SentenceType
1317PRE_BUFFER_COUNT = 5
1418
1519
16- async def sendAudioMessage (conn , sentenceType , audios , text ):
20+ async def sendAudioMessage (conn : "ConnectionHandler" , sentenceType , audios , text ):
1721 if conn .tts .tts_audio_first_sentence :
1822 conn .logger .bind (tag = TAG ).info (f"发送第一段语音: { text } " )
1923 conn .tts .tts_audio_first_sentence = False
@@ -47,7 +51,7 @@ async def sendAudioMessage(conn, sentenceType, audios, text):
4751 await conn .close ()
4852
4953
50- async def _wait_for_audio_completion (conn ):
54+ async def _wait_for_audio_completion (conn : "ConnectionHandler" ):
5155 """
5256 等待音频队列清空并等待预缓冲包播放完成
5357
@@ -70,7 +74,9 @@ async def _wait_for_audio_completion(conn):
7074 conn .logger .bind (tag = TAG ).debug ("音频发送完成" )
7175
7276
73- async def _send_to_mqtt_gateway (conn , opus_packet , timestamp , sequence ):
77+ async def _send_to_mqtt_gateway (
78+ conn : "ConnectionHandler" , opus_packet , timestamp , sequence
79+ ):
7480 """
7581 发送带16字节头部的opus数据包给mqtt_gateway
7682 Args:
@@ -92,7 +98,9 @@ async def _send_to_mqtt_gateway(conn, opus_packet, timestamp, sequence):
9298 await conn .websocket .send (complete_packet )
9399
94100
95- async def sendAudio (conn , audios , frame_duration = AUDIO_FRAME_DURATION ):
101+ async def sendAudio (
102+ conn : "ConnectionHandler" , audios , frame_duration = AUDIO_FRAME_DURATION
103+ ):
96104 """
97105 发送音频包,使用 AudioRateController 进行精确的流量控制
98106
@@ -121,7 +129,9 @@ async def sendAudio(conn, audios, frame_duration=AUDIO_FRAME_DURATION):
121129 )
122130
123131
124- def _get_or_create_rate_controller (conn , frame_duration , is_single_packet ):
132+ def _get_or_create_rate_controller (
133+ conn : "ConnectionHandler" , frame_duration , is_single_packet
134+ ):
125135 """
126136 获取或创建 RateController 和 flow_control
127137
@@ -177,7 +187,7 @@ def _get_or_create_rate_controller(conn, frame_duration, is_single_packet):
177187 return conn .audio_rate_controller , conn .audio_flow_control
178188
179189
180- def _start_background_sender (conn , rate_controller , flow_control ):
190+ def _start_background_sender (conn : "ConnectionHandler" , rate_controller , flow_control ):
181191 """
182192 启动后台发送循环任务
183193
@@ -201,7 +211,7 @@ async def send_callback(packet):
201211
202212
203213async def _send_audio_with_rate_control (
204- conn , audio_list , rate_controller , flow_control , send_delay
214+ conn : "ConnectionHandler" , audio_list , rate_controller , flow_control , send_delay
205215):
206216 """
207217 使用 rate_controller 发送音频包
@@ -233,7 +243,7 @@ async def _send_audio_with_rate_control(
233243 rate_controller .add_audio (packet )
234244
235245
236- async def _do_send_audio (conn , opus_packet , flow_control ):
246+ async def _do_send_audio (conn : "ConnectionHandler" , opus_packet , flow_control ):
237247 """
238248 执行实际的音频发送
239249 """
@@ -254,7 +264,7 @@ async def _do_send_audio(conn, opus_packet, flow_control):
254264 flow_control ["sequence" ] = sequence + 1
255265
256266
257- async def send_tts_message (conn , state , text = None ):
267+ async def send_tts_message (conn : "ConnectionHandler" , state , text = None ):
258268 """发送 TTS 状态消息"""
259269 if text is None and state == "sentence_start" :
260270 return
@@ -281,7 +291,7 @@ async def send_tts_message(conn, state, text=None):
281291 await conn .websocket .send (json .dumps (message ))
282292
283293
284- async def send_stt_message (conn , text ):
294+ async def send_stt_message (conn : "ConnectionHandler" , text ):
285295 """发送 STT 状态消息"""
286296 end_prompt_str = conn .config .get ("end_prompt" , {}).get ("prompt" )
287297 if end_prompt_str and end_prompt_str == text :
0 commit comments