#print("pwk_key_long_press {}".format(self.pwk_key_long_press))
if self.pwk_key_long_press == 0:
self.pwk_key_long_press = 1
- #EventMesh.publish("AI_key_long_press")
- # EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"])
- self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频
- EventMesh.publish("set_led_state", {"battery": "empty"})
- utime.sleep_ms(2000)
- self.aud.stopPlayStream()
- self.aud.stopAll()
- Power.powerDown()
- #print("AI_key_release")
- #EventMesh.publish("pwk_key_long_press")
- #EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_Long_Press})
+ EventMesh.publish("publish_AI_btnInfo", {"func": self._shutdown_tone_play_and_powerdown})
if self.pwk_key_status == 1:#松开
#print("pwk_key_timer_cb:{}".format(self.pwk_key_timer_count))
try:
except Exception:
allow_power_off = False
if allow_power_off:
- EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"])
- #self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频
- EventMesh.publish("set_led_state", {"battery": "empty"})
- utime.sleep_ms(2000)
- self.aud.stopPlayStream()
- print("stopAll:8")
- self.aud.stopAll()
- Power.powerDown()
+ EventMesh.publish("publish_AI_btnInfo", {"func": self._shutdown_tone_play_and_powerdown})
if self.KeyAI_status == 1:#松开
#print("KeyAI_timer_cb:{}".format(self.KeyAI_timer_count))
try:
print("pwk_key_double_press")
EventMesh.publish("pwk_key_double_press")
+ def _persist_vc_mode_before_powerdown(self):
+ try:
+ m = EventMesh.publish("get_vc_mode")
+ if m is not None:
+ EventMesh.publish("set_storage_config", ["VC_mode", int(m)])
+ except Exception:
+ pass
+
def pwk_key_Long_Press(self,event=None,msg=None):
self.check_rest_timer_count = 0
print("pwk_key_long_press!!!!!!!")
print("pwk_key_long_press!!!!!!!")
-
- # EventMesh.publish("paly_iccid_imei")
- # return
- EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"])
- #self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频
- EventMesh.publish("set_led_state", {"battery": "empty"})
- utime.sleep_ms(3000)
- self.aud.stopPlayStream()
- self.aud.stopAll()
- Power.powerDown()
- #print("AI_key_release")
- #EventMesh.publish("pwk_key_long_press")
+ self._shutdown_tone_play_and_powerdown()
def pwk_key_release(self):
self.check_rest_timer_count = 0
self.aud.stopAll()
except Exception:
pass
+ self._persist_vc_mode_before_powerdown()
try:
Power.powerDown()
except Exception:
try:
for i in (msg or []):
n = str(i)
- if ("Low_power" in n) or ("didianguanji" in n):
+ if (
+ ("Low_power" in n)
+ or ("didianguanji" in n)
+ or ("zhengzaiguanji" in n)
+ ):
return True
except Exception:
pass
return False
+ def _shutdown_tone_play_and_powerdown(self):
+ """关机提示音:先断 VC TTS,再播 zhengzaiguanji,最后断电。"""
+ self._persist_vc_mode_before_powerdown()
+ try:
+ EventMesh.publish("battery_priority_mute_vc")
+ except Exception:
+ pass
+ try:
+ EventMesh.publish("Close_VC")
+ except Exception:
+ pass
+ try:
+ utime.sleep_ms(80)
+ except Exception:
+ pass
+ EventMesh.publish("audio_montage_play", ["zhengzaiguanji.mp3"])
+ EventMesh.publish("set_led_state", {"battery": "empty"})
+ utime.sleep_ms(3000)
+ self.aud.stopPlayStream()
+ self.aud.stopAll()
+ Power.powerDown()
+
def audio_montage_play(self,event,msg):
self.wakeup_BT()
bat_pri = self._audio_is_battery_priority(msg)
continue
if op == OP_LONG_UPLINK:
try:
- if getattr(self, "_long_dialogue_key_arm", 0) == 1:
+ if (
+ getattr(self, "_long_dialogue_key_arm", 0) == 1
+ and not getattr(self, "_vc_boot_standby", 0)
+ ):
self._long_dialogue_publish_listening("server.started")
except Exception:
pass
except Exception:
pass
+ def _vc_handle_asr_empty(self, reason="asr.completed"):
+ """ASR completed 无有效文本:清轮次态 + Notify 播报(与 _ptt_abort_short_record 同出口)。"""
+ hint = "我没有听清,请再说一遍"
+ try:
+ if "vc_config" in globals() and vc_config:
+ cfg = vc_config.get_attr("ASR_EMPTY_HINT_TTS", None)
+ if cfg is not None and str(cfg).strip():
+ hint = str(cfg).strip()
+ except Exception:
+ pass
+ try:
+ print("[VC][ASR] completed empty -> clear + notify ({})".format(reason))
+ except Exception:
+ pass
+ try:
+ self.AI_ASR_str = ""
+ self.AI_ASR_str_tmp = ""
+ self._last_asr_result_text = ""
+ self._asr_completed_tone_done = 0
+ except Exception:
+ pass
+ if self._vc_is_long_dialogue_mode():
+ self._vc_handle_asr_empty_long(hint, reason)
+ return
+ self._vc_handle_asr_empty_ptt(hint, reason)
+
+ def _vc_handle_asr_empty_long(self, hint, reason="asr.completed"):
+ """长对话:空 ASR 提示播完后复用 sid 重新开麦(蓝慢闪)。"""
+ try:
+ self._vc_reopen_uplink_after_empty_hint = 0
+ if (
+ getattr(self, "Close_flag", 0) == 0
+ and getattr(self, "AI_client_VC_isconnect", 0) == 1
+ and getattr(self, "_vc_session_started", 0) == 1
+ ):
+ self._vc_reopen_uplink_after_empty_hint = 1
+ except Exception:
+ pass
+ try:
+ self._server_vad_silence = 0
+ self._vc_defer_vad_uplink_stop = 0
+ self._vc_vad_had_active_after_silence = 0
+ except Exception:
+ pass
+ try:
+ if getattr(self, "VC_record_state_flag", 0) != 0:
+ self.VC_Speech_End()
+ except Exception:
+ pass
+ try:
+ if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(
+ self, "_vc_session_started", 0
+ ) == 1:
+ self._vc_send_protocol_cancel("asr.empty")
+ except Exception:
+ pass
+ try:
+ self._pending_tts_kind = "asr_empty_hint"
+ self._last_tts_kind = "asr_empty_hint"
+ self.request_send_synthesis(hint)
+ print("[VC][ASR] long empty hint -> synthesis, reopen_after=1")
+ except Exception as e:
+ usys.print_exception(e)
+
+ def _vc_handle_asr_empty_ptt(self, hint, reason="asr.completed"):
+ """单对话:idle + Notify(不断线重连的轻量路径仍走 play_tts)。"""
+ try:
+ self._ptt_await_server = 0
+ self._ptt_audio_created = 0
+ self._ptt_defer_speech_until_asr = 0
+ self._ptt_record_open_ms = 0
+ self.AI_key_long_press_flag = 0
+ self._ptt_idle_latch = 1
+ self._vc_reopen_uplink_after_empty_hint = 0
+ except Exception:
+ pass
+ try:
+ if getattr(self, "VC_record_state_flag", 0) != 0:
+ self.VC_Speech_End()
+ except Exception:
+ pass
+ try:
+ if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(
+ self, "_vc_session_started", 0
+ ) == 1:
+ self._vc_send_protocol_cancel("asr.empty")
+ except Exception:
+ pass
+ try:
+ EventMesh.publish("play_tts", [hint, 0, "prompt"])
+ except Exception:
+ pass
+ try:
+ self._vc_publish_led("idle", "asr.empty")
+ except Exception:
+ pass
+
+ def _vc_long_reopen_after_asr_empty_hint(self):
+ """长对话:空 ASR 提示 TTS 播完后重新上行(复用当前 sid)。"""
+ if not getattr(self, "_vc_reopen_uplink_after_empty_hint", 0):
+ return False
+ self._vc_reopen_uplink_after_empty_hint = 0
+ if not self._vc_is_long_dialogue_mode():
+ return False
+ try:
+ if getattr(self, "Close_flag", 0) == 1:
+ return False
+ if getattr(self, "AI_client_VC_isconnect", 0) != 1:
+ return False
+ if getattr(self, "_vc_session_started", 0) != 1:
+ return False
+ except Exception:
+ return False
+ try:
+ self._server_vad_silence = 0
+ self._vc_defer_vad_uplink_stop = 0
+ self._vc_vad_had_active_after_silence = 0
+ self.Close_flag = 0
+ self._vc_clear_stale_record_state("asr.empty/reopen")
+ self._long_dialogue_publish_listening("asr.empty/reopen")
+ self.request_uplink_start("asr.empty/reopen")
+ print("[VC][ASR] long empty hint done -> reopen uplink")
+ return True
+ except Exception as e:
+ usys.print_exception(e)
+ return False
+
def _handle_pwk_short_release(self, event=None, msg=None, merged_ai_tail=False):
"""电源键短按:读留言/会议;否则按 UX 表 cancel+Close(清 sid)。"""
if self._vc_ms_keys_blocked("pwk_release"):
except Exception:
return 0
+ def _vc_persist_vc_mode(self):
+ """关机/模式切换:把当前对话模式写入存储。"""
+ try:
+ EventMesh.publish("set_storage_config", ["VC_mode", int(self.VC_mode)])
+ except Exception:
+ pass
+
def _ptt_should_hold_listening_led(self):
"""单对话打断/cancel 等待/录音:保持聆听蓝快闪,勿经 fsm 回到 playing/thinking。"""
try:
except Exception:
pass
def battery_priority_mute_vc(self, event=None, msg=None):
- """低电/关机提示音:短暂停 VC TTS,避免被流式播放挡住。"""
+ """低电/关机提示音:停 VC TTS 下行并关会话,避免流式 TTS 抢播本地提示音。"""
+ try:
+ self.VC_stop_play_audio_flag = 0
+ self._drop_tts_audio = 1
+ except Exception:
+ pass
try:
self._ptt_hard_mute_local_playback()
except Exception:
self.VC_Play_End()
except Exception:
pass
+ try:
+ if getattr(self, "VC_satrt_flag", 0) == 1:
+ EventMesh.publish("publish_AI_btnInfo", {"func": self.Close})
+ except Exception:
+ pass
def _vc_turn_current(self):
try:
return int(getattr(self, "_vc_turn_gen", 0))
self.is_auth = 1 #是否开启dmp鉴权
#self.VC_mode = 1 #0:短按连续问答模式 1:长按录音模式
self.VC_mode = EventMesh.publish("get_storage_config","VC_mode")#
+ _vc_mode_default = 0
+ try:
+ if "vc_config" in globals() and vc_config:
+ _vc_mode_default = int(vc_config.get_int("VC_MODE_DEFAULT", 0))
+ except Exception:
+ pass
if self.VC_mode == None:
- self.VC_mode = 0
+ self.VC_mode = _vc_mode_default
try:
self.VC_mode = int(self.VC_mode)
except Exception:
- self.VC_mode = 0
+ self.VC_mode = _vc_mode_default
self.is_up_error= 0 #是否上传错误id
self.Close_flag =0 #=1时,连续对话播放完音频就退出
# 叠加态 / 模式切换(vc_overlay,兼容 _vc_ms_phase)
self._turn_has_asr_completed = 0
self._turn_has_vad_silence = 0
self._turn_has_tts_started = 0
+ self._vc_reopen_uplink_after_empty_hint = 0
# VC 会话是否已进入 started(服务端 session initialized)
# 重要:在收到 started 前发送音频帧会触发服务端 "session not initialized" → failed → 端侧重连死循环
self._vc_session_started = 0
# 开机/提示音:本轮 started 不自动开麦(避免问候 TTS 前误进 listening)
self._skip_started_uplink_once = 0
+ self._vc_boot_standby = 0
self._last_tts_kind = ""
self._pending_tts_kind = ""
# Push-To-Talk(VAD关闭/单对话)是否已发送 audio.create
self._pending_tts_kind = "boot_prompt"
self._last_tts_kind = "boot_prompt"
self._skip_started_uplink_once = 1
+ self._vc_boot_standby = 1
+ self.Close_flag = 1
except Exception:
pass
EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start})
# EventMesh.publish("publish_AI_btnInfo",{"func":self.Close})
# EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(10)})
self._uplink_request_stop("BT_AI_key_press/compat2")
- self._vc_arm_record_start_tone()
self._uplink_request_start("BT_AI_key_press/compat2")
self.AI_VC_start()
self._vc_ms_pending_tts_str = tts_str
except Exception:
pass
- EventMesh.publish("set_storage_config", ["VC_mode", self.VC_mode])
+ self._vc_persist_vc_mode()
try:
self._vc_mode_strategy = strategy_for_mode(self.VC_mode)
self._vc_mode_strategy_mode = int(self.VC_mode)
self.VC_satrt_kws_flag = 1
if self.VC_Thinking_timerout < 100:
self.VC_Thinking_timerout = 0
- # 长对话:按键唤醒建链时先蓝慢闪聆听;单对话仍只在真开录时切灯
+ # 长对话:用户按键唤醒建链时先蓝慢闪;开机问候(_vc_boot_standby)保持 idle
try:
- if getattr(self, "VC_mode", -1) == 0:
+ if (
+ getattr(self, "VC_mode", -1) == 0
+ and not getattr(self, "_vc_boot_standby", 0)
+ ):
self._long_dialogue_publish_listening("AI_VC_start")
except Exception:
pass
def Close_VC(self,event,msg):
EventMesh.publish("publish_AI_btnInfo",{"func":self.Close})
def Close(self):
+ try:
+ self._vc_persist_vc_mode()
+ except Exception:
+ pass
try:
ms_phase = int(getattr(self, "_vc_ms_phase", 0))
if ms_phase in (1, 2, 3) and getattr(self, "_vc_close_reason", "") != "mode_switch_play_end":
self._skip_started_uplink_once = 0
except Exception:
pass
+ try:
+ self._vc_boot_standby = 0
+ except Exception:
+ pass
try:
self._vc_reset_dialogue_mode_state()
except Exception:
_vc_play_ai_key_record_start_tone = _vc_play_record_start_tone
def _vc_uplink_reason_is_user_key(self, reason):
+ """按键/PTT 开录(非 server.started、play_end、asr.empty 等自动开麦)。"""
r = str(reason or "")
if r.startswith("key.press") or r.startswith("BT_AI_key_press"):
return True
+ if r.startswith("ptt_begin") or r == "asr.started/defer":
+ return True
if r == "canceled" and (
getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
or getattr(self, "is_ruppt_tts", 0) == 1
return True
return False
+ def _vc_should_play_record_start_tone(self, reason):
+ """按键开录播 start.mp3;idle 按 AI 建链后 server.started 首开麦也算按键开录。"""
+ if self._vc_uplink_reason_is_user_key(reason):
+ return True
+ r = str(reason or "")
+ if (
+ r.startswith("server.started")
+ and getattr(self, "_long_dialogue_key_arm", 0) == 1
+ ):
+ return True
+ return False
+
def _vc_on_server_record_uplink_ready(self, via):
"""WS started / asr.started:用户开录走 _vc_user_record_entry,此处仅保留兼容。"""
if str(via or "") != "asr.started":
pass
def _vc_user_record_entry(self, reason=""):
- """用户开录统一入口:长对话与 PTT 分流(提示音均为 start.mp3)。"""
+ """开录统一入口;start.mp3 仅按键/PTT 开录时播。"""
if getattr(self, "VC_mode", -1) == 1:
self._vc_user_record_entry_ptt(reason)
else:
def _vc_user_record_entry_long(self, reason=""):
"""
- 长对话打断/续聊:
- 1) VC_Speech_Start 仅缓冲(record_state=1 不上传)
- 2) 播 start.mp3
- 3) 提示音结束后再 VC_Speech_Start_now + 蓝慢闪
+ 长对话开麦:
+ - 按键/打断续聊:先 start.mp3,滴声结束后再上行+蓝慢闪
+ - 自动续聊(server.started/play_end 等):不播 start,直接上行
"""
try:
self._vc_user_record_open_reason = str(reason or "")
pass
played = False
try:
- played = self._vc_play_record_start_tone(via=reason)
+ if self._vc_should_play_record_start_tone(reason):
+ played = self._vc_play_record_start_tone(via=reason)
except Exception:
played = False
if not played:
total_ms = 200
self._vc_user_record_tone_timer.stop()
self._vc_user_record_tone_timer.start(total_ms, 0, self._vc_user_record_tone_timer_cb)
- print("[VC][user_record] entry_long reason={} tone_ms={}".format(reason, total_ms))
+ print("[VC][user_record] entry_long key_tone reason={} tone_ms={}".format(reason, total_ms))
except Exception as e:
usys.print_exception(e)
try:
def _vc_user_record_entry_ptt(self, reason=""):
"""
- 单对话 PTT 长按:立即录音+上行;并行播 start.mp3;松手 end_speech 停传。
+ 单对话 PTT 长按:立即录音+上行;按键开录时并行播 start.mp3;松手 end_speech 停传。
"""
try:
self._vc_user_record_open_reason = str(reason or "")
except Exception:
pass
try:
- self._vc_play_record_start_tone(via=reason)
+ if self._vc_should_play_record_start_tone(reason):
+ self._vc_play_record_start_tone(via=reason)
except Exception:
pass
try:
self._vc_reset_long_idle_timer(reason)
except Exception:
pass
+ self._vc_user_record_entry(reason)
try:
if getattr(self, "VC_mode", 0) == 0:
self._long_dialogue_key_arm = 0
except Exception:
pass
- self._vc_user_record_entry(reason)
def _vc_orchestrator_play_end_cb(self, tag=""):
"""btn 编排线程:TTS 播完统一收尾。"""
self._vc_led_sync_from_fsm()
except Exception:
pass
+ try:
+ self._vc_long_reopen_after_asr_empty_hint()
+ except Exception:
+ pass
try:
self._vc_notify_queue_flush()
except Exception: