专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
DSP
Linux 下的C语言实现播放WAV文件
2019-07-13 15:32
发布
生成海报
站内文章
/
DSP
14957
0
1384
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define OPEN_DSP_FAILED 0x00000001 /*打开 dsp 失败!*/ #define SAMPLERATE_STATUS 0x00000002 /*samplerate status failed*/ #define SET_SAMPLERATE_FAILED 0x00000003 /*set samplerate failed*/ #define CHANNELS_STATUS 0x00000004 /*Channels status failed*/ #define SET_CHANNELS_FAILED 0x00000005 /*set channels failed*/ #define FMT_STATUS 0x00000006 /*FMT status failed*/ #define SET_FMT_FAILED 0x00000007 /*set fmt failed*/ #define OPEN_FILE_FAILED 0x00000008 /*opem filed failed*/ int P8100_Audio_Play(char *pathname,int nSampleRate,int nChannels,int fmt) { int dsp_fd,mix_fd,status,arg; dsp_fd = open("/dev/dsp" , O_WRONLY); /*open dsp*/ if(dsp_fd < 0) { return OPEN_DSP_FAILED; } arg = nSampleRate; status = ioctl(dsp_fd,SOUND_PCM_WRITE_RATE,&arg); /*set samplerate*/ if(status < 0) { close(dsp_fd); return SAMPLERATE_STATUS; } if(arg != nSampleRate) { close(dsp_fd); return SET_SAMPLERATE_FAILED; } arg = nChannels; /*set channels*/ status = ioctl(dsp_fd, SOUND_PCM_WRITE_CHANNELS, &arg); if(status < 0) { close(dsp_fd); return CHANNELS_STATUS; } if( arg != nChannels) { close(dsp_fd); return SET_CHANNELS_FAILED; } arg = fmt; /*set bit fmt*/ status = ioctl(dsp_fd, SOUND_PCM_WRITE_BITS, &arg); if(status < 0) { close(dsp_fd); return FMT_STATUS; } if(arg != fmt) { close(dsp_fd); return SET_FMT_FAILED; }/*到此设置好了DSP的各个参数*/ FILE *file_fd = fopen(pathname,"r"); if(file_fd == NULL) { close(dsp_fd); return OPEN_FILE_FAILED; } int num = 3*nChannels*nSampleRate*fmt/8; int get_num; char buf[num]; while(feof(file_fd) == 0) { get_num = fread(buf,1,num,file_fd); write(dsp_fd,buf,get_num); if(get_num != num) { close(dsp_fd); fclose(file_fd); return 0; } } close(dsp_fd); fclose(file_fd); return 0; } /* *test */ int main() { int value; value = P8100_Audio_Play("/windows/C/WINDOWS/Media/Windows Startup.wav",44100,2,16);//注意播放文件的路径哦!! fprintf(stderr,"value is %d",value); return 0; }
以上代码在嵌入式LINUX平台上测试OK!
Ta的文章
更多
>>
Linux 下的C语言实现播放WAV文件
0 个评论
热门文章
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