From 6b1488bc3aaac953c3fa7c29fa6302a000ccbaed Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sun, 16 Feb 2025 20:42:55 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=AE=8C=E6=88=902=E5=88=86=E9=92=9F?= =?UTF-8?q?=E4=B8=8D=E8=AF=B4=E8=AF=9D=EF=BC=8C=E8=87=AA=E5=8A=A8=E6=96=AD?= =?UTF-8?q?=E5=BC=80=E8=BF=9E=E6=8E=A5=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 3 +++ core/connection.py | 1 + core/handle/audioHandle.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/config.yaml b/config.yaml index fae28769..2e616e13 100644 --- a/config.yaml +++ b/config.yaml @@ -46,6 +46,9 @@ prompt: | # 使用完声音文件后删除文件(Delete the sound file when you are done using it) delete_audio: true +# 没有语音输入多久后断开连接(秒),默认2分钟,即120秒 +close_connection_no_voice_time: 120 + # 是否启用私有配置(Enable private configuration),启用后可以每个设备有不同的配置 # 目前这个模块还在开发中,建议:不要修改use_private_config选项 use_private_config: false diff --git a/core/connection.py b/core/connection.py index 6b86e2ac..6ed57b44 100644 --- a/core/connection.py +++ b/core/connection.py @@ -52,6 +52,7 @@ class ConnectionHandler: self.client_audio_buffer = bytes() self.client_have_voice = False self.client_have_voice_last_time = 0.0 + self.client_no_voice_last_time = 0.0 self.client_voice_stop = False # asr相关变量 diff --git a/core/handle/audioHandle.py b/core/handle/audioHandle.py index cce6612f..ba711854 100644 --- a/core/handle/audioHandle.py +++ b/core/handle/audioHandle.py @@ -18,8 +18,10 @@ async def handleAudioMessage(conn, audio): # 如果本次没有声音,本段也没声音,就把声音丢弃了 if have_voice == False and conn.client_have_voice == False: + await no_voice_close_connect(conn) conn.asr_audio.clear() return + conn.client_no_voice_last_time = 0.0 conn.asr_audio.append(audio) # 如果本段有声音,且已经停止了 if conn.client_voice_stop: @@ -146,3 +148,17 @@ async def schedule_with_interrupt(delay, coro): await coro except asyncio.CancelledError: pass + + +async def no_voice_close_connect(conn): + if conn.client_no_voice_last_time == 0.0: + conn.client_no_voice_last_time = time.time() * 1000 + else: + no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time + close_connection_no_voice_time = conn.config.get("close_connection_no_voice_time", 120) + print(no_voice_time) + if no_voice_time > 1000 * close_connection_no_voice_time: + conn.client_abort = False + conn.asr_server_receive = False + prompt = "时间过得真快,我都好久没说话了。请你用十个字左右话跟我告别,以“再见”或“拜拜拜”为结尾" + await startToChat(conn, prompt)