linux下编写alsa测试程序
2013-03-01 17:21
309 查看
1. 编译alsa-lib的静态库
configure --enable-static=yes --enable-shared=no
2. 编译命令
gcc test_audio.c libasound.a -I. -lm -lrt -ldl -lpthread
-lrt 指向一个小型通用程序库(lists, hash table, socket, etc), clock_gettime包含在这个库中
-ldl 动态打开动态链接库
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <alsa/formats.h>
int main()
{
snd_pcm_t *handle = NULL;
snd_pcm_hw_params_t *hw_params = NULL;
snd_pcm_uframes_t chunk_size = 1024;
unsigned char *audiobuf = NULL;
int err = 0;
int fd = 0;
int fd_size = 0;
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (err < 0)
{
printf("snd_pcm_open error: %s", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_malloc(&hw_params);
if (err < 0)
{
printf("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_any(handle, hw_params);
if (err < 0)
{
printf("snd_pcm_hw_params_any: %s\n", snd_strerror(err));
return -1;
}
fd = open("/mnt/sound/music.wav", O_RDONLY, 0);
if (fd < 0)
{
printf("open music.wav error\n");
return -1;
}
fd_size = lseek(fd, 0, SEEK_END);
if (fd_size < 0)
{
printf("lseek music.wav error\n");
return -1;
}
lseek(fd, 0, SEEK_SET);
audiobuf = (unsigned char*)malloc(fd_size);
if (NULL == audiobuf)
{
printf("audiobuf malloc error");
return -1;
}
memset(audiobuf, 0, sizeof(audiobuf));
err = read(fd, audiobuf, fd_size);
printf("%d %d\n", err, fd_size);
if (err < 0)
{
printf("read from music.wav error\n");
return -1;
}
WaveHeader *wavehead = audiobuf;
if (wavehead->magic != WAV_RIFF || wavehead->type != WAV_WAVE)
{
printf("WaveHeader = 0x%x\n", AU_MAGIC);
printf("head_magic = 0x%x\n", wavehead->magic);
return -1;
}
WaveChunkHeader *chunkheader = (WaveChunkHeader*)(audiobuf + sizeof(WaveHeader));
if (chunkheader->type != WAV_FMT)
{
printf("0x%x 0x%x \n", chunkheader->type, chunkheader->length);
return -1;
}
WaveFmtBody *wavefmtbody = (WaveFmtBody*)(audiobuf + sizeof(WaveHeader) + sizeof(WaveChunkHeader));
if (wavefmtbody->format != WAV_FMT_PCM)
{
printf("wavefmtbody->format = 0x%x \n", wavefmtbody->format);
return -1;
}
err = snd_pcm_hw_params_set_channels(handle, hw_params, wavefmtbody->channels);
if (err < 0)
{
printf("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err));
return -1;
}
printf("snd_pcm_hw_params_set_channels = %d\n", wavefmtbody->channels);
err = snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE);
if (err < 0)
{
printf("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
return -1;
}
printf("snd_pcm_hw_params_set_format = %d\n", wavefmtbody->bit_p_spl);
chunkheader = (WaveChunkHeader*)(audiobuf + sizeof(WaveHeader) + sizeof(WaveChunkHeader) + sizeof(WaveFmtBody));
if (chunkheader->type != WAV_DATA)
{
printf("0x%x 0x%x \n", chunkheader->type, chunkheader->length);
return -1;
}
snd_pcm_writei()
return 0;
configure --enable-static=yes --enable-shared=no
2. 编译命令
gcc test_audio.c libasound.a -I. -lm -lrt -ldl -lpthread
-lrt 指向一个小型通用程序库(lists, hash table, socket, etc), clock_gettime包含在这个库中
-ldl 动态打开动态链接库
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <alsa/formats.h>
int main()
{
snd_pcm_t *handle = NULL;
snd_pcm_hw_params_t *hw_params = NULL;
snd_pcm_uframes_t chunk_size = 1024;
unsigned char *audiobuf = NULL;
int err = 0;
int fd = 0;
int fd_size = 0;
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (err < 0)
{
printf("snd_pcm_open error: %s", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_malloc(&hw_params);
if (err < 0)
{
printf("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_any(handle, hw_params);
if (err < 0)
{
printf("snd_pcm_hw_params_any: %s\n", snd_strerror(err));
return -1;
}
fd = open("/mnt/sound/music.wav", O_RDONLY, 0);
if (fd < 0)
{
printf("open music.wav error\n");
return -1;
}
fd_size = lseek(fd, 0, SEEK_END);
if (fd_size < 0)
{
printf("lseek music.wav error\n");
return -1;
}
lseek(fd, 0, SEEK_SET);
audiobuf = (unsigned char*)malloc(fd_size);
if (NULL == audiobuf)
{
printf("audiobuf malloc error");
return -1;
}
memset(audiobuf, 0, sizeof(audiobuf));
err = read(fd, audiobuf, fd_size);
printf("%d %d\n", err, fd_size);
if (err < 0)
{
printf("read from music.wav error\n");
return -1;
}
WaveHeader *wavehead = audiobuf;
if (wavehead->magic != WAV_RIFF || wavehead->type != WAV_WAVE)
{
printf("WaveHeader = 0x%x\n", AU_MAGIC);
printf("head_magic = 0x%x\n", wavehead->magic);
return -1;
}
WaveChunkHeader *chunkheader = (WaveChunkHeader*)(audiobuf + sizeof(WaveHeader));
if (chunkheader->type != WAV_FMT)
{
printf("0x%x 0x%x \n", chunkheader->type, chunkheader->length);
return -1;
}
WaveFmtBody *wavefmtbody = (WaveFmtBody*)(audiobuf + sizeof(WaveHeader) + sizeof(WaveChunkHeader));
if (wavefmtbody->format != WAV_FMT_PCM)
{
printf("wavefmtbody->format = 0x%x \n", wavefmtbody->format);
return -1;
}
err = snd_pcm_hw_params_set_channels(handle, hw_params, wavefmtbody->channels);
if (err < 0)
{
printf("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err));
return -1;
}
printf("snd_pcm_hw_params_set_channels = %d\n", wavefmtbody->channels);
err = snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE);
if (err < 0)
{
printf("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
return -1;
}
printf("snd_pcm_hw_params_set_format = %d\n", wavefmtbody->bit_p_spl);
chunkheader = (WaveChunkHeader*)(audiobuf + sizeof(WaveHeader) + sizeof(WaveChunkHeader) + sizeof(WaveFmtBody));
if (chunkheader->type != WAV_DATA)
{
printf("0x%x 0x%x \n", chunkheader->type, chunkheader->length);
return -1;
}
snd_pcm_writei()
return 0;
相关文章推荐
- linux下通过编写测试程序的方式来确认arm-linux-gnueabihf-gcc-4.6lib和.h文件的安装位置,为复制libnl做准备
- linux环境下C语言实现非阻塞方式读取字符串数据的串口测试程序,即串口工具的编写
- linux按键驱动编写及测试程序
- Linux下apache服务器安装,sqlite安装,apache启动,关闭,重启,编写cig程序进行测试,浏览器访问cig程序
- Windows 上面搭建 Spark + Pycharm/idea scala/python 本地编写Spark程序,测试通过后再提交到Linux集群上
- 使用socket的Linux上的C语言helloworld多线程服务器和客户端测试程序
- 在Android平台下的基于Linux-C 的测试程序
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- 编写Linux下的UDP Client/Server程序
- ARM Linux 2.6.36 内核 USB读卡器 测试程序
- 利用socket编写windows和linux通讯程序
- linux触摸屏touch screen调用测试程序
- Linux下自定义命令行实用程序及命令的man手册的编写
- 编写Linux下的UDP Client/Server程序
- 【linux C语言开发】有关enum枚举类型的一个测试程序
- linux下C程序从编写到执行完整过程
- Linux 环境下 Gsoap webservice C程序编写
- 编写在Android的Linux系统中直接运行的可执行程序
- Android底层开发(二)之编写驱动测试程序 第五步
- Linux编写C程序指导