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 | |
| 11 | #define MBTK_AUD_DEMO_WAV "/data/demo.wav" |
| 12 | |
| 13 | #define MBTK_AUD_DEMO_WAV1 "/data/demo1.wav" |
| 14 | |
| 15 | |
| 16 | int play_hdl = 0; |
| 17 | static int record_fd = 0; |
| 18 | |
| 19 | |
| 20 | int Ql_cb_playback(int hdl, int result) |
| 21 | { |
| 22 | printf("%s: hdl=%d, result=%d\n\r", __func__, hdl, result); |
| 23 | if (result == AUD_PLAYER_FINISHED || result == AUD_PLAYER_NODATA) |
| 24 | { |
| 25 | printf("%s: play finished\n\r", __func__); |
| 26 | } |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | void record_cb_func(int cb_result, char* databuf, unsigned int len) |
| 31 | { |
| 32 | int rc; |
| 33 | |
| 34 | if(NULL != databuf && len > 0 && record_fd > 0) |
| 35 | { |
| 36 | //for debug:save into file |
| 37 | rc = write(record_fd, databuf, len); |
| 38 | if (rc < 0) { |
| 39 | printf("%s: error writing to file!\n", __FUNCTION__); |
| 40 | } else if (rc < len) { |
| 41 | printf("%s: wrote less the buffer size!\n", __FUNCTION__); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | int MBTK_wav_pcm16Le_check(int fd) |
| 48 | { |
| 49 | struct wav_header hdr; |
| 50 | |
| 51 | if (fd <= 0) |
| 52 | return -1; |
| 53 | |
| 54 | if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
| 55 | { |
| 56 | printf("\n%s: cannot read header\n", __FUNCTION__); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | printf("hdr.riff_id:%X, hdr.riff_fmt:%X, hdr.fmt_id:%X", hdr.riff_id, hdr.riff_fmt, hdr.fmt_id); |
| 61 | |
| 62 | if ((hdr.riff_id != ID_RIFF) |
| 63 | || (hdr.riff_fmt != ID_WAVE) |
| 64 | || (hdr.fmt_id != ID_FMT)) |
| 65 | { |
| 66 | printf("\n%s: is not a riff/wave file\n", __FUNCTION__); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16)) { |
| 71 | printf("\n%s: is not pcm format\n", __FUNCTION__); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | if (hdr.bits_per_sample != 16) { |
| 76 | printf("\n%s: is not 16bit per sample\n", __FUNCTION__); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | printf("audio_format: %d,num_channels: %d,sample_rate: %d,byte_rate: %d,bits_per_sample: %d data_sz: %d\n", |
| 81 | hdr.audio_format, hdr.num_channels, hdr.sample_rate, hdr.byte_rate, hdr.bits_per_sample, hdr.data_sz); |
| 82 | |
| 83 | return hdr.data_sz; |
| 84 | } |
| 85 | |
| 86 | int MBTK_wav_pcm16Le_set(int fd) |
| 87 | { |
| 88 | struct wav_header hdr; |
| 89 | |
| 90 | if (fd <= 0) |
| 91 | return -1; |
| 92 | |
| 93 | memset(&hdr, 0, sizeof(struct wav_header)); |
| 94 | |
| 95 | hdr.riff_id = ID_RIFF; |
| 96 | hdr.riff_fmt = ID_WAVE; |
| 97 | hdr.fmt_id = ID_FMT; |
| 98 | hdr.fmt_sz = 16; |
| 99 | hdr.audio_format = FORMAT_PCM; |
| 100 | hdr.num_channels = 1; |
| 101 | hdr.sample_rate = 8000; |
| 102 | hdr.bits_per_sample = 16; |
| 103 | hdr.byte_rate = (8000 * 1 * hdr.bits_per_sample) / 8; |
| 104 | hdr.block_align = (hdr.bits_per_sample * 1) / 8; |
| 105 | hdr.data_id = ID_DATA; |
| 106 | hdr.data_sz = 0; |
| 107 | |
| 108 | hdr.riff_sz = hdr.data_sz + 44 - 8; |
| 109 | if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | unsigned char* handle_file1(char *audioFilePath) |
| 117 | { |
| 118 | long filesize; |
| 119 | unsigned char *pcmBuf = NULL; |
| 120 | FILE *fp = NULL; |
| 121 | int size; |
| 122 | |
| 123 | // 处理文件格式 |
| 124 | fp = fopen(audioFilePath, "rb"); |
| 125 | if (!fp) { |
| 126 | printf("%s:fopen failed",__func__); |
| 127 | } |
| 128 | |
| 129 | fseek(fp,0,SEEK_END); |
| 130 | filesize=ftell(fp); |
| 131 | printf("%s:filesize:%d\n", __func__, filesize); |
| 132 | fseek(fp,44,SEEK_SET); |
| 133 | pcmBuf =(unsigned char *)malloc(sizeof(char)*filesize); |
| 134 | memset(pcmBuf,0,sizeof(char)*filesize); |
| 135 | // fread(pcmBuf, filesize-44,1, fp); |
| 136 | fread(pcmBuf, 1, filesize-44, fp); |
| 137 | fclose(fp); |
| 138 | |
| 139 | printf("strlen(pcmBuf):%d\n", strlen(pcmBuf)); |
| 140 | |
| 141 | return pcmBuf; |
| 142 | } |
| 143 | |
| 144 | void *audio_thread(void *arg) |
| 145 | { |
| 146 | printf("audio_thread(1)----------\n"); |
| 147 | Ql_Playback_Samprate_Set(1); |
| 148 | int play_hdl = 0; |
| 149 | int fd = 0; |
| 150 | int ret; |
| 151 | int file_size =0; |
| 152 | int file_size1 =0; |
| 153 | unsigned char *pcmBuf = NULL; |
| 154 | unsigned char *pcmBuf1 = NULL; |
| 155 | |
| 156 | play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback); |
| 157 | if(0 == play_hdl) |
| 158 | { |
| 159 | printf("Ql_AudPlayer_Open fail\n"); |
| 160 | } |
| 161 | |
| 162 | fd = open(MBTK_AUD_DEMO_WAV, O_RDWR); |
| 163 | if (fd <= 0) |
| 164 | { |
| 165 | printf("Open fail\n"); |
| 166 | } |
| 167 | |
| 168 | int fd1 = open(MBTK_AUD_DEMO_WAV1, O_RDWR); |
| 169 | printf("fd1:%d\n", fd1); |
| 170 | if (fd1 <= 0) |
| 171 | { |
| 172 | printf("Open fail\n"); |
| 173 | } |
| 174 | |
| 175 | file_size = MBTK_wav_pcm16Le_check(fd); |
| 176 | |
| 177 | file_size1 = MBTK_wav_pcm16Le_check(fd1); |
| 178 | printf("file_size:%d, file_size1:%d\n", file_size, file_size1); |
| 179 | |
| 180 | pcmBuf = handle_file1(MBTK_AUD_DEMO_WAV); |
| 181 | |
| 182 | pcmBuf1 = handle_file1(MBTK_AUD_DEMO_WAV1); |
| 183 | |
| 184 | if(file_size > 0 && file_size1 > 0 ) |
| 185 | { |
| 186 | if(pcmBuf != NULL) |
| 187 | { |
| 188 | ret = Ql_AudPlayer_Play(play_hdl, pcmBuf, file_size); |
| 189 | printf("ret:%d\n", ret); |
| 190 | } |
| 191 | |
| 192 | Ql_Rxgain_Set(3); |
| 193 | if(pcmBuf1 != NULL) |
| 194 | { |
| 195 | ret = Ql_AudPlayer_Play(play_hdl, pcmBuf1, file_size1); |
| 196 | printf("ret:%d\n", ret); |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | Ql_AudPlayer_Close(play_hdl); |
| 202 | close(fd); |
| 203 | close(fd1); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void *audio_play_file_thread(void *arg) |
| 209 | { |
| 210 | char operator[10]; |
| 211 | |
| 212 | int fd = open(MBTK_AUD_DEMO_WAV, O_RDWR); |
| 213 | if (fd <= 0) |
| 214 | return 0; |
| 215 | |
| 216 | int fd1 = open(MBTK_AUD_DEMO_WAV1, O_RDWR); |
| 217 | if (fd1 <= 0) |
| 218 | return 0; |
| 219 | |
| 220 | Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0); |
| 221 | |
| 222 | Ql_Rxgain_Set(3); |
| 223 | |
| 224 | Ql_AudPlayer_PlayFrmFile(play_hdl, fd1, 0); |
| 225 | |
| 226 | close(fd); |
| 227 | close(fd1); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | int aplay_thread(char *audioFilePath) |
| 234 | { |
| 235 | int res; |
| 236 | pthread_t play_thread; |
| 237 | |
| 238 | res = pthread_create(&play_thread, NULL, audio_thread, audioFilePath); |
| 239 | if (res != 0) { |
| 240 | printf("%s:pthread_create failed",__func__); |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | int aplay_file_thread(int play_hdl) |
| 249 | { |
| 250 | int res; |
| 251 | pthread_t play_thread; |
| 252 | int hdl = play_hdl; |
| 253 | |
| 254 | res = pthread_create(&play_thread, NULL, audio_play_file_thread, &hdl); |
| 255 | if (res != 0) { |
| 256 | printf("%s:pthread_create failed",__func__); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | void aplay(void) |
| 266 | { |
| 267 | char operator[10]; |
| 268 | char databuf[1024]; |
| 269 | int opt = 0; |
| 270 | int fd = 0; |
| 271 | int size = 0; |
| 272 | int state; |
| 273 | int handler = 0; |
| 274 | int file_size = 0 ; |
| 275 | |
| 276 | while(1) |
| 277 | { |
| 278 | printf("=========aplay========2\n" |
| 279 | "\t 0 Open PCM\n" |
| 280 | "\t 1 Play Stream\n" |
| 281 | "\t 2 Play file\n" |
| 282 | "\t 3 Close\n" |
| 283 | "\t 4 play stream thread\n" |
| 284 | "\t 5 pause\n" |
| 285 | "\t 6 repause\n" |
| 286 | "\t 7 play file thread\n" |
| 287 | "\t 8 play mp3\n" |
| 288 | "\t others exit\n\n" |
| 289 | "operator >> "); |
| 290 | |
| 291 | fflush(stdin); |
| 292 | fgets(operator, sizeof(operator), stdin); |
| 293 | opt = atoi(operator); |
| 294 | switch (opt) |
| 295 | { |
| 296 | case 0: |
| 297 | Ql_Playback_Samprate_Set(1); |
| 298 | play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback); |
| 299 | if(0 == play_hdl) |
| 300 | printf("Ql_AudPlayer_Open fail\n"); |
| 301 | |
| 302 | printf("\nplay_hdl:%d\n", play_hdl); |
| 303 | break; |
| 304 | case 1: |
| 305 | if(0 == play_hdl) |
| 306 | continue; |
| 307 | |
| 308 | fd = open(MBTK_AUD_DEMO_WAV, O_RDWR); |
| 309 | if (fd <= 0) |
| 310 | continue; |
| 311 | |
| 312 | file_size = MBTK_wav_pcm16Le_check(fd); |
| 313 | printf("file_size:%d\n", file_size); |
| 314 | if(file_size > 0 ) |
| 315 | { |
| 316 | unsigned char *pcmBuf = NULL; |
| 317 | char *p1 = pcmBuf; |
| 318 | pcmBuf = handle_file1(MBTK_AUD_DEMO_WAV); |
| 319 | if(pcmBuf != NULL) |
| 320 | { |
| 321 | if(-1 == Ql_AudPlayer_Play(play_hdl, pcmBuf, file_size)) |
| 322 | printf("\n------\n"); |
| 323 | } |
| 324 | |
| 325 | } |
| 326 | |
| 327 | // Ql_Rxgain_Set(5); |
| 328 | |
| 329 | close(fd); |
| 330 | break; |
| 331 | case 2: |
| 332 | if(0 == play_hdl) |
| 333 | continue; |
| 334 | |
| 335 | fd = open(MBTK_AUD_DEMO_WAV, O_RDWR); |
| 336 | if (fd <= 0) |
| 337 | continue; |
| 338 | |
| 339 | if(1) |
| 340 | { |
| 341 | Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 44); |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | printf("aplay file type error\n"); |
| 346 | } |
| 347 | close(fd); |
| 348 | break; |
| 349 | case 3: |
| 350 | if(0 == play_hdl) |
| 351 | continue; |
| 352 | Ql_AudPlayer_Close(play_hdl); |
| 353 | break; |
| 354 | case 4: |
| 355 | aplay_thread(MBTK_AUD_DEMO_WAV); |
| 356 | break; |
| 357 | case 5: |
| 358 | if(0 == play_hdl) |
| 359 | continue; |
| 360 | Ql_AudPlayer_Pause(play_hdl); |
| 361 | break; |
| 362 | case 6: |
| 363 | if(0 == play_hdl) |
| 364 | continue; |
| 365 | Ql_AudPlayer_Resume(play_hdl); |
| 366 | break; |
| 367 | case 7: |
| 368 | if(0 == play_hdl) |
| 369 | continue; |
| 370 | aplay_file_thread(play_hdl); |
| 371 | break; |
| 372 | case 8: |
| 373 | // Ql_Mp3_To_Wav("/data/demo.wav", "/data/mp3demo.mp3", play_hdl); |
| 374 | Ql_Mp3_To_Play("/data/mp3demo.mp3", play_hdl, 0); |
| 375 | break; |
| 376 | default: |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | sleep(1); |
| 381 | } |
| 382 | |
| 383 | printf("aplay exit\n"); |
| 384 | return ; |
| 385 | } |
| 386 | void arec(void) |
| 387 | { |
| 388 | int ret; |
| 389 | char operator[10]; |
| 390 | int opt; |
| 391 | int hdl = 0; |
| 392 | |
| 393 | while(1) |
| 394 | { |
| 395 | printf("=======arec======\n" |
| 396 | "\t 0 Open PCM\n" |
| 397 | "\t 1 Start Record\n" |
| 398 | "\t 2 Get state\n" |
| 399 | "\t 3 Pause\n" |
| 400 | "\t 4 Resume\n" |
| 401 | "\t 5 Stop\n" |
| 402 | "\t 6 Close\n" |
| 403 | "\t others exit\n\n" |
| 404 | "operator >> "); |
| 405 | |
| 406 | fflush(stdin); |
| 407 | fgets(operator, sizeof(operator), stdin); |
| 408 | opt = atoi(operator); |
| 409 | switch (opt) |
| 410 | { |
| 411 | case 0: |
| 412 | Ql_Playback_Samprate_Set(0); |
| 413 | hdl = Ql_AudRecorder_Open(NULL, record_cb_func); |
| 414 | if (hdl == 0) |
| 415 | return ; |
| 416 | break; |
| 417 | case 1: |
| 418 | if(0 == hdl) |
| 419 | { |
| 420 | printf("audio is not initialized yet.\n"); |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | if(0 != record_fd) |
| 425 | { |
| 426 | printf("audio It's already being recorded.\n"); |
| 427 | continue; |
| 428 | } |
| 429 | record_fd = open(MBTK_AUD_DEMO_WAV, O_RDWR|O_CREAT|O_TRUNC, 0644); |
| 430 | if (record_fd <= 0) |
| 431 | printf("file open error\n"); |
| 432 | |
| 433 | if(0 == MBTK_wav_pcm16Le_set(record_fd)) |
| 434 | { |
| 435 | ret = Ql_AudRecorder_StartRecord(); |
| 436 | if(0 != ret) |
| 437 | { |
| 438 | printf("audio record error: %d\n", ret); |
| 439 | close(record_fd); |
| 440 | record_fd = 0; |
| 441 | } |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | printf("arec set file header error\n"); |
| 446 | close(record_fd); |
| 447 | record_fd = 0; |
| 448 | } |
| 449 | break; |
| 450 | case 2: |
| 451 | // printf("arec state : %d\n", state); |
| 452 | break; |
| 453 | case 3: |
| 454 | break; |
| 455 | case 4: |
| 456 | break; |
| 457 | case 5: |
| 458 | break; |
| 459 | case 6: |
| 460 | Ql_AudRecorder_Close(); |
| 461 | if(record_fd > 0) |
| 462 | { |
| 463 | close(record_fd); |
| 464 | record_fd = 0; |
| 465 | } |
| 466 | break; |
| 467 | default: |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | sleep(1); |
| 472 | } |
| 473 | |
| 474 | printf("arec exit\n"); |
| 475 | return ; |
| 476 | } |
| 477 | int main(void) |
| 478 | { |
| 479 | char operator[10]; |
| 480 | int opt; |
| 481 | |
| 482 | // printf("Ql_Mp3_To_Wav()\n"); |
| 483 | // mbtk_audio_mp3_to_wav("/data/demo.wav", "/data/mp3demo.mp3"); |
| 484 | // Ql_Mp3_To_Wav("/data/demo.wav", "/data/mp3demo.mp3"); |
| 485 | |
| 486 | while(1) |
| 487 | { |
| 488 | printf("=========audio main=========\n" |
| 489 | "\t0 exit\n" |
| 490 | "\t1 aplay\n" |
| 491 | "\t2 arec\n" |
| 492 | "\t3 set speaker Volume\n" |
| 493 | "\t4 get speaker Volume\n" |
| 494 | "\t5 set mic Volume\n" |
| 495 | "\t6 get mic Volume\n" |
| 496 | "\t7 tts\n" |
| 497 | "\t8 tone\n" |
| 498 | "operator: >> "); |
| 499 | |
| 500 | fgets(operator, sizeof(operator), stdin); |
| 501 | fflush(stdin); |
| 502 | opt = atoi(operator); |
| 503 | switch (opt) |
| 504 | { |
| 505 | case 0: |
| 506 | printf("main exit\n"); |
| 507 | return 0; |
| 508 | case 1: |
| 509 | aplay(); |
| 510 | break; |
| 511 | case 2: |
| 512 | arec(); |
| 513 | break; |
| 514 | case 3: |
| 515 | mbtk_at_rec(NULL); |
| 516 | break; |
| 517 | case 4: |
| 518 | mbtk_at_play(NULL); |
| 519 | break; |
| 520 | case 5: |
| 521 | break; |
| 522 | case 6: |
| 523 | break; |
| 524 | case 7: |
| 525 | break; |
| 526 | case 8: |
| 527 | break; |
| 528 | default: |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | sleep(1); |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | |
| 540 | |