更新
authorp15692025051 <846603105@qq.com>
Mon, 14 Apr 2025 08:58:15 +0000 (16:58 +0800)
committerp15692025051 <846603105@qq.com>
Mon, 14 Apr 2025 08:58:15 +0000 (16:58 +0800)
2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c
2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.h
2.code/aicos/app/AI_AMP_2_5/bk_websocket.c

index 30aace0..896a6ae 100644 (file)
@@ -45,7 +45,7 @@ static char *AI_TTS_str = NULL;
 static aos_timer_ptr AI_VC_1S_timer = NULL;
 static void AI_VC_send_satrt(void);
 static aos_status AI_VC_send_data(char *data,u32 len,u8 AIC_WEBSOCKET_OPCODE);
-
+VC_state_type VC_state = VC_State_None;
 static void AI_VC_send_task(void *thread_input);//websocket 发送线程
 static void AI_VC_read_task(void *thread_input);//websocket 读线程
 static void AI_VC_put_play_data(char *data,u32 len);//音频数据入队播放
@@ -86,6 +86,30 @@ int bk_printf(const char *format, ...)
     aic_free(out);
        return ret;
 }
+//用于定时器打印
+int bk_msg_printf(const char *format, ...)
+{
+    int ret=0;
+    int bk_printf_count = 3*2048;
+       va_list args;
+       char *out = NULL;
+    out = aic_zalloc(bk_printf_count);
+    if(!out)
+    {
+        //uart_write(ai_uart_handle,"bk_printf ERROR!!!!!\r\n", strlen("bk_printf ERROR!!!!!\r\n"));
+        return 0;
+    }
+       va_start( args, format );
+    ret = vsnprintf(out, bk_printf_count, format, args);
+    memcpy(out+ret,"\r\n",2);
+    ret+=2;
+    //uart_write(ai_uart_handle,out, ret);
+    AI_AMP_MSG_put(out,ret,AI_PRINTF);
+    //aos_sleep(100);
+       va_end( args );
+    aic_free(out);
+       return ret;
+}
 #define  count_len  50
 void print_long_log(u8 *data,int data_len)
 {
@@ -197,7 +221,7 @@ static void AI_VC_send_satrt(void)
     cJSON_AddNumberToObject(outbound_cfg, "channel", 1);
     cJSON_AddNumberToObject(outbound_cfg, "volume", 100);
     cJSON_AddNumberToObject(outbound_cfg, "speech_rate", 0);
-    cJSON_AddNumberToObject(outbound_cfg, "chunk_interval_ms", 50);
+    cJSON_AddNumberToObject(outbound_cfg, "chunk_interval_ms", 100);
     cJSON_AddStringToObject(outbound_cfg, "voice", "zh_female_linjianvhai_moon_bigtts");
     cJSON_AddItemToObject(outbound_attributes, "cfg", outbound_cfg);
     cJSON_AddItemToObject(payload, "outbound_attributes", outbound_attributes);
@@ -272,26 +296,6 @@ void AI_VC_send_ping_ping(void)
 static int websocket_connect_VC(void)
 {
     //连接服务器
-    // if(AI_VC_client_websocket_handle == 0)
-    // {
-    //     aic_network_enable_pdp_nic_capdump();//打开网络调试
-    //     AI_VC_client_websocket_handle  = aic_websocket_client_create("AI_VC_2_5", 10*1024);
-    //     app_logi("AI_AMP_2_5_task:aic_websocket_client_create :%d \n",AI_VC_client_websocket_handle);
-    //     //aos_sleep(1000);
-        
-    //     int status = aic_websocket_client_connect(AI_VC_client_websocket_handle, "118.145.158.207", 0, 22086,
-    //         "118.145.158.207", sizeof("118.145.158.207") - 1,
-    //         (u8 *)"/vc/chat?is_auth=0", sizeof("/vc/chat?is_auth=0") - 1,
-    //         (u8 *)"ws", sizeof("ws") - 1,
-    //         NX_WAIT_FOREVER);
-    //     app_logi("AI_AMP_2_5_task:aic_websocket_client_connect :%d \n",status);
-    //     if(status)//连接失败
-    //     {
-    //         aic_websocket_delete(AI_VC_client_websocket_handle,5000);
-    //         AI_VC_client_websocket_handle = 0;
-    //         return -1;
-    //     }
-    // }
     if(AI_client_VC == NULL)
     {
         AI_client_VC = init_websocket_client("118.145.158.207","/vc/chat?is_auth=0",22086);
@@ -476,7 +480,7 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程
                     {
                         VC_state = VC_State_End;//VC_State_Listening;
                         
-                        AI_AMP_MSG_put(0,0,VC_RECORD_START);//打开录音
+                        //AI_AMP_MSG_put(0,0,VC_RECORD_START);//打开录音
                     }
                 }
                 else if(code == 2)//二进制
@@ -496,6 +500,7 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程
 
                 AI_AMP_MSG_put("vc连接断开了",strlen("vc连接断开了"),VC_CONNECT_ERROR);
             }
+            aos_sleep(1);
         }else
         {
             bk_printf("AI_VC_read_task:error AI_VC_client_websocket_handle=%d\n",AI_VC_client_websocket_handle);
@@ -511,7 +516,23 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程
 
 static void AI_VC_put_play_data(char *data,u32 len)
 {
+    u32 count_tmp = 0;
+    
+    while(1)
+    {
+        aos_get_queue_used_count(AI_VC_play_queue,&count_tmp);
+        if(count_tmp>100)//大于100个缓存
+        {
+            //bk_printf("AI_VC_put_play_data:count_tmp = %d",count_tmp);
+            aos_sleep(1);
+        }else
+        {
+            break;
+        }
+        
 
+    }
+    
     AI_VC_audio_queue_t AI_VC_play_queue_data;
     AI_VC_play_queue_data.data = aic_malloc(len);
 
@@ -565,7 +586,7 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程
                 // }
                 
                 
-                bk_printf("VCVC:play1 ");
+                //bk_printf("VCVC:play1 ");
                 int audio_play_time = AI_VC_play_queue_data.len*1000/32000+1;
                 ret = app_audio_write(APP_AUDIO_TYPE_STREAM_DUPLEX,
                     AI_VC_play_queue_data.data, AI_VC_play_queue_data.len, AOS_WAIT_FOREVER);
@@ -574,7 +595,7 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程
                     bk_printf("app_audio_write error, ret=%d,audio_play_time=%d\n", ret,audio_play_time);
                 
                 //ret = bk_stream_play_write(AI_VC_play_queue_data.data,AI_VC_play_queue_data.len);
-                bk_printf("VCVC:play2 ret=%d,len=%d ",ret,AI_VC_play_queue_data.len);
+                //bk_printf("VCVC:play2 ret=%d,len=%d ",ret,AI_VC_play_queue_data.len);
                 aic_free(AI_VC_play_queue_data.data);
                 if(VC_state == VC_State_End)
                 {
@@ -698,8 +719,15 @@ static void AI_VC_record_task(void *thread_input)//录音线程
     AI_VC_audio_queue_t AI_VC_play_record_data;
     while(1)
     {
+        //bk_printf("AI_VC_record_task 1 ");
         ret = app_audio_read(APP_AUDIO_TYPE_STREAM_DUPLEX,
-            vrecord_buf, 3200, 100);
+            vrecord_buf, 3200, 101);
+        //bk_printf("AI_VC_record_task 2 len = %d,VC_state = %d",ret,VC_state);
+        if(ret<=0)
+        {
+            bk_printf("AI_VC_record_task continue len = %d,VC_state = %d",ret,VC_state);
+            continue;
+        }
         if(VC_state == VC_State_None)//开启的时候,先缓存音频
         {
 
@@ -717,7 +745,7 @@ static void AI_VC_record_task(void *thread_input)//录音线程
                  break;
             }
             AI_VC_send_data(vrecord_buf,ret,AIC_WEBSOCKET_OPCODE_BINARY_FRAME);
-            bk_printf("AI_VC_record_task send_data len = %d",ret);
+            //bk_printf("AI_VC_record_task send_data len = %d",ret);
         }else
         {
             char count_t = 200;
@@ -730,8 +758,10 @@ static void AI_VC_record_task(void *thread_input)//录音线程
                  break;
             }
         }
-        aos_sleep(40);
+        aos_sleep(1);
     }
