zoukankan      html  css  js  c++  java
  • 使用/dev/dsp的wav文件播放器源码

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

  • 相关阅读:
    Crazyflie 2.0 System Architecture
    HDU 4856 Tunnels(BFS+状压DP)
    scp报错:Host key verification failed. REMOTE HOST IDENTIFICATION HAS CHANGED!
    HDU 4175 Class Schedule (暴力+一点dp)
    正則表達式
    匿名訪问之(一)web application级别
    Android UI布局之TableLayout
    cocos2d-x-3.3rc2-003 cocos中的引用计数和自己主动释放池
    一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作
    hdu Swipe Bo(bfs+状态压缩)错了多次的题
  • 原文地址:https://www.cnblogs.com/songfeixiang/p/3733787.html
Copyright © 2011-2022 走看看