From: p15692025051 <846603105@qq.com> Date: Wed, 5 Jun 2024 07:17:36 +0000 (+0800) Subject: ai文件提交 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=8b918df3e371d8d536adb1081ee6bb7bf3a47b43;p=mirrors%2FASR1603_G5.git ai文件提交 --- diff --git a/2.sdk/project/W8/sdk/services/microPython/ports/quectel/core/ai.py b/2.sdk/project/W8/sdk/services/microPython/ports/quectel/core/ai.py new file mode 100644 index 00000000..f34bae66 --- /dev/null +++ b/2.sdk/project/W8/sdk/services/microPython/ports/quectel/core/ai.py @@ -0,0 +1,626 @@ +import utime +import audio +from machine import Pin +import osTimer +import _thread +from queue import Queue +from ui import style_font_youyuan_20,style_screen,Screen,style_font_youyuan_20_black,style_img,LCD_SIZE_H,LCD_SIZE_W +import uwebsocket +import usys +import EventMesh +import constants +import ujson +import lvgl as lv + +def sub_utf8_String(string,length): + if length >= len(string): + return string + i = 0 + p = 0 + while True: + ch = ord(string[i]) + #1111110x + if ch >= 252: + p = p + 6 + #111110xx + elif ch >= 248: + p = p + 5 + #11110xxx + elif ch >= 240: + p = p + 4 + #1110xxxx + elif ch >= 224: + p = p + 3 + #110xxxxx + elif ch >= 192: + p = p + 2 + else: + p = p + 1 + if p >= length: + break + else: + i = p + + return string[0:i] + +style_AI_btn = lv.style_t() +style_AI_btn.init() +style_AI_btn.set_bg_color(lv.color_make(0x1E, 0x26, 0xff)) +style_AI_btn.set_border_width(0) +style_AI_btn.set_shadow_opa(0) + +style_common_bg_rect_btn_pressed = lv.style_t() +style_common_bg_rect_btn_pressed.init() +style_common_bg_rect_btn_pressed.set_bg_color(lv.color_make(0x1E, 0x26, 0xff)) +style_common_bg_rect_btn_pressed.set_bg_opa(100) +style_common_bg_rect_btn_pressed.set_border_width(0) +style_common_bg_rect_btn_pressed.set_shadow_opa(0) + +# style_font_20_ai_green = lv.style_t() +# style_font_20_ai_green.init() +# style_font_20_ai_green.set_text_color(lv.color_make(0x00, 0xf0, 0x00)) +# style_font_20_ai_green.set_bg_color(lv.color_make(0x00, 0xff, 0x2a)) +# style_font_20_ai_green.set_text_font_v2("font_28.bin",28,0) + +# style_font_20_ai_white = lv.style_t() +# style_font_20_ai_white.init() +# style_font_20_ai_white.set_text_color(lv.color_make(0x00, 0xf0, 0x00)) +# style_font_20_ai_white.set_bg_color(lv.color_make(0xff, 0xff, 0xff)) +# style_font_20_ai_white.set_text_font_v2("font_28.bin",28,0) + +style_font_20_ai_white = lv.style_t() +style_font_20_ai_white.init() +style_font_20_ai_white.set_radius(0) +style_font_20_ai_white.set_bg_color(lv.color_make(0xff, 0xff, 0xff)) +#style_font_20_ai_white.set_bg_grad_color(lv.color_make(0x21,0x95,0xf6)) +style_font_20_ai_white.set_bg_grad_dir(lv.GRAD_DIR.NONE) +style_font_20_ai_white.set_bg_opa(255) +style_font_20_ai_white.set_text_color(lv.color_make(0x00, 0x00, 0x00)) +style_font_20_ai_white.set_text_font_v2("font_28.bin",28,0) + +style_font_20_ai_green = lv.style_t() +style_font_20_ai_green.init() +style_font_20_ai_green.set_radius(0) +style_font_20_ai_green.set_bg_color(lv.color_make(0x00, 0xff, 0x2a)) +#style_font_20_ai_green.set_bg_grad_color(lv.color_make(0x21,0x95,0xf6)) +style_font_20_ai_green.set_bg_grad_dir(lv.GRAD_DIR.NONE) +style_font_20_ai_green.set_bg_opa(255) +style_font_20_ai_green.set_text_color(lv.color_make(0x00, 0x00, 0x00)) +style_font_20_ai_green.set_text_font_v2("font_28.bin",28,0) + +AI_screen = lv.obj() +print("AI_screen") +AI_screen.set_scrollbar_mode(lv.SCROLLBAR_MODE.OFF) +AI_screen.add_style(style_screen, lv.PART.MAIN | lv.STATE.DEFAULT) + +AI_screen_list_screen = lv.obj(AI_screen) +AI_screen_list_screen.set_pos(0, 0) +AI_screen_list_screen.set_size(LCD_SIZE_W, LCD_SIZE_H-60) +AI_screen_list_screen.set_scrollbar_mode(lv.SCROLLBAR_MODE.OFF) +AI_screen_list_screen.add_style(style_screen, lv.PART.MAIN | lv.STATE.DEFAULT) + + + +AI_press_btn = lv.btn(AI_screen) +AI_press_btn.set_pos(10, (LCD_SIZE_H-60)+4) +AI_press_btn.set_size(LCD_SIZE_W-20, 56) +AI_press_btn.add_style(style_AI_btn, lv.PART.MAIN | lv.STATE.DEFAULT) +AI_press_btn.add_style(style_common_bg_rect_btn_pressed, lv.PART.MAIN | lv.STATE.PRESSED) +AI_press_btn.set_style_radius(30,0) + +AI_press_label = lv.label(AI_screen) +AI_press_label.set_text("单击说话") +AI_press_label.set_size(LCD_SIZE_W, 38) +AI_press_label.set_pos(0,(LCD_SIZE_H-60)+22) +AI_press_label.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0) +AI_press_label.add_style(style_font_youyuan_20, lv.PART.MAIN | lv.STATE.DEFAULT) + + +AI_press_label_countdown = lv.label(AI_screen_list_screen) +AI_press_label_countdown.set_pos(30, 20) +AI_press_label_countdown.set_size(LCD_SIZE_W-30, 80) +AI_press_label_countdown.set_text(" ") +AI_press_label_countdown.set_long_mode(lv.label.LONG.WRAP) +AI_press_label_countdown.set_style_text_align(lv.TEXT_ALIGN.RIGHT, 0) +AI_press_label_countdown.add_style(style_font_20_ai_green, lv.PART.MAIN | lv.STATE.DEFAULT) + +AI_label = lv.label(AI_screen_list_screen) +AI_label.set_pos(0, 130) +AI_label.set_size(LCD_SIZE_W-30, 80) +AI_label.set_text(" ") +AI_label.set_long_mode(lv.label.LONG.WRAP) +AI_label.set_style_text_align(lv.TEXT_ALIGN.LEFT, 0) +AI_label.add_style(style_font_20_ai_white, lv.PART.MAIN | lv.STATE.DEFAULT) + + +class AIScreen(Screen):#AI_GUI + # + NAME = "AI_success" + + def __init__(self): + super().__init__() + self.meta = AI_screen + self.meta_type = "popup" + self.Audio_buf_size = 8*1024 + self.record_timer = osTimer() + self.record = audio.Record() + #self.record.end_callback(self.record_callback) + self.record.gain_set(4,10) + self.aud = audio.Audio(0) + self.audio_play_start = 0 + self.audio_play_state = 0 #音频播放完状态 + self.aud.setCallback(self.audio_cb) + + self.AI_audio_queue_thread_id = 0 + self.AI_send_data_queue_thread_id = 0 + self.AI_client_chat_recv_id = 0 + self.AI_client_tts_recv_id = 0 + self.AI_client_asr_recv_id = 0 + self.AI_client_asr = None + self.AI_client_asr_isconnect = 0 #是否连接成功 + self.AI_client_asr_num = 0 + self.AI_client_chat = None + self.AI_client_chat_isconnect = 0 #是否连接成功 + self.AI_client_chat_num = 1 + self.AI_client_chat_play_str = "" + self.AI_client_tts = None + self.AI_client_tts_isconnect = 0 #是否连接成功 + self.AI_client_tts_num = 2 + self.AI_client_tts_timer = osTimer() + self.AI_client_tts_data_len = 0 + self.press_state = 0 + self.AI_label_str = "" + # AI_press_btn.add_event_cb(lambda e: EventMesh.callback_wrapper(self.talking_cb(e)), + # lv.EVENT.LONG_PRESSED, None) + # AI_press_btn.add_event_cb(lambda e: EventMesh.callback_wrapper(self.sending_cb(e)), + # lv.EVENT.RELEASED, None) + AI_press_btn.add_event_cb(lambda e: EventMesh.callback_wrapper(self.talking_cb(e)), + lv.EVENT.RELEASED, None) + def initialization(self): + self.aud.setVolume(11)#设置最大声录音 + self.AI_audio_queue = Queue(100) + self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) + + self.AI_send_data_queue = Queue(100) + self.AI_send_data_queue_thread_id = _thread.start_new_thread(self.AI_send_data_queue_thread, ()) + self.AI_client_tts_timer.start(1000,1,self.AI_client_tts_timer_cb) + EventMesh.publish("publish_message_event_ui",{"func":self.websocket_connect,"data":""}) + def websocket_connect(self,data): + self.websocket_connect_asr() + self.websocket_connect_chat() + self.websocket_connect_tts() + + pass + + def audio_cb(self,event): + if event == 0: + self.audio_play_state =1 + print('audio-play start.') + elif event == 7: + self.audio_play_state = 0 + # if self.AI_audio_queue.size() > 0: + # data = self.AI_audio_queue.get() + # print("audio_queue:data:{}".format(len(data))) + # if len(data) > 1: + # try: + # # while self.audio_play_state : + # # utime.sleep_ms(1) + # EventMesh.publish("system_sleep_rest")#防止黑屏 + # tmp=self.aud.playStream(3, data)#播放音频流文件 + # self.audio_play_state =1 + # print("playStream:{}".format(tmp)) + # except Exception as e: + # usys.print_exception(e) + print('audio-play finish.') + + #按下录音 + def talking_cb(self,e): + EventMesh.publish("publish_message_event_ui",{"func":self.talking,"data":e}) + #按下录音 + def talking(self,e): + #self.AI_client_tts_send("创建客户端, debug=True输出日志")#TTS语音播放AI结果 + #return + if 1:#EventMesh.publish(constants.EVENT.GET_CONNECT) == 1:#有卡时触发sos报警 无卡时自动关机 + if 1:#self.audio_play_start == 0: + self.audio_play_start = 1 + print("talking1") + #self.AI_list.add_flag(lv.obj.FLAG.HIDDEN)#隐藏列表选项方框 + if self.AI_client_asr_isconnect == 0: + self.websocket_connect_asr() + if self.AI_client_tts_isconnect == 0: + self.websocket_connect_tts() + if self.AI_client_chat_isconnect == 0: + self.websocket_connect_chat() + try: + self.record.stop()#关闭录音 + self.record.stream_start(audio.Record.AMRNB, 8000, 60) + except Exception as e: + usys.print_exception(e) + self.record_timer.start(1*1000,1,self.record_timer_cb) + self.aud.stopAll() + self.AI_client_chat_play_str = "" + self.AI_send_data_queue.clear()#清音频队列 + #self.AI_list.add_flag(lv.obj.FLAG.HIDDEN)#隐藏列表选项方框 + + AI_label.clear_flag(lv.obj.FLAG.HIDDEN) + AI_label.set_text("语音识别中...") + AI_press_label_countdown.clear_flag(lv.obj.FLAG.HIDDEN) + #AI_press_label_countdown.set_text("{}S".format(str(self.countdown))) + self.press_state = 1 + self.AI_client_asr_start() + else: + EventMesh.publish("load_screen", {"screen": "no_sim_popup_screen"}) + + #松开确认结果 + def sending_cb(self,e): + EventMesh.publish("publish_message_event_ui",{"func":self.sending,"data":e}) + def sending(self,e): + if self.press_state == 1: + if 1:#EventMesh.publish(constants.EVENT.GET_CONNECT) == 1:#有卡时触发sos报警 无卡时自动关机 + self.record.stop()#关闭录音 + self.record_timer.stop()#关闭定时器 + # self.record.stream_start(audio.Record.AMRNB, 8000, 2) + # self.record_timer.start(2*1000,0,self.record_timer_cb) + AI_label.clear_flag(lv.obj.FLAG.HIDDEN) + #AI_label.set_text(" ") + AI_press_label_countdown.clear_flag(lv.obj.FLAG.HIDDEN) + #AI_press_label_countdown.set_text(" ") + #Wechat_log("sending_cb1") + #EventMesh.publish("publish_message_event_ui",{"func":self.sending,"data":e}) + #self.sending(e) + #Wechat_log("sending_cb2") + self.audio_play_start = 0 + else: + self.press_state = 0 + #EventMesh.publish("load_screen", {"screen": "no_sim_popup_screen"}) + else: + pass + #定时器 + def record_timer_cb(self,*argc): + EventMesh.publish("system_sleep_rest")#防止黑屏 + Record_read_buf = bytearray(self.Audio_buf_size) + Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) + print("record_timer_cb:{}".format(Record_len)) + if Record_len >0: + read_buf = bytes(Record_read_buf[:Record_len]) + self.AI_client_asr_send(read_buf) +##################################################### + #AI识别链路 + def websocket_connect_chat(self): + print("websocket_connect_chat:{}".format(self.AI_client_chat_isconnect)) + #if self.AI_client_chat_isconnect == 0: + # 创建客户端, debug=True输出日志, ip和端口需要自己填写, 或者是域名 + self.AI_client_chat = uwebsocket.Client.connect('ws://121.40.67.151:9099/chat/ws', debug=True) + # 线程接收数据 + if self.AI_client_chat_recv_id != 0: + try: + _thread.stop_thread(self.AI_client_chat_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_chat_recv_id = 0 + self.AI_client_chat_recv_id = _thread.start_new_thread(self.AI_client_chat_recv, (self.AI_client_chat,)) + self.AI_client_chat_isconnect = 1 + def websocket_deconnect_chat(self): + if self.AI_client_chat_isconnect == 1: + self.AI_client_chat_isconnect = 0 + self.AI_client_chat.close() + + def AI_client_chat_send(self,data_str): + chat_data_str = "{\"type\":\"chat\",\"content\":\"%s\"}"%(data_str) + #self.AI_client_chat.send(chat_data_str) + self.AI_send_data_queue.put([self.AI_client_chat_num,chat_data_str]) + #Ai chat读数据 + def AI_client_chat_recv(self,cli): + while True: + try: + # 死循环接收数据 + recv_data = cli.recv() + self.AI_client_chat_isconnect = 1 + try: + #print("AI_client_chat_recv recv_data = {}".format(recv_data)) + jsload_data = ujson.loads(recv_data) + print("AI_client_chat_recv jsload_data = {}".format(jsload_data)) + if jsload_data["content"]: + #self.AI_client_tts_send(jsload_data["content"])#TTS语音播放AI结果 + self.AI_label_str+=str(jsload_data["content"]) + self.AI_client_chat_play_str+=str(jsload_data["content"]) + AI_label.set_text(self.AI_label_str) + EventMesh.publish("system_sleep_rest")#防止黑屏 + else: + # if self.AI_label_str: + # self.AI_client_tts_send(self.AI_label_str)#TTS语音播放AI结果 + self.AI_label_str = "" + if jsload_data['type']=='start': + self.AI_client_tts_start() + except Exception as e: + usys.print_exception(e) + if not recv_data: + # 服务器关闭连接或客户端关闭连接 + print("AI_client_chat_recv close") + #self.AI_client_chat_isconnect = 0 + #self.websocket_deconnect_chat() + break + except Exception as e: + usys.print_exception(e) + break + self.AI_client_tts_isconnect = 0 + self.AI_client_tts_recv_id = 0 +############################################################################## + #文字转语音链路 + def websocket_connect_tts(self): + print("websocket_connect_tts:{}".format(self.AI_client_tts_isconnect)) + #if self.AI_client_tts_isconnect == 0: + self.AI_client_tts = uwebsocket.Client.connect('ws://121.40.67.151:9099/tts/long-text-stream', debug=True) + # 线程接收数据 + if self.AI_client_tts_recv_id != 0: + try: + _thread.stop_thread(self.AI_client_tts_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_tts_recv_id = 0 + _thread.stack_size(20*1024) + self.AI_client_tts_recv_id = _thread.start_new_thread(self.AI_client_tts_recv, (self.AI_client_tts,)) + _thread.stack_size(8*1024) + self.AI_client_tts_isconnect = 1 + def websocket_deconnect_tts(self): + if self.AI_client_tts_isconnect == 1: + self.AI_client_tts_isconnect = 0 + self.AI_client_tts.close() + def AI_client_tts_timer_cb(self,*argc): + if len(self.AI_client_chat_play_str) >0 and self.AI_client_tts_data_len < 50*1024: + play_str = sub_utf8_String(self.AI_client_chat_play_str,30)#按中文切割字符串 + self.AI_client_tts_send(play_str)#TTS语音播放AI结果 + self.AI_client_chat_play_str = self.AI_client_chat_play_str[len(play_str):] + print("TTS play:{}!!!!!!".format(play_str)) + def AI_client_tts_send(self,data_str): + data_l ={"header": {"event": "RunSynthesis","trace_id": "3fa6b9e6a30b407ea38ad7ae69e6c291"},"payload": {"text":"%s"%(data_str)}} + json_data = ujson.dumps(data_l) + #print("AI_client_tts_send:{}".format(json_data)) + # self.record.stop()#关闭录音 + # self.record_timer.stop()#关闭定时器 + #self.AI_client_tts.send(json_data) + self.AI_send_data_queue.put([self.AI_client_tts_num,json_data]) + def AI_client_tts_start(self): + data_l ={"header": {"event": "StartSynthesis","trace_id": "3fa6b9e6a30b407ea38ad7ae69e6c291"}, + "payload": {"voice":"xiaoyun","format":"mp3","sample_rate":8000,"volume":50,"speech_rate":0,}} + print("\r\nAI_client_tts_start!!!!!!!!!!!!\r\n") + json_data = ujson.dumps(data_l) + #print("AI_client_tts_send:{}".format(json_data)) + self.record.stop()#关闭录音 + self.record_timer.stop()#关闭定时器 + #self.AI_client_tts.send(json_data) + self.AI_send_data_queue.put([self.AI_client_tts_num,json_data]) + #文字转语音链路读数据 + def AI_client_tts_recv(self,cli): + tts_data = bytes() + + while True: + try: + # 死循环接收数据 + recv_data = cli.recv() + + self.AI_client_tts_isconnect = 1 + try: + print("AI_client_tts_recv :{}".format(len(recv_data))) + # #结束标志{"header":{"event":"EndSynthesis","msg":"success","status":20000000,"task_id":"ffd727e620000803280c5a6498d0e321"}} + # if isinstance(recv_data, str): + # print("AI_client_tts_recv:str recv_data:{}".format(recv_data)) + # if recv_data.find('\"event\":\"EndSynthesis\"') != -1: + # tts_data = tts_data[9:] + # self.AI_audio_queue.put(tts_data) + # print("tts_data:{}".format(len(tts_data))) + # self.AI_client_tts_data_len +=len(tts_data) + # tts_data = b'' + # else: + # tts_data+=recv_data + self.AI_client_tts_data_len +=len(recv_data) + self.AI_audio_queue.put(recv_data) + #utime.sleep_ms(1000) + except Exception as e: + usys.print_exception(e) + # print(recv_data) + # print(type(recv_data)) + if not recv_data: + # 服务器关闭连接或客户端关闭连接 + print("AI_client_tts_recv close") + #self.AI_client_tts_isconnect = 0 + break + except Exception as e: + usys.print_exception(e) + break + self.AI_client_tts_isconnect = 0 + self.AI_client_tts_recv_id = 0 +####################################################################### + #语音转文字链路 + def websocket_connect_asr(self): + print("websocket_connect_asr:{}".format(self.AI_client_asr_isconnect)) + #if self.AI_client_asr_isconnect == 0: + self.AI_client_asr = uwebsocket.Client.connect('ws://121.40.67.151:9099/asr/ws', debug=True) + # 线程接收数据 + if self.AI_client_asr_recv_id != 0: + try: + _thread.stop_thread(self.AI_client_asr_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_asr_recv_id = 0 + self.AI_client_asr_recv_id = _thread.start_new_thread(self.AI_client_asr_recv, (self.AI_client_asr,)) + self.AI_client_asr_isconnect = 1 + def websocket_deconnect_asr(self): + if self.AI_client_asr_isconnect == 1: + self.AI_client_asr_isconnect = 0 + self.AI_client_asr.close() + def AI_client_asr_send(self,data_bytes): + #self.AI_client_asr.send(data_bytes) + self.AI_send_data_queue.put([self.AI_client_asr_num,data_bytes]) + def AI_client_asr_start(self): + self.AI_send_data_queue.put([self.AI_client_asr_num,"{\"header\":{\"event\":\"StartTranscription\",\"trace_id\":\"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"amr\",\"sample_rate\":8000}}"]) + #语音转文字链路读数据 + def AI_client_asr_recv(self,cli): + + while True: + try: + # 死循环接收数据 + recv_data = cli.recv() + try: + if isinstance(recv_data, str): + jsload_data = ujson.loads(recv_data) + print("jsload_data = {}".format(jsload_data)) + if jsload_data["result"]["result"]: + AI_press_label_countdown.set_text(str(jsload_data["result"]["result"])) + if jsload_data["result"]["is_end"] == True: #{"type":"chat","content":"你好"} + + #chat_data = bytearray(chat_data_str) + AI_label.set_text("思考中...") + self.AI_client_chat_send(str(jsload_data["result"]["result"])) + print("chat_data:{}".format(str(jsload_data["result"]["result"]))) + self.record.stop()#关闭录音 + self.record_timer.stop()#关闭定时器 + elif jsload_data["result"]["is_end"] == False: + AI_label.set_text("语音识别中...") + except Exception as e: + usys.print_exception(e) + if not recv_data: + # 服务器关闭连接或客户端关闭连接 + print("AI_client_asr_recv close") + #self.AI_client_asr_isconnect = 0 + #self.websocket_deconnect_asr() + break + except Exception as e: + + + usys.print_exception(e) + break + self.AI_client_asr_isconnect = 0 + self.AI_client_asr_recv_id = 0 + ############################################### + #发送数据队列 + def AI_send_data_queue_thread(self): + while True: + #data[0]是websocket连接对象编号,data[1]是数据 + data = self.AI_send_data_queue.get() + if self.AI_client_asr_isconnect == 0: + self.websocket_connect_asr() + if self.AI_client_tts_isconnect == 0: + self.websocket_connect_tts() + if self.AI_client_chat_isconnect == 0: + self.websocket_connect_chat() + + if data != None: + try: + #data["func"](data["data"]) + # self.record.stop() + # self.aud.playStream(4, data) + # self.record.stream_start(audio.Record.AMRNB, 8000, 60) + # 发送数据 + #bytes_data = bytes(data[1]) + #data[0].send(data[1]) + if data[0] == self.AI_client_chat_num: + self.AI_client_chat.send(data[1]) + print("chat send_data:{}".format(len(data[1]))) + if data[0] == self.AI_client_asr_num: + print("asr send_data:{}".format(len(data[1]))) + self.AI_client_asr.send(data[1]) + if data[0] == self.AI_client_tts_num: + print("tts send_data:{}".format(len(data[1]))) + self.AI_client_tts.send(data[1]) + except Exception as e: + print("DeviceInfo_queue_thread:ERROR:{}".format(e)) + usys.print_exception(e) + if data[0] == self.AI_client_chat_num: + self.AI_client_chat_isconnect = 0 + self.websocket_connect_chat() + if data[0] == self.AI_client_asr_num: + self.AI_client_asr_isconnect = 0 + self.websocket_connect_asr() + if data[0] == self.AI_client_tts_num: + count =10 + while 1: + count-=1 + if count<=0: + break + self.AI_client_tts_isconnect = 0 + self.websocket_connect_tts() + if self.AI_client_tts_isconnect == 1: + self.AI_client_tts.send(data[1]) + ############################################ + # 音频队列 + def AI_audio_queue_thread(self): + while True: + if self.audio_play_state == 0: + data = self.AI_audio_queue.get() + print("audio_queue:data:{}".format(len(data))) + + if len(data) > 1: + # f = open("/usr/test.mp3", "w+") + # f.write(data) + try: + # while self.audio_play_state: + # utime.sleep_ms(1) + print("1playStream!!!!!!!") + #data = data[9:] + tmp=self.aud.playStream(3, data)#播放音频流 + print("2playStream:tmp:{} data len:{}".format(tmp,len(data))) + EventMesh.publish("system_sleep_rest")#防止黑屏 + self.AI_client_tts_data_len -=len(data) + except Exception as e: + print("AI_audio_queue_thread:ERROR:") + usys.print_exception(e) + + def Close(self): + try: + #销毁线程 + if self.AI_audio_queue_thread_id != 0: + try: + _thread.stop_thread(self.AI_audio_queue_thread_id) + except Exception as e: + usys.print_exception(e) + self.AI_audio_queue_thread_id = 0 + if self.AI_send_data_queue_thread_id != 0: + try: + _thread.stop_thread(self.AI_send_data_queue_thread_id) + except Exception as e: + usys.print_exception(e) + self.AI_send_data_queue_thread_id = 0 + if self.AI_client_chat_recv_id != 0: + try: + _thread.stop_thread(self.AI_client_chat_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_chat_recv_id = 0 + if self.AI_client_tts_recv_id != 0: + try: + _thread.stop_thread(self.AI_client_tts_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_tts_recv_id = 0 + if self.AI_client_asr_recv_id != 0: + try: + _thread.stop_thread(self.AI_client_asr_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_asr_recv_id = 0 + except Exception as e: + usys.print_exception(e) + self.AI_send_data_queue.clear() + self.record_timer.stop() + self. AI_client_tts_timer.stop() + self.aud.stopAll() + self.AI_client_chat_play_str = "" + self.record.stop()#关闭录音 + try: + self.websocket_deconnect_asr() + self.websocket_deconnect_tts() + self.websocket_deconnect_chat() + except Exception as e: + usys.print_exception(e) + def done_return(self): + EventMesh.publish("load_screen", {"screen": "main_menu"}) + + + def btn1_release(self): + EventMesh.publish("load_screen", {"screen": "main_menu"}) + pass + + def btn8_press(self): + EventMesh.publish("load_screen", {"screen": "main_menu"}) + pass \ No newline at end of file