3.2.录音与回放
#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;
}
关闭/dev/dsp设备可以通过命令:fuser -k /dev/dsp
