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