xref: /OK3568_Linux_fs/external/rk_pcba_test/mic_test_Linux/vibrate_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 #include "math.h"
16 
17 #include "vibrate_test.h"
18 //#define printf(format, args...) fprintf (stderr, format, args)
19 
20 #define LOG_TAG "i2c_controller"
21 
22 #define FRAME_LENGTH 128
23 
24 #define DATA_TYPE float
25 
26 /*const int g_mic_table[5] = {
27 7,1,8,2,9
28 };*/
29 
30 //static FILE * fp_in;
31 //static FILE * fp_out[12];
32 static int test_result;
33 
check_vibration(int signal,int input[],int input_length)34 int check_vibration(int signal, int input[], int input_length)
35 {
36 	float min = 0.5;
37 	float max = 0.0;
38 	DATA_TYPE sum0l = 0.0;
39 	DATA_TYPE sum0 = 0.0;
40 	int ind = 1;
41 	int i = 0, k = 0, j = 0, offset = 0;
42 	int first_index;
43 	long count = 0;;
44 
45 	printf("start to check vibration, signal is %d\n", signal);
46 	if(input == NULL || input_length < 44800)
47 	{
48 		printf("error in input pcm, content : %d , length less than one frame\n", input_length);
49 		return -21;
50 	}
51 
52 	i += 20;
53 	// 读取文件的第一个sample,取其index最为比较的标准
54 	first_index = input[i];
55 	first_index = (first_index >> 8) & 0xf;
56 	//if(first_index != 0x07 && first_index != 0x01 && first_index != 0x08 && first_index != 0x02 && first_index != 0x09)
57 	//{
58 	//	printf("pass check\n");
59 	//	return 0;
60 	//}
61 	int filesize = (input_length - 80)/sizeof(DATA_TYPE);
62 	int noofframe = filesize/FRAME_LENGTH;
63 
64 	// 靠近风口的声道特殊处理
65 	/*if(first_index == 3 | first_index == 9)
66 	{
67 		printf("this is a special channel");
68 		//fseek(fp_test,57600,SEEK_SET);
69 		i = 57600;
70 	}
71 	else*/
72 	{
73 		//printf("this is a normal channel");
74 		//fseek(fp_test,44800,SEEK_SET);
75 		//i = 44800;
76 		i = 44800;
77 	}
78 	int *temp_intp = (int *)malloc( FRAME_LENGTH * sizeof(int) );
79 
80 	DATA_TYPE maxA = 0;
81 	//while(!feof(fp_test))
82 	while(i + FRAME_LENGTH < input_length)
83 	{
84 		count++;
85 		k = k + 1;
86 		if (k < noofframe - 10)
87 		{
88 			//fread(temp_intp,sizeof(int),FRAME_LENGTH,fp_test);
89 			for(j = i, offset = 0; offset < FRAME_LENGTH; j++, offset++)
90 		    {
91 			    temp_intp[offset] = input[j];
92 		    }
93 			i += FRAME_LENGTH;
94 			//printf("input_length : %d, i : %d", input_length, i);
95 			sum0l = sum0;
96 			sum0 = 0.0;
97 
98 			for (j = 0; j < FRAME_LENGTH; j++)
99 			{
100 				int tmp = temp_intp[j] >> 8;
101 				float temp_float;
102 				temp_float = (float)tmp/0x800000;
103 
104 				DATA_TYPE temp = 0;
105 				if(temp_float>0)
106 				{
107 					temp = temp_float;
108 				}
109 				else
110 				{
111 					temp = -temp_float;
112 				}
113 				if(temp>maxA)
114 				{
115 					maxA = temp;
116 
117 				}
118 				sum0 += (temp_float) * (temp_float);
119 
120 			}
121 
122 			if (k == 1)
123 			{
124 				sum0 = sum0;
125 			}
126 			else
127 			{
128 				sum0 = (1-0.99) * sum0 + 0.99 * sum0l;
129 			}
130 
131 			if (sum0<min)
132 			{
133 				min = sum0;
134 			}
135 			if (sum0>max)
136 			{
137 				max = sum0;
138 			}
139 
140 		}
141 		else
142 		{
143 			break;
144 		}
145 	}
146 
147 	free(temp_intp);
148 	printf("max is %2.3f\tmin is %2.3f\trate is %2.3f\n", max, min, max/(min+0.1));
149 
150 	if (max>80*(min+.1))
151 	{
152 		ind = 1;
153 	}
154 	else
155 	{
156 		ind = 0;
157 	}
158 
159 	printf("result is %d\tmaxA is %2.3f \n",ind, maxA);
160 	return ind;
161 
162 }
163 
vibrateTestWr(int audio_data[],int audio_length)164 int* vibrateTestWr(int audio_data[], int audio_length)
165 {
166 	int *buf;
167 	static int ccids[13];
168 	int signal = 0;
169 	int i = 0, ret = 0;
170 	int rlength = audio_length / 10;
171 	int pcmIndex[12];
172 	int *pcmInput[12];
173 
174         buf = audio_data;
175 	memset(pcmIndex, 0, sizeof(pcmIndex));
176 	memset(ccids, 0, sizeof(ccids));
177 
178 	if(buf == NULL) {
179 		return NULL;
180 	}
181 	printf("start to check audio , vibrationtest!!!\n");
182 	for(i = 0; i <12; i++)
183 	{
184 		pcmInput[i] = malloc(sizeof(int) * rlength);
185 		if(pcmInput[i] == NULL)
186 		{
187 			printf("pcmInput malloc failed \n");
188 			return NULL;
189 		}
190 	}
191 
192 	printf("split audio begin\n");
193 	while(i < audio_length)
194 	{
195         signal = (buf[i]>>8)&15;
196 		if(signal > 0 && signal <= 12)
197         {
198 			signal--;
199 			if(pcmIndex[signal] < rlength)
200 			{
201                 *(pcmInput[signal] + pcmIndex[signal]) = buf[i];
202 			    pcmIndex[signal] ++;
203 			}
204         }
205 		else
206 		{
207             printf("signal : %d , less than 1 or larger than 12 \n", signal);
208 		}
209 		i++;
210 	}
211 	printf("split audio success\n");
212 
213 	// 检查频谱
214 	for(i = 0; i < 12; i++)
215 	{
216 		ret = check_vibration(i, pcmInput[i], pcmIndex[i]);
217 		if(ret != 0)
218 		{
219 			printf("check vibration fail\n");
220 			ccids[i] = 1;
221 		} else {
222 			printf("check vibration success\n");
223 		}
224 	}
225 	for(i = 0; i <12; i++)
226 	{
227 		free(pcmInput[i]);
228 	}
229 
230 	return ccids;
231 }
232 
233 
234