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)