昨天写了个利用OSS 音频驱动写的一个LINUX下的一个音频的录取与播放,写完后,录制了,听了下,播放速度太快了,然后调节了下播放时那个每次读取的数据的大小,开始都以2的N次方来调节,不是快了,就是慢了,折腾了一天,今天再看了下,试了把它调成中间的那个不是2的N次方的其他数,再调节试了下,咦。。得了,靠就这小问题就浪费了一天的时间。。 以下是程序,还不是最终完善的那个。。
#include "sounddev.h"
sounddev::sounddev()
{
QMessageBox::about(NULL,"test","loving china");
}
int sounddev::record()
{
int id,fd,i,j;
char buf[4*1024];
fd = open("/dev/dsp",O_RDONLY);
if(fd<0)
{
perror("Couldn't open the file /dev/audio:");
return -1;
}
id = open("test.wav",O_RDWR | O_CREAT,755);
if(id<0)
{
perror("Couldn't open the file test.wav:");
return -1;
}
/*************Set the ioctl********************/
i= (0X0001 <<16)+0X0001;
if(ioctl(fd,SNDCTL_DSP_SETFRAGMENT,(char *)&i)==-1){perror("Couldn't set the buffer:");return -1;}
i =0;
if(ioctl(fd,SNDCTL_DSP_RESET,(char *)&i)==-1)perror("reset error:");
if(ioctl(fd,SNDCTL_DSP_SYNC,(char *)&i)==-1)perror("sync error:");
i =1;
if(ioctl(fd,SNDCTL_DSP_NONBLOCK,(char *)&i)==-1)perror("nonblock error:");
i=(4*48000);
if(ioctl(fd,SNDCTL_DSP_SPEED,(char *)&i)==-1){perror("speed error:");return -1;}
i=1;
if(ioctl(fd,SNDCTL_DSP_CHANNELS,(char *)&i)==-1)perror("channel error:");
i=AFMT_S16_LE;
if(ioctl(fd,SNDCTL_DSP_SETFMT,(char *)&i)==-1){perror("setfmt error:");return -1;}
if(i!=AFMT_S16_LE){printf("the device is not suppor the AFMT_S16_LE");return -1;}
i=3;
if(ioctl(fd,SNDCTL_DSP_SETTRIGGER,(char *)&i)==-1)perror("settrigger error:");
i=3;
if(ioctl(fd,SNDCTL_DSP_SETFRAGMENT,(char *)&i)==-1)perror("setfragment error");
i=1;
if(ioctl(fd,SNDCTL_DSP_PROFILE,(char *)&i)==-1)perror("profile error");
/*****************TO Work*********************/
i=0;
for(j=0;j<1000;)
{
i=read(fd,buf,sizeof(buf));
printf("In the %dth time it had get %d bytes
",j,i);
if(i>0)
{
write(id,buf,i);
j++;
}
}
close(fd);
close(id);
return 0;
}
int sounddev::play()
{
int id,fd,i,j;
char buf[512];
fd = open("/dev/dsp",O_WRONLY);
if(fd<0)
{
perror("Couldn't open the file /dev/audio:");
return -1;
}
id = open("test.wav",O_RDWR | O_CREAT,755);
if(id<0)
{
perror("Couldn't open the file test.wav:");
return -1;
}
/*************Set the ioctl********************/
i= (0X0002 <<16)+0X0001;
if(ioctl(fd,SNDCTL_DSP_SETFRAGMENT,(char *)&i)==-1){perror("Couldn't set the buffer:");return -1;}
i =0;
if(ioctl(fd,SNDCTL_DSP_RESET,(char *)&i)==-1)perror("reset error:");
if(ioctl(fd,SNDCTL_DSP_SYNC,(char *)&i)==-1)perror("sync error:");
i =1;
if(ioctl(fd,SNDCTL_DSP_NONBLOCK,(char *)&i)==-1)perror("nonblock error:");
i=(4*48000);
if(ioctl(fd,SNDCTL_DSP_SPEED,(char *)&i)==-1){perror("speed error:");return -1;}
i=1;
if(ioctl(fd,SNDCTL_DSP_CHANNELS,(char *)&i)==-1)perror("channel error:");
i=AFMT_S16_LE;
if(ioctl(fd,SNDCTL_DSP_SETFMT,(char *)&i)==-1){perror("setfmt error:");return -1;}
if(i!=AFMT_S16_LE){printf("the device is not suppor the AFMT_S16_LE");return -1;}
i=3;
if(ioctl(fd,SNDCTL_DSP_SETTRIGGER,(char *)&i)==-1)perror("settrigger error:");
i=3;
if(ioctl(fd,SNDCTL_DSP_SETFRAGMENT,(char *)&i)==-1)perror("setfragment error");
i=1;
if(ioctl(fd,SNDCTL_DSP_PROFILE,(char *)&i)==-1)perror("profile error");
/*****************TO Work*********************/
i=0;
for(j=0;j<8000;)
{
i=read(id,buf,sizeof(buf));
printf("In the %dth time it had read %d bytes
",j,i);
if(i>0)
{
write(fd,buf,i);
//ioctl(fd,SNDCTL_DSP_SYNC);
j++;
}
}
close(fd);
close(id);
return 0;
}