免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3958 | 回复: 4
打印 上一主题 下一主题

linux音频设备驱动问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-06 11:20 |只看该作者 |倒序浏览
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>

#define BUF_LENGTH 2000
#define CHANNELS 0  /*0--single channel;2--double channel*/
#define SAMPLE_RATE 8000   

int set_fmt(int audio_fd, int bits, int rate, int channel)
{
        int status = -1;

        if( -1 == ioctl(audio_fd, SNDCTL_DSP_SAMPLESIZE, &bits))
        {
                perror("ioctl SNDCTL_DSP_SAMPLESIZE");
                exit(1);
        }

        if(-1 == ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate))
        {
                perror("ioctl SNDCTL_DSP_SPEED");
                exit(1);
        }

        if(-1 == ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channel))
        {
                perror("ioctl SNDCTL_DSP_CHANNELS");
                exit(1);
        }

        return(0);
}

main()
{
        int audio_fd = 0;
        int bytes = 0;
        int i = 0;
        char buf[BUF_LENGTH];

       
        audio_fd = open("/dev/dsp", O_RDWR);
        if(audio_fd < 0)
        {
                perror("open /dev/dsp");
                exit(1);
        }
       
        if(-1 == (set_fmt(audio_fd, AFMT_U8, SAMPLE_RATE, CHANNELS)))
        {
                perror("set_fmt");
                exit(1);
        }

        for(i=0; i<5000; i++)
        {
                if(-1 == (bytes = read(audio_fd, buf, BUF_LENGTH)))
                {
                        perror("read");
                        exit(1);
                }
                if(-1 == write(audio_fd, buf, bytes))
                {
                        perror("write");
                        exit(1);
                }
               
        }
       
        printf("It'll be closed\n");
        close(audio_fd);
}

使用这段代码编译后,为什么不能使用阿?没有任何错误信息,就是不出声!!

论坛徽章:
0
2 [报告]
发表于 2008-05-06 13:02 |只看该作者

回复 #1 cc-liuwei 的帖子

请分开些,打开设备的时候不要用O_RDWR!
O_RDONLY - record
O_WRONLY - playback

OSS里面好像建议这样做

论坛徽章:
0
3 [报告]
发表于 2008-05-06 14:08 |只看该作者
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>

#define BUF_LENGTH 2000
#define CHANNELS 0  /*0--single channel;2--double channel*/
#define SAMPLE_RATE 8000   

int set_fmt(int audio_fd, int bits, int rate, int channel)
{
        int status = -1;

        if( -1 == ioctl(audio_fd, SNDCTL_DSP_SAMPLESIZE, &bits))
        {
                perror("ioctl SNDCTL_DSP_SAMPLESIZE");
                exit(1);
        }

        if(-1 == ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate))
        {
                perror("ioctl SNDCTL_DSP_SPEED");
                exit(1);
        }

        if(-1 == ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channel))
        {
                perror("ioctl SNDCTL_DSP_CHANNELS");
                exit(1);
        }

        return(0);
}

main()
{
        int audio_fd = 0;
        int bytes = 0;
        int i = 0;
        char buf[BUF_LENGTH];

       
        audio_fd = open("/dev/dsp", O_RDONLY);
        if(audio_fd < 0)
        {
                perror("open /dev/dsp");
                exit(1);
        }
       
        if(-1 == (set_fmt(audio_fd, AFMT_U8, SAMPLE_RATE, CHANNELS)))
        {
                perror("set_fmt");
                exit(1);
        }

        for(i=0; i<500; i++)
        {
                if(-1 == (bytes = read(audio_fd, buf, BUF_LENGTH)))
                {
                        perror("read");
                        exit(1);
                }
        }
        close(audio_fd);
       
        audio_fd = open("/dev/dsp", O_WRONLY);
        if(audio_fd < 0)
        {
                perror("open /dev/dsp");
                exit(1);
        }
       
        if(-1 == (set_fmt(audio_fd, AFMT_U8, SAMPLE_RATE, CHANNELS)))
        {
                perror("set_fmt");
                exit(1);
        }

        for(i=0; i<500; i++)
        {
        if(-1 == write(audio_fd, buf, bytes))
                {
                        perror("write");
                        exit(1);
                }
               
        }
       
        printf("It'll be closed\n");
        close(audio_fd);
}
是这样写马?不行阿,好像打开设备就有问题……程序就是到open这里就运行不了了……

论坛徽章:
0
4 [报告]
发表于 2008-05-06 19:23 |只看该作者

回复 #3 cc-liuwei 的帖子

我用你的代码编译过,试过了, 代码逻辑没有问题。但是运行时间可能有些久,是不是你说的运行不了就是指的程序没有结束呢?如果是这样,那请你多等一会,如果不是,就再说了。反正在我这边是没有问题的.

时间久的原因:低采样率和采样精度,因为8K 8bit的采样率每秒产生的数据量是很低的。填充总长是2K*500的buffer需要一定时间。可以选择小的循环次数来做测试。

论坛徽章:
0
5 [报告]
发表于 2008-05-23 12:37 |只看该作者
我也用你的編程試過,是可以跑的,只是時間會比較久。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP