使用/dev/dsp的wav文件播放器源码
2010-04-07 19:09
351 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>
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;
}
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>
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;
}
相关文章推荐
- 使用/dev/dsp的wav文件播放器源码
- 使用/dev/dsp的wav文件播放器源码
- Wave 文件(9): 使用 waveOut... 函数播放 wav 文件
- 使用getCurrentPosition方法实时获取当前Geolocation信息(附源码文件)--html5、JavaScript
- 蔡军生先生第二人生的源码分析(六十九)使用LLXmlTree类来分析XML配置文件
- [加密解密]使用CryptoAPI进行文件加密解密——源码
- 解决Android使用Eclipse查看源代码的Source not found,附带Android源码文件
- 前端WEB播放器使用伪m3u8文件格式播放视频的方法
- 在Qt工程文件中根据不同版本号使用不同源码
- 使用wininetAPI下载internet文件的源码
- 如何使用MAP文件找到程序崩溃时源码是哪行异常
- WAV文件有多大?MP3文件有多大?使用Lame 压缩比是多少?
- 操作 Wave 文件(11): 使用 waveIn...函数录制 wav 文件
- python使用wxPython打开并播放wav文件的方法
- [C] tcharall(让所有平台支持TCHAR)v1.1。源码托管到github、添加CMake编译配置文件、使用doxygen规范注释
- java日志文件,log4j.properties使用举例,源码
- 使用struts2实现多文件上传源码
- 使用dex2jar 与xjad 反编译APK文件,查看源码
- Linux CentOS audio: /dev/dsp: 没有那个文件或目录
- (8): 使用 TMediaPlayer 录制 wav 文件