hc
2024-08-16 a24a44ff9ca902811b99aa9663d697cf452e08ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
 
#ifdef PCBA_3308
#include "mic_test_Linux/vibrate_test.h"
#include "mic_test_Linux/record_test.h"
#endif
 
extern int* recordTestWr(int audio_data[], int audio_length);
extern int* vibrateTestWr(int audio_data[], int audio_length);
 
#define LOG_TAG "echo_ringmic_test"
#include "common.h"
 
#define AUDIO_CHANNAL_CNT 8
#define RINGMIC_BAD_ERROR 93 /* has bad mic */
 
 
#define DEBUG_ON 0
#if DEBUG_ON
   #define FLAGS O_WRONLY | O_CREAT | O_TRUNC
   #define MODE S_IRWXU | S_IXGRP | S_IROTH | S_IXOTH
   void save_audio_file(char *path, char *buff, int len)
   {
       int fd = 0;
       int ret = 0;
       char shell_cmd[200] = {0};
       sprintf(shell_cmd, "rm -rf %s", path);
       system(shell_cmd);
       if ((fd = open(path, FLAGS, MODE)) == -1) {
           log_err("%s Create %s failed, %s\n", __func__,
               path, strerror(errno));
           return;
       }
       ret = write(fd, buff, len);
       if (ret != len) {
           log_err("%s Write %s failed, %s\n", __func__,
               path, strerror(errno));
           return;
       }
       log_info("%s file Saved successfully!\n", path);
       close(fd);
       return;
   }
#else
   void save_audio_file(char *paht, char *buff, int len);
#endif
 
int *bytesToInt(char *src, int *length)
{
   int value = 0, offset = 0, i = 0;
   int *ret = NULL;
   int int_len = 0;
 
   int_len = (*length / 4) + 1;
   ret = (int *)malloc(int_len * 4);
   if (!ret)
       return NULL;
 
   while ((offset < *length) && (*length - offset >= 4)) {
       value = (int)((src[offset] & 0xFF) |
                 ((src[offset + 1] & 0xFF) << 8) |
                 ((src[offset + 2] & 0xFF) << 16) |
                 ((src[offset + 3] & 0xFF) << 24));
       offset += 4;
       ret[i++] = value;
   }
 
   *length = int_len;
   return ret;
}
 
#ifdef PCBA_PX3SE
int linear_amplification(void *data, void *out, int bytes)
{
    int i = 0, j = 0;
   int mChannels = AUDIO_CHANNAL_CNT;
   for (i = 0; i < bytes / 4 ; ) {
       for (j = 0; j < mChannels; j++) {
           int tmp = 0;
           int tmp_data = (*((int *)data + i + j));
#if 1
           tmp_data = tmp_data << 2;
           if(tmp_data > 32767) {
               tmp_data = 32767;
           } else if(tmp_data < -32768) {
               tmp_data = -32768;
           }
#endif
           tmp = ((((tmp_data & 0xfffff000) >> 12) << 12) | ((j+1) << 8)) & 0xffffff00;
           *((int *)out + i + j) = tmp;
       }
       i += mChannels;
   }
    return 0;
}
#endif
 
#ifdef PCBA_3308
int linear_amplification(void *data, void *out, int bytes)
{
    int i = 0, j = 0;
   int mChannels = AUDIO_CHANNAL_CNT;
   for (i = 0; i < bytes / 2; ) {
       for (j = 0; j < mChannels; j++) {
           short raw = (*((short *)data + i + j));
           int tmp_data = (int)raw;
#if 1
           tmp_data = tmp_data << 4;
           if(tmp_data > 32767) {
               tmp_data = 32767;
           } else if(tmp_data < -32768) {
               tmp_data = -32768;
           }
#endif
           *((short *)out + i + j) = (short)tmp_data;
       }
       i += mChannels;
   }
    return 0;
}
#endif
 
#ifdef PCBA_3229GVA
int linear_amplification(void *data, void *out, int bytes)
{
    int i = 0, j = 0;
   int mChannels = AUDIO_CHANNAL_CNT;
   for (i = 0; i < bytes / 2; ) {
       for (j = 0; j < mChannels; j++) {
           short raw = (*((short *)data + i + j));
           int tmp_data = (int)raw;
#if 1
           tmp_data = tmp_data << 7;
           if(tmp_data > 32767) {
               tmp_data = 32767;
           } else if(tmp_data < -32768) {
               tmp_data = -32768;
           }
#endif
           *((short *)out + i + j) = (short)tmp_data;
       }
       i += mChannels;
   }
    return 0;
}
#endif
 
int preProcessBuffer(void *data, void *out, int bytes)
{
   int i = 0, j = 0;
 
   for (i = 0; i < bytes / 2 ; ) {
       for (j = 0; j < AUDIO_CHANNAL_CNT; j++) {
           int tmp = 0;
           short tmp_data = (*((short *)data + i + j));
           tmp = ((tmp_data) << 16 | ((j+1) << 8)) & 0xffffff00;
           *((int *)out + i + j) = tmp;
       }
       i += AUDIO_CHANNAL_CNT;
   }
 
   return 0;
}
 
