柒牌:
authorazao <821478748@qq.com>
Wed, 29 Oct 2025 10:21:54 +0000 (18:21 +0800)
committerazao <821478748@qq.com>
Fri, 31 Oct 2025 05:59:55 +0000 (13:59 +0800)
添加语音更改nlp判断
完善AI翻译功能
金三:
修改语音对话中断的bug

2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/ai_VC.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/main_init.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/protocol.py
2.sdk/project/K8/sdk/services/microPython/ports/quectel/core/ai_VC.py

index 0ad3f43..5f5e0e8 100644 (file)
@@ -2511,11 +2511,13 @@ class AI_VC(Abstract):
         self.play_new_voice_flag =0 #播放文字留言flag
         EventMesh.subscribe("play_tts",self.play_tts)
         EventMesh.subscribe("play_new_voice",self.play_new_voice)
-        EventMesh.subscribe("play_music",self.play_music)
+        EventMesh.subscribe("play_music",self.play_music)#播放音乐
+        EventMesh.subscribe("play_trans_tts",self.play_trans_tts)#接收翻译tts
         EventMesh.subscribe("set_vad_silence",self.set_vad_silence)
         EventMesh.subscribe("kws_even",self.kws_even)
         EventMesh.subscribe("change_volume",self.change_volume)    
-        EventMesh.subscribe("find_cmd",self.find_cmd) 
+        EventMesh.subscribe("find_cmd",self.find_cmd) #查询命令
+        EventMesh.subscribe("change_voice",self.change_voice) #改变音色
         self.voice_timer = osTimer()#语音留言10S按下按键确定是否播放的判断定时器
         self.voice_cnt = 0 #留言定时器计数
         self.voicerupt_flag = 0#留言选择按键接听标志位
@@ -4806,32 +4808,32 @@ class AI_VC(Abstract):
                         self.AI_TTS_str ="服务器地址"+self.server_ip
                         return 0
                 
-                if text.find("讲话") != -1 or text.find("说话") != -1:
-                    if text.find("快") != -1 and text.find("太") == -1 :
-                        self.speech_rate += 10
-                        if self.speech_rate > 50:
-
-                            self.AI_TTS_str = "语速已经是最快了" 
-                            self.speech_rate = 50
-                        else:
+                # if text.find("讲话") != -1 or text.find("说话") != -1:
+                #     if text.find("快") != -1 and text.find("太") == -1 :
+                #         self.speech_rate += 10
+                #         if self.speech_rate > 50:
+
+                #             self.AI_TTS_str = "语速已经是最快了" 
+                #             self.speech_rate = 50
+                #         else:
                             
-                            EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate])
+                #             EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate])
 
-                            self.AI_TTS_str = "明白,语速已经调快了,当前语速是{}".format(self.speech_rate)
+                #             self.AI_TTS_str = "明白,语速已经调快了,当前语速是{}".format(self.speech_rate)
                             
-                        return 0
-                    if text.find("慢") != -1 and text.find("太") == -1 :
-                        self.speech_rate -= 10
-                        if self.speech_rate < -50:
-                            self.speech_rate = -50
-                            #EventMesh.publish("audio_montage_play",["yinliangyizuixiao.mp3"])
-                            #self.aud.play(1,0,'U:/static/yinliangyizuixiao.mp3')#音量已最小
-                            self.AI_TTS_str = "语速已经是最慢了"
-                        else:
-                            EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate])
-
-                            self.AI_TTS_str = "明白,语速已经调慢了,当前语速是{}".format(self.speech_rate)
-                        return 0
+                #         return 0
+                #     if text.find("慢") != -1 and text.find("太") == -1 :
+                #         self.speech_rate -= 10
+                #         if self.speech_rate < -50:
+                #             self.speech_rate = -50
+                #             #EventMesh.publish("audio_montage_play",["yinliangyizuixiao.mp3"])
+                #             #self.aud.play(1,0,'U:/static/yinliangyizuixiao.mp3')#音量已最小
+                #             self.AI_TTS_str = "语速已经是最慢了"
+                #         else:
+                #             EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate])
+
+                #             self.AI_TTS_str = "明白,语速已经调慢了,当前语速是{}".format(self.speech_rate)
+                #         return 0
                 if len(text)<22 and(text.find("聆听") != -1 or text.find("明天")!= -1) :
                     #print("text len :{}".format(len(text)))
                     if text.find("快") != -1 and text.find("太") == -1 :
@@ -5490,27 +5492,50 @@ class AI_VC(Abstract):
             battery = EventMesh.publish("get_battery")
             print("电量查询:{}".format(battery))
             tts_play_str = "电量剩余百分之{}".format(battery)
