您的位置:首页 > 运维架构 > Linux

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: