简历项目内容
语音手势识别智能终端 TCP通信线程
void *thread_tcp_xml(void *parg)
{
int socket_fd;
int rt;
int len;
pthread_t tid_touch, tid_led;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0)
{
perror("create socket fail:");
return -1;
}
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(54321);
dest_addr.sin_addr.s_addr = inet_addr("10.12.1.84");
struct timeval timeout = { 5, 0 };
rt = setsockopt(socket_fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof timeout);
if (rt < 0)
{
perror("setsockopt SO_RCVTIMEO:");
return -1;
}
rt = setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof timeout);
if (rt < 0)
{
perror("setsockopt SO_RCVTIMEO:");
return -1;
}
int on = 1;
setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
rt = connect(socket_fd, (struct sockaddr *)&dest_addr, sizeof dest_addr);
if (rt < 0)
{
perror("connect fail:");
system("madplay 11.mp3");
printf("连接服务器失败!\n");
LED_SWITCH(D8, LED_OFF);
return -1;
}
else
{
printf("连接服务器成功!\n");
system("madplay 12.mp3");
LED_SWITCH(D8, LED_ON);
}
while (1)
{
if (flag == 12 )
{
printf("start recording...");
system("rm cmd.pcm");
system("arecord -d3 -c1 -r16000 -traw -fS16_LE cmd.pcm");
tcp_send_pcm(socket_fd, "cmd.pcm");
tcp_recv_xml(socket_fd);
xmlChar *id = parse_xml("result.xml","cmd");
printf("flag =%d \n",flag);
if (flag == 12)
{
flag = 0;
}
}
}
}
XML文件处理
xmlChar *__get_cmd_id(xmlDocPtr doc, xmlNodePtr cur, char *NodeName)
{
xmlChar *key, *id;
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if ((!xmlStrcmp(cur->name, (const xmlChar *)NodeName)))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("%s: %s\n",NodeName, key);
xmlFree(key);
id = xmlGetProp(cur, (const xmlChar *)"id");
printf("id: %s\n", id);
flag = atoi(id);
xmlFree(doc);
return id;
}
cur = cur->next;
}
xmlFree(doc);
return NULL;
}
xmlChar *parse_xml(char *xmlfile, char *NodeName)
{
xmlDocPtr doc;
xmlNodePtr cur1, cur2;
doc = xmlParseFile(xmlfile);
if (doc == NULL)
{
fprintf(stderr,"Document not parsed successfully. \n");
return NULL;
}
cur1 = xmlDocGetRootElement(doc);
if(cur1 == NULL)
{
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return NULL;
}
if(xmlStrcmp(cur1->name, (const xmlChar *)"nlp"))
{
fprintf(stderr,"document of the wrong type, root node != nlp");
xmlFreeDoc(doc);
return NULL;
}
cur1 = cur1->xmlChildrenNode;
while (cur1 != NULL)
{
if ((!xmlStrcmp(cur1->name, (const xmlChar *)"result")))
{
cur2 = cur1->xmlChildrenNode;
while(cur2 != NULL)
{
if((!xmlStrcmp(cur2->name, (const xmlChar *)"confidence")))
{
xmlChar *key = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
printf("confidence: %s\n",key);
if(atoi((char *)key) <30)
{
xmlFree(doc);
fprintf(stderr, "sorry, I'm NOT sure what you say.\n");
return NULL;
}
}
if((!xmlStrcmp(cur2->name, (const xmlChar *)"object")))
{
return __get_cmd_id(doc, cur2, NodeName);
}
cur2 = cur2->next;
}
}
cur1 = cur1->next;
}
xmlFreeDoc(doc);
return NULL;
}
TCP接收XML文件
```c
int tcp_recv_xml(int socket_fd)
{
char *xml_buf = calloc(1, 1024);
int n = recv(socket_fd, xml_buf, 1024, 0);
if (n <= 0)
{
perror("tcp recv:");
return -1;
}
printf("%d bytes has been recv\n", n);
int fd = open("result.xml", O_CREAT | O_RDWR | O_TRUNC);
if (fd < 0)
{
printf("create result.xml fail\n");
return -1;
}
n = write(fd, xml_buf, n);
printf("%d bytes has been write to result.xml\n", n);
close(fd);
return 0;
}
发送PCM录音文件
int tcp_send_pcm(int socket_fd, const char *pcm_file)
{
int fd;
fd = open(pcm_file, O_RDONLY);
if (fd < 0)
{
printf("open %s\n", pcm_file);
return -1;
}
off_t pcm_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
char *pcm_buf = calloc(1, pcm_size);
read(fd, pcm_buf, pcm_size);
int m = send(socket_fd, pcm_buf, pcm_size, 0);
printf("%d bytes has been sent\n", m);
free(pcm_buf);
return 0;
}
主函数部分
int main(int argc, char **argv)
{
pthread_t tid_audio, tid_speechrec, tid_tcp, tid_pic, tid_music, tid_camera, tid_vedio, tid_led, tid_ges;
login();
pthread_create(&tid_ges, NULL, thread_gesture, NULL);
pthread_create(&tid_led, NULL, thread_led, NULL);
pthread_create(&tid_vedio, NULL, thread_vedio, NULL);
pthread_create(&tid_camera, NULL, thread_camera, NULL);
pthread_create(&tid_music, NULL, thread_music, NULL);
pthread_create(&tid_audio,NULL,thread_audio,NULL);
pthread_create(&tid_tcp,NULL,thread_tcp_xml,NULL);
pthread_create(&tid_pic,NULL,thread_pic,NULL);
touch();
return 0;
}
|