使用/dev/dsp的wav文件播放器源码
2019-07-13 09:47发布
生成海报
转载于:http://blog.csdn.net/dux003/article/details/5459423
#include
#include
#include
#include
#include
void usage(const char* self)
{
printf("usage:/n");
printf("/t%s
[-c channels -r rate -s samplesize] wavfile/n",
self);
};
int set_fmt(int fd, int channels, int rate, int
samplesize)
{
int c =
channels;
if
(ioctl(fd, SNDCTL_DSP_CHANNELS, &c) == -1)
exit(1);
if
(ioctl(fd, SNDCTL_DSP_SPEED, &rate) == -1)
exit(1);
if
(ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &samplesize) ==
-1)
exit(1);
return
0;
}
int main(int argc, char **argv)
{
int i =
1;
char*
filename = NULL;
int channels
= 1;
int
samplerate = 8000;
int
samplesize = 16;
int
dsp;
int
fd;
char
buf[1024];
int
len;
if
(argc%2)
{
usage(argv[0]);
exit(1);
}
while (i
< argc)
{
if (argv[i][0] != '-')
{
filename = argv[i];
i++;
}
else
{
if (i+1 < argc)
{ switch
(argv[i][1])
{
case 'c':
channels = atoi(argv[i+1]);
i += 2;
break;
case 'r':
samplerate = atoi(argv[i+1]);
i += 2;
break;
case 's':
samplesize = atoi(argv[i+1]);
i += 2;
break;
default:
perror("bad option/n");
exit(1);
}
}
else
{
perror("bad options/n");
exit(1);
}
}
}
dsp =
open("/dev/dsp", O_RDWR);
if (dsp ==
-1)
{
perror("can not open /dev/dsp/n");
exit(1);
}
set_fmt(dsp,
channels, samplerate, samplesize);
fd =
open(filename, O_RDWR);
if (fd ==
-1)
{
close(dsp);
fprintf(stderr, "can not open file %s/n",
filename);
exit(1);
}
while ((len
= read(fd, buf, 1024)) > 0)
{
write(dsp, buf, len);
}
close(fd);
close(dsp);
return
0;
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