1 #include "kws.h"
2 #include "ulog/ulog.h"
3 #include "aiagent_service.h"
4 #include "aiagent_common.h"
5 #if (BOARD_HAAS100 == 1)
6 #include "led.h"
7 #endif
8 #include "uvoice_init.h"
9 #include "uvoice_types.h"
10 #include "uvoice_event.h"
11 #include "uvoice_player.h"
12 #include "uvoice_recorder.h"
13 
14 #include "uvoice_os.h"
15 
16 #define TAG "kws"
17 
18 static os_task_t play_task;
19 static uvoice_player_t *uvocplayer;
20 static char text_source[256];
21 static int source_sample_rate;
22 static int source_channels;
23 
get_format_from_name(char * name,media_format_t * format)24 static int get_format_from_name(char *name, media_format_t *format)
25 {
26     if (!name || !format) {
27         LOGE(TAG, "arg null !\n");
28         return -1;
29     }
30 
31     if (strstr(name, ".mp3") || strstr(name, ".MP3"))
32         *format = MEDIA_FMT_MP3;
33     else if (strstr(name, ".wav") || strstr(name, ".WAV"))
34         *format = MEDIA_FMT_WAV;
35     else if (strstr(name, ".aac") || strstr(name, ".AAC"))
36         *format = MEDIA_FMT_AAC;
37     else if (strstr(name, ".m4a") || strstr(name, ".M4A"))
38         *format = MEDIA_FMT_M4A;
39     else if (strstr(name, ".pcm") || strstr(name, ".PCM"))
40         *format = MEDIA_FMT_PCM;
41     else if (strstr(name, ".spx") || strstr(name, ".SPX"))
42         *format = MEDIA_FMT_SPX;
43     else if (strstr(name, ".ogg") || strstr(name, ".OGG"))
44         *format = MEDIA_FMT_OGG;
45     else if (strstr(name, ".amrwb") || strstr(name, ".AMRWB"))
46         *format = MEDIA_FMT_AMRWB;
47     else if (strstr(name, ".amr") || strstr(name, ".AMR"))
48         *format = MEDIA_FMT_AMR;
49     else if (strstr(name, ".opus") || strstr(name, ".OPUS"))
50         *format = MEDIA_FMT_OPS;
51     else if (strstr(name, ".flac") || strstr(name, ".FLAC"))
52         *format = MEDIA_FMT_FLAC;
53 
54     return 0;
55 }
56 
play_music(void * arg)57 static void *play_music(void *arg)
58 {
59     media_format_t format = MEDIA_FMT_UNKNOWN;
60     get_format_from_name(text_source, &format);
61 
62     if (uvocplayer->set_source(text_source)) {
63         LOGE(TAG, "set source failed !\n");
64         return NULL;
65     }
66 
67     if (format == MEDIA_FMT_OPS || format == MEDIA_FMT_SPX)
68         uvocplayer->set_pcminfo(source_sample_rate, source_channels, 16, 1280);
69 
70     if (uvocplayer->start()) {
71         LOGE(TAG, "start failed !\n");
72         uvocplayer->clr_source();
73     }
74 
75     return NULL;
76 }
77 
play_local_mp3(void)78 static int32_t play_local_mp3(void)
79 {
80     int32_t random;
81 
82     random = rand() % 240;
83     LOG("random: %d\n", random);
84     memset(text_source, 0, sizeof(text_source));
85     if (random < 100) {
86         strcpy(text_source, "fs:/data/mp3/haas_intro.mp3");
87     } else if (random > 100 && random < 150) {
88         strcpy(text_source, "fs:/data/mp3/haasxiaozhushou.mp3");
89     } else if (random > 150 && random < 200) {
90         strcpy(text_source, "fs:/data/mp3/zhurenwozai.mp3");
91     } else {
92         strcpy(text_source, "fs:/data/mp3/eiwozai.mp3");
93     }
94     os_task_create(&play_task,
95         "play music task", play_music,
96         NULL, 8192, 0);
97     return 0;
98 }
99 
kws_callback(ai_result_t * result)100 static int32_t kws_callback(ai_result_t *result)
101 {
102     int32_t kws_ret = (int32_t)*result;
103     player_state_t player_state = -1;
104 
105     if (kws_ret) {
106         led_switch(1, LED_ON);
107         aos_msleep(200);
108         led_switch(2, LED_ON);
109         aos_msleep(200);
110         led_switch(3, LED_ON);
111         aos_msleep(200);
112         led_switch(4, LED_ON);
113         aos_msleep(200);
114         led_switch(5, LED_ON);
115         aos_msleep(200);
116         led_switch(6, LED_ON);
117 
118         /*play local tts*/
119         play_local_mp3();
120 
121         uvocplayer->wait_complete();
122 
123         led_switch(1, LED_OFF);
124         led_switch(2, LED_OFF);
125         led_switch(3, LED_OFF);
126         led_switch(4, LED_OFF);
127         led_switch(5, LED_OFF);
128         led_switch(6, LED_OFF);
129     }
130 
131     return 0;
132 }
133 
kws_init(void)134 int32_t kws_init(void)
135 {
136     int32_t ret;
137     ai_config_t config;
138 
139     ret = aiagent_service_init("kws", AI_MODEL_KWS);
140     if (ret < 0) {
141         LOGE(TAG, "aiagent_service_init failed");
142         return -1;
143     }
144 
145     config.channel = 2;
146     aiagent_service_config(&config);
147 
148     /*Init sound driver*/
149     audio_install_codec_driver();
150 
151     /*Init uvoice to play mp3*/
152     uvoice_init();
153 
154     /*create uvoice player*/
155     uvocplayer = uvoice_player_create();
156     if (!uvocplayer) {
157         LOGE(TAG, "create media player failed !\n");
158         return;
159     }
160     uvocplayer->eq_enable(0);
161 
162     /*set play volume*/
163     uvocplayer->set_volume(10);
164 
165     return aiagent_service_model_infer(NULL, NULL, kws_callback);
166 }
167 
kws_uninit(void)168 int32_t kws_uninit(void)
169 {
170     aiagent_service_uninit();
171     return 0;
172 }
173