+    aos_sleep(1);
+    bk_printf("AI_VC_record_task end.....");
     //app_audio_stop(APP_AUDIO_TYPE_STREAM_DUPLEX, NULL);
 }
 
@@ -794,6 +824,7 @@ void VC_record_End(void)
 }
 static void AI_VC_1S_timeout(void *param)
 {
+    //bk_msg_printf("VC_END_play_timerout1!!!!!!!!!");
     if(close_timerout>0)
     {
         close_timerout--;
@@ -815,9 +846,10 @@ static void AI_VC_1S_timeout(void *param)
         VC_END_play_timerout --;
         if(VC_END_play_timerout<=0)
         {
-            bk_printf("VC_END_play_timerout=%d",VC_END_play_timerout);
+            //bk_msg_printf("VC_END_play_timerout1=%d",VC_END_play_timerout);
             VC_state = VC_State_Listening;//重新开始了
             AI_AMP_MSG_put(0,0,VC_RECORD_START);//开启录音
+            //bk_msg_printf("VC_END_play_timerout2=%d",VC_END_play_timerout);
         }
     }
     
@@ -842,6 +874,7 @@ void AI_key_long_press(void)
 void AI_VC_start(void)
 {
     int ret = 0;
+    bk_printf("AI_VC_start !!!!!!!! ");
     VC_state = VC_State_None;
     app_audio_param_t param;
     param.channel = 1;
@@ -859,7 +892,7 @@ void AI_VC_start(void)
     if(AI_VC_1S_timer == NULL)
     {
         AI_VC_1S_timer = aos_create_timer("AI_VC_1S_timer",
-            AI_VC_1S_timeout, NULL,
+            AI_VC_1S_timeout, &AI_VC_1S_timer,
             1000,
             AOS_AUTO_ACTIVATE, AOS_BE_PERIOD);
         aic_assert(AI_VC_1S_timer);
@@ -896,13 +929,14 @@ void AI_VC_Close(void)
 aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type)
 {
     AI_AMP_MSG_queue_t AI_AMP_MSG_queue_data = {0};
+    //bk_msg_printf("AI_AMP_MSG_put1 MSG_Type:%d",MSG_Type);
     if(len >0)
     {
         AI_AMP_MSG_queue_data.data = aic_malloc(len+1);
         memset(AI_AMP_MSG_queue_data.data ,0,len+1);
         if(!AI_AMP_MSG_queue_data.data)
         {
-            bk_printf("AI_VC_send_data aic_malloc ERROR!!!!!!!!!!");
+            //bk_msg_printf("AI_VC_send_data aic_malloc ERROR!!!!!!!!!!");
             return AOS_ERROR;
         }
         memcpy(AI_AMP_MSG_queue_data.data,data,len);
@@ -916,7 +950,7 @@ aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type)
             aic_free(AI_AMP_MSG_queue_data.data);
         return AOS_ERROR;
     }
-    
+    //bk_msg_printf("AI_AMP_MSG_put2 MSG_Type:%d",MSG_Type);
     return AOS_SUCCESS;
 }
 
@@ -977,6 +1011,10 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程
             case AI_KEY_LONG_PRESS:
                 AI_key_long_press();
                 break;  
+            case AI_PRINTF:
+                if(AI_AMP_msg.data)
+                uart_write(ai_uart_handle,AI_AMP_msg.data, AI_AMP_msg.len);
+                break;  
             default:
                 break;
         }
