经典ALSA音频读取通过UDP局域网播放

alsa_udp_r.c:

#include "alsa/asoundlib.h"

struct snd_pcm

{

    snd_pcm_t *handle;

    snd_pcm_uframes_t frame;

};

int init_pcm_capture(snd_pcm_t **handle,snd_pcm_uframes_t *frame)

{

    //1.定义pcm句柄

    snd_pcm_hw_params_t *params = NULL;

    //2.打开pcm设备---采集方式打开

    //(SND_PCM_STREAM_CAPTURE:打开模式)

    snd_pcm_open(handle, "default", SND_PCM_STREAM_CAPTURE, 0);

    //3 分配参数空间, 设置默认参数

    snd_pcm_hw_params_alloca(¶ms);

    //分配空间

    snd_pcm_hw_params_any(*handle, params);  //默认值

    //4.设置交错模式

    snd_pcm_hw_params_set_access(*handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

    //5设置量化

    snd_pcm_hw_params_set_format(*handle, params, SND_PCM_FORMAT_S16_LE);

    //6设置通道数据

    snd_pcm_hw_params_set_channels(*handle, params, 2);

    //7设置采样率

    int val = 44100;

    snd_pcm_hw_params_set_rate_near(*handle, params, &val,0);

    //8把设置好的参数设置到pcm设备

    snd_pcm_hw_params(*handle,params);

    //9.分配一个周期空间

    //获取一个周期的帧数

    snd_pcm_hw_params_get_period_size(params, frame, 0);

}

int init_pcm_playback(snd_pcm_t **handle,snd_pcm_uframes_t *frame)

{

    //1.定义pcm句柄

    //snd_pcm_t *handle = NULL;

    snd_pcm_hw_params_t *params = NULL;

    //2.打开pcm设备---采集方式打开

    //(SND_PCM_STREAM_CAPTURE:打开模式)

    snd_pcm_open(handle, "default", SND_PCM_STREAM_PLAYBACK, 0);

    //3 分配参数空间, 设置默认参数

    snd_pcm_hw_params_alloca(¶ms);

    //分配空间

    snd_pcm_hw_params_any(*handle, params);  //默认值

    //4.设置交错模式

    snd_pcm_hw_params_set_access(*handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

    //5设置量化

    snd_pcm_hw_params_set_format(*handle, params, SND_PCM_FORMAT_S16_LE);

    //6设置通道数据

snd_pcm_hw_params_set_channels(*handle, params, 2);

    //7设置采样率

    int val = 44100;

    snd_pcm_hw_params_set_rate_near(*handle, params, &val,0);

    //8把设置好的参数设置到pcm设备

    snd_pcm_hw_params(*handle,params);

    //9.分配一个周期空间

    //获取一个周期的帧数

    snd_pcm_hw_params_get_period_size(params, frame, 0);

}

int init_udp_sockfd(int *sockfd,struct sockaddr_in *s_addr,int argc,char **argv)

{

    if(argc < 1)

    {

    printf("input error\n");

    exit(0);

    }

    if((*sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)

    {

perror("socket");

exit(0);

}

bzero(s_addr,sizeof(struct sockaddr_in));

s_addr->sin_family = AF_INET;

s_addr->sin_port  = htons(12345);

s_addr->sin_addr.s_addr = inet_addr(argv[1]);

int sin_size = 1;

if((setsockopt(*sockfd,SOL_SOCKET,SO_REUSEADDR,&sin_size,sizeof(int))) < 0)

{

perror("setsockopt");

exit(0);

}

return 0;

}

int main(int argc,char **argv)

{

    //初始化udp

    int sockfd;

    struct sockaddr_in s_addr;

    init_udp_sockfd(&sockfd,&s_addr,argc,argv);

    //初始化pcm

    struct snd_pcm sp_cp;

    struct snd_pcm sp_pb;

    sp_cp.handle = NULL;

    sp_cp.frame  = 0;

    sp_pb.handle = NULL;

    sp_pb.frame  = 0;    

    init_pcm_capture(&sp_cp.handle,&sp_cp.frame);

    //init_pcm_playback(&pb.handle,&sp_pb.frame);

    int size = sp_cp.frame * 4;//计算一个周期所需要的存储空间

    unsigned char *buffer = malloc(size);//分配一个周期的空间

    while(1)

    {

    snd_pcm_readi(sp_cp.handle, buffer, sp_cp.frame);//采集一个周期数据

    if(sendto(sockfd,buffer,size,0,(struct sockaddr *)&s_addr,sizeof(s_addr)) < 0)

       {

            perror("sendto");

            exit(0);

        }

    }

    //销毁pcm句柄

    snd_pcm_drain(sp_cp.handle);

    //关闭pcm设备

    snd_pcm_close(sp_cp.handle);

    //释放堆空间(存储一个周期数据)

    free(buffer);

    return 0;

}

上面代码属于嵌入式板上获取音频信息并且通过UDP发送到目标IP,使用./可执行文件名字 目标IP

alsa_net_s.c:

#include "alsa/asoundlib.h"

struct snd_pcm

{

snd_pcm_t *handle;

snd_pcm_uframes_t frame;

};

int init_pcm_playback(snd_pcm_t **handle,snd_pcm_uframes_t *frame)

{

//1.定义pcm句柄

snd_pcm_hw_params_t *params = NULL;

//2.打开pcm设备---采集方式打开

//(SND_PCM_STREAM_CAPTURE:打开模式)

snd_pcm_open(handle, "default", SND_PCM_STREAM_PLAYBACK, 0);

//3 分配参数空间, 设置默认参数

snd_pcm_hw_params_alloca(¶ms);

//分配空间

snd_pcm_hw_params_any(*handle, params);  //默认值

//4.设置交错模式

snd_pcm_hw_params_set_access(*handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

//5设置量化

snd_pcm_hw_params_set_format(*handle, params, SND_PCM_FORMAT_S16_LE);

//6设置通道数据

snd_pcm_hw_params_set_channels(*handle, params, 2);

//7设置采样率

int val = 44100;

snd_pcm_hw_params_set_rate_near(*handle, params, &val,0);

//8把设置好的参数设置到pcm设备

snd_pcm_hw_params(*handle,params);

//9.分配一个周期空间

//获取一个周期的帧数

snd_pcm_hw_params_get_period_size(params, frame, 0);

}

int init_udp_sockfd(int *sockfd,struct sockaddr_in *s_addr,int argc,char **argv)

{

if(argc < 1)

{

printf("input error\n");

exit(0);

}

if((*sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)

{

perror("socket");

exit(0);

}

memset(s_addr,0,sizeof(struct sockaddr_in));

s_addr->sin_family = AF_INET;

s_addr->sin_port  = htons(12345);

s_addr->sin_addr.s_addr = inet_addr(argv[1]);

int sin_size = 1;

if((setsockopt(*sockfd,SOL_SOCKET,SO_REUSEADDR,&sin_size,sizeof(int))) < 0)

{

perror("setsockopt");

exit(0);

}

if(bind(*sockfd,(struct sockaddr *)s_addr,sizeof(struct sockaddr_in)) < 0)

{

perror("bind");

exit(0);

}

return 0;

}

int main(int argc,char **argv)

{

//初始化udp

int sockfd;

struct sockaddr_in s_addr,c_addr;

int len = sizeof(s_addr);

init_udp_sockfd(&sockfd,&s_addr,argc,argv);

//初始化pcm

struct snd_pcm sp_cp;

struct snd_pcm sp_pb;

sp_cp.handle = NULL;

sp_cp.frame  = 0;

sp_pb.handle = NULL;

sp_pb.frame  = 0;

//init_pcm_capture(&sp_cp.handle,&sp_cp.frame_cap);

init_pcm_playback(&sp_pb.handle,&sp_pb.frame);

int size = sp_pb.frame * 4;//计算一个周期所需要的存储空间

unsigned char *buffer = malloc(size);//分配一个周期的空间

while(1)

{

if(recvfrom(sockfd,buffer,size,0,(struct sockaddr *)&c_addr,&len) < 0)

{

perror("recvfrom");

exit(0);

}

snd_pcm_writei(sp_pb.handle, buffer, sp_pb.frame);//采集一个周期数据

}

//销毁pcm句柄

snd_pcm_drain(sp_pb.handle);

//关闭pcm设备

snd_pcm_close(sp_pb.handle);

//释放堆空间(存储一个周期数据)

free(buffer);

return 0;

}

上面代码绑定本机IP和端口接收UDP信息,把接收到的音频信息播放出来,使用./可执行文件名字 本机IP

你可能感兴趣的:(经典ALSA音频读取通过UDP局域网播放)