ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÒôƵ±à³Ì

·¢²¼Ê±¼ä:2011-04-29 15:22:42À´Ô´:ºìÁª×÷Õß:zlonglove
ÔõôÔÚlinuxÖнøÐÐÒôƵ±à³Ì£¿£¿£¿
#include
#include
#include
#include
#include
#include
#include

//¼Òôʱ¼ä
#define LENGTH 3

//²ÉÑùƵÂÊ
#define RATE 8000

//Á¿»¯Î»Êý
#define SIZE 16

//ÉùµÀÊýÄ¿
#define CHANNELS 2

//±£´æÂ¼ÒôµÄÒôƵÊý¾Ý
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main(void){
//ÉùÒôÉ豸µÄÎļþÃèÊö·û
int fd;

int arg;
//ÓÃÓÚ±£´æioctlµÄ·µ»ØÖµ
int status;

//´ò¿ªÉùÒôÉ豸
fd=open("/dev/dsp",O_RDWR);

if(fd<0){
perror("Cannot open /dev/dsp device");
return 1;
}

//ÒÔÏÂÉèÖÃÉù¿¨²ÎÊý
//ÉèÖòÉÑùʱµÄÁ¿»¯Î»Êý
arg=SIZE;
status=ioctl(fd,SOUND_PCM_WRITE_BITS,&arg);

if(status==-1){
perror("Cannot set SOUND_PCM_WRITE_BITS");
return 1;
}

//ÉèÖòÉÑùÉùµÀÊýÄ¿
arg=CHANNELS;

status=ioctl(fd,SOUND_PCM_WRITE_CHANNELS,&arg);
if(status==-1){
perror("Cannot set SOUND_PCM_WRITE_CHANNELS");
return 1;
}

//ÉèÖòÉÑùƵÂÊ
arg=RATE;

status=ioctl(fd,SOUND_PCM_WRITE_RATE,&arg);

if(status==-1){
perror("Cannot set SOUND_PCM_WRITE_RATE");
return 1;
}

//һֱ¼Òô£¬Ö±µ½°´Ï¡° Control-C¡±Í£Ö¹
while(1){
printf("Recording ...:\n");
status=read(fd,buf,sizeof(buf));

if(status==-1){
perror("read wrong number of bytes");
}

printf("Play ...:\n");
status=write(fd,buf,sizeof(buf));
if(status != sizeof(buf)) perror("wrote wrong number of bytes");

//ÔÚ¼ÌÐøÂ¼ÒôǰµÈ´ý»Ø·Å½áÊø
status=ioctl(fd,SOUND_PCM_SYNC,0);

if(status==-1) perror("Cannot set SOUND_PCM_SYNC");
}
return 0;
}
Ôõô»Ø·ÃûÓÐÉùÒô£¿£¿£¿£¿
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