之前买了一块micro2440来学习嵌入式linux
目前在做到音频这块的时候,遇到了问题就是无法实现播放,并且在录音程序中生成的文件,在板子自带的播放器以及拷贝到window上都无法播放
这是源码,希望给以指教
放音:
fd = open("/dev/dsp", O_RDWR);//soundcard device
if (fd < 0)
{
perror("open of /dev/dsp failed");
exit(1);
}
arg = SIZE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if (arg != SIZE)
perror("unable to set sample size");
/* 设置采样时的声道数目 */
arg = CHANNELS;
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
if (arg != CHANNELS)
perror("unable to set number of channels");
/* 设置采样时的采样频率 */
arg = RATE;
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");
/* 循环,直到按下Control-C */
//////////////////////////////////////////////
//
fdfile = open("11.wav", O_RDONLY|O_NONBLOCK);
if (fdfile < 0) {
perror("open of /dev/dsp failed");
exit(1);
}
while(read(fdfile,buf1,buflen))
{
//////////////////////////////////////////////
//
status = write(fd, buf1, buflen);
if (status != sizeof(buf))
perror("wrote wrong number of bytes");
/* 在继续录音前等待回放结束 */
//status = ioctl(fd, SOUND_PCM_SYNC, 0);
//if (status == -1)
//perror("SOUND_PCM_SYNC ioctl failed");
}
录音:
arg1=FORMAT;
int result = ioctl(fd, SNDCTL_DSP_SETFMT, &arg1);
if ( result == -1 )
{
perror("ioctl sample format");
return -1;
}
printf("the format:%d\n",arg);
// 设置采样时的量化位数
arg1 = SIZE;
status1 = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg1);
if (status1 == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if (arg1 != SIZE)
perror("unable to set sample size");
printf( "bits:%d \n",arg);
// 设置采样时的声道数目
arg1 = CHANNELS;
status1 = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg1);
if (status1 == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
if (arg != CHANNELS)
perror("unable to set number of channels");\
printf( " channels:%d \n",arg);
// 设置采样时的采样频率
arg1 = RATE;
status1 = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg1);
if (status1 == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");
FILE * in;
in=fopen("zzz.wav","wb");
while( status = read(fd, buf2,buflen))
{
if (status != sizeof(buf))
perror("read wrong number of bytes");
//保存声音到文件
fwrite(buf2,buflen,1,in);
}
fclose(in);