liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include "mbtk_type.h" |
| 2 | #include <fcntl.h> |
| 3 | #include <stdint.h> |
| 4 | #include <limits.h> |
| 5 | #include <termios.h> |
| 6 | #include <stdarg.h> |
| 7 | // #include "ql_at.h" |
| 8 | #include "ql/ql_audio.h" |
| 9 | // #include "mopen_tts.h" |
| 10 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame^] | 11 | #ifdef MBTK_PLATFORM_ASR1803 |
| 12 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 13 | #define MBTK_AUD_DEMO_WAV "/data/demo.wav" |
| 14 | |
| 15 | static int record_fd = 0; |
| 16 | |
| 17 | int Ql_cb_playback(int hdl, int result) |
| 18 | { |
| 19 | printf("%s: hdl=%d, result=%d\n\r", __func__, hdl, result); |
| 20 | if (result == AUD_PLAYER_FINISHED || result == AUD_PLAYER_NODATA) |
| 21 | { |
| 22 | printf("%s: play finished\n\r", __func__); |
| 23 | } |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | void record_cb_func(int cb_result, char* databuf, unsigned int len) |
| 28 | { |
| 29 | int rc; |
| 30 | |
| 31 | if(NULL != databuf && len > 0 && record_fd > 0) |
| 32 | { |
| 33 | //for debug:save into file |
| 34 | rc = write(record_fd, databuf, len); |
| 35 | if (rc < 0) { |
| 36 | printf("%s: error writing to file!\n", __FUNCTION__); |
| 37 | } else if (rc < len) { |
| 38 | printf("%s: wrote less the buffer size!\n", __FUNCTION__); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | int MBTK_wav_pcm16Le_check(int fd) |
| 43 | { |
| 44 | struct wav_header hdr; |
| 45 | |
| 46 | if (fd <= 0) |
| 47 | return -1; |
| 48 | |
| 49 | if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
| 50 | { |
| 51 | printf("\n%s: cannot read header\n", __FUNCTION__); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | if ((hdr.riff_id != ID_RIFF) |
| 56 | || (hdr.riff_fmt != ID_WAVE) |
| 57 | || (hdr.fmt_id != ID_FMT)) |
| 58 | { |
| 59 | printf("\n%s: is not a riff/wave file\n", __FUNCTION__); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16)) { |
| 64 | printf("\n%s: is not pcm format\n", __FUNCTION__); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | if (hdr.bits_per_sample != 16) { |
| 69 | printf("\n%s: is not 16bit per sample\n", __FUNCTION__); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int MBTK_wav_pcm16Le_set(int fd) |
| 77 | { |
| 78 | struct wav_header hdr; |
| 79 | |
| 80 | if (fd <= 0) |
| 81 | return -1; |
| 82 | |
| 83 | memset(&hdr, 0, sizeof(struct wav_header)); |
| 84 | |
| 85 | hdr.riff_id = ID_RIFF; |
| 86 | hdr.riff_fmt = ID_WAVE; |
| 87 | hdr.fmt_id = ID_FMT; |
| 88 | hdr.fmt_sz = 16; |
| 89 | hdr.audio_format = FORMAT_PCM; |
| 90 | hdr.num_channels = 1; |
| 91 | hdr.sample_rate = 8000; |
| 92 | hdr.bits_per_sample = 16; |
| 93 | hdr.byte_rate = (8000 * 1 * hdr.bits_per_sample) / 8; |
| 94 | hdr.block_align = (hdr.bits_per_sample * 1) / 8; |
| 95 | hdr.data_id = ID_DATA; |
| 96 | hdr.data_sz = 0; |
| 97 | |
| 98 | hdr.riff_sz = hdr.data_sz + 44 - 8; |
| 99 | if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | void aplay(void) |
| 106 | { |
| 107 | char operator[10]; |
| 108 | char databuf[1024]; |
| 109 | int opt = 0; |
| 110 | int fd = 0; |
| 111 | int size = 0; |
| 112 | int state; |
| 113 | int play_hdl = 0; |
| 114 | |
| 115 | while(1) |
| 116 | { |
| 117 | printf("=========aplay========\n" |
| 118 | "\t 0 Open PCM\n" |
| 119 | "\t 1 Play Stream\n" |
| 120 | "\t 2 Play file\n" |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 121 | "\t 3 play mp3\n" |
| 122 | "\t 4 Close Player\n" |
| 123 | "\t 5 exit\n" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 124 | "\t others exit\n\n" |
| 125 | "operator >> "); |
| 126 | |
| 127 | fflush(stdin); |
| 128 | fgets(operator, sizeof(operator), stdin); |
| 129 | opt = atoi(operator); |
| 130 | switch (opt) |
| 131 | { |
| 132 | case 0: |
| 133 | play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback); |
| 134 | if(0 == play_hdl) |
| 135 | printf("Ql_AudPlayer_Open fail\n"); |
| 136 | break; |
| 137 | case 1: |
| 138 | if(0 == play_hdl) |
| 139 | continue; |
| 140 | |
| 141 | fd = open(MBTK_AUD_DEMO_WAV, O_RDWR); |
| 142 | if (fd <= 0) |
| 143 | continue; |
| 144 | |
| 145 | if(0 == MBTK_wav_pcm16Le_check(fd)) |
| 146 | { |
| 147 | memset(databuf, 0, sizeof(databuf)); |
| 148 | while(0 < (size = read(fd, databuf, sizeof(databuf)))) |
| 149 | { |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 150 | // Ql_Rxgain_Set(2); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 151 | if(-1 == Ql_AudPlayer_Play(play_hdl, databuf, size)) |
| 152 | break; |
| 153 | } |
| 154 | printf("aplay Stream end \n"); |
| 155 | } |
| 156 | |
| 157 | close(fd); |
| 158 | break; |
| 159 | case 2: |
| 160 | if(0 == play_hdl) |
| 161 | continue; |
| 162 | |
| 163 | fd = open(MBTK_AUD_DEMO_WAV, O_RDWR); |
| 164 | if (fd <= 0) |
| 165 | continue; |
| 166 | |
| 167 | if(0 == MBTK_wav_pcm16Le_check(fd)) |
| 168 | { |
zhangzh | 8711cea | 2023-10-20 10:47:43 +0800 | [diff] [blame] | 169 | Ql_Rxgain_Set(11); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 170 | Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | printf("aplay file type error\n"); |
| 175 | } |
| 176 | close(fd); |
| 177 | break; |
| 178 | case 3: |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 179 | // aplay_thread(MBTK_AUD_DEMO_WAV); |
| 180 | Ql_Rxgain_Set(11); |
| 181 | Ql_Mp3_To_Play("/data/mp3demo.mp3", play_hdl, 0); |
| 182 | break; |
| 183 | case 4: |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 184 | if(0 == play_hdl) |
| 185 | continue; |
| 186 | Ql_AudPlayer_Close(play_hdl); |
| 187 | break; |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 188 | case 5: |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 189 | default: |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | sleep(1); |
| 194 | } |
| 195 | |
| 196 | printf("aplay exit\n"); |
| 197 | return ; |
| 198 | } |
| 199 | void arec(void) |
| 200 | { |
| 201 | int ret; |
| 202 | char operator[10]; |
| 203 | int opt; |
| 204 | int hdl = 0; |
| 205 | |
| 206 | while(1) |
| 207 | { |
| 208 | printf("=======arec======\n" |
| 209 | "\t 0 Open PCM\n" |
| 210 | "\t 1 Start Record\n" |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 211 | // "\t 2 Get state\n" |
| 212 | // "\t 3 Pause\n" |
| 213 | // "\t 4 Resume\n" |
| 214 | // "\t 5 Stop\n" |
| 215 | "\t 2 Stop/Close Recorder\n" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 216 | "\t others exit\n\n" |
| 217 | "operator >> "); |
| 218 | |
| 219 | fflush(stdin); |
| 220 | fgets(operator, sizeof(operator), stdin); |
| 221 | opt = atoi(operator); |
| 222 | switch (opt) |
| 223 | { |
| 224 | case 0: |
| 225 | hdl = Ql_AudRecorder_Open(NULL, record_cb_func); |
| 226 | if (hdl == 0) |
| 227 | return ; |
| 228 | break; |
| 229 | case 1: |
| 230 | if(0 == hdl) |
| 231 | { |
| 232 | printf("audio is not initialized yet.\n"); |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | if(0 != record_fd) |
| 237 | { |
| 238 | printf("audio It's already being recorded.\n"); |
| 239 | continue; |
| 240 | } |
| 241 | record_fd = open(MBTK_AUD_DEMO_WAV, O_RDWR|O_CREAT|O_TRUNC, 0644); |
| 242 | if (record_fd <= 0) |
| 243 | printf("file open error\n"); |
| 244 | |
| 245 | if(0 == MBTK_wav_pcm16Le_set(record_fd)) |
| 246 | { |
| 247 | ret = Ql_AudRecorder_StartRecord(); |
| 248 | if(0 != ret) |
| 249 | { |
| 250 | printf("audio record error: %d\n", ret); |
| 251 | close(record_fd); |
| 252 | record_fd = 0; |
| 253 | } |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | printf("arec set file header error\n"); |
| 258 | close(record_fd); |
| 259 | record_fd = 0; |
| 260 | } |
| 261 | break; |
| 262 | case 2: |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 263 | Ql_AudRecorder_Close(); |
| 264 | if(record_fd > 0) |
| 265 | { |
| 266 | close(record_fd); |
| 267 | record_fd = 0; |
| 268 | } |
| 269 | break; |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 270 | case 3: |
| 271 | // break; |
| 272 | case 4: |
| 273 | // break; |
| 274 | case 5: |
| 275 | // break; |
| 276 | case 6: |
| 277 | // break; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 278 | default: |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | sleep(1); |
| 283 | } |
| 284 | |
| 285 | printf("arec exit\n"); |
| 286 | return ; |
| 287 | } |
| 288 | int main(void) |
| 289 | { |
| 290 | char operator[10]; |
| 291 | int opt; |
| 292 | |
| 293 | while(1) |
| 294 | { |
| 295 | printf("=========audio main=========\n" |
| 296 | "\t0 exit\n" |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 297 | "\t1 Ql_aplay\n" |
| 298 | "\t2 Ql_arec\n" |
| 299 | "\t3 audio record\n" |
| 300 | "\t4 player stream\n" |
| 301 | // "\t5 set mic Volume\n" |
| 302 | // "\t6 get mic Volume\n" |
| 303 | // "\t7 tts\n" |
| 304 | // "\t8 tone\n" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 305 | "operator: >> "); |
| 306 | |
| 307 | fgets(operator, sizeof(operator), stdin); |
| 308 | fflush(stdin); |
| 309 | opt = atoi(operator); |
| 310 | switch (opt) |
| 311 | { |
| 312 | case 0: |
| 313 | printf("main exit\n"); |
| 314 | return 0; |
| 315 | case 1: |
| 316 | aplay(); |
| 317 | break; |
| 318 | case 2: |
| 319 | arec(); |
| 320 | break; |
| 321 | case 3: |
| 322 | mbtk_at_rec(NULL); |
| 323 | break; |
| 324 | case 4: |
| 325 | mbtk_at_play(NULL); |
| 326 | break; |
| 327 | case 5: |
| 328 | break; |
| 329 | case 6: |
| 330 | break; |
| 331 | case 7: |
| 332 | break; |
| 333 | case 8: |
| 334 | break; |
| 335 | default: |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | sleep(1); |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame^] | 344 | |
| 345 | #else |
| 346 | int main(int argc, char *argv[]) |
| 347 | { |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | #endif |