#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#define LOCALPORT 4567
#define LENGTH 2
#define RATE 8000
#define SIZE 8
#define CHANNELS 1
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8*100];
int main(int argc,char *argv[])
{
int mysock,len;
struct sockaddr_in addr;
int i=0;
//char msg[256];
int addr_len;
if((mysock=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("error");
exit(1);
}
else
{
printf("socket created .\n");
printf("socketet id:%d \n",mysock);
}
addr_len=sizeof(struct sockaddr_in);
bzero(&addr,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(LOCALPORT);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(mysock,(struct sockaddr *)&addr,sizeof(struct sockaddr))<0)
{
perror("connect");
exit(1);
}
else
{
printf("bind ok.\n");
printf("local port :%d \n",LOCALPORT);
}
int fd;
int arg;
int status;
fd = open("/dev/dsp", O_RDWR);
if (fd < 0)
{
perror("open of /dev/dsp failed");
exit(1);
}
arg = SIZE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if (arg != SIZE)
perror("unable to set sample size");
arg = CHANNELS;
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
if (arg != CHANNELS)
perror("unable to set number of channels");
arg = RATE;
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");
while(1)
{
printf("You said:\n");
bzero(buf,sizeof(buf));
len=recvfrom(mysock,buf,sizeof(buf),0,(struct sockaddr *)&addr,&addr_len);
status = write(fd, buf, sizeof(buf));
if (status != sizeof(buf))
perror("wrote wrong number of bytes");
status = ioctl(fd, SOUND_PCM_SYNC, 0);
if (status == -1)
perror("SOUND_PCM_SYNC ioctl failed");
printf("%d :",i);
i++;
printf("message from : %s \n",inet_ntoa(addr.sin_addr));
printf("message length : %d \n",len);
printf("message : %s \n\n",buf);
sendto(mysock,buf,len,0,(struct sockaddr *)&addr,addr_len);
}
}
UDP_AUDIO.zip_arm_arm音频采集_audio_音频 udp_音频客户端
版权申诉
17 浏览量
2022-09-20
19:55:07
上传
评论
收藏 2KB ZIP 举报
林当时
- 粉丝: 100
- 资源: 1万+
最新资源
- 基于STM32F103单片机(寄存器版)+OV7725摄像头模块照相机实验实验例程源码.zip
- 基于Python实现的桥梁易损性分析源码+代码注释.zip
- 基于python开发使用深度学习去预测股票后续的价格+源码+文档(毕业设计&课程设计&项目开发)
- flowable-designer-5.22.0.zip
- threadmanager.cpp
- 腾讯云小程序 - 一站式开发与部署平台
- 基于JSP+Java+Servlet采用MVC模式开发的购物网站+源码(毕业设计&课程设计&项目开发)
- fastgestures安装包,模拟mac的触控板收拾,两指代表右击, 三指拖拽
- 基于组态王的升降式横移立体车库控制系统+源码(毕业设计&课程设计&项目开发)
- 基于python+Django和协同过滤算法的电影推荐系统+源码(毕业设计&课程设计&项目开发)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