在Linux下,音频设备程序的实现与文件系统的操作密切相关。Linux将各种设备以文件的形式给出统一的接口,这样的设计使得对设备的编程与对文件的操作基本相同,对Linux内核的系统调用也基本一致,从而简化了设备编程。
如何对各种音频设备进行操作是在Linux上进行音频编程的关键,通过内核提供的一组系统调用,应用程序能够访问声卡驱动程序提供的各种音频设备接口,这是在Linux下进行音频编程最简单也是最直接的方法。
声卡不是Linux控制台的一部分,它是一个特殊的设备。声卡主要提供3个重要的特征:
这3个特征都有它们自己的设备驱动程序接口
混音设备(如音量、平衡或者贝斯)可以通过/dev/mixer接口来控制。
为了满足兼容性的需要,还提供了一个/dev/audio设备,该设备可用于读SUN_law的声音数据,但它是映射到数字取样设备的。
程序员可以使用ioctl()来操作这些设备,ioctl()请求是在linux/soundcard.h中定义的,它们以SNDCTL_开头。
系统调用open可以获得对声卡的访问权,同时还能为随后的系统调用做好准备,其函数原型如下所示:
int open(const char *pathname, int flags, int mode);
如果open系统调用失败,它将返回-1,同时还会设置全局变量errno,指明是什么原因导致了错误的发生。
read用来从声卡读取数据,其函数原型如下所示:
int read(int fd, char *buf, size_t count);
如果read系统调用成功完成,它将返回从声卡实际读取的字节数,通常情况会比count的值小一些;如果read系统调用失败,它将返回-1,同时还会设置全局变量errno,来指明是什么原因导致了错误的发生。
int handle = open("/dev/dsp", O_WRONLY); if (handle == -1) { perror("open /dev/dsp"); return -1; }
int setting = 0xnnnnssss; int result = ioctl(handle, SNDCTL_DSP_SETFRAGMENT, &setting); if (result == -1) { perror("ioctl buffer size"); return -1; }在设置缓冲区大小时,参数setting实际上由两部分组成,其低16位标明缓冲区的尺寸,相应的计算公式为buffer_size = 2^ssss,即若参数setting低16位的值为16,那么相应的缓冲区的大小会被设置为65536字节。参数setting的高16位则用来标明分片(fragment)的最大序号,它的取值范围从2到0x7FFF,其中0x7FFF表示没有任何限制。
int channels = 0; // 0=mono 1=stereo int result = ioctl(handle, SNDCTL_DSP_STEREO, &channels); if ( result == -1 ) { perror("ioctl channel number"); return -1; } if (channels != 0) { // 只支持立体声 }
int format = AFMT_U8; int result = ioctl(handle, SNDCTL_DSP_SETFMT, &format); if ( result == -1 ) { perror("ioctl sample format"); return -1; }
int rate = 22050; int result = ioctl(handle, SNDCTL_DSP_SPEED, &rate); if ( result == -1 ) { perror("ioctl sample format"); return -1; }
#include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<sys/ioctl.h> #include<linux/soundcard.h> #include<stdio.h> #include<stdlib.h> #include<errno.h> #define DEV "/dev/dsp" /*device name*/ #define RATE 48000 /*sample rate*/ #define SIZE 16 /*data size*/ #define CHANNELS 1 /*channels*/ #define TIME 3 /*times to be recorded*/ #define LENGTH RATE*SIZE*CHANNELS*TIME/8 /*lenght of datas*/ #define ACT_RECORD 0 /*action record*/ #define ACT_PLAY 1 /*action playback*/ #define ACT_RECORD_PLAY 2 /*action record and playback*/ int open_device(const char*, unsigned int); int close_device(unsigned int); int set_format(unsigned int, unsigned int, unsigned int, unsigned int); int main() { int i; int fd; /*file descriptor*/ unsigned char* buf; /*buffer for sound datas*/ buf = malloc(LENGTH); printf("open......\n"); /*open the sound device*/ fd = open_device(DEV,ACT_RECORD_PLAY); if(fd == -1){ fprintf(stderr,"Open Audio Device %s failed:%s\n",DEV,strerror(errno)); return -1; } printf("open successed\n"); printf("set......\n"); /*set the format of device*/ if(set_format(fd, SIZE, CHANNELS, RATE) < 0){ fprintf(stderr,"Cann't set %s in bit %d, channel %d, sample rate %d\n",DEV,SIZE,CHANNELS,RATE); return -1; } printf("set successed\n"); /*write data to device*/ for(i = 0; i < 10; i++){ printf("Say...\n"); if(read(fd, buf, LENGTH) < 0){ fprintf(stderr,"Read sound failed:%s\n",strerror(errno)); printf("read\n"); return -1; } printf("Listen...\n"); if(write(fd, buf, LENGTH) < 0){ fprintf(stderr,"Write sound failed:%s\n",strerror(errno)); printf("write\n"); return -1; } if(ioctl(fd, SOUND_PCM_SYNC, 0) < 0){ fprintf(stderr,"Sound sync failed:%s\n",strerror(errno)); printf("sync\n"); return -1; } } /*delete the ram and close the file*/ free(buf); if(close_device(fd) < 0){ fprintf(stderr,"Cann't close device %s:%s\n",DEV,strerror(errno)); return -1; } return 0; } /********************************************** * open_device():open sound device ***** params: ***** * dev_name --> device name, such as '/dev/dsp' * flag --> distinguish the action of open device(ACT_RECORD or ACT_PLAY) ***** return: ***** * fild descriptor of sound device if success, -1 if failed ***********************************************/ int open_device(const char* dev_name, unsigned int flag){ int dev_fd; /*open device*/ if(flag == ACT_RECORD){ if((dev_fd = open(dev_name, O_RDONLY)) < 0){ return -1; } } else if(flag == ACT_PLAY){ if((dev_fd = open(dev_name, O_WRONLY)) < 0){ return -1; } } else if(flag == ACT_RECORD_PLAY){ if((dev_fd = open(dev_name, O_RDWR)) < 0){ return -1; } } return dev_fd; } /********************************************** * close_device():close sound device ***** params: ***** * dev_fd --> the sound device's file descriptor ***** return: ***** * 0 if success, -1 if failed ***********************************************/ int close_device(unsigned int dev_fd){ return (close(dev_fd)); } /********************************************** * set_format():set record and playback format ***** params: ***** * fd --> device file descriptor * chn --> channel(MONO or STEREO) * bits --> data size(8bits or 16bits) * sr --> sample rate(8KHz or 16KHz or ....) ***** return: ***** * 0 if success, -1 if failed ***** notes: ***** * parameter setting order should be like as below: * 1. data size(number of bits) * 2. number of channel(mono or stereo) * 3. sample rate ***********************************************/ int set_format(unsigned int fd, unsigned int bits, unsigned int chn, unsigned int sr){ int ioctl_val; /*set data size*/ ioctl_val = bits; if(ioctl(fd, SOUND_PCM_WRITE_BITS, &ioctl_val) == -1){ fprintf(stderr,"Set SOUND_PCM_WRITE_BITS %d bits failed:%s\n",bits,strerror(errno)); return -1; } if(ioctl_val != bits){ fprintf(stderr,"Don't support %d bits,supported %d\n",bits,ioctl_val); return -1; } /*set number of channel*/ ioctl_val = chn; if(ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &ioctl_val) == -1){ fprintf(stderr,"Set SOUND_PCM_WRITE_CHANNELS %d failed:%s\n",chn,strerror(errno)); return -1; } if(ioctl_val != chn){ fprintf(stderr,"Don't support %d channel,supported %d\n",chn,ioctl_val); return -1; } /*set sample rate*/ ioctl_val = sr; if(ioctl(fd, SOUND_PCM_WRITE_RATE, &ioctl_val) == -1){ fprintf(stderr,"Set SOUND_PCM_WRITE_RATE %d failed:%s\n",sr,strerror(errno)); return -1; } if(ioctl_val != sr){ fprintf(stderr,"Don't support %d sample rate,supported %d\n",sr,ioctl_val); return -1; } return 0; }