我编了一个录音的小程序,在PC的linux下没有问题,在ZLG MagicARM2410试验开发平台的arm linux下运行就是不行,录不了音。 以下是代码:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <time.h> #include <fcntl.h> #include <sys/soundcard.h>
int main( int argc, char *argv[] ) { int format = AFMT_S16_NE; // AFMT_S16_NE AFMT_U8 int channels = 2; int speed = 44100; size_t buf_size = 819200; char *buf;
printf( "update date:%s %s
", __DATE__, __TIME__ );
buf = malloc( buf_size ); if( buf == NULL ) return printf( "[%d] buf malloc error!
!", errno ); if( argc > 1 ) // play { int dd, fd; int bytes;
dd = open( "/dev/dsp", O_WRONLY ); if( dd == -1 ) return printf( "[%d] device open error!
", errno );
if( ioctl(dd,SNDCTL_DSP_SYNC,0) == -1 ) return printf( "[%d] ioctl(SNDCTL_DSP_SYNC) error!
", errno );
if( ioctl(dd,SNDCTL_DSP_SETFMT,&format) == -1 ) return printf( "[%d] ioctl(SNDCTL_DSP_SETFMT) error!
", errno );
if( ioctl(dd,SNDCTL_DSP_CHANNELS,&channels) == -1) return printf( "[%d] ioctl(SNDCTL_DSP_CHANNELS) error!
", errno );
if( ioctl(dd,SNDCTL_DSP_SPEED,&speed) == -1 ) return printf( "[%d] ioctl(SNDCTL_DSP_SPEED) error!
", errno ); fd = open( "rec", O_RDONLY ); if( fd == -1 ) return printf( "[%d] file open error!
", errno ); printf( "start play ...
" ); while( (bytes=read(fd,buf,buf_size)) > 0 ) { bytes = write( dd, buf, bytes ); if( bytes == -1 ) return printf( "[%d] device play error!
", errno ); } if( bytes != 0 ) return printf( "[%d] file read error!
", errno ); } else // record { int dd, fd; clock_t start_time;
dd = open( "/dev/dsp", O_RDONLY ); if( dd == -1 ) return printf( "[%d] device open error!
", errno );
if( ioctl(dd,SNDCTL_DSP_SYNC,0) == -1 ) return printf( "[%d] ioctl(SNDCTL_DSP_SYNC) error!
", errno );
if( ioctl(dd,SNDCTL_DSP_SETFMT,&format) == -1 ) return printf( "[%d] ioctl(SNDCTL_DSP_SETFMT) error!
", errno );
if( ioctl(dd,SNDCTL_DSP_CHANNELS,&channels) == -1) return printf( "[%d] ioctl(SNDCTL_DSP_CHANNELS) error!
", errno );
if( ioctl(dd,SNDCTL_DSP_SPEED,&speed) == -1 ) return printf( "[%d] ioctl(SNDCTL_DSP_SPEED) error!
", errno );
fd = open( "rec", O_WRONLY|O_CREAT ); if( fd == -1 ) return printf( "[%d] file open error!
", errno ); printf( "start record ...
" ); start_time = clock(); while( clock() - start_time < CLOCKS_PER_SEC * 5 ) { int bytes; bytes = read( dd, buf, buf_size ); // 在ARM平台下这里始终返回0 if( bytes == -1 ) return printf( "[%d] device record error!
", errno ); bytes = write( fd, buf, bytes ); if( bytes == -1 ) return printf( "[%d] file write error!
", errno ); } close( fd ); close( dd ); } printf( "end.
" );
return 0; }
调用read对声音设备操作始终返回0。
我对麦克风讲话时也没听见耳机有任何声音,怀疑是麦克风没开或音量太小,哪位前辈来指点一下啊?
|