-            EventMesh.publish("play_tts",[tts_play_str,1])
-        elif "信号" in msg:
-            signal = net.csqQueryPoll()
-            tts_play_str = "当前信号{}".format(signal)
-            EventMesh.publish("play_tts",[tts_play_str,1])
-        elif "卡号" in msg:
-            iccid = sim.getIccid()
-            if iccid != -1 and len(iccid) > 5:
-                print("iccid{}".format(iccid))
-                tts_play_str = "当前卡号为:{}".format(iccid)
-            else:
-                tts_play_str = "卡号查询失败"
-            EventMesh.publish("play_tts",[tts_play_str,1])
-        elif "设备号" in msg:
-            imei = modem.getDevImei()
-            if imei != -1 and len(imei) > 5:
-                print("IMEI:{}".format(imei))
-                tts_play_str = "当前设备号为:{}".format(imei)
-            else:
-                tts_play_str = "设备号查询失败"
-            EventMesh.publish("play_tts",[tts_play_str,1])
+            EventMesh.publish("play_tts",[tts_play_str,0])
+    #改变音色判断
+    def change_voice(self,event ,msg):
+        voice = msg
+        if "男声" in msg or "普通话" in msg:
+            self.VC_TTS_voice_key = "男"
+        elif "女声" in msg:
+            self.VC_TTS_voice_key = "女"
+        elif "童声" in msg:
+            self.VC_TTS_voice_key = "童"    
+        elif "香港粤语" in msg:
+            self.VC_TTS_voice_key = "香港粤语"
+        elif "四川话" in msg:
+            self.VC_TTS_voice_key = "四川"    
+        elif "上海话" in msg:
+            self.VC_TTS_voice_key = "上海" 
+        elif "安徽合肥话" in msg:
+            self.VC_TTS_voice_key = "安徽合肥"    
+        elif "山东话" in msg:
+            self.VC_TTS_voice_key = "山东话" 
+        elif "陕西话" in msg:
+            self.VC_TTS_voice_key = "陕西"    
+        elif "广东话" in msg:
+            self.VC_TTS_voice_key = "广东" 
+        elif "成都话" in msg:
+            self.VC_TTS_voice_key = "成都"    
+        elif "湖南话" in msg:
+            self.VC_TTS_voice_key = "湖南" 
+        elif "苏州话" in msg:
+            self.VC_TTS_voice_key = "苏州"    
+        elif "东北话" in msg:
+            self.VC_TTS_voice_key = "东北" 
+        elif "河南话" in msg:
+            self.VC_TTS_voice_key = "河南"    
+        elif "湖北话" in msg:
+            self.VC_TTS_voice_key = "湖北" 
+        elif "闽南话" in msg:
+            self.VC_TTS_voice_key = "闽南"    
+        elif "内蒙古话" in msg:
+            self.VC_TTS_voice_key = "内蒙古话" 
+        EventMesh.publish("set_storage_config",["VC_TTS_voice_key",self.VC_TTS_voice_key])
+        tts_play_str = "声音改为{}".format(voice)
+        EventMesh.publish("play_tts",[tts_play_str,0])
+        
     #语音留言
     def play_new_voice(self,event = None,msg= None):
         EventMesh.publish("publish_AI_btnInfo",{"func":self.play_new_voice_cb,"data":msg}) 
@@ -5571,6 +5596,31 @@ class AI_VC(Abstract):
             # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) 
         except Exception as e:
                 usys.print_exception(e)   
