update:完成2分钟不说话,自动断开连接的功能

This commit is contained in:
hrz
2025-02-16 20:42:55 +08:00
parent 4e29a5a37b
commit 6b1488bc3a
3 changed files with 20 additions and 0 deletions
+3
View File
@@ -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
+1
View File
@@ -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相关变量
+16
View File
@@ -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)