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