资源描述:
《linux下ALSA播放声音的源程序.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、linux下ALSA播放声音的源程序#include/*UsethenewerALSAAPI*/#defineALSA_PCM_NEW_HW_PARAMS_API#includeintmain(){ longloops; intrc; intsize; snd_pcm_t*handle; snd_pcm_hw_params_t*params; unsignedintval; intdir; snd_pcm_ufra
2、mes_tframes; snd_pcm_uframes_tperiodsize; char*buffer; FILE*fp=fopen("play.wav","rb"); if(fp==NULL) return0; fseek(fp,100,SEEK_SET); /*OpenPCMdeviceforplayback.*/ rc=snd_pcm_open(&handle,"default", SND_PCM_STREAM_PLAYBAC
3、K,0);//SND_PCM_NONBLOCK); if(rc<0){ fprintf(stderr, "unabletoopenpcmdevice:%s", snd_strerror(rc)); exit(1); } /*Allocateahardwareparametersobject.*/ snd_pcm_hw_params_alloca(¶ms); /*Fillitinwithdefaultvalues.*/ snd
4、_pcm_hw_params_any(handle,params); /*Setthedesiredhardwareparameters.*/ /*Interleavedmode*/ snd_pcm_hw_params_set_access(handle,params, SND_PCM_ACCESS_RW_INTERLEAVED); /*Signed16-bitlittle-endianformat*/ snd_pcm_hw_params_se
5、t_format(handle,params, SND_PCM_FORMAT_S16_LE); /*Twochannels(stereo)*/ snd_pcm_hw_params_set_channels(handle,params,1); /*44100bits/secondsamplingrate(CDquality)*/ val=8000; snd_pcm_hw_params_set_rate_near(handle,pa
6、rams, &val,&dir); /*Setperiodsizeto32frames.*/ frames=32; periodsize=frames*2; rc=snd_pcm_hw_params_set_buffer_size_near(handle,params,&periodsize); if(rc<0) { printf("Unabletosetbuffersize%li:%s",frames
7、*2,snd_strerror(rc)); } periodsize/=2; rc=snd_pcm_hw_params_set_period_size_near(handle,params,&periodsize,0); if(rc<0) { printf("Unabletosetperiodsize%li:%s",periodsize, snd_strerror(rc)); } /*Writetheparameterstothed
8、river*/ rc=snd_pcm_hw_params(handle,params); if(rc<0){ fprintf(stderr, "unabletosethwparameters:%s", snd_strerror(rc)); } /*Useabufferlargeenoughtoholdoneperiod*/ snd_pcm_hw_params_get_period_size(params,&f