增加4G ai对话 有bug待后续完善
authorhcj <1224667075@qq.com>
Tue, 17 Jun 2025 10:01:29 +0000 (18:01 +0800)
committerhcj <1224667075@qq.com>
Tue, 17 Jun 2025 10:01:29 +0000 (18:01 +0800)
code/sdk/application/wearable/service/ble_service/bleservice/bleService.c
code/sdk/drivers/peripheral/slip/cat1Service/cat1_at_cmd.cpp
code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.cpp
code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.h
code/sdk/drivers/peripheral/utils/custom_cmd_service.c
code/sdk/drivers/peripheral/utils/custom_utils.h
code/sdk/middleware/services/media/foundation/sample/wrapper/voice_call_volte_sample_wrapper.cpp

index b2ac61c..c09a761 100644 (file)
@@ -2401,7 +2401,15 @@ static void blesrvAi_send_service_task(void const *arg)
                     AIStreamWrite(mail.msg_data,mail.msg_len);\r
                 }\r
                 else{\r
-                    send_ai_bin(mail.msg_data,mail.msg_len);\r
+                    if(server_handle==2)\r
+                    {\r
+                        uapi_stop_asr();\r
+                    }\r
+                    else\r
+                    {\r
+                        send_ai_bin(mail.msg_data,mail.msg_len);\r
+                    }\r
+                    \r
                 }\r
             }\r
         }\r
@@ -2493,7 +2501,7 @@ int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type)
     // uint32_t singlebuflen= ble_nus_max_data_len-20;\r
     uint32_t singlebuflen = 0;\r
     if(getselect4gorbtai() == 1) {\r
-        singlebuflen = 600;//4G\r
+        singlebuflen = 288*5;//4G\r
     } else {\r
         singlebuflen = ble_nus_max_data_len-20;//BT\r
     }\r
@@ -2573,7 +2581,10 @@ int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type)
                 {\r
                     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"len<=singlebuflen Error blesrv Check send failed! err=0x%X %d! \r\n", ret,send_msg_data_into_Ai_msg_queue_count);\r
                 }\r
+                \r
             }\r
+\r
+            \r
            \r
 \r
             blesrv_mail.service_handle = 2;\r
index 44b1d6e..c1ddbc0 100644 (file)
@@ -489,7 +489,7 @@ static uint32_t start_asr(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
 
     return 0;
 }
-extern void record_test(void);
+extern void uapi_start_asr_task(void);
 
 static uint32_t sendbindata(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
 {
@@ -502,7 +502,7 @@ static uint32_t sendbindata(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
         printf("CAT1TEST ATCMD Convert Command Failed\n");
         return ERRCODE_FAIL;
     }
-    record_test();
+    uapi_start_asr_task();
     return 0;
 }
 #include "dev_authentication.h"
index e7dcda5..88f52bc 100644 (file)
 #include "cat1_ctl.h"
 #include "voice_call_volte_sample_wrapper.h"
 
+#include "audio_capture_wrapper.h"
+#include <ui_service.h>
+#include <bleService.h>
+#include "cat1_ws.h"
+
 
 #ifdef __cplusplus
 extern "C" {
@@ -122,8 +127,9 @@ extern "C" {
 #include <stdint.h>
 
 extern osStatus_t osDelay(uint32_t ticks);
-#define cat1_msleep(ms) osDelay(ms) /* osDelay(1) : 1ms */
+#define cat1_msleep(ms) LOS_Msleep(ms) /* osDelay(1) : 1ms */
 
+static osTimerId_t g_timer_time_sync = NULL;
 
 void bk_sleep(uint32_t time)
 {
@@ -145,8 +151,9 @@ void uapi_start_tts_single_txt(char *buf);
 
 void uapi_start_tts_single(void);
 
-uint8_t server_state=0;
+static uint8_t server_state=0;
 
+static uint8_t disconnect_server=0;
 // free
 
 // 定义一个结构体来存储解析后的URL部分
@@ -463,7 +470,8 @@ int send_handshake(WebSocketClient *client,const char *headers) {
 
 // 发送 WebSocket 消息
 int send_websocket_message(WebSocketClient *client, const unsigned char *message, size_t message_len, WebSocketMessageType type) {
-    unsigned char *frame=( unsigned char *)aic_zalloc(message_len + 50);
+
+    uint8_t* frame=( uint8_t *)aic_zalloc(message_len + 50);
     int offset = 0;
     int ret = -1;
     if(!frame)
@@ -476,8 +484,12 @@ int send_websocket_message(WebSocketClient *client, const unsigned char *message
 
     // 第二个字节:设置掩码位和负载长度
     if (message_len < 126) {
+
         frame[offset++] = 0x80 | message_len;
-    } else if (message_len < 65536) {
+
+    }
+     else if (message_len < 65536) 
+     {
         frame[offset++] = 0x80 | 126;
         uint16_t len = htons((uint16_t)message_len);
         memcpy(frame + offset, &len, 2);
@@ -504,6 +516,8 @@ int send_websocket_message(WebSocketClient *client, const unsigned char *message
     offset += message_len;
 
     // 发送帧
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"offset %d\r\n",offset);
+
     if (send(client->sockfd, frame, offset, 0) < 0) {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send message failed\r\n");
         ret =  -1;
@@ -511,8 +525,9 @@ int send_websocket_message(WebSocketClient *client, const unsigned char *message
     }
     ret = 0;
 send_websocket_message_ERROR:
-    free(frame);
     bk_sleep(message_len/1000+1);
+    free(frame);
+    // bk_sleep(message_len/1000+1);
     return ret;
 }
 
@@ -538,7 +553,7 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
         if (bytes_received <= 0) 
         {
             if (bytes_received == 0) {
-                printf("Connection closed by server.\n");
+                printf("Connection lwip_closed by server.\n");
             } else {
                 printf("receive message failed");
             }
@@ -569,7 +584,7 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
     bytes_received = recv(client->sockfd, frame+1, 1, 0);
     if (bytes_received <= 0) {
         if (bytes_received == 0) {
-            printf("Connection closed by server.\n");
+            printf("Connection lwip_closed by server.\n");
         } else {
             printf("receive message failed");
         }
@@ -599,6 +614,12 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
     if(payload_len>len)
     {
         printf("receive_websocket_message:len =%d ERROR",payload_len);
+
+        // if(*code == WS_BINARY)
+        // {
+
+        // }
+        // fopen
         recv(client->sockfd, buf, len, 0);
         return -1;
     }
@@ -732,7 +753,7 @@ WebSocketClient *init_websocket_client(const char *url, const char *headers,uint
 
     client->sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (client->sockfd < 0) {
-        printf("socket creation failed");
+        printf("socket creation failed %d \r\n",client->sockfd);
         goto client_ERROR;
     }
 
@@ -765,7 +786,7 @@ WebSocketClient *init_websocket_client(const char *url, const char *headers,uint
 
     if (connect(client->sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
         printf("init_websocket_client connection failed\r\n");
-        close(client->sockfd);
+        lwip_close(client->sockfd);
         goto client_ERROR;
     }
      printf("socket creation succ\r\n");
@@ -777,7 +798,7 @@ WebSocketClient *init_websocket_client(const char *url, const char *headers,uint
     client->port = parsed_url.port;
     if (send_handshake(client,headers) < 0) { //发送头
          printf("socket send_handshake client_ERROR");
-        close(client->sockfd);
+        lwip_close(client->sockfd);
         goto client_ERROR;
     }
 
@@ -827,11 +848,11 @@ client_ERROR:
 }
 
 // 关闭 WebSocket 客户端
-void close_websocket_client(WebSocketClient *client) {
+void lwip_close_websocket_client(WebSocketClient *client) {
     if(client)
     {
-        printf("close_websocket_client ");
-        // close(client->sockfd);
+        printf("lwip_close_websocket_client ");
+        lwip_close(client->sockfd);
         free(client);
     }
     
@@ -1027,13 +1048,14 @@ void bk_websocket_demo(void *data) {
         if(server_state==0b00000111)
         {
             // uapi_start_asr();
+            //  uapi_start_tts_single_txt("2025英超夏季转会窗分阶段开启第一阶段2025年6月1日至2025年6月11日凌晨2:00北京时间第二阶段常规夏窗2025年6月16日至2025年9月2日凌晨2:00北京时间本贴将持续更新2025年夏窗以曼联转会相关为主的新闻和消息。");
         }
 
         
         // send_ai_txt(1,"你是谁");
         set_ai_chat_service_init_flag(1);
         WebSocketMessageType type;
-        while (1)
+        while (disconnect_server==0)
         {
             memset_s(recv_buf,1024,0,1024);
             int len= receive_websocket_message(chat_client,(unsigned char *)recv_buf,1024,&type);
@@ -1045,8 +1067,8 @@ void bk_websocket_demo(void *data) {
             printf("bk_websocket_demo %s",recv_buf);
         }
 
-        
-        close_websocket_client(chat_client);
+        set_ai_chat_service_init_flag(0);
+        lwip_close_websocket_client(chat_client);
         chat_client=NULL;
         is_bk_websocket_chat=0;
         return ;
@@ -1060,7 +1082,7 @@ void bk_websocket_demo(void *data) {
 #include "common_def.h"
 #include "los_printf.h"
 
-#define CAT1_SERVICE_TASK_SIZE       (3072)
+#define CAT1_SERVICE_TASK_SIZE       (2048)
 #define CAT1_SERVICE_TASK_PRIO       5
 
 
@@ -1079,7 +1101,7 @@ errcode_t chat_server_init(void)
     taskcfg.create_queue_handle = NULL;
     taskcfg.attr.name="chat_server_init";
     taskcfg.attr.priority = TASK_PRIORITY_APP;
-    taskcfg.attr.stack_size = CAT1_SERVICE_TASK_SIZE;
+    taskcfg.attr.stack_size = 0x1000;
     taskcfg.func = bk_websocket_demo;
     taskcfg.use_mem = USE_EXTEND_MEM;
   
@@ -1105,20 +1127,49 @@ WebSocketClient *asr_client;
 int32_t uapi_tts_paly_init(void);
 
 int32_t uapi_tts_paly_stop(void);
+
+
 //查询是否可以对话
 void uapi_start_asr(void)
 {
+//    bool state = true;
+//     SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
+
     char *jsonString = "{\"header\":{\"event\":\"StartTranscription\",\"trace_id\": \
-     \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}";
+      \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}";
 
     if (send_websocket_message(asr_client,( const unsigned char *)jsonString,strlen(jsonString),WS_TEXT)==0)
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
     else
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message  erro\n");
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message  erro\n");
+        // lwip_close_websocket_client(asr_client);
+        // asr_client=NULL;
+        // disconnect_server=1;
+    }
 
     // uapi_tts_paly_stop();
 }
 
+void uapi_start_asr_task(void)
+{
+
+    char *jsonString = "{\"header\":{\"event\":\"StartTranscription\",\"trace_id\": \
+     \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}";
+
+    if (send_websocket_message(asr_client,( const unsigned char *)jsonString,strlen(jsonString),WS_TEXT)==0)
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
+    else
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message  erro\n");
+        // lwip_close_websocket_client(asr_client);
+        // asr_client=NULL;
+        // disconnect_server=1;
+    }
+}
+
+
+
 //停止对话
 void uapi_stop_asr(void)
 {
@@ -1128,18 +1179,26 @@ void uapi_stop_asr(void)
 
     if (send_websocket_message(asr_client,( const unsigned char *)jsonString,strlen(jsonString),WS_TEXT)==0)
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
+    else
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message  erro\n");
+        // lwip_close_websocket_client(asr_client);
+        // asr_client=NULL;
+
+        // disconnect_server=1;
+    }
 }
 
 
 
 void send_ai_bin(char *buf,uint32_t len)
 {
-    // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_ai_bin 0\n");
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_ai_bin %d\n",len);
 
     if (send_websocket_message(asr_client,( const unsigned char *)buf,len,WS_BINARY)==0)
     {
-        // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
-        bk_sleep(10);
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
+        bk_sleep(300);
     }
     else
     {
@@ -1151,10 +1210,11 @@ void send_ai_bin(char *buf,uint32_t len)
 void record_test(void)
 {
     char buf[1024]={0};
+    bk_sleep(1000);
 
-    FILE *fp=fopen("/user/aiRecord.sbc","rb+");
+    FILE *fp=fopen("/user/aiRecord.bin","rb+");
     int read_size;
-
+    int read_size_num=0;
     uint32_t pack=1024;
 
     if(fp==NULL)
@@ -1169,15 +1229,15 @@ void record_test(void)
         if(read_size!=0)
         {
             send_ai_bin(buf,read_size);
-            bk_sleep(10);
+            bk_sleep(100);
+            read_size_num+=read_size;
+
         }
     
     } while (read_size==1024);
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"record_test %d\n",read_size_num);
     fclose(fp);
 }
-#include "audio_capture_wrapper.h"
-#include <ui_service.h>
-#include <bleService.h>
 
 // {"status":20000000,"msg":"success","task_id":"80228c9f62821ffe3f867e1a5042d887","event":"TranscriptionStarted","result":null}
 // {"status":20000000,
@@ -1185,7 +1245,17 @@ void record_test(void)
 // "event":"SentenceEnd","result":{"result":"你好你好你好你好。",
 // "is_end":true,"sentence_id":"fcb889fe74a0920efe14939de2fd2b1b",
 // "start_time":760,"end_time":2280}} 
+void asr_record_test(void)
+{
+    record_test();
+    bk_sleep(500);
+    uapi_stop_asr();
+}
+
+
  char result_str_asr[256]={0};
+uint8_t asr_is_start=0;
+
 bool aer_result_analysis(char *buf)
 {
     cJSON* root = cJSON_Parse((const char *)buf);
@@ -1237,6 +1307,8 @@ bool aer_result_analysis(char *buf)
         memcpy_s(result_str_asr,256,result_str,strlen(result_str));
         SendMsgToUIService(UI_AI_JUMP_TO_RESULT_VIEW, 0, nullptr, 0);//先跳转到结果界面
         ble_set_ai_Question_str(result_str_asr);
+        stop_AudioCaptureProcess_AI();
+        // uapi_stop_asr();
         send_ai_txt(0,result_str_asr);
 
         // uapi_stop_asr();
@@ -1247,12 +1319,38 @@ bool aer_result_analysis(char *buf)
         //查询结果
         // streamingRecordAndSend_AI(0);
         // startAudioCaptureTask();
-        bool state = true;
-        SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
+        if(asr_is_start==0)
+        {
+            asr_is_start=1;
+
+            bool state = true;
+            SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
+
+            // do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_AI_SEND_TEST, nullptr, 0);
+           
+        }
+        else
+        {
+            // uapi_stop_asr();
+        }
+      
+    }
+     if(strcmp(event_str,"TranscriptionCompleted")==0) //可以对话了,切换到聆听界面
+    {
+        //查询结果
+        // streamingRecordAndSend_AI(0);
+        // startAudioCaptureTask();
+        {
+            asr_is_start=0;
+        }
+    
     }
     if(strcmp(event_str,"Failed")==0)
     {
         stop_AudioCaptureProcess_AI();
+        asr_is_start=0;
+        // uapi_stop_asr();
+        return false;
     }
 
     cJSON_Delete(root);
@@ -1321,7 +1419,9 @@ void bk_websocket_asr(void *data) {
             }
         }
         if (asr_client== NULL){
-           return;
+
+            is_bk_websocket_asr=0;
+            return;
         }
 
         uapi_cat1_keep_wakeup(1);
@@ -1330,31 +1430,35 @@ void bk_websocket_asr(void *data) {
         if(server_state==0b00000111)
         {
             // uapi_start_asr();
+            //  uapi_start_tts_single_txt("2025英超夏季转会窗分阶段开启第一阶段2025年6月1日至2025年6月11日凌晨2:00北京时间第二阶段常规夏窗2025年6月16日至2025年9月2日凌晨2:00北京时间本贴将持续更新2025年夏窗以曼联转会相关为主的新闻和消息。");
         }
        
         asr_recv_buf=(char *)malloc(0x400);
-        
+        set_ai_asr_service_init_flag(1);
         // uapi_start_asr();
         // SendMsgToUIService(UI_4G_AI_INIT_STAUS, 0,&state, sizeof(bool));
-        set_ai_asr_service_init_flag(1);
-        while (1)
+        
+        while (disconnect_server==0)
         {
             memset_s(asr_recv_buf,1024,0,1024);
             int len=  receive_websocket_message(asr_client,(unsigned char *)asr_recv_buf,1024,&type);
             // if(type==WS_CONT || 
             if( type==WS_CLOSE || len==-1)
             {
-                printf("WS_CLOSE bk_websocket_asr\r\n");
+                printf("WS_lwip_close bk_websocket_asr\r\n");
                 break;
             }
             printf(" bk_websocket_asr %d\r\n",type);
             printf("bk_websocket_demo %s %d",asr_recv_buf,type);
-            aer_result_analysis(asr_recv_buf);
+            if(false== aer_result_analysis(asr_recv_buf))
+            {
+                break;
+            }
             //
         }
-
+        set_ai_asr_service_init_flag(0);
         free(asr_recv_buf);
-        close_websocket_client(asr_client);
+        lwip_close_websocket_client(asr_client);
         asr_client=NULL;
         // return;
     }
@@ -1459,19 +1563,19 @@ void uapi_start_tts_single(void)
  
  void uapi_start_tts_single_txt(char *buf)
 {
-    char *jsonString = (char *)malloc(512);
+    char *jsonString = (char *)malloc(strlen(buf)+300);// [512] ={0};
  
-    sprintf(jsonString, " {  \"header\": {  \
-        \"event\": \"LongTextStream\",\
-        \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\" \
-    }, \
-    \"payload\": { \
-        \"voice\":\"xiaoyun\", \
-        \"format\":\"mp3\", \
-        \"sample_rate\":16000, \
-        \"volume\":50, \
-        \"speech_rate\":0, \
-        \"text\":\"%s\" \
+    sprintf(jsonString, " {  \"header\": { \
+        \"event\": \"LongTextStream\", \
+        \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\"\
+    },\
+    \"payload\": {\
+        \"voice\":\"xiaoyun\",\
+        \"format\":\"pcm\",\
+        \"sample_rate\":16000,\
+        \"volume\":50,\
+        \"speech_rate\":0,\
+        \"text\":\"%s\"\
      } } ",buf);
 
      printf("uapi_start_tts : %s\r\n",jsonString);
@@ -1480,8 +1584,7 @@ void uapi_start_tts_single(void)
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
 
     free(jsonString);
-    jsonString = NULL;
-}
+    }
 
 // char *tts_txt_str=
 
@@ -1508,9 +1611,55 @@ void send_tts_txt(char *msg)
 }
 
 
+// send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf,len,200);
+
+void tts_cache_file(uint8_t *data, uint32_t len)
+{
+    FILE *fp= fopen("/user/tts.bin","ab+");
+    if(fp)
+    {
+        fwrite(data,1,len,fp);
+        fclose(fp);
+    }
+}
+
+
+void tts_paly()
+{
+    char buf[1024]={0};
+
+    FILE *fp=fopen("/user/tts.bin","rb+");
+    int read_size;
+
+    uint32_t pack=1024;
+
+    if(fp==NULL)
+    {
+        return;
+    }
+    do
+    {
+        memset_s(buf,1024,0,1024);
+        read_size=fread(buf,1,1024,fp);
+
+        if(read_size!=0)
+        {
+            AIStreamWrite((uint8_t *)buf,read_size);
+            bk_sleep(10);
+        }
+    
+    } while (read_size==1024);
+    fclose(fp);
 
+    fp= fopen("/user/tts.bin","wb+");
+    if(fp)
+    {
+        fclose(fp);
+    }
+}
 
 uint8_t  is_bk_websocket_tts=0;
+uint32_t bk_websocket_tts_tick=0;
 
 
 void bk_websocket_tts(void *data) {
@@ -1565,12 +1714,16 @@ void bk_websocket_tts(void *data) {
         if(server_state==0b00000111)
         {
             // uapi_start_asr();
+            // uapi_start_tts_single_txt("2025英超夏季转会窗分阶段开启第一阶段2025年6月1日至2025年6月11日凌晨2:00北京时间第二阶段常规夏窗2025年6月16日至2025年9月2日凌晨2:00北京时间本贴将持续更新2025年夏窗以曼联转会相关为主的新闻和消息。");
         }
 
+//    uapi_start_tts_single();
        
         char* tts_recv_buf=(char *)malloc(1024*4);
 
-        while (1)
+        set_ai_tts_server_init_flag(1);
+
+        while (disconnect_server==0)
         {
             
             
@@ -1579,21 +1732,38 @@ void bk_websocket_tts(void *data) {
             // if(type==WS_CONT || 
             if( type==WS_CLOSE || len==-1)//|| type==WS_CONT )
             {
-                printf("WS_CLOSE bk_websocket_tts\r\n");
+                printf("WS_lwip_close bk_websocket_tts\r\n");
                 break;
             }
-            printf("bk_websocket_tts  bk_websocket_tts %d\r\n",type);
+            // printf("bk_websocket_tts  bk_websocket_tts %d\r\n",type);
             if(type==WS_BINARY)
             {
+                if(bk_websocket_tts_tick==0)
+                {
+                    bk_websocket_tts_tick = (uint32_t)osKernelGetTickCount();
+                }
+                // tts_cache_file((uint8_t *)tts_recv_buf,len);——
                 // AIStreamWrite((uint8_t *)tts_recv_buf,len);
                 send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf,len,200);
+
+                // 
+                bk_sleep(1);
+                printf("bk_websocket_demo %d\n",len);
+            }
+            else
+            {
+                printf("bk_websocket_demo %s %d %d\n",tts_recv_buf,bk_websocket_tts_tick,(uint32_t)osKernelGetTickCount());
+                bk_websocket_tts_tick=0;
+                // tts_paly();
             }
 
             // aer_result_analysis(asr_recv_buf);
-            printf("bk_websocket_demo %s %d",tts_recv_buf,type);
+           
         }
 
-        close_websocket_client(tts_client);
+        set_ai_tts_server_init_flag(0);
+
+        lwip_close_websocket_client(tts_client);
         tts_client=NULL;
         // return;
     }
@@ -1613,7 +1783,7 @@ errcode_t tts_server_init(void)
     app_task_definition_t taskcfg={0};
     taskcfg.create_queue_handle = NULL;
     taskcfg.attr.name="tts_server_init";
-    taskcfg.attr.priority = TASK_PRIORITY_APP;
+    taskcfg.attr.priority = (osPriority_t)35;
     taskcfg.attr.stack_size = 0x1000;
     taskcfg.func = bk_websocket_tts;
     taskcfg.use_mem = USE_EXTEND_MEM;
@@ -1669,12 +1839,25 @@ int32_t uapi_tts_paly_stop(void)
 }
 
 
+static void time_sync_func(void)
+{
+
+}
+
+
 
 
 bool init_ai_task(void)
 {
     if(is_init_ai)
     {
+        disconnect_server=0;
+
+         g_timer_time_sync = osTimerNew((osTimerFunc_t)time_sync_func, osTimerPeriodic, NULL, NULL);
+        if (g_timer_time_sync == NULL)
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"g_timer_time_sync create failed\n");
+
+
         uapi_tts_paly_init();
         asr_server_init();
         chat_server_init();
@@ -1683,8 +1866,32 @@ bool init_ai_task(void)
     }
 
     return true;
+}
 
-   
+
+
+bool dinit_ai_task(void)
+{
+    if(is_init_ai)
+    {
+        disconnect_server=0;
+
+        if(g_timer_time_sync==NULL)
+        {
+            g_timer_time_sync = osTimerNew((osTimerFunc_t)time_sync_func, osTimerPeriodic, NULL, NULL);
+        }
+        if (g_timer_time_sync == NULL)
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"g_timer_time_sync create failed\n");
+
+
+        uapi_tts_paly_init();
+        asr_server_init();
+        chat_server_init();
+        tts_server_init();
+        // is_init_ai=0;
+    }
+
+    return true;
 }
 
 
index c48092d..9ae2c7f 100644 (file)
@@ -20,6 +20,9 @@ uint8_t get_ai_chat_service_init_flag(void);
 void set_ai_chat_service_init_flag(uint8_t flag);
 uint8_t get_ai_tts_server_init_flag(void);
 void set_ai_tts_server_init_flag(uint8_t flag);
+void uapi_start_asr_task(void);
+
+void asr_record_test(void);
 #ifdef __cplusplus
 #if __cplusplus
 }
index 4e75f92..1e2f9b5 100644 (file)
@@ -338,7 +338,10 @@ static void custom_cmd_service_check_task(void const *arg)
             hfp_ag_cmd_conn_last_hfp();
             break;
         
-        
+         case CUSTOM_CMD_MSG_AI_SEND_TEST:
+             asr_record_test();
+             
+            break;
         default:
             PRINT("Unknown CUSTOM_CMD_MSG cmd 0x%02X \r\n", mail.cmd);
             break;
index b9f3b58..db7c88e 100644 (file)
@@ -266,6 +266,8 @@ uint32_t uapi_calendar_store_date(void);
 
 #define CUSTOM_CMD_MSG_CONNECT_AG 0x1B
 
+#define CUSTOM_CMD_MSG_AI_SEND_TEST 0x1C
+
 typedef enum {
     BR_HFP_CONNECT=1,
     BR_HFP_DISCONNECT,
index d9529de..9ca9a9a 100755 (executable)
@@ -1159,7 +1159,7 @@ static int32_t RunVoiceCallVolteAI(VoiceCallVolteContext *context)
 
     // 16000 32000 44100 48000
     AudioRendererConfig  renderConfig = {};
-    renderConfig.audioFormat =  MP3;
+    renderConfig.audioFormat =  PCM;
     renderConfig.channelCount = 1;
     renderConfig.linkDir = AUDIO_DOWN_LINK;
     renderConfig.sampleRate = 16000;
@@ -1234,6 +1234,7 @@ static void *VoiceAIThread(void *priv)
 }
 
 #include "bleService.h"
+#include "cat1_ws.h"
 static uint32_t AI_audio_size=0;
 static uint8_t Cur_AI_Model= 0;
 
@@ -1243,7 +1244,11 @@ uint8_t AudioCaptureTask_is_run=0;
 
 void stop_AudioCaptureProcess_AI(void)
 {
-    g_sample_needStop=1;
+    if(AudioCaptureTask_is_run)
+    {
+        g_sample_needStop=1;
+    }
+   
 }
 
 static void* AudioCaptureProcess_AI(void* arg)
@@ -1284,6 +1289,8 @@ static void* AudioCaptureProcess_AI(void* arg)
 
     AudioCaptureTask_is_run=1;
 
+    FILE *fp= fopen("/user/aiRecord.bin","wb+");
+
     while (!g_sample_needStop) {
         LOS_Msleep(10);
         CapturerBuffer capBuf = {};
@@ -1293,13 +1300,21 @@ static void* AudioCaptureProcess_AI(void* arg)
         int32_t readlen = g_stream_in_uplink->ObtainBuffer(capBuf, true);
         if(readlen>0)
         {
-            //  MEDIA_ERR_LOG("readlen %d",readlen);
+             MEDIA_ERR_LOG("readlen %d",readlen);
             send_msg_data_into_Ai_msg_queue((void *)data_buf,(uint16_t)readlen,0);
+            // fwrite(data_buf,1,readlen,fp);
             memset_s(data_buf,1024,0,1024);
         }
         
         // 蓝牙接口发送实时录音数据
     }
+    uint8_t start_data[19]={0};
+    AI_data_assembly(0,start_data,Cur_AI_Model);
+    fclose(fp);
+    send_msg_data_into_Ai_msg_queue((void *)start_data,19,2);
+
+   
+
     free(data_buf);
     data_buf = NULL;
     MEDIA_ERR_LOG("AI_audio_size %d",AI_audio_size);
@@ -1312,6 +1327,8 @@ static void* AudioCaptureProcess_AI(void* arg)
 
     g_sample_needStop=0;
 
+     uapi_start_asr_task();
+
     MEDIA_INFO_LOG("AudioCaptureProcess end");
     AudioCaptureTask_is_run=0;
     return NULL;