+
+    #AI翻译接收TTS
+    def play_trans_tts(self,event = None,msg= None):
+        EventMesh.publish("publish_AI_btnInfo",{"func":self.play_trans_tts_cb,"data":msg}) 
+    def play_trans_tts_cb(self,data):
+        try:
+            new_voice_url = data #音频地址
+            if new_voice_url != "":
+                self.AI_new_voice_url = new_voice_url
+            if self.AI_VC_send_data_queue.empty()!=True:
+                self.AI_VC_send_data_queue.clear()
+            if self.AI_ASR_send_data_queue.empty()!=True:
+                self.AI_ASR_send_data_queue.clear()
+            EventMesh.publish("open_led_all")
+            self.AI_ASR_send_buf_len = 0
+            self.VC_Speech_End()
+            self.websocket_deconnect_VC()
+            self.AI_VC_start()
+            EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言
+            self.play_new_voice_flag = 1
+            # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End})
+            # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) 
+            # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) 
+        except Exception as e:
+                usys.print_exception(e)   
     def AI_client_new_voice(self): #上传未读的语音地址/包括音频地址也可以
         json_data = \
         {
index 460ac18..68ab734 100644 (file)
@@ -250,6 +250,7 @@ def main_init():
             pcr.add_support(pro.IOTWeb_user_unbind_devProtoCol())
             pcr.add_support(pro.IOTWeb_play_MusicProtoCol())
             pcr.add_support(pro.IOTWeb_Nlp_ConfigProtoCol())
+            pcr.add_support(pro.IOTWeb_play_trans_TTSProtoCol())
             sm = pro.SockMangager()
 
             sm.add_support_resolver(pcr)
index 0b109ea..75a413b 100644 (file)
@@ -2597,12 +2597,21 @@ def ai_cmd_handle(cmd):
         find = cmd[voice_start_index:]#查询截取
         if  "快" in find:
             EventMesh.publish("rate_move",1)
+            volume = "变快"
         elif "慢" in find:
             EventMesh.publish("rate_move",0)
+            volume = "变慢"
+        tts_play_str = "语速已经" + volume
+        EventMesh.publish("play_tts",[tts_play_str,0])
     elif "会议纪要" in cmd:#当指令中有会议纪要时,进入会议纪要模式
         EventMesh.publish("come_meet_mode")#进入会议纪要
     elif "翻译" in cmd:#当指令中有翻译时,进入AI翻译模式
         EventMesh.publish("come_trans_mode")#进入AI翻译纪要
+    elif "声音改为" in cmd:#当指令中有声音改为时,进入音色修改
+        voice_start_index = cmd.index("声音改为") + len("声音改为")#动态判断截取部分
+        find = cmd[voice_start_index:]#查询截取
+        EventMesh.publish("change_voice",find)
+
 #NLP指令
 class IOTWeb_Nlp_ConfigProtoCol(ProtoCol):
     """ai_cmd"""
@@ -2635,6 +2644,38 @@ class IOTWeb_Nlp_ConfigProtoCol(ProtoCol):
         ai_cmd_handle(cmdName)
         print("recv push_music{}".format(msg))
 
+#翻译播放TTS
+class IOTWeb_play_trans_TTSProtoCol(ProtoCol):
+    """tts_audio_url"""
+    TYPE = "tts_audio_url"
+
+    def send(self, event, msg):
+        # print("tts_audio_url{}".format(msg))
+        # 通过websocket发送到服务器端
+        date_tmp = utime.localtime()
+        time_tmp = utime.mktime(date_tmp)
+        tts_audio_url = {
+            "cmd":"tts_audio_url_complete",
+            "ts":time_tmp,
+            "msg":"返回状态",
+            "data":None
+        }
+        send_data = b''
+        send_data = protocol_send_total_data(tts_audio_url)
+        print("send_data tts_audio_url{}".format(send_data))
+        EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data)
+
+    def recv(self, event, msg):
+        """
+        @param event:上行下发push_music
+        @param msg: 接收到的下行数据
+        """
+        #提取录音播放名称
+        voiceUrl = msg["data"]
+        print("提取到的翻译路径为{}".format(voiceUrl))
+        EventMesh.publish("play_trans_tts",voiceUrl)
+        print("recv tts_audio_url{}".format(msg))
+
 #音乐播放
 class IOTWeb_play_MusicProtoCol(ProtoCol):
     """push_music"""
index 837e970..1324e7b 100644 (file)
@@ -1372,6 +1372,7 @@ class AI_VC(Abstract):
         self.Close_flag =0 #=1时,连续对话播放完音频就退出
         self.AI_alarm = _ai_alarm.AI_alarm()
         self.buffer = bytearray(1024*8) #音频缓存区
+        self.tts_completed = 0#音频是否播放过
         self.stop_tts = 0#音频是否播放完
         #表情变化状态以及检索词
         self.face_status = 0 # 0为正常,1为哭泣,2为愤怒,3为疑惑
@@ -3149,6 +3150,7 @@ class AI_VC(Abstract):
                         elif jsload_data["event"] == "tts.completed" :#tts结束
                             EventMesh.publish("open_led_green")#绿色
                             EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Start})
+                            self.tts_completed = 1
                             self.LocalRespondingEnded_timerout = 0
                             self.VC_state = VC_State_TTSEnd
                             pass
@@ -3163,9 +3165,10 @@ class AI_VC(Abstract):
                             #EventMesh.publish("open_led_green")#绿色
                             self.LocalRespondingEnded_timerout = 3
                             self.VC_state = VC_State_End
-                            if self.stop_tts == 1:
+                            if self.stop_tts == 1 and self.tts_completed == 1:#已经播放了音频并且音频停止了
                                 self.AI_client_VC_end_tts()#音频结束
                                 self.stop_tts = 0
+                                self.tts_completed = 0
                             EventMesh.publish("send_device_VC_state")#上传VC状态
                             pass
                         elif jsload_data["event"] == "command" :#命令匹配