Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
#include <stdio.h>
#include 
<errno.h>
#include 
<string.h>
#include 
<unistd.h>
#include 
<fcntl.h>
#include 
<math.h>
#if defined(__OpenBSD__) || defined(__NetBSD__)
#include 
<soundcard.h>
#else
#include 
<sys/soundcard.h>
#endif
#include 
<sys/ioctl.h>
#include 
<sndfile.h>

#define BUFFER_LEN 4096

int _open_default_oss_device(char **dev_path,int id,int blocking);

int main(int argc,char *argv[])
{
    
if(argc<2)
        
return 0;

    SF_INFO info;
    SNDFILE
* file = sf_open(argv[1],SFM_READ,&info);
    
if(!file)
        
return 0;

    printf(
"samplerate:%d\n",info.samplerate);
    printf(
"channel:%d\n",info.channels);

    
char* dsp = NULL;
    
int dp = _open_default_oss_device(&dsp,0,0);
    if(dp < 0)
    {
        printf(
"dsp:%d\n",dp);
        perror("oss bad:");
        sf_close(file);
        return -1; 
    }
    
int tmp = info.channels;
    
int ret = ioctl(dp,SNDCTL_DSP_CHANNELS,&tmp);
    tmp 
= info.samplerate;
    ret 
= ioctl(dp,SNDCTL_DSP_SPEED,&tmp);
    tmp 
= AFMT_S16_LE;
    ioctl(dp,SNDCTL_DSP_SAMPLESIZE,
&tmp);

    
long  len = 0;
    
short buffer[BUFFER_LEN];
    
while((len=sf_read_short(file,buffer,BUFFER_LEN))!=0)
    {
        write(dp,(
char*)buffer,len*2);
    }

    close(dp);
    sf_close(file);
    free(dsp);
    
return 0;
}

int _open_default_oss_device(char **dev_path,int id,int blocking)
{
    
int fd;
    
char buf[80];

    
if(id>0)
    {
        sprintf(buf,
"/dev/sound/dsp%d",id);
        
if(!(*dev_path = strdup(buf)))
            
return -1;
    }
    
else
    {
        
if(!(*dev_path = strdup("/dev/sound/dsp")))
            
return -1;
    }

#ifdef BROKEN_OSS
    fd 
= open(*dev_path, O_WRONLY | O_NONBLOCK);
#else
    fd 
= open(*dev_path, O_WRONLY);
#endif

    
if(fd < 0)
    {
        free(
*dev_path);
        
if(id>0)
        {
            sprintf(buf,
"/dev/dsp%d",id);
            
if(!(*dev_path = strdup(buf)))
                
return -1;
        }
        
else
        {
            
if(!(*dev_path = strdup("/dev/dsp")))
                
return -1;
        }
#ifdef BROKEN_OSS
        fd 
= open(*dev_path, O_WRONLY | O_NONBLOCK);
#else
        fd 
= open(*dev_path, O_WRONLY);
#endif
    }

#ifdef BROKEN_OSS
    
if(fd >= 0 && blocking)
    {
        
if(fcntl(fd, F_SETFL, 0< 0)
        {
            close(fd);
            fd 
= -1;
        }
    }
#endif
    
if(fd < 0)
    {
        free(
*dev_path);
        
*dev_path = NULL;
    }
    
return fd;
}

只是在的我的ubuntu13.0.4下执行执行 ./text music.wav 会提示错误 返回-1 提示 no such directory
在网上搜有人说需要加载snd_pcm_oss
modprobe snd_pcm_oss
可我加载的时候提示
FATAL: Module snd_pcm_oss not found.
谁知道这个如何弄?

另外查了下可以加载alsa-oss
之后调用aoss ./text music.wav来播放音乐
可以这个不是我要的,非命令行下不能加这个前缀
posted on 2013-07-19 22:11 ccsdu2009 阅读(1597) 评论(0)  编辑 收藏 引用 所属分类: 音频视频

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理