2 qq 30524957 qq_30524957 于 2016.03.07 22:04 提问

自己用c写的录音代码播放没声音,可以保证播放部分没有问题,等大神

#include
#include
#include
#include
#include

typedef struct WAV
{
char riff[4];
long len;
char type[4];
char fmt[4];
char tmp[4];
short pcm;
short channel;
long sample;
long rate;
short framesize;
short bit;
char data[4];
long dblen;
}wav_t;

int main(int argc,char** argv)
{
printf("--size=%d\n",sizeof(wav_t));
int wavfd=open(argv[1],O_CREAT|O_WRONLY);
if(wavfd==-1)
{
printf("wavfd err=%s\n",strerror(errno));
return -1;

}

int speed=44100;
int channel=2;
int bit=16;
struct WAV head={"RIFF",44,"WAVE","fmt ","",0x10,channel,speed,channel*speed*bit/8,channel*bit/8,bit,"data",0};
// printf("channel=%d,speed=%d,bit=%d\n",head.channel,head.sample,head.bit);
write(wavfd,&head,sizeof(head));
close(wavfd);
wavfd=open(argv[1],O_CREAT|O_WRONLY);
int fd=open("/dev/dsp",O_RDONLY);
if(fd==-1)
{
printf("fd err=%s\n",strerror(errno));
return -1;

}
ioctl(fd,SNDCTL_DSP_CHANNELS,&channel);
ioctl(fd,SNDCTL_DSP_SPEED,&speed);
ioctl(fd,SNDCTL_DSP_SETFMT,&bit);
char buf[4096];
int len=0,t=0;
while(1)
{
int size=read(fd,buf,4096);
write(wavfd,buf,size);
len+=size;
t=len/(channel*speed*bit/8);
if(t>=5)break;
}
int all=len+44;
lseek(wavfd,0x04,0);
write(wavfd,&all,4);
lseek(wavfd,0x28,0);
write(wavfd,&len,4);
close(wavfd);
close(fd);
}

2个回答

devmiao
devmiao   Ds   Rxr 2016.03.07 23:11
qq_30524957
qq_30524957 多谢!
接近 2 年之前 回复
zhang6318
zhang6318   2016.03.08 17:22

用/dev/snd/*p0设备文件

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!