您的位置:首页 > 运维架构 > Linux

Linux下c语言简单实现播放wav

2018-01-08 14:16 405 查看
  Linux下的声音设备编程比大多数人想象的要简单得多。一般说来,我们常用的声音设备是内部扬声器和声卡,它们都对应/dev目录下的一个或多个设备文件,我们象打开普通文件一样打开它们,用ioctl()函数设置一些参数,然后对这些打开的特殊文件进写操作。
  由于这些文件不是普通的文件,所以我们不能用ANSI C(标准C)的fopen、fclose等来操作文件,而应该使用系统文件I/O处理函数(open、read、write、lseek和close)来处理这些设备文件。ioctl()或许是Linux下最庞杂的函数,它可以控制各种文件的属性,在Linux声音设备编程中,最重要的就是使用此函数正确设置必要的参数。
       以下程序实现了在Linux下播放Online.wav的功能。程序首先调用fstat函数获得文件相关信息(主要是文件大小信息)。通过malloc函数分配指定的内存空间,并将online.wav读入内存;然后,打开声卡设备文件,设置声卡参数;再调用write函数完成文件的播放:

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#define Audio_Device "/dev/dsp"
//不同的声卡有着不同的播放参数,这些参数可以使用file命令获得
#define Sample_Size 16 //there're two kinds of bits,8 bits and 16 bits
#define Sample_Rate 8000 //sampling rate

int play_sound(char *filename)
{
    struct stat stat_buf;
    unsigned char *buf = NULL;
    int handler,fd;
    int result;
    int arg,status;
  
    //打开声音文件,将文件读入内存
    fd=open(filename,O_RDONLY);
    if(fd<0) 
     return -1;
    if(fstat(fd,&stat_buf))
    {
        close(fd);
        return -1;
    }
    if(!stat_buf.st_size)
    {
        close(fd);
        return -1;
   }
   buf=malloc(stat_buf.st_size);
   if(!buf)
   {
       close(fd);
       return -1;
   }
   if(read(fd,buf,stat_buf.st_size)<0)
   {
       free(buf);
       close(fd);
       return -1;
   }
   //打开声卡设备,并设置声卡播放参数,这些参数必须与声音文件参数一致
   handler=open(Audio_Device,O_WRONLY);
   if(handler==-1)
   {
       perror("open Audio_Device fail");
       return -1;
   }
 
   arg=Sample_Rate;
   status=ioctl(handler,SOUND_PCM_WRITE_RATE,&arg);
   if(status==-1)
   {
       perror("error from SOUND_PCM_WRITE_RATE ioctl");
       return -1;
   }
   arg=Sample_Size;
   status=ioctl(handler,SOUND_PCM_WRITE_BITS,&arg);
   if(status==-1)
   {
       perror("error from SOUND_PCM_WRITE_BITS ioctl");
       return -1;
   }
  //向端口写入,播放
   result=write(handler,buf,stat_buf.st_size);
   if(result==-1)
   {
       perror("Fail to play the sound!");
       return -1;
   }
  
   free(buf);
   close(fd);
   close(handler);
  
    return result;
}

int main(void)
{
   play_sound("/root/Online.wav");
   
   exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux wav 音乐播放器