关闭

使用/dev/dsp的wav文件播放器源码

722人阅读 评论(0) 收藏 举报
分类:

转载于: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;

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:318764次
    • 积分:5002
    • 等级:
    • 排名:第5615名
    • 原创:162篇
    • 转载:172篇
    • 译文:1篇
    • 评论:24条
    交流分享
    欢迎大家加入QQ群:469897460 一起来分享交流技术
    最新评论