xref: /OK3568_Linux_fs/external/rk_pcba_test/mic_test_Linux/record_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <signal.h>
5 #include <pthread.h>
6 #include <fcntl.h>
7 #include <linux/input.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <net/if.h>
12 #include <stdio.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 
16 #include "record_test.h"
17 #define LOG_TAG "i2c_controller"
18 //#define printf(format, args...) fprintf (stderr, format, args)
19 
20 #define	    THRES_REC_FREQ	(20000)
21 #define 	THRES_REC_MAX	(32000)
22 #define 	THRES_REC_MIN	(1000)
23 #define 	THRES_REC_PASS_RATE	(60)
24 #define 	THRES_REC_TEST_TIME	(3)
25 // ԭʼ��Ƶ����Sum(xi^2)ʱ������unsigned int32��Χ�ڵ�����ֵ
26 #define ORIGINAL_AUDIO_LEFTNUM        (13)
27 // ����¼����Ƶ����ORIGINAL_AUDIO_LEFTNUM �Ļ��������ӵ�����ֵ
28 #define LEFT_SHIFT                    (1)
29 // ¼����Ƶ����Sum(xi^2)ʱ������unsigned int32��Χ�ڵ�����ֵ
30 #define RECORD_AUDIO_LEFTNUM          (ORIGINAL_AUDIO_LEFTNUM +LEFT_SHIFT )
31 // Sum(xi*yj)������int32��Χ�ڵ�����ֵ
32 #define MOLE_LEFTNUM                  (RECORD_AUDIO_LEFTNUM + 1)
33 
34 static int check_index(int signal, int input[], int input_length);
35 static int check_agreement(int signal, int input[], int input_length);
36 
37 //static FILE * fp_in;
38 //static FILE * fp_out[12];
39 static int index_result;
40 static int agreement_result;
41 
42 static short rec_400hz[] =
43 {
44 	0x0001,0x071B,0x0E0A,0x149E,0x1AB2,0x201D,0x24BE,0x2877,0x2B32,0x2CDB,
45 	0x2D6B,0x2CDC,0x2B32,0x2878,0x24BE,0x201E,0x1AB2,0x149E,0x0E08,0x071B,
46 	0x0000,0xF8E6,0xF1F7,0xEB62,0xE54E,0xDFE3,0xDB41,0xD789,0xD4CE,0xD325,
47 	0xD296,0xD324,0xD4CF,0xD788,0xDB42,0xDFE3,0xE54E,0xEB62,0xF1F7,0xF8E5,
48 	0xFFFF,0x071B,0x0E08,0x149E,0x1AB2,0x201D,0x24BE,0x2877,0x2B32,0x2CDC,
49 	0x2D6B,0x2CDC,0x2B31,0x2878,0x24BE,0x201D,0x1AB3,0x149E,0x0E09,0x071B,
50 	0x0000,0xF8E5,0xF1F7,0xEB62,0xE54E,0xDFE3,0xDB43,0xD788,0xD4CE,0xD324,
51 	0xD295,0xD324,0xD4CE,0xD789,0xDB42,0xDFE3,0xE54F,0xEB61,0xF1F8,0xF8E5,
52 	0x0001,0x071B,0x0E09,0x149E,0x1AB2,0x201D,0x24BD,0x2878,0x2B32,0x2CDC,
53 	0x2D6B,0x2CDB,0x2B32,0x2876,0x24BE,0x201D,0x1AB2,0x149E,0x0E09,0x071B,
54 	0x0000,0xF8E5,0xF1F7,0xEB61,0xE54E,0xDFE2,0xDB42,0xD788,0xD4CF,0xD325,
55 	0xD295,0xD324,0xD4CF,0xD788,0xDB42,0xDFE3,0xE54E,0xEB61,0xF1F7,0xF8E5,
56 	0x0000,0x071B,0x0E08,0x149E,0x1AB3,0x201E,0x24BE,0x2877,0x2B32,0x2CDB,
57 	0x2D6B,0x2CDB,0x2B31,0x2878,0x24BE,0x201D,0x1AB2,0x149E,0x0E08,0x071B,
58 	0x0000,0xF8E5,0xF1F7,0xEB62,0xE54E,0xDFE2,0xDB42,0xD789,0xD4CF,0xD325,
59 	0xD295,0xD324,0xD4CE,0xD788,0xDB41,0xDFE3,0xE54E,0xEB61,0xF1F7,0xF8E6,
60 	0xFFFF,0x071B,0x0E09,0x149E,0x1AB2,0x201E,0x24BD,0x2877,0x2B32,0x2CDB,
61 	0x2D6B,0x2CDB,0x2B31,0x2877,0x24BF,0x201E,0x1AB1,0x149D,0x0E09,0x071B,
62 	0x0000,0xF8E6,0xF1F7,0xEB62,0xE54E,0xDFE2,0xDB42,0xD789,0xD4CE,0xD325,
63 	0xD296,0xD325,0xD4CE,0xD788,0xDB42,0xDFE3,0xE54E,0xEB61,0xF1F7,0xF8E5,
64 	0x0000,0x071A,0x0E09,0x149F,0x1AB1,0x201C,0x24BE,0x2877,0x2B31,0x2CDC,
65 	0x2D6B,0x2CDB,0x2B32,0x2877,0x24BD,0x201D,0x1AB3,0x149E,0x0E09,0x071B,
66 	0x0000,0xF8E5,0xF1F8,0xEB61,0xE54E,0xDFE3,0xDB42,0xD789,0xD4CE,0xD325,
67 	0xD295,0xD325,0xD4CF,0xD788,0xDB42,0xDFE3,0xE54F,0xEB62,0xF1F7,0xF8E5,
68 	0x0000,0x071B,0x0E08,0x149E,0x1AB2,0x201C,0x24BE,0x2877,0x2B31,0x2CDC,
69 	0x2D6A,0x2CDB,0x2B32,0x2877,0x24BE,0x201D
70 };
71 
72 
73 static short rec_word_mark[] = //  1000hz 48k rate
74 {
75 	0x013B,0x0695,0x0E9E,0x17D9,0x209A,0x27CD,0x2D72,0x3227,0x3682,0x3A8B,
76 	0x3DCB,0x3FAD,0x3FFF,0x3F06,0x3D28,0x3A9F,0x3747,0x32DC,0x2D48,0x26BB,
77 	0x1F9A,0x182F,0x1087,0x0887,0x002F,0xF7AD,0xEF56,0xE771,0xE023,0xD95C,
78 	0xD319,0xCD75,0xC8B0,0xC4FD,0xC26E,0xC0EF,0xC068,0xC0DB,0xC25D,0xC503,
79 	0xC8CA,0xCD87,0xD30D,0xD942,0xE012,0xE784,0xEF77,0xF7BF,0x0018,0x085C,
80 	0x1074,0x184D,0x1FCF,0x26D4,0x2D21,0x3299,0x3727,0x3ACC,0x3D81,0x3F32,
81 	0x3FC6,0x3F38,0x3D87,0x3ACE,0x3724,0x3290,0x2D1C,0x26D5,0x1FD9,0x1854,
82 	0x1071,0x0853,0x000D,0xF7B9,0xEF82,0xE795,0xE019,0xD937,0xD2FA,0xCD7C,
83 	0xC8D3,0xC518,0xC26A,0xC0D2,0xC04F,0xC0DE,0xC276,0xC51B,0xC8C9,0xCD72,
84 	0xD2F7,0xD93F,0xE027,0xE799,0xEF7B,0xF7AD,0x0005,0x0857,0x1080,0x185E,
85 	0x1FD4,0x26C7,0x2D12,0x3293,0x3731,0x3ADD,0x3D86,0x3F29,0x3FB9,0x3F32,
86 	0x3D8F,0x3ADD,0x372C,0x3289,0x2D0F,0x26CC,0x1FE0,0x1860,0x107A,0x084D,
87 	0xFFFF,0xF7B3,0xEF85,0xE79F,0xE01F,0xD932,0xD2F3,0xCD77,0xC8D5,0xC523,
88 	0xC271,0xC0D2,0xC049,0xC0D6,0xC276,0xC523,0xC8D1,0xCD71,0xD2F2,0xD939,
89 	0xE025,0xE79E,0xEF82,0xF7AC,0x0000,0x0851,0x107E,0x1864,0x1FDA,0x26C9,
90 	0x2D0D,0x328E,0x3730,0x3ADF,0x3D8C,0x3F2B,0x3FB8,0x3F2E,0x3D8B,0x3ADE,
91 	0x372E,0x328A,0x2D0B,0x26CA,0x1FDD,0x1863,0x107D,0x084F,0x0000,0xF7AF,
92 	0xEF83,0xE79D,0xE024,0xD934,0xD2F2,0xCD73,0xC8D2,0xC523,0xC274,0xC0D4,
93 	0xC048,0xC0D5,0xC277,0xC522,0xC8D3,0xCD72,0xD2F2,0xD936,0xE023,0xE79E,
94 	0xEF83,0xF7AF,0x0000,0x0851,0x107D,0x1863,0x1FDD,0x26CA,0x2D0F,0x328C,
95 	0x372E,0x3ADF,0x3D8D,0x3F2C,0x3FB6,0x3F2B,0x3D8C,0x3ADD,0x372E,0x328C,
96 	0x2D0D,0x26C9,0x1FDC,0x1862,0x107E,0x0853,0x0002,0xF7AE,0xEF83,0xE79D,
97 	0xE025,0xD936,0xD2F2,0xCD74,0xC8D4,0xC523,0xC275,0xC0D5,0xC049,0xC0D4,
98 	0xC273,0xC522,0xC8D3,0xCD73,0xD2F3,0xD936,0xE023,0xE79C,0xEF80,0xF7AF,
99 	0x0000,0x0851,0x107F,0x1860,0x1FDC,0x26CB,0x2D0E,0x328D,0x372E,0x3ADE,
100 	0x3D8B,0x3F2C,0x3FB8,0x3F2B,0x3D8D,0x3ADF
101 };
102 
103 /* Q15,���1/sqrt(x).����x��ΧΪ[0.25,1),����10bit�� */
104 const unsigned short g_s16Table_sqrt_10Bit[769] = {
105 65535,65408,65281,65155,65030,64905,64781,64658,64535,64414,64292,64172,64052,63933,63814,63696,63579,63463,63347,63232,
106 63117,63003,62889,62777,62664,62553,62442,62331,62222,62112,62004,61895,61788,61681,61575,61469,61363,61258,61154,61050,
107 60947,60845,60742,60641,60540,60439,60339,60239,60140,60041,59943,59845,59748,59651,59555,59459,59364,59269,59175,59081,
108 58987,58894,58801,58709,58617,58526,58435,58344,58254,58165,58075,57986,57898,57810,57722,57635,57548,57462,57376,57290,
109 57205,57120,57035,56951,56867,56784,56700,56618,56535,56453,56372,56291,56210,56129,56049,55969,55889,55810,55731,55653,
110 55574,55497,55419,55342,55265,55188,55112,55036,54960,54885,54810,54735,54661,54587,54513,54439,54366,54293,54221,54148,
111 54076,54004,53933,53862,53791,53720,53650,53580,53510,53440,53371,53302,53233,53165,53097,53029,52961,52894,52826,52760,
112 52693,52627,52560,52494,52429,52363,52298,52233,52169,52104,52040,51976,51912,51849,51785,51722,51660,51597,51535,51473,
113 51411,51349,51288,51226,51165,51104,51044,50984,50923,50863,50804,50744,50685,50626,50567,50508,50450,50391,50333,50275,
114 50218,50160,50103,50046,49989,49932,49876,49819,49763,49707,49652,49596,49541,49485,49430,49376,49321,49266,49212,49158,
115 49104,49050,48997,48943,48890,48837,48784,48731,48679,48627,48574,48522,48470,48419,48367,48316,48265,48214,48163,48112,
116 48061,48011,47961,47911,47861,47811,47761,47712,47663,47613,47564,47516,47467,47418,47370,47322,47273,47225,47178,47130,
117 47082,47035,46988,46941,46894,46847,46800,46754,46707,46661,46615,46569,46523,46477,46432,46386,46341,46296,46251,46206,
118 46161,46116,46072,46027,45983,45939,45895,45851,45807,45764,45720,45677,45633,45590,45547,45504,45462,45419,45376,45334,
119 45292,45249,45207,45165,45124,45082,45040,44999,44957,44916,44875,44834,44793,44752,44711,44671,44630,44590,44550,44510,
120 44470,44430,44390,44350,44310,44271,44232,44192,44153,44114,44075,44036,43997,43959,43920,43882,43843,43805,43767,43729,
121 43691,43653,43615,43577,43540,43502,43465,43428,43390,43353,43316,43279,43243,43206,43169,43133,43096,43060,43024,42987,
122 42951,42915,42879,42844,42808,42772,42737,42701,42666,42631,42595,42560,42525,42490,42456,42421,42386,42352,42317,42283,
123 42248,42214,42180,42146,42112,42078,42044,42010,41977,41943,41910,41876,41843,41809,41776,41743,41710,41677,41644,41611,
124 41579,41546,41514,41481,41449,41416,41384,41352,41320,41288,41256,41224,41192,41160,41129,41097,41065,41034,41003,40971,
125 40940,40909,40878,40847,40816,40785,40754,40723,40693,40662,40631,40601,40571,40540,40510,40480,40450,40420,40390,40360,
126 40330,40300,40270,40241,40211,40182,40152,40123,40093,40064,40035,40006,39977,39948,39919,39890,39861,39832,39803,39775,
127 39746,39718,39689,39661,39632,39604,39576,39548,39520,39492,39464,39436,39408,39380,39352,39325,39297,39269,39242,39215,
128 39187,39160,39133,39105,39078,39051,39024,38997,38970,38943,38916,38890,38863,38836,38810,38783,38756,38730,38704,38677,
129 38651,38625,38599,38572,38546,38520,38494,38469,38443,38417,38391,38365,38340,38314,38289,38263,38238,38212,38187,38162,
130 38136,38111,38086,38061,38036,38011,37986,37961,37936,37911,37887,37862,37837,37813,37788,37764,37739,37715,37690,37666,
131 37642,37617,37593,37569,37545,37521,37497,37473,37449,37425,37401,37378,37354,37330,37307,37283,37260,37236,37213,37189,
132 37166,37142,37119,37096,37073,37050,37027,37003,36980,36957,36935,36912,36889,36866,36843,36820,36798,36775,36753,36730,
133 36708,36685,36663,36640,36618,36596,36573,36551,36529,36507,36485,36463,36441,36419,36397,36375,36353,36331,36309,36287,
134 36266,36244,36222,36201,36179,36158,36136,36115,36093,36072,36051,36029,36008,35987,35966,35945,35924,35903,35882,35861,
135 35840,35819,35798,35777,35756,35735,35715,35694,35673,35653,35632,35612,35591,35571,35550,35530,35509,35489,35469,35448,
136 35428,35408,35388,35368,35347,35327,35307,35287,35267,35247,35228,35208,35188,35168,35148,35129,35109,35089,35070,35050,
137 35030,35011,34991,34972,34953,34933,34914,34894,34875,34856,34837,34817,34798,34779,34760,34741,34722,34703,34684,34665,
138 34646,34627,34608,34589,34571,34552,34533,34514,34496,34477,34458,34440,34421,34403,34384,34366,34347,34329,34310,34292,
139 34237,34219,34201,34183,34164,34146,34128,34110,34092,34074,34056,34038,34020,34002,33985,33967,33949,33931,33913,33896,
140 33878,33860,33843,33825,33807,33790,33772,33755,33737,33720,33703,33685,33668,33650,33633,33616,33599,33581,33564,33547,
141 33530,33513,33496,33478,33461,33444,33427,33410,33393,33377,33360,33343,33326,33309,33292,33276,33259,33242,33225,33209,
142 33192,33175,33159,33142,33126,33109,33093,33076,33060,33043,33027,33011,32994,32978,32962,32945,32929,32913,32897,32881,
143 32864,32848,32832,32816,32800,32784
144 };
145 
146 const int g_mic_table[5] = {
147 7,1,8,2,9
148 };
149 
I32MultI16(unsigned int x,unsigned short y)150 unsigned int I32MultI16(unsigned int x, unsigned short y)
151 {
152 	unsigned int iSum = 0;
153 	unsigned int tmp;
154 
155 	iSum = iSum + (x>>16)*(y);
156 	tmp = x&0xFFFF;
157 	tmp = tmp*y;
158 	tmp = tmp>>16;
159 	iSum = iSum + tmp;
160 	return iSum;
161 }
162 
I32MultI32(unsigned int x,unsigned int y)163 unsigned int I32MultI32(unsigned int x,unsigned int y)
164 {
165 	unsigned int iSum = 0;
166 	unsigned int tmpy,tmpx,tmp;
167 	iSum = iSum + (x>>16)*(y>>16);
168 
169 	tmpy = y&0xFFFF;
170 	tmpx = x>>16;
171 	tmp = (tmpx*tmpy)>>16;
172 	iSum = iSum + (tmp);
173 
174 	tmpx = x&0xFFFF;
175 	tmpy = y>>16;
176 	tmp = (tmpx*tmpy)>>16;
177 	iSum = iSum + tmp;
178 	return iSum;
179 }
180 
181 // ���
GetDenomValueByTable(unsigned int s32ParamB,unsigned int s32ParamC,short * psQ)182 unsigned short GetDenomValueByTable(unsigned int s32ParamB, unsigned int s32ParamC, short * psQ)
183 {
184 	unsigned int i32Result = 0;
185 	short sindex = 0;
186 	unsigned int i32Denom;
187 	unsigned short sValue;
188 
189 	short s16Q = 0;
190 
191 	i32Denom = I32MultI32(s32ParamB,s32ParamC);
192 
193 #if FLOAT_FLAG
194 	double x =  1.0 /sqrt((double)i32Denom);
195 #endif
196 
197 	if(0 == i32Denom){
198 		++i32Denom;
199 	}
200 
201 	if (!(i32Denom & 0xFFFF0000))
202 	{
203 		/* All of first 16 bits are zero */
204 		i32Denom <<= 16;
205 		s16Q = 8;
206 	}
207 	if (!(i32Denom & 0xFF000000))
208 	{
209 		/* All of first 8 bits are zero */
210 		i32Denom <<= 8;
211 		s16Q += 4;
212 	}
213 	if (!(i32Denom & 0xF0000000))
214 	{
215 		/* All of first 4 bits are zero */
216 		i32Denom <<= 4;
217 		s16Q += 2;
218 	}
219 	if (!(i32Denom & 0xC0000000))
220 	{
221 		/* All of first 2 bits are zero */
222 		i32Denom <<= 2;
223 		s16Q += 1;
224 	}
225 
226 
227 	//s32ParamB -= 0x80000000;
228 	i32Denom -= 0x3FE00001;
229 
230 	sindex = i32Denom >> 22;
231 
232 	sValue = g_s16Table_sqrt_10Bit[sindex];
233 
234 	/****************************************/
235 #if LOG_FLAG
236 	double tx = 1.0*32768*32768*2/(1<<s16Q);
237 	double tmp = x*tx;
238 	assert(fabs(tmp-sValue) < 60);
239 #endif
240 	/********************************************/
241 
242 	*psQ = s16Q;
243 	return sValue;
244 }
245 
246 /* ��ȡ��ĸ��sum(xi^2)��sum(yj^2)��ֵ */
GetDenomValue(short * px,int inLen,short nQ)247 unsigned int GetDenomValue(short * px, int inLen, short nQ)
248 {
249 	unsigned int iSum = 0;
250 	int k;
251 	for( k = 0; k < inLen; k++)
252 	{
253 		iSum += (px[k]*px[k])>>nQ;
254 	}
255 
256 	return iSum;
257 }
258 
259 
260 /* ��ȡs32ParamA/s32ParamB */
CalcInverse(unsigned int s32ParamA,unsigned short s32ParamB,short s16QB)261 unsigned int CalcInverse(unsigned int s32ParamA,unsigned short s32ParamB, short s16QB)
262 {
263 	unsigned int i32Result;
264 	if( 0 == s32ParamA)
265 	{
266 		return 0;
267 	}
268 
269 	i32Result = I32MultI16(s32ParamA, s32ParamB);
270 	return i32Result;
271 }
272 
xcorr(short * px,short * py,int inLen,unsigned short us16Thresh)273 int xcorr(short* px, short* py, int inLen,unsigned short us16Thresh)
274 {
275 	int nOverLapLen;                  // �غϳ���
276 	int x,y;
277 
278 	int i32CrossCorr;                 // �����ֵ
279 
280 	int k;
281 	int index = 0;
282 
283 	short  nQx, nQy, nQxy;            // sum(xi*yj), sum(xi^2), sum(yj^2)������ֵ
284 
285 	unsigned int i32Denomx,i32Denomy; // sum(xi^2), sum(yj^2)
286 	unsigned short s16Denom;          // 1/sqrt(sum(xi^2)*sum(yj^2)) ͨ�������
287 	short  nDQ;                       // sum(xi^2), sum(yj^2)  Qֵ
288 
289 	unsigned int i32Result;
290 
291 	short nQ;
292 	unsigned short sqrt_2 = 46341;   // Q15
293 	/* ��sum(xi*yj), sum(xi^2), sum(yj^2)������ֵ */
294 	//GetQ(px,py,inLen,&nQxy,&nQx,&nQy);
295 	nQx = ORIGINAL_AUDIO_LEFTNUM;
296 	nQy = RECORD_AUDIO_LEFTNUM;
297 	nQxy = MOLE_LEFTNUM;
298 	if(0 != (nQx+nQy)%2)
299 	{
300 		nQ = (nQx+nQy+1)/2 - nQxy;
301 	}
302 	else
303 	{
304 		nQ = (nQx+nQy)/2 - nQxy;
305 	}
306 	/* ��sum(xi*yj), sum(xi^2) */
307 	i32Denomx = GetDenomValue(px,inLen,nQx);
308 	i32Denomy = GetDenomValue(py,inLen,nQy);
309 
310 	/* �����1/sqrt(sum(yj^2)*sum(xi^2))�Ͷ�Ӧ��Qֵ */
311 	s16Denom = GetDenomValueByTable(i32Denomx,i32Denomy,&nDQ);
312 
313 	/* ����sum(xi*yj) */
314 	index = 0;
315 	/* �غϳ��ȴ�1 --> inLen*/
316 	for(nOverLapLen = 1; nOverLapLen <= inLen; nOverLapLen++ )
317 	{
318 		i32CrossCorr = 0;
319 		for(k = 0;k < nOverLapLen;k++)
320 		{
321 			x = px[k];
322 			y = py[inLen-nOverLapLen+k];
323 
324 			i32CrossCorr += ((x*y)>>nQxy);
325 		}
326 		if(i32CrossCorr < 0)  /* ����ȫ������ */
327 		{
328 			i32CrossCorr = 0;
329 			i32Result = 0;
330 			continue;
331 		}
332 
333 		i32Result = CalcInverse(i32CrossCorr,s16Denom,nDQ);
334 		if(0 == (nQx+nQy)%2)
335 		{
336 			i32Result = (i32Result >>(16-nDQ+nQ));
337 		}
338 		else
339 		{
340 			i32Result = (I32MultI16(i32Result,sqrt_2) >> (15-nDQ+nQ));
341 		}
342 		if(i32Result > 32767)
343 			i32Result  =  32767;
344 
345 		if(i32Result>us16Thresh)
346 		{
347 			return i32Result;
348 		}
349 
350 	}
351 	/* �غϳ��ȴ�inLen-1 --> 1 */
352 	for(nOverLapLen = inLen-1;nOverLapLen>=1;nOverLapLen--)
353 	{
354 		i32CrossCorr = 0;
355 		for(k = 0;k < nOverLapLen;k++)
356 		{
357 			y = py[k];
358 			x = px[inLen-nOverLapLen+k];
359 			i32CrossCorr += ((x*y)>>nQxy);
360 		}
361 
362 		if(i32CrossCorr < 0)  /* ����ȫ������ */
363 		{
364 			i32CrossCorr = 0;
365 			i32Result = 0;
366 			continue;
367 		}
368 
369 		i32Result = CalcInverse(i32CrossCorr,s16Denom,nDQ);
370 		if(0 == (nQx+nQy)%2)
371 		{
372 			i32Result = (i32Result >> (16-nDQ+nQ));
373 		}
374 		else
375 		{
376 			i32Result = (I32MultI16(i32Result,sqrt_2) >> (15-nDQ+nQ));
377 		}
378 		if(i32Result > 32767)
379 			i32Result  =  32767;
380 
381 		if(i32Result>us16Thresh)
382 		{
383 			return i32Result;
384 		}
385 	}
386 
387 	return i32Result;
388 }
389 
CheckMicTable(int num)390 int CheckMicTable(int num)
391 {
392 	int ret = 0, j = 0;
393 	for(j = 0; j < 5; j++)
394 	{
395 		if(num == g_mic_table[j])
396 		{
397 			return j + 1;
398 		}
399 	}
400     return ret;
401 }
402 
recordTestWr(int audio_data[],int audio_length)403 int* recordTestWr(int audio_data[], int audio_length)
404 {
405 	int *buf;
406 	static int ccids[13];
407 
408 	int signal = 0;
409 	int i = 0, ret = 0;
410 	int rlength = audio_length / 10;
411 	int pcmIndex[12];
412 	int *pcmInput[12];
413 
414 	printf("start to check audio , recordtest!!!\n");
415 	buf = audio_data;
416 	memset(pcmIndex, 0, sizeof(pcmIndex));
417 	memset(ccids, 0, sizeof(ccids));
418 	if(NULL == buf)
419 	{
420 		return NULL;
421 	}
422 
423 	for(i = 0; i <12; i++)
424 	{
425 		pcmInput[i] = malloc(sizeof(int) * rlength);
426 		if(pcmInput[i] == NULL)
427 		{
428 			printf("pcmInput malloc failed \n");
429 			return NULL;
430 		}
431 	}
432 
433 	printf("split audio begin\n");
434 	while(i < audio_length)
435 	{
436         signal = (buf[i]>>8)&15;
437 		if(signal > 0 && signal <= 12)
438         {
439 			signal--;
440 			if(pcmIndex[signal] < rlength)
441 			{
442                 *(pcmInput[signal] + pcmIndex[signal]) = buf[i];
443 			    pcmIndex[signal] ++;
444 			}
445         }
446 		else
447 		{
448             printf("signal : %d , less than 1 or larger than 12 \n", signal);
449 		}
450 		i++;
451 	}
452 	printf("split audio success\n");
453 
454 	// �����
455 	for(i = 0; i < 12; i++)
456 	{
457 		ret = check_index(i, pcmInput[i], pcmIndex[i]);
458 		if(ret < 0)
459 		{
460 			printf("check index fail\n");
461 			if(ret != 0)
462 			{
463 				ccids[i] = 1;
464 			}
465 		}
466 	 }
467 
468 	 // ���һ����
469 	for(i = 0; i < 12; i++)
470 	{
471 		ret = check_agreement(i, pcmInput[i], pcmIndex[i]);
472 		if(ret < 0)
473 		{
474 
475 			if(ret != 0)
476 			{
477 				printf("check agreement fail\n");
478 				ccids[i] = 1;
479 			}else {
480 				printf("check agreement success\n");
481 			}
482 
483 		}
484 	}
485 
486 	for(i = 0; i <12; i++)
487 	{
488 		free(pcmInput[i]);
489 	}
490 
491 	return ccids;
492 }
493 
494 // �����Ƶ�ļ��б��
check_index(int signal,int input[],int input_length)495 static int check_index(int signal, int input[], int input_length)
496 {
497 	int i = 0, j =0;
498 	int buf;
499 	int channel_index, first_index;
500 
501 	// ���������20���ֽ�
502 	i = 20;
503 	first_index = (input[i] >> 8) & 0xf;
504 	i++;
505 	while(i < input_length)
506 	{
507 		channel_index = (input[i] >> 8) & 0xf;
508 		if(channel_index != first_index)
509 		{
510 			printf("check index fail, lost data, file index is %d, first_index is %d, channel_index is %d", signal, first_index, channel_index);
511 			return -12;
512 		}
513 		i++;
514 	}
515 
516 	printf("check index success, channel_index is %d\n", channel_index);
517 
518 	return 0;
519 }
520 
521 // ���һ����
check_agreement(int signal,int input[],int input_length)522 static int check_agreement(int signal, int input[], int input_length)
523 {
524 	int i = 0, j = 0, offset = 0;
525 	printf("start to check agreement, signal is %d\n", signal);
526 	if(input == NULL || input_length < 40000)
527 	{
528 		printf("error in input pcm, content : %d , length less than one frame\n", input_length);
529 		return -21;
530 	}
531 
532 	int buf[256]; // ��Ƶ�ļ��е�ԭʼ����
533 	short buf_shift[256]; // ��λ�Ժ������
534 	unsigned long pass_count = 0; // ͨ���Ĵ���
535 	unsigned long total_count = 0; // �ܵĴ���
536 	int pass_rate = 0;
537 	int first_index = 0;
538 
539 	// ���������20���ֽ�
540 	i = 20;
541 
542 	// ��ȡ�ļ��ĵ�һ��sample��ȡ��index��Ϊ�Ƚϵı�׼
543 	//fread(&first_index_in_file, sizeof(int), 1, fp_out[file_index]);
544     first_index = input[i];
545     i ++;
546 	first_index = (first_index >> 8) & 0xf;
547 	//if(first_index == 0x06)
548 	//{
549 	//	printf("check pass");
550 	//	return 0;
551 	//}
552 
553 	//fseek(fp_out[file_index], 64000, SEEK_SET); // ������ʼ��1s
554 	//fseek(fp_out[file_index], 10240, SEEK_SET); //����0.08s
555     i = i + 10240;
556 
557     while( i < input_length && input_length - i > 256)
558     {
559         total_count++;
560 		for(j = i, offset = 0; offset < 256; j++, offset++)
561 		{
562 			buf_shift[offset] = (input[j] >> 16) & 0xffff;
563 		}
564         i = i + 256;
565 
566 		if(xcorr(buf_shift, rec_400hz, 256, 20480) >= THRES_REC_FREQ)
567 		{
568 			printf("check agreemnt, %d success\n", pass_count);
569 			pass_count++;
570 		}else {
571 			printf("check agreemnt, %d failed\n", pass_count);
572 		}
573     }
574 
575 	pass_rate = pass_count * 100 / total_count;
576 	printf("signal : %d, pass_count is %d, total_count is %d, pass_rate is %d\n", signal, pass_count, total_count, pass_rate);
577 	if(pass_rate < THRES_REC_PASS_RATE)
578 	{
579 		//printf("check agreement fail");
580 		return -1;
581 	}
582 	return 0;
583 }
584