static int add_channel(char *src, char **dst, int len)
{
   int fd = 0;
   int dst_len = 0;
   int ret = 0;
 
   dst_len = len * 2;
   *dst = (char *)malloc(dst_len);
   if (!*dst)
       return -ENOMEM;
 
   preProcessBuffer(src, *dst, len);
 
   return dst_len;
}
/*
 * This function includes a ring mic recording test
 * and vibration test, select the test type by flag.
 *    flag = 0:ring mic record test.
 *    flag = 1:ring mic vibration test.
 */
int ringmic_test(char *result, int flag)
{
   int *record_ret = NULL;
   int fd = 0;
   int rf_len = 0; /* record file length */
   int pre_len = 0;
   char *rf_buff = NULL; /* record file buffer */
   char *pre_buff = NULL;
   char *gain_buff = NULL;
   int *buffer = NULL;
   char path[128] = {0};
   int ret = 0, i = 0;
 
   /*
    * According to flag choose to perform "recording test"
    * or "vibration test". recording at least 10 seconds.
    */
#ifdef PCBA_PX3SE
   if (flag) {
       log_info("Start vibration test.\n");
       sprintf(path, "%s", "/tmp/ringmic_vibration.pcm");
       /* Play the specified file, and recording. */
       system("arecord -t raw -f S16_LE -c 8 -r 16000 -d 1 /tmp/ringmic_vibration.pcm");
       system("rm /tmp/ringmic_vibration.pcm");
       system("killall -9 arecord");
       system("aplay /data/vibration.wav &");
       usleep(200000);
       system("arecord -t raw -f S16_LE -c 8 -r 16000 /tmp/ringmic_vibration.pcm &");
   } else {
       log_info("Start record test.\n");
       sprintf(path, "%s", "/tmp/ringmic_record.pcm");
       /* Play the specified file, and recording. */
       system("arecord -t raw -f S16_LE -c 8 -r 16000 -d 1 /tmp/ringmic_record.pcm");
       system("rm /tmp/ringmic_record.pcm");
       system("killall -9 arecord");
       system("aplay /data/rectest_400hz.wav &");
       usleep(200000);
       system("arecord -t raw -f S16_LE -c 8 -r 16000 -d 15 /tmp/ringmic_record.pcm &");
   }
   /* recording at least 10 seconds.*/
   log_info("Recording...\n");
   sleep(15);
#endif
 
#ifdef PCBA_3308
   if (flag) {
       log_info("Start vibration test.\n");
       sprintf(path, "%s", "/tmp/ringmic_vibration.pcm");
       /* Play the specified file, and recording. */
       system("killall -9 arecord");
       system("rm /tmp/ringmic_vibration.pcm");
       system("aplay /data/vibration.wav &");
       usleep(200000);
       system("arecord -t raw -f S16_LE -c 8 -r 16000 -d 15 /tmp/ringmic_vibration.pcm");
   } else {
       log_info("Start record test.\n");
       sprintf(path, "%s", "/tmp/ringmic_record.pcm");
       /* Play the specified file, and recording. */
       system("killall -9 arecord");
       system("rm /tmp/ringmic_record.pcm");
       system("aplay /data/rectest_400hz.wav &");
       usleep(200000);
       system("arecord -t raw -f S16_LE -c 8 -r 16000 -d 15 /tmp/ringmic_record.pcm");
   }
#endif
 
#ifdef PCBA_3229GVA
//TODO:
......
#endif
   system("killall -9 arecord");
   system("killall aplay");
 
   log_info("Parsing audio file...\n");
   fd = open(path, O_RDONLY);
   if (fd <= 0) {
       log_err("open %s failed(%s).\n", path, strerror(errno));
       return errno;
   }
   /* get length of the record file. */
   rf_len = lseek(fd, 0, SEEK_END);
   rf_buff = (char *)malloc(rf_len);
   if (!rf_buff) {
       close(fd);
       return -ENOMEM;
   }
   memset(rf_buff, 0, rf_len);
   /* Read from the beginning of the file. */
   lseek(fd, 0, SEEK_SET);
   ret = read(fd, rf_buff, rf_len);
   if (ret != rf_len) {
       log_err("read %s failed(%s).\n", path, strerror(errno));
       close(fd);
       free(rf_buff);
       return errno;
   }
   close(fd);
 
#ifdef PCBA_PX3SE
   /* Increase gain */
   gain_buff = (char *)malloc(rf_len);
   if (!gain_buff) {
       close(fd);
       free(rf_buff);
       return -ENOMEM;
   }
   memset(gain_buff, 0, rf_len);
   linear_amplification(rf_buff, gain_buff, rf_len);
   memcpy(rf_buff, gain_buff, rf_len);
   free(gain_buff);
#endif
#ifdef PCBA_PX3SE
   FILE *fp;
   if (flag)
       fp = fopen("/tmp/gain-vib.pcm", "wb");
   else
       fp = fopen("/tmp/gain-rec.pcm", "wb");
   if (fp) {
       fwrite(rf_buff, 1, rf_len, fp);
       fclose(fp);
   }
#endif
#ifdef PCBA_3229GVA
//TODO:
......
#endif
 
   /* Add channel numbers to the original recording file */
   pre_len = add_channel(rf_buff, &pre_buff, rf_len);
   if (pre_len < 0) {
       free(rf_buff);
       return pre_len;
   }
   free(rf_buff);
 
#ifdef PCBA_3308
    FILE *fp;
    if (flag)
        fp = fopen("/tmp/addchan-vib.pcm", "wb");
    else
        fp = fopen("/tmp/addchan-rec.pcm", "wb");
    if (fp) {
        fwrite(pre_buff, 1, pre_len, fp);
        fclose(fp);
    }
#endif
 
   buffer = bytesToInt(pre_buff, &pre_len);
   if (!buffer) {
       log_err("bytesToInt() failed!\n");
       free(pre_buff);
       return -ENOMEM;
   }
   free(pre_buff);
 
#ifdef PCBA_3308
    if (flag)
        fp = fopen("/tmp/bytesToInt-vib.pcm", "wb");
    else
        fp = fopen("/tmp/bytesToInt-rec.pcm", "wb");
    if (fp) {
        fwrite((int *)buffer+16000, 4, pre_len - 32000, fp);
        fclose(fp);
    }
 
#endif
   /* Call library interface */
   if (!flag) {
    #ifdef PCBA_PX3SE
       record_ret = recordTestWr((int *)buffer, pre_len - 1280);
    #endif
    #ifdef PCBA_3308
        record_ret = recordTestWr((int *)buffer + 16000, pre_len - 32000);
    #endif
    #ifdef PCBA_3229GVA
        //TODO:
        record_ret =
    #endif
       printf("\n");
       for (i = 0; i < AUDIO_CHANNAL_CNT; i++) {
           if (*(record_ret + i)) {
               log_info("recordTest:#%d mic is bad!\n", i);
               *result++ = 1;
           } else {
               log_info("recordTest:#%d mic is ok!\n", i);
               *result++ = 0;
           }
       }
 
       system("rm -rf /tmp/ringmic_record.pcm");
   } else {
    #ifdef PCBA_PX3SE
       record_ret = vibrateTestWr((int *)buffer, pre_len - 1280);
    #endif
    #ifdef PCBA_3308
        record_ret = vibrateTestWr((int *)buffer + 16000, pre_len - 32000);
    #endif
    #ifdef PCBA_3229GVA
        //TODO:
        record_ret =
    #endif
       printf("\n");
       for (i = 0; i < AUDIO_CHANNAL_CNT; i++) {
           if (*(record_ret + i)) {
               log_info("vibrationTest:#%d mic is bad!\n", i);
               *result++ = 1;
           } else {
               log_info("vibrationTest:#%d mic is ok!\n", i);
               *result++ = 0;
           }
       }
 
       system("rm -rf /data/ringmic_vibration.pcm");
   }
 
   free(buffer);
   return 0;
}
 
