Skip to content

Commit

Permalink
聊天页 新增 直接语音对话 开关,如果启用了,将在首次运行时直接进行语音识别,而不需手动点击开始按键。针对有些系统按键无法触发的情况下,配…
Browse files Browse the repository at this point in the history
…合连续对话和唤醒词使用
  • Loading branch information
Ikaros-521 committed Aug 6, 2024
1 parent 36e4ae8 commit 9a7fc2a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@
},
"talk": {
"key_listener_enable": false,
"direct_run_talk": false,
"device_index": "1",
"no_recording_during_playback": true,
"no_recording_during_playback_sleep_interval": 1.0,
Expand Down
1 change: 1 addition & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@
},
"talk": {
"key_listener_enable": false,
"direct_run_talk": false,
"device_index": "1",
"no_recording_during_playback": true,
"no_recording_during_playback_sleep_interval": 1.0,
Expand Down
36 changes: 32 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ def do_listen_and_comment(status=True):
is_recording = True

config = Config(config_path)
# 是否启用按键监听,不启用的话就不用执行了
if not config.get("talk", "key_listener_enable"):
# 是否启用按键监听和直接对话,没启用的话就不用执行了
if not config.get("talk", "key_listener_enable") and not config.get("talk", "direct_run_talk"):
is_recording = False
return

Expand Down Expand Up @@ -797,16 +797,44 @@ def key_listener():
except KeyboardInterrupt:
os._exit(0)

# 直接运行语音对话
def direct_run_talk():
global \
do_listen_and_comment_thread, \
stop_do_listen_and_comment_thread_event, \
is_recording

if not is_recording:
# 是否启用连续对话模式
if config.get("talk", "continuous_talk"):
stop_do_listen_and_comment_thread_event.clear()
do_listen_and_comment_thread = threading.Thread(
target=do_listen_and_comment, args=(True,)
)
do_listen_and_comment_thread.start()
else:
stop_do_listen_and_comment_thread_event.clear()
do_listen_and_comment_thread = threading.Thread(
target=do_listen_and_comment, args=(False,)
)
do_listen_and_comment_thread.start()

# 从配置文件中读取触发键的字符串配置
trigger_key = config.get("talk", "trigger_key")
stop_trigger_key = config.get("talk", "stop_trigger_key")

# 是否启用了 按键监听
if config.get("talk", "key_listener_enable"):
logger.info(
f"单击键盘 {trigger_key} 按键进行录音喵~ 由于其他任务还要启动,如果按键没有反应,请等待一段时间"
f"单击键盘 {trigger_key} 按键进行录音喵~ 由于其他任务还要启动,如果按键没有反应,请等待一段时间(如果使用本地ASR,请等待模型加载完成后使用)"
)

# 创建并启动按键监听线程
# 是否启用了直接运行对话,如果启用了,将在首次运行时直接进行语音识别,而不需手动点击开始按键。针对有些系统按键无法触发的情况下,配合连续对话和唤醒词使用
if config.get("talk", "direct_run_talk"):
logger.info("直接运行对话模式,首次运行时将直接进行语音识别,而不需手动点击开始按键(如果使用本地ASR,请等待模型加载完成后使用)")
direct_run_talk()

# 创建并启动按键监听线程,放着也是在聊天模式下,让程序一直阻塞用的
thread = threading.Thread(target=key_listener)
thread.start()

Expand Down
5 changes: 4 additions & 1 deletion webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,7 @@ def common_textarea_handle(content):
"""
if True:
config_data["talk"]["key_listener_enable"] = switch_talk_key_listener_enable.value
config_data["talk"]["direct_run_talk"] = switch_talk_direct_run_talk.value
config_data["talk"]["device_index"] = select_talk_device_index.value
config_data["talk"]["no_recording_during_playback"] = switch_talk_no_recording_during_playback.value
config_data["talk"]["no_recording_during_playback_sleep_interval"] = round(float(input_talk_no_recording_during_playback_sleep_interval.value), 2)
Expand Down Expand Up @@ -5669,7 +5670,9 @@ async def fish_speech_load_model(data):


with ui.row():
switch_talk_key_listener_enable = ui.switch('启用按键监听', value=config.get("talk", "key_listener_enable")).style(switch_internal_css)
switch_talk_key_listener_enable = ui.switch('启用按键监听', value=config.get("talk", "key_listener_enable")).style(switch_internal_css).tooltip("启用后,可以通过键盘单击下放配置的录音按键,启动语音识别对话功能")
switch_talk_direct_run_talk = ui.switch('直接语音对话', value=config.get("talk", "direct_run_talk")).style(switch_internal_css).tooltip("如果启用了,将在首次运行时直接进行语音识别,而不需手动点击开始按键。针对有些系统按键无法触发的情况下,配合连续对话和唤醒词使用")

audio_device_info_list = common.get_all_audio_device_info("in")
logger.info(f"声卡输入设备={audio_device_info_list}")
audio_device_info_dict = {str(device['device_index']): device['device_info'] for device in audio_device_info_list}
Expand Down

0 comments on commit 9a7fc2a

Please sign in to comment.