1 
2 #include "aos/kernel.h"
3 #include "speech_memory.h"
4 #include "aqe_kws.h"
5 #include "mcu_audio.h"
6 #include "ulog/ulog.h"
7 #include "aiagent_engine.h"
8 #include "aiagent_service.h"
9 #include "engine/kws_engine.h"
10 #include "a7_cmd.h"
11 
12 #define TAG "kws"
13 
14 AqeKwsConfig kws_cfg;
15 AqeKwsState *kws_st = NULL;
16 uint32_t kws_cnt = 0;
17 static bool kws_running = false;
18 static aiagent_engine_t *eng = NULL;
19 static aos_task_t kws_task_handle;
20 static aos_sem_t kws_sem;
21 static int32_t kws_ret = 1;
22 static int8_t kws_channel = 0;
23 static bool kws_exited = false;
24 static bool kws_inited = false;
25 
callback_main(void * p)26 static void callback_main(void *p)
27 {
28     kws_running = true;
29     kws_exited = false;
30     while (1) {
31         aos_sem_wait(&kws_sem, AOS_WAIT_FOREVER);
32         if (kws_running) {
33             if (eng->callback)
34                 eng->callback((ai_result_t)&kws_ret);
35             else
36                 LOG("callback is not set\n");
37         }
38         else
39             break;
40     }
41     kws_exited = true;
42 }
43 
my_kws_process(uint8_t * buf,uint32_t len)44 static void my_kws_process(uint8_t *buf, uint32_t len)
45 {
46     int32_t r = 0, w = 0;
47     int16_t thres_tmp[1] = {60};
48     A7_CMD_T *cmd = (A7_CMD_T *)buf;
49 
50     if (kws_exited)
51         return;
52 
53     if ((cmd->p1 == 1) && (cmd->p2 == 1)) {
54         LOG("wakeup\n");
55         aos_sem_signal(&kws_sem);
56     }
57 }
58 
kws_engine_init(aiagent_engine_t * eng)59 int32_t kws_engine_init(aiagent_engine_t *eng)
60 {
61    if (!eng)
62         return -1;
63 
64     /*enable dsp kws*/
65     enable_a7_kws(1);
66 
67     if (!kws_inited) {
68         aos_sem_new(&kws_sem, 0);
69         set_a7_cmd_callback_handler(my_kws_process);
70         kws_inited = true;
71         LOGI(TAG, "aos_task_new_ext kws_task\n");
72     }
73 
74     return 0;
75 }
76 
kws_engine_delete(aiagent_engine_t * eng)77 static void kws_engine_delete(aiagent_engine_t *eng)
78 {
79     if (!eng)
80         return;
81 
82     free(eng);
83 }
84 
kws_engine_available(void)85 static bool kws_engine_available(void)
86 {
87     return true;
88 }
89 
kws_engine_uninit(aiagent_engine_t * eng)90 int32_t kws_engine_uninit(aiagent_engine_t *eng)
91 {
92     int32_t ret;
93 
94     if (!eng)
95         return -1;
96 
97     /*enable dsp kws*/
98     enable_a7_kws(0);
99 
100     kws_running = false;
101     aos_sem_signal(&kws_sem);
102     while (!kws_exited) {
103         aos_msleep(50);
104     }
105     aos_task_delete(&kws_task_handle);
106 
107     kws_st = NULL;
108     LOGI(TAG, "aos_task_delete kws_task\n");
109     return 0;
110 }
111 
kws_engine_config(aiagent_engine_t * eng)112 int32_t kws_engine_config(aiagent_engine_t *eng)
113 {
114     kws_engine_config_t *config;
115     config = (kws_engine_config_t *)eng->config;
116     kws_channel = config->channel;
117 }
118 
kws_engine_model_infer(aiagent_engine_t * eng)119 int32_t kws_engine_model_infer(aiagent_engine_t *eng)
120 {
121     if (!eng)
122         return -1;
123 
124     aos_task_new_ext(&kws_task_handle,
125         "kws_callback", callback_main,
126         NULL, 1024 * 120, AOS_DEFAULT_APP_PRI);
127 
128     return 0;
129 }
130 
kws_engine_create(int engineid)131 static aiagent_engine_t *kws_engine_create(int engineid)
132 {
133     /* Malloc ai agent eng*/
134     eng = (aiagent_engine_t *) malloc(sizeof(aiagent_engine_t));
135     if (!eng) {
136         LOGE(TAG, "malloc camera device fail\n");
137         return NULL;
138     }
139     eng->is_dummy = false;
140 
141     /* Set the function pointers */
142     eng->ai_engine_init = kws_engine_init;
143     eng->ai_engine_uninit = kws_engine_uninit;
144     eng->ai_engine_config = kws_engine_config;
145     eng->ai_engine_model_infer = kws_engine_model_infer;
146     eng->ai_engine_free = kws_engine_delete;
147 
148     return eng;
149 }
150 
151 aiagent_context_t kws_engine = {
152     KWS_NAME, "kws(keyword spotting) engine",
153     kws_engine_available, kws_engine_create
154 };
155