int main()
{
   char buf[COMMAND_VALUESIZE] = {0};
   char result[COMMAND_VALUESIZE] = RESULT_PASS;
   unsigned char record_ret[AUDIO_CHANNAL_CNT] = {0};
   unsigned char vibration_ret[AUDIO_CHANNAL_CNT] = {0};
   char *start = NULL;
   int ispass = 1;
   int i = 0, ret = 0;
#ifdef PCBA_PX3SE
   system("amixer set Playback 30%");
#endif
#ifdef PCBA_3308
   system("amixer set Master Playback 50%");
#endif
#ifdef PCBA_3229GVA
    //TODO:
......
#endif
   start = buf;
   memcpy(start, "vibration:", strlen("vibration:"));
   start = start + strlen("vibration:");
   ret = ringmic_test(vibration_ret, 1);
   if (ret) {
       memcpy(start, "error", strlen("error"));
       start += strlen("error");
       ispass = 0;
   } else {
       for (i = 0; i < AUDIO_CHANNAL_CNT; i++) {
           if (vibration_ret[i]) {
               ispass = 0;
               *(start++) = '1' + i;
           }
       }
   }
   *start++ = ';';
   sleep(1);
 
   memcpy(start, "record:", strlen("record:"));
   start = start + strlen("record:");
   ret = ringmic_test(record_ret, 0);
   if (ret) {
       memcpy(start, "error", strlen("error"));
       start += strlen("error");
       strcpy(result, RESULT_FAIL);
       ispass = 0;
   } else {
       for (i = 0; i < AUDIO_CHANNAL_CNT; i++) {
           if (record_ret[i]) {
               ispass = 0;
               *(start++) = '1' + i;
           }
       }
   }
   *start++ = ';';
 
   if (!ispass) {
       strcpy(result, RESULT_FAIL);
       ret = -RINGMIC_BAD_ERROR;
   }
 
   send_msg_to_server(buf, result, ret);
   return 0;
}