index 9daf8bd..a9a403f 100644 (file)
@@ -12,6 +12,7 @@ typedef enum AI_AMP_MSG
     AI_KEY_PRESS,//AI键按下
     AI_KEY_RELEASE,//AI键释放
     AI_KEY_LONG_PRESS,//AI键长按
+    AI_PRINTF,//打印
 } AI_AMP_MSG_Type;
 typedef struct
 {
@@ -37,7 +38,7 @@ typedef enum
     VC_State_DialogError=13,
     VC_State_End=14,
 }VC_state_type;
-VC_state_type VC_state = VC_State_None;
+extern VC_state_type VC_state ;
 //  #define APP_CFG_TASK_STK_SIZE       (4096u)
 //  #define APP_CFG_TASK_COM_PRIO       (35u)
 //  typedef int32_t (*srv_func_callback)(void *p_param, uint32_t size); // TODO: temp and common define this
@@ -57,4 +58,6 @@ void AI_VC_start(void);
 void AI_VC_Close(void);
 int bk_printf(const char *format, ...);
 void bk_print_hex(u8 *data,int data_len);
+//用于定时器打印
+int bk_msg_printf(const char *format, ...);
 aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type);
\ No newline at end of file
index 9891be4..75cc87e 100644 (file)
@@ -243,6 +243,7 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
     unsigned char masking_key[4];
     unsigned int bytes_received = 0;
     int recv_len = 0 ;
+    static u8 last_byte = 0;
     // while(1)
     // {
     //     recv_len = recv(client->sockfd, frame+bytes_received, 2-bytes_received, 0);
@@ -263,8 +264,14 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
         }
         if(frame[0]!= 0x80 && frame[0]!= 0x81 && frame[0]!= 0x82 && frame[0]!= 0x88 && frame[0]!= 0x89 && frame[0]!= 0x8A)
         {
-            bk_printf("receive_websocket_message:hend =0x%X,0x%X ERROR!!!!!!!!!",frame[0],frame[1]);
-            continue;
+            bk_printf("receive_websocket_message:hend =0x%X,0x%X ,last_byte =0x%x ERROR!!!!!!!!!",frame[0],frame[1],last_byte);
+
+            if(last_byte!= 0x80 && last_byte!= 0x81 && last_byte!= 0x82 && last_byte!= 0x88 && last_byte!= 0x89 && last_byte!= 0x8A)
+            {
+                bk_printf("receive_websocket_message:hend =0x%X,0x%X ,last_byte =0x%x ERROR222222222!!!!!!!!!",frame[0],frame[1],last_byte);
+                continue;
+            }
+                
         }else
         {
             break;
@@ -372,6 +379,7 @@ int  receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l
     {
         payload_len = 0;
     }
+    last_byte = buf[payload_len-1];
     return payload_len;//parse_frame(buf, bytes_received, buf,code);
